ptp_chardev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PTP 1588 clock support - character device implementation.
  4. *
  5. * Copyright (C) 2010 OMICRON electronics GmbH
  6. */
  7. #include <linux/module.h>
  8. #include <linux/posix-clock.h>
  9. #include <linux/poll.h>
  10. #include <linux/sched.h>
  11. #include <linux/slab.h>
  12. #include <linux/timekeeping.h>
  13. #include <linux/nospec.h>
  14. #include "ptp_private.h"
  15. static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
  16. enum ptp_pin_function func, unsigned int chan)
  17. {
  18. struct ptp_clock_request rq;
  19. int err = 0;
  20. memset(&rq, 0, sizeof(rq));
  21. switch (func) {
  22. case PTP_PF_NONE:
  23. break;
  24. case PTP_PF_EXTTS:
  25. rq.type = PTP_CLK_REQ_EXTTS;
  26. rq.extts.index = chan;
  27. err = ops->enable(ops, &rq, 0);
  28. break;
  29. case PTP_PF_PEROUT:
  30. rq.type = PTP_CLK_REQ_PEROUT;
  31. rq.perout.index = chan;
  32. err = ops->enable(ops, &rq, 0);
  33. break;
  34. case PTP_PF_PHYSYNC:
  35. break;
  36. default:
  37. return -EINVAL;
  38. }
  39. return err;
  40. }
  41. int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
  42. enum ptp_pin_function func, unsigned int chan)
  43. {
  44. struct ptp_clock_info *info = ptp->info;
  45. struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin];
  46. unsigned int i;
  47. /* Check to see if any other pin previously had this function. */
  48. for (i = 0; i < info->n_pins; i++) {
  49. if (info->pin_config[i].func == func &&
  50. info->pin_config[i].chan == chan) {
  51. pin1 = &info->pin_config[i];
  52. break;
  53. }
  54. }
  55. if (pin1 && i == pin)
  56. return 0;
  57. /* Check the desired function and channel. */
  58. switch (func) {
  59. case PTP_PF_NONE:
  60. break;
  61. case PTP_PF_EXTTS:
  62. if (chan >= info->n_ext_ts)
  63. return -EINVAL;
  64. break;
  65. case PTP_PF_PEROUT:
  66. if (chan >= info->n_per_out)
  67. return -EINVAL;
  68. break;
  69. case PTP_PF_PHYSYNC:
  70. if (chan != 0)
  71. return -EINVAL;
  72. break;
  73. default:
  74. return -EINVAL;
  75. }
  76. if (info->verify(info, pin, func, chan)) {
  77. pr_err("driver cannot use function %u on pin %u\n", func, chan);
  78. return -EOPNOTSUPP;
  79. }
  80. /* Disable whatever function was previously assigned. */
  81. if (pin1) {
  82. ptp_disable_pinfunc(info, func, chan);
  83. pin1->func = PTP_PF_NONE;
  84. pin1->chan = 0;
  85. }
  86. ptp_disable_pinfunc(info, pin2->func, pin2->chan);
  87. pin2->func = func;
  88. pin2->chan = chan;
  89. return 0;
  90. }
  91. int ptp_open(struct posix_clock *pc, fmode_t fmode)
  92. {
  93. return 0;
  94. }
  95. long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
  96. {
  97. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  98. struct ptp_sys_offset_extended *extoff = NULL;
  99. struct ptp_sys_offset_precise precise_offset;
  100. struct system_device_crosststamp xtstamp;
  101. struct ptp_clock_info *ops = ptp->info;
  102. struct ptp_sys_offset *sysoff = NULL;
  103. struct ptp_system_timestamp sts;
  104. struct ptp_clock_request req;
  105. struct ptp_clock_caps caps;
  106. struct ptp_clock_time *pct;
  107. unsigned int i, pin_index;
  108. struct ptp_pin_desc pd;
  109. struct timespec64 ts;
  110. int enable, err = 0;
  111. switch (cmd) {
  112. case PTP_CLOCK_GETCAPS:
  113. case PTP_CLOCK_GETCAPS2:
  114. memset(&caps, 0, sizeof(caps));
  115. caps.max_adj = ptp->info->max_adj;
  116. caps.n_alarm = ptp->info->n_alarm;
  117. caps.n_ext_ts = ptp->info->n_ext_ts;
  118. caps.n_per_out = ptp->info->n_per_out;
  119. caps.pps = ptp->info->pps;
  120. caps.n_pins = ptp->info->n_pins;
  121. caps.cross_timestamping = ptp->info->getcrosststamp != NULL;
  122. if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
  123. err = -EFAULT;
  124. break;
  125. case PTP_EXTTS_REQUEST:
  126. case PTP_EXTTS_REQUEST2:
  127. memset(&req, 0, sizeof(req));
  128. if (copy_from_user(&req.extts, (void __user *)arg,
  129. sizeof(req.extts))) {
  130. err = -EFAULT;
  131. break;
  132. }
  133. if (cmd == PTP_EXTTS_REQUEST2) {
  134. /* Tell the drivers to check the flags carefully. */
  135. req.extts.flags |= PTP_STRICT_FLAGS;
  136. /* Make sure no reserved bit is set. */
  137. if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
  138. req.extts.rsv[0] || req.extts.rsv[1]) {
  139. err = -EINVAL;
  140. break;
  141. }
  142. /* Ensure one of the rising/falling edge bits is set. */
  143. if ((req.extts.flags & PTP_ENABLE_FEATURE) &&
  144. (req.extts.flags & PTP_EXTTS_EDGES) == 0) {
  145. err = -EINVAL;
  146. break;
  147. }
  148. } else if (cmd == PTP_EXTTS_REQUEST) {
  149. req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
  150. req.extts.rsv[0] = 0;
  151. req.extts.rsv[1] = 0;
  152. }
  153. if (req.extts.index >= ops->n_ext_ts) {
  154. err = -EINVAL;
  155. break;
  156. }
  157. req.type = PTP_CLK_REQ_EXTTS;
  158. enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
  159. err = ops->enable(ops, &req, enable);
  160. break;
  161. case PTP_PEROUT_REQUEST:
  162. case PTP_PEROUT_REQUEST2:
  163. memset(&req, 0, sizeof(req));
  164. if (copy_from_user(&req.perout, (void __user *)arg,
  165. sizeof(req.perout))) {
  166. err = -EFAULT;
  167. break;
  168. }
  169. if (((req.perout.flags & ~PTP_PEROUT_VALID_FLAGS) ||
  170. req.perout.rsv[0] || req.perout.rsv[1] ||
  171. req.perout.rsv[2] || req.perout.rsv[3]) &&
  172. cmd == PTP_PEROUT_REQUEST2) {
  173. err = -EINVAL;
  174. break;
  175. } else if (cmd == PTP_PEROUT_REQUEST) {
  176. req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
  177. req.perout.rsv[0] = 0;
  178. req.perout.rsv[1] = 0;
  179. req.perout.rsv[2] = 0;
  180. req.perout.rsv[3] = 0;
  181. }
  182. if (req.perout.index >= ops->n_per_out) {
  183. err = -EINVAL;
  184. break;
  185. }
  186. req.type = PTP_CLK_REQ_PEROUT;
  187. enable = req.perout.period.sec || req.perout.period.nsec;
  188. err = ops->enable(ops, &req, enable);
  189. break;
  190. case PTP_ENABLE_PPS:
  191. case PTP_ENABLE_PPS2:
  192. memset(&req, 0, sizeof(req));
  193. if (!capable(CAP_SYS_TIME))
  194. return -EPERM;
  195. req.type = PTP_CLK_REQ_PPS;
  196. enable = arg ? 1 : 0;
  197. err = ops->enable(ops, &req, enable);
  198. break;
  199. case PTP_SYS_OFFSET_PRECISE:
  200. case PTP_SYS_OFFSET_PRECISE2:
  201. if (!ptp->info->getcrosststamp) {
  202. err = -EOPNOTSUPP;
  203. break;
  204. }
  205. err = ptp->info->getcrosststamp(ptp->info, &xtstamp);
  206. if (err)
  207. break;
  208. memset(&precise_offset, 0, sizeof(precise_offset));
  209. ts = ktime_to_timespec64(xtstamp.device);
  210. precise_offset.device.sec = ts.tv_sec;
  211. precise_offset.device.nsec = ts.tv_nsec;
  212. ts = ktime_to_timespec64(xtstamp.sys_realtime);
  213. precise_offset.sys_realtime.sec = ts.tv_sec;
  214. precise_offset.sys_realtime.nsec = ts.tv_nsec;
  215. ts = ktime_to_timespec64(xtstamp.sys_monoraw);
  216. precise_offset.sys_monoraw.sec = ts.tv_sec;
  217. precise_offset.sys_monoraw.nsec = ts.tv_nsec;
  218. if (copy_to_user((void __user *)arg, &precise_offset,
  219. sizeof(precise_offset)))
  220. err = -EFAULT;
  221. break;
  222. case PTP_SYS_OFFSET_EXTENDED:
  223. case PTP_SYS_OFFSET_EXTENDED2:
  224. if (!ptp->info->gettimex64) {
  225. err = -EOPNOTSUPP;
  226. break;
  227. }
  228. extoff = memdup_user((void __user *)arg, sizeof(*extoff));
  229. if (IS_ERR(extoff)) {
  230. err = PTR_ERR(extoff);
  231. extoff = NULL;
  232. break;
  233. }
  234. if (extoff->n_samples > PTP_MAX_SAMPLES
  235. || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
  236. err = -EINVAL;
  237. break;
  238. }
  239. for (i = 0; i < extoff->n_samples; i++) {
  240. err = ptp->info->gettimex64(ptp->info, &ts, &sts);
  241. if (err)
  242. goto out;
  243. extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
  244. extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
  245. extoff->ts[i][1].sec = ts.tv_sec;
  246. extoff->ts[i][1].nsec = ts.tv_nsec;
  247. extoff->ts[i][2].sec = sts.post_ts.tv_sec;
  248. extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
  249. }
  250. if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff)))
  251. err = -EFAULT;
  252. break;
  253. case PTP_SYS_OFFSET:
  254. case PTP_SYS_OFFSET2:
  255. sysoff = memdup_user((void __user *)arg, sizeof(*sysoff));
  256. if (IS_ERR(sysoff)) {
  257. err = PTR_ERR(sysoff);
  258. sysoff = NULL;
  259. break;
  260. }
  261. if (sysoff->n_samples > PTP_MAX_SAMPLES) {
  262. err = -EINVAL;
  263. break;
  264. }
  265. pct = &sysoff->ts[0];
  266. for (i = 0; i < sysoff->n_samples; i++) {
  267. ktime_get_real_ts64(&ts);
  268. pct->sec = ts.tv_sec;
  269. pct->nsec = ts.tv_nsec;
  270. pct++;
  271. if (ops->gettimex64)
  272. err = ops->gettimex64(ops, &ts, NULL);
  273. else
  274. err = ops->gettime64(ops, &ts);
  275. if (err)
  276. goto out;
  277. pct->sec = ts.tv_sec;
  278. pct->nsec = ts.tv_nsec;
  279. pct++;
  280. }
  281. ktime_get_real_ts64(&ts);
  282. pct->sec = ts.tv_sec;
  283. pct->nsec = ts.tv_nsec;
  284. if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
  285. err = -EFAULT;
  286. break;
  287. case PTP_PIN_GETFUNC:
  288. case PTP_PIN_GETFUNC2:
  289. if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
  290. err = -EFAULT;
  291. break;
  292. }
  293. if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
  294. || pd.rsv[3] || pd.rsv[4])
  295. && cmd == PTP_PIN_GETFUNC2) {
  296. err = -EINVAL;
  297. break;
  298. } else if (cmd == PTP_PIN_GETFUNC) {
  299. pd.rsv[0] = 0;
  300. pd.rsv[1] = 0;
  301. pd.rsv[2] = 0;
  302. pd.rsv[3] = 0;
  303. pd.rsv[4] = 0;
  304. }
  305. pin_index = pd.index;
  306. if (pin_index >= ops->n_pins) {
  307. err = -EINVAL;
  308. break;
  309. }
  310. pin_index = array_index_nospec(pin_index, ops->n_pins);
  311. if (mutex_lock_interruptible(&ptp->pincfg_mux))
  312. return -ERESTARTSYS;
  313. pd = ops->pin_config[pin_index];
  314. mutex_unlock(&ptp->pincfg_mux);
  315. if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd)))
  316. err = -EFAULT;
  317. break;
  318. case PTP_PIN_SETFUNC:
  319. case PTP_PIN_SETFUNC2:
  320. if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) {
  321. err = -EFAULT;
  322. break;
  323. }
  324. if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
  325. || pd.rsv[3] || pd.rsv[4])
  326. && cmd == PTP_PIN_SETFUNC2) {
  327. err = -EINVAL;
  328. break;
  329. } else if (cmd == PTP_PIN_SETFUNC) {
  330. pd.rsv[0] = 0;
  331. pd.rsv[1] = 0;
  332. pd.rsv[2] = 0;
  333. pd.rsv[3] = 0;
  334. pd.rsv[4] = 0;
  335. }
  336. pin_index = pd.index;
  337. if (pin_index >= ops->n_pins) {
  338. err = -EINVAL;
  339. break;
  340. }
  341. pin_index = array_index_nospec(pin_index, ops->n_pins);
  342. if (mutex_lock_interruptible(&ptp->pincfg_mux))
  343. return -ERESTARTSYS;
  344. err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
  345. mutex_unlock(&ptp->pincfg_mux);
  346. break;
  347. default:
  348. err = -ENOTTY;
  349. break;
  350. }
  351. out:
  352. kfree(extoff);
  353. kfree(sysoff);
  354. return err;
  355. }
  356. __poll_t ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
  357. {
  358. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  359. poll_wait(fp, &ptp->tsev_wq, wait);
  360. return queue_cnt(&ptp->tsevq) ? EPOLLIN : 0;
  361. }
  362. #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
  363. ssize_t ptp_read(struct posix_clock *pc,
  364. uint rdflags, char __user *buf, size_t cnt)
  365. {
  366. struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
  367. struct timestamp_event_queue *queue = &ptp->tsevq;
  368. struct ptp_extts_event *event;
  369. unsigned long flags;
  370. size_t qcnt, i;
  371. int result;
  372. if (cnt % sizeof(struct ptp_extts_event) != 0)
  373. return -EINVAL;
  374. if (cnt > EXTTS_BUFSIZE)
  375. cnt = EXTTS_BUFSIZE;
  376. cnt = cnt / sizeof(struct ptp_extts_event);
  377. if (mutex_lock_interruptible(&ptp->tsevq_mux))
  378. return -ERESTARTSYS;
  379. if (wait_event_interruptible(ptp->tsev_wq,
  380. ptp->defunct || queue_cnt(queue))) {
  381. mutex_unlock(&ptp->tsevq_mux);
  382. return -ERESTARTSYS;
  383. }
  384. if (ptp->defunct) {
  385. mutex_unlock(&ptp->tsevq_mux);
  386. return -ENODEV;
  387. }
  388. event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
  389. if (!event) {
  390. mutex_unlock(&ptp->tsevq_mux);
  391. return -ENOMEM;
  392. }
  393. spin_lock_irqsave(&queue->lock, flags);
  394. qcnt = queue_cnt(queue);
  395. if (cnt > qcnt)
  396. cnt = qcnt;
  397. for (i = 0; i < cnt; i++) {
  398. event[i] = queue->buf[queue->head];
  399. queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
  400. }
  401. spin_unlock_irqrestore(&queue->lock, flags);
  402. cnt = cnt * sizeof(struct ptp_extts_event);
  403. mutex_unlock(&ptp->tsevq_mux);
  404. result = cnt;
  405. if (copy_to_user(buf, event, cnt))
  406. result = -EFAULT;
  407. kfree(event);
  408. return result;
  409. }