ptp_chardev.c 8.5 KB

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