ptp_chardev.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. ts = ktime_to_timespec64(xtstamp.device);
  177. precise_offset.device.sec = ts.tv_sec;
  178. precise_offset.device.nsec = ts.tv_nsec;
  179. ts = ktime_to_timespec64(xtstamp.sys_realtime);
  180. precise_offset.sys_realtime.sec = ts.tv_sec;
  181. precise_offset.sys_realtime.nsec = ts.tv_nsec;
  182. ts = ktime_to_timespec64(xtstamp.sys_monoraw);
  183. precise_offset.sys_monoraw.sec = ts.tv_sec;
  184. precise_offset.sys_monoraw.nsec = ts.tv_nsec;
  185. if (copy_to_user((void __user *)arg, &precise_offset,
  186. sizeof(precise_offset)))
  187. err = -EFAULT;
  188. break;
  189. case PTP_SYS_OFFSET:
  190. sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
  191. if (IS_ERR(sysoff)) {
  192. err = PTR_ERR(sysoff);
  193. sysoff = NULL;
  194. break;
  195. }
  196. if (sysoff->n_samples > PTP_MAX_SAMPLES) {
  197. err = -EINVAL;
  198. break;
  199. }
  200. pct = &sysoff->ts[0];
  201. for (i = 0; i < sysoff->n_samples; i++) {
  202. getnstimeofday64(&ts);
  203. pct->sec = ts.tv_sec;
  204. pct->nsec = ts.tv_nsec;
  205. pct++;
  206. ptp->info->gettime64(ptp->info, &ts);
  207. pct->sec = ts.tv_sec;
  208. pct->nsec = ts.tv_nsec;
  209. pct++;
  210. }
  211. getnstimeofday64(&ts);
  212. pct->sec = ts.tv_sec;
  213. pct->nsec = ts.tv_nsec;
  214. if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
  215. err = -EFAULT;
  216. break;
  217. case PTP_PIN_GETFUNC:
  218. if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
  219. err = -EFAULT;
  220. break;
  221. }
  222. pin_index = pd.index;
  223. if (pin_index >= ops->n_pins) {
  224. err = -EINVAL;
  225. break;
  226. }
  227. if (mutex_lock_interruptible(&ptp->pincfg_mux))
  228. return -ERESTARTSYS;
  229. pd = ops->pin_config[pin_index];
  230. mutex_unlock(&ptp->pincfg_mux);
  231. if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
  232. err = -EFAULT;
  233. break;
  234. case PTP_PIN_SETFUNC:
  235. if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
  236. err = -EFAULT;
  237. break;
  238. }
  239. pin_index = pd.index;
  240. if (pin_index >= ops->n_pins) {
  241. err = -EINVAL;
  242. break;
  243. }
  244. if (mutex_lock_interruptible(&ptp->pincfg_mux))
  245. return -ERESTARTSYS;
  246. err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
  247. mutex_unlock(&ptp->pincfg_mux);
  248. break;
  249. default:
  250. err = -ENOTTY;
  251. break;
  252. }
  253. kfree(sysoff);
  254. return err;
  255. }
  256. unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
  257. {
  258. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  259. poll_wait(fp, &ptp->tsev_wq, wait);
  260. return queue_cnt(&ptp->tsevq) ? POLLIN : 0;
  261. }
  262. #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
  263. ssize_t ptp_read(struct posix_clock *pc,
  264. uint rdflags, char __user *buf, size_t cnt)
  265. {
  266. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  267. struct timestamp_event_queue *queue = &ptp->tsevq;
  268. struct ptp_extts_event *event;
  269. unsigned long flags;
  270. size_t qcnt, i;
  271. int result;
  272. if (cnt % sizeof(struct ptp_extts_event) != 0)
  273. return -EINVAL;
  274. if (cnt > EXTTS_BUFSIZE)
  275. cnt = EXTTS_BUFSIZE;
  276. cnt = cnt / sizeof(struct ptp_extts_event);
  277. if (mutex_lock_interruptible(&ptp->tsevq_mux))
  278. return -ERESTARTSYS;
  279. if (wait_event_interruptible(ptp->tsev_wq,
  280. ptp->defunct || queue_cnt(queue))) {
  281. mutex_unlock(&ptp->tsevq_mux);
  282. return -ERESTARTSYS;
  283. }
  284. if (ptp->defunct) {
  285. mutex_unlock(&ptp->tsevq_mux);
  286. return -ENODEV;
  287. }
  288. event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
  289. if (!event) {
  290. mutex_unlock(&ptp->tsevq_mux);
  291. return -ENOMEM;
  292. }
  293. spin_lock_irqsave(&queue->lock, flags);
  294. qcnt = queue_cnt(queue);
  295. if (cnt > qcnt)
  296. cnt = qcnt;
  297. for (i = 0; i < cnt; i++) {
  298. event[i] = queue->buf[queue->head];
  299. queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
  300. }
  301. spin_unlock_irqrestore(&queue->lock, flags);
  302. cnt = cnt * sizeof(struct ptp_extts_event);
  303. mutex_unlock(&ptp->tsevq_mux);
  304. result = cnt;
  305. if (copy_to_user(buf, event, cnt))
  306. result = -EFAULT;
  307. kfree(event);
  308. return result;
  309. }