ptp_chardev.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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/timekeeping.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_sys_offset_precise precise_offset;
  113. struct ptp_pin_desc pd;
  114. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  115. struct ptp_clock_info *ops = ptp->info;
  116. struct ptp_clock_time *pct;
  117. struct timespec64 ts;
  118. struct system_device_crosststamp xtstamp;
  119. int enable, err = 0;
  120. unsigned int i, pin_index;
  121. switch (cmd) {
  122. case PTP_CLOCK_GETCAPS:
  123. memset(&caps, 0, sizeof(caps));
  124. caps.max_adj = ptp->info->max_adj;
  125. caps.n_alarm = ptp->info->n_alarm;
  126. caps.n_ext_ts = ptp->info->n_ext_ts;
  127. caps.n_per_out = ptp->info->n_per_out;
  128. caps.pps = ptp->info->pps;
  129. caps.n_pins = ptp->info->n_pins;
  130. caps.cross_timestamping = ptp->info->getcrosststamp != NULL;
  131. if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
  132. err = -EFAULT;
  133. break;
  134. case PTP_EXTTS_REQUEST:
  135. if (copy_from_user(&req.extts, (void __user *)arg,
  136. sizeof(req.extts))) {
  137. err = -EFAULT;
  138. break;
  139. }
  140. if (req.extts.index >= ops->n_ext_ts) {
  141. err = -EINVAL;
  142. break;
  143. }
  144. req.type = PTP_CLK_REQ_EXTTS;
  145. enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
  146. err = ops->enable(ops, &req, enable);
  147. break;
  148. case PTP_PEROUT_REQUEST:
  149. if (copy_from_user(&req.perout, (void __user *)arg,
  150. sizeof(req.perout))) {
  151. err = -EFAULT;
  152. break;
  153. }
  154. if (req.perout.index >= ops->n_per_out) {
  155. err = -EINVAL;
  156. break;
  157. }
  158. req.type = PTP_CLK_REQ_PEROUT;
  159. enable = req.perout.period.sec || req.perout.period.nsec;
  160. err = ops->enable(ops, &req, enable);
  161. break;
  162. case PTP_ENABLE_PPS:
  163. if (!capable(CAP_SYS_TIME))
  164. return -EPERM;
  165. req.type = PTP_CLK_REQ_PPS;
  166. enable = arg ? 1 : 0;
  167. err = ops->enable(ops, &req, enable);
  168. break;
  169. case PTP_SYS_OFFSET_PRECISE:
  170. if (!ptp->info->getcrosststamp) {
  171. err = -EOPNOTSUPP;
  172. break;
  173. }
  174. err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
  175. if (err)
  176. break;
  177. memset(&precise_offset, 0, sizeof(precise_offset));
  178. ts = ktime_to_timespec64(xtstamp.device);
  179. precise_offset.device.sec = ts.tv_sec;
  180. precise_offset.device.nsec = ts.tv_nsec;
  181. ts = ktime_to_timespec64(xtstamp.sys_realtime);
  182. precise_offset.sys_realtime.sec = ts.tv_sec;
  183. precise_offset.sys_realtime.nsec = ts.tv_nsec;
  184. ts = ktime_to_timespec64(xtstamp.sys_monoraw);
  185. precise_offset.sys_monoraw.sec = ts.tv_sec;
  186. precise_offset.sys_monoraw.nsec = ts.tv_nsec;
  187. if (copy_to_user((void __user *)arg, &precise_offset,
  188. sizeof(precise_offset)))
  189. err = -EFAULT;
  190. break;
  191. case PTP_SYS_OFFSET:
  192. sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
  193. if (IS_ERR(sysoff)) {
  194. err = PTR_ERR(sysoff);
  195. sysoff = NULL;
  196. break;
  197. }
  198. if (sysoff->n_samples > PTP_MAX_SAMPLES) {
  199. err = -EINVAL;
  200. break;
  201. }
  202. pct = &sysoff->ts[0];
  203. for (i = 0; i < sysoff->n_samples; i++) {
  204. getnstimeofday64(&ts);
  205. pct->sec = ts.tv_sec;
  206. pct->nsec = ts.tv_nsec;
  207. pct++;
  208. ptp->info->gettime64(ptp->info, &ts);
  209. pct->sec = ts.tv_sec;
  210. pct->nsec = ts.tv_nsec;
  211. pct++;
  212. }
  213. getnstimeofday64(&ts);
  214. pct->sec = ts.tv_sec;
  215. pct->nsec = ts.tv_nsec;
  216. if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
  217. err = -EFAULT;
  218. break;
  219. case PTP_PIN_GETFUNC:
  220. if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
  221. err = -EFAULT;
  222. break;
  223. }
  224. pin_index = pd.index;
  225. if (pin_index >= ops->n_pins) {
  226. err = -EINVAL;
  227. break;
  228. }
  229. if (mutex_lock_interruptible(&ptp->pincfg_mux))
  230. return -ERESTARTSYS;
  231. pd = ops->pin_config[pin_index];
  232. mutex_unlock(&ptp->pincfg_mux);
  233. if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
  234. err = -EFAULT;
  235. break;
  236. case PTP_PIN_SETFUNC:
  237. if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
  238. err = -EFAULT;
  239. break;
  240. }
  241. pin_index = pd.index;
  242. if (pin_index >= ops->n_pins) {
  243. err = -EINVAL;
  244. break;
  245. }
  246. if (mutex_lock_interruptible(&ptp->pincfg_mux))
  247. return -ERESTARTSYS;
  248. err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
  249. mutex_unlock(&ptp->pincfg_mux);
  250. break;
  251. default:
  252. err = -ENOTTY;
  253. break;
  254. }
  255. kfree(sysoff);
  256. return err;
  257. }
  258. unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
  259. {
  260. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  261. poll_wait(fp, &ptp->tsev_wq, wait);
  262. return queue_cnt(&ptp->tsevq) ? POLLIN : 0;
  263. }
  264. #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
  265. ssize_t ptp_read(struct posix_clock *pc,
  266. uint rdflags, char __user *buf, size_t cnt)
  267. {
  268. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  269. struct timestamp_event_queue *queue = &ptp->tsevq;
  270. struct ptp_extts_event *event;
  271. unsigned long flags;
  272. size_t qcnt, i;
  273. int result;
  274. if (cnt % sizeof(struct ptp_extts_event) != 0)
  275. return -EINVAL;
  276. if (cnt > EXTTS_BUFSIZE)
  277. cnt = EXTTS_BUFSIZE;
  278. cnt = cnt / sizeof(struct ptp_extts_event);
  279. if (mutex_lock_interruptible(&ptp->tsevq_mux))
  280. return -ERESTARTSYS;
  281. if (wait_event_interruptible(ptp->tsev_wq,
  282. ptp->defunct || queue_cnt(queue))) {
  283. mutex_unlock(&ptp->tsevq_mux);
  284. return -ERESTARTSYS;
  285. }
  286. if (ptp->defunct) {
  287. mutex_unlock(&ptp->tsevq_mux);
  288. return -ENODEV;
  289. }
  290. event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
  291. if (!event) {
  292. mutex_unlock(&ptp->tsevq_mux);
  293. return -ENOMEM;
  294. }
  295. spin_lock_irqsave(&queue->lock, flags);
  296. qcnt = queue_cnt(queue);
  297. if (cnt > qcnt)
  298. cnt = qcnt;
  299. for (i = 0; i < cnt; i++) {
  300. event[i] = queue->buf[queue->head];
  301. queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
  302. }
  303. spin_unlock_irqrestore(&queue->lock, flags);
  304. cnt = cnt * sizeof(struct ptp_extts_event);
  305. mutex_unlock(&ptp->tsevq_mux);
  306. result = cnt;
  307. if (copy_to_user(buf, event, cnt))
  308. result = -EFAULT;
  309. kfree(event);
  310. return result;
  311. }