ptp_chardev.c 8.7 KB

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