ptp_chardev.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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/compat.h>
  8. #include <linux/module.h>
  9. #include <linux/posix-clock.h>
  10. #include <linux/poll.h>
  11. #include <linux/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/timekeeping.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/nospec.h>
  16. #include "ptp_private.h"
  17. static int ptp_disable_pinfunc(struct ptp_clock_info *ops,
  18. enum ptp_pin_function func, unsigned int chan)
  19. {
  20. struct ptp_clock_request rq;
  21. int err = 0;
  22. memset(&rq, 0, sizeof(rq));
  23. switch (func) {
  24. case PTP_PF_NONE:
  25. break;
  26. case PTP_PF_EXTTS:
  27. rq.type = PTP_CLK_REQ_EXTTS;
  28. rq.extts.index = chan;
  29. err = ops->enable(ops, &rq, 0);
  30. break;
  31. case PTP_PF_PEROUT:
  32. rq.type = PTP_CLK_REQ_PEROUT;
  33. rq.perout.index = chan;
  34. err = ops->enable(ops, &rq, 0);
  35. break;
  36. case PTP_PF_PHYSYNC:
  37. break;
  38. default:
  39. return -EINVAL;
  40. }
  41. return err;
  42. }
  43. void ptp_disable_all_events(struct ptp_clock *ptp)
  44. {
  45. struct ptp_clock_info *info = ptp->info;
  46. unsigned int i;
  47. mutex_lock(&ptp->pincfg_mux);
  48. /* Disable any pins that may raise EXTTS events */
  49. for (i = 0; i < info->n_pins; i++)
  50. if (info->pin_config[i].func == PTP_PF_EXTTS)
  51. ptp_disable_pinfunc(info, info->pin_config[i].func,
  52. info->pin_config[i].chan);
  53. /* Disable the PPS event if the driver has PPS support */
  54. if (info->pps) {
  55. struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
  56. info->enable(info, &req, 0);
  57. }
  58. mutex_unlock(&ptp->pincfg_mux);
  59. }
  60. int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
  61. enum ptp_pin_function func, unsigned int chan)
  62. {
  63. struct ptp_clock_info *info = ptp->info;
  64. struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin];
  65. unsigned int i;
  66. /* Check to see if any other pin previously had this function. */
  67. for (i = 0; i < info->n_pins; i++) {
  68. if (info->pin_config[i].func == func &&
  69. info->pin_config[i].chan == chan) {
  70. pin1 = &info->pin_config[i];
  71. break;
  72. }
  73. }
  74. if (pin1 && i == pin)
  75. return 0;
  76. /* Check the desired function and channel. */
  77. switch (func) {
  78. case PTP_PF_NONE:
  79. break;
  80. case PTP_PF_EXTTS:
  81. if (chan >= info->n_ext_ts)
  82. return -EINVAL;
  83. break;
  84. case PTP_PF_PEROUT:
  85. if (chan >= info->n_per_out)
  86. return -EINVAL;
  87. break;
  88. case PTP_PF_PHYSYNC:
  89. if (chan != 0)
  90. return -EINVAL;
  91. break;
  92. default:
  93. return -EINVAL;
  94. }
  95. if (info->verify(info, pin, func, chan)) {
  96. pr_err("driver cannot use function %u and channel %u on pin %u\n",
  97. func, chan, pin);
  98. return -EOPNOTSUPP;
  99. }
  100. /* Disable whichever pin was previously assigned to this function and
  101. * channel.
  102. */
  103. if (pin1) {
  104. ptp_disable_pinfunc(info, func, chan);
  105. pin1->func = PTP_PF_NONE;
  106. pin1->chan = 0;
  107. }
  108. /* Disable whatever function was previously assigned to the requested
  109. * pin.
  110. */
  111. ptp_disable_pinfunc(info, pin2->func, pin2->chan);
  112. pin2->func = func;
  113. pin2->chan = chan;
  114. return 0;
  115. }
  116. int ptp_open(struct posix_clock_context *pccontext, fmode_t fmode)
  117. {
  118. struct ptp_clock *ptp = container_of(pccontext->clk, struct ptp_clock, clock);
  119. struct timestamp_event_queue *queue;
  120. char debugfsname[32];
  121. queue = kzalloc(sizeof(*queue), GFP_KERNEL);
  122. if (!queue)
  123. return -EINVAL;
  124. queue->mask = bitmap_alloc(PTP_MAX_CHANNELS, GFP_KERNEL);
  125. if (!queue->mask) {
  126. kfree(queue);
  127. return -EINVAL;
  128. }
  129. bitmap_set(queue->mask, 0, PTP_MAX_CHANNELS);
  130. spin_lock_init(&queue->lock);
  131. scoped_guard(spinlock_irq, &ptp->tsevqs_lock)
  132. list_add_tail(&queue->qlist, &ptp->tsevqs);
  133. pccontext->private_clkdata = queue;
  134. /* Debugfs contents */
  135. sprintf(debugfsname, "0x%p", queue);
  136. queue->debugfs_instance =
  137. debugfs_create_dir(debugfsname, ptp->debugfs_root);
  138. queue->dfs_bitmap.array = (u32 *)queue->mask;
  139. queue->dfs_bitmap.n_elements =
  140. DIV_ROUND_UP(PTP_MAX_CHANNELS, BITS_PER_BYTE * sizeof(u32));
  141. debugfs_create_u32_array("mask", 0444, queue->debugfs_instance,
  142. &queue->dfs_bitmap);
  143. return 0;
  144. }
  145. int ptp_release(struct posix_clock_context *pccontext)
  146. {
  147. struct timestamp_event_queue *queue = pccontext->private_clkdata;
  148. struct ptp_clock *ptp =
  149. container_of(pccontext->clk, struct ptp_clock, clock);
  150. debugfs_remove(queue->debugfs_instance);
  151. pccontext->private_clkdata = NULL;
  152. scoped_guard(spinlock_irq, &ptp->tsevqs_lock)
  153. list_del(&queue->qlist);
  154. bitmap_free(queue->mask);
  155. kfree(queue);
  156. return 0;
  157. }
  158. static long ptp_clock_getcaps(struct ptp_clock *ptp, void __user *arg)
  159. {
  160. struct ptp_clock_caps caps = {
  161. .max_adj = ptp->info->max_adj,
  162. .n_alarm = ptp->info->n_alarm,
  163. .n_ext_ts = ptp->info->n_ext_ts,
  164. .n_per_out = ptp->info->n_per_out,
  165. .pps = ptp->info->pps,
  166. .n_pins = ptp->info->n_pins,
  167. .cross_timestamping = ptp->info->getcrosststamp != NULL,
  168. .adjust_phase = ptp->info->adjphase != NULL &&
  169. ptp->info->getmaxphase != NULL,
  170. };
  171. if (caps.adjust_phase)
  172. caps.max_phase_adj = ptp->info->getmaxphase(ptp->info);
  173. return copy_to_user(arg, &caps, sizeof(caps)) ? -EFAULT : 0;
  174. }
  175. static long ptp_extts_request(struct ptp_clock *ptp, unsigned int cmd, void __user *arg)
  176. {
  177. struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS };
  178. struct ptp_clock_info *ops = ptp->info;
  179. unsigned int supported_extts_flags;
  180. if (copy_from_user(&req.extts, arg, sizeof(req.extts)))
  181. return -EFAULT;
  182. if (cmd == PTP_EXTTS_REQUEST2) {
  183. /* Tell the drivers to check the flags carefully. */
  184. req.extts.flags |= PTP_STRICT_FLAGS;
  185. /* Make sure no reserved bit is set. */
  186. if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
  187. req.extts.rsv[0] || req.extts.rsv[1])
  188. return -EINVAL;
  189. /* Ensure one of the rising/falling edge bits is set. */
  190. if ((req.extts.flags & PTP_ENABLE_FEATURE) &&
  191. (req.extts.flags & PTP_EXTTS_EDGES) == 0)
  192. return -EINVAL;
  193. } else {
  194. req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
  195. memset(req.extts.rsv, 0, sizeof(req.extts.rsv));
  196. }
  197. if (req.extts.index >= ops->n_ext_ts)
  198. return -EINVAL;
  199. supported_extts_flags = ptp->info->supported_extts_flags;
  200. /* The PTP_ENABLE_FEATURE flag is always supported. */
  201. supported_extts_flags |= PTP_ENABLE_FEATURE;
  202. /* If the driver does not support strictly checking flags, the
  203. * PTP_RISING_EDGE and PTP_FALLING_EDGE flags are merely hints
  204. * which are not enforced.
  205. */
  206. if (!(supported_extts_flags & PTP_STRICT_FLAGS))
  207. supported_extts_flags |= PTP_EXTTS_EDGES;
  208. /* Reject unsupported flags */
  209. if (req.extts.flags & ~supported_extts_flags)
  210. return -EOPNOTSUPP;
  211. scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux)
  212. return ops->enable(ops, &req, req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0);
  213. }
  214. static long ptp_perout_request(struct ptp_clock *ptp, unsigned int cmd, void __user *arg)
  215. {
  216. struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT };
  217. struct ptp_perout_request *perout = &req.perout;
  218. struct ptp_clock_info *ops = ptp->info;
  219. if (copy_from_user(perout, arg, sizeof(*perout)))
  220. return -EFAULT;
  221. if (cmd == PTP_PEROUT_REQUEST2) {
  222. if (perout->flags & ~PTP_PEROUT_VALID_FLAGS)
  223. return -EINVAL;
  224. /*
  225. * The "on" field has undefined meaning if
  226. * PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat it
  227. * as reserved, which must be set to zero.
  228. */
  229. if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) &&
  230. !mem_is_zero(perout->rsv, sizeof(perout->rsv)))
  231. return -EINVAL;
  232. if (perout->flags & PTP_PEROUT_DUTY_CYCLE) {
  233. /* The duty cycle must be subunitary. */
  234. if (perout->on.sec > perout->period.sec ||
  235. (perout->on.sec == perout->period.sec &&
  236. perout->on.nsec > perout->period.nsec))
  237. return -ERANGE;
  238. }
  239. if (perout->flags & PTP_PEROUT_PHASE) {
  240. /*
  241. * The phase should be specified modulo the period,
  242. * therefore anything equal or larger than 1 period
  243. * is invalid.
  244. */
  245. if (perout->phase.sec > perout->period.sec ||
  246. (perout->phase.sec == perout->period.sec &&
  247. perout->phase.nsec >= perout->period.nsec))
  248. return -ERANGE;
  249. }
  250. } else {
  251. perout->flags &= PTP_PEROUT_V1_VALID_FLAGS;
  252. memset(perout->rsv, 0, sizeof(perout->rsv));
  253. }
  254. if (perout->index >= ops->n_per_out)
  255. return -EINVAL;
  256. if (perout->flags & ~ops->supported_perout_flags)
  257. return -EOPNOTSUPP;
  258. scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux)
  259. return ops->enable(ops, &req, perout->period.sec || perout->period.nsec);
  260. }
  261. static long ptp_enable_pps(struct ptp_clock *ptp, bool enable)
  262. {
  263. struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS };
  264. struct ptp_clock_info *ops = ptp->info;
  265. if (!capable(CAP_SYS_TIME))
  266. return -EPERM;
  267. scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux)
  268. return ops->enable(ops, &req, enable);
  269. }
  270. typedef int (*ptp_crosststamp_fn)(struct ptp_clock_info *,
  271. struct system_device_crosststamp *);
  272. static long ptp_sys_offset_precise(struct ptp_clock *ptp, void __user *arg,
  273. ptp_crosststamp_fn crosststamp_fn)
  274. {
  275. struct ptp_sys_offset_precise precise_offset;
  276. struct system_device_crosststamp xtstamp;
  277. struct timespec64 ts;
  278. int err;
  279. if (!crosststamp_fn)
  280. return -EOPNOTSUPP;
  281. err = crosststamp_fn(ptp->info, &xtstamp);
  282. if (err)
  283. return err;
  284. memset(&precise_offset, 0, sizeof(precise_offset));
  285. ts = ktime_to_timespec64(xtstamp.device);
  286. precise_offset.device.sec = ts.tv_sec;
  287. precise_offset.device.nsec = ts.tv_nsec;
  288. ts = ktime_to_timespec64(xtstamp.sys_realtime);
  289. precise_offset.sys_realtime.sec = ts.tv_sec;
  290. precise_offset.sys_realtime.nsec = ts.tv_nsec;
  291. ts = ktime_to_timespec64(xtstamp.sys_monoraw);
  292. precise_offset.sys_monoraw.sec = ts.tv_sec;
  293. precise_offset.sys_monoraw.nsec = ts.tv_nsec;
  294. return copy_to_user(arg, &precise_offset, sizeof(precise_offset)) ? -EFAULT : 0;
  295. }
  296. typedef int (*ptp_gettimex_fn)(struct ptp_clock_info *,
  297. struct timespec64 *,
  298. struct ptp_system_timestamp *);
  299. static long ptp_sys_offset_extended(struct ptp_clock *ptp, void __user *arg,
  300. ptp_gettimex_fn gettimex_fn)
  301. {
  302. struct ptp_sys_offset_extended *extoff __free(kfree) = NULL;
  303. struct ptp_system_timestamp sts;
  304. if (!gettimex_fn)
  305. return -EOPNOTSUPP;
  306. extoff = memdup_user(arg, sizeof(*extoff));
  307. if (IS_ERR(extoff))
  308. return PTR_ERR(extoff);
  309. if (extoff->n_samples > PTP_MAX_SAMPLES || extoff->rsv[0] || extoff->rsv[1])
  310. return -EINVAL;
  311. switch (extoff->clockid) {
  312. case CLOCK_REALTIME:
  313. case CLOCK_MONOTONIC:
  314. case CLOCK_MONOTONIC_RAW:
  315. break;
  316. case CLOCK_AUX ... CLOCK_AUX_LAST:
  317. if (IS_ENABLED(CONFIG_POSIX_AUX_CLOCKS))
  318. break;
  319. fallthrough;
  320. default:
  321. return -EINVAL;
  322. }
  323. sts.clockid = extoff->clockid;
  324. for (unsigned int i = 0; i < extoff->n_samples; i++) {
  325. struct timespec64 ts;
  326. int err;
  327. err = gettimex_fn(ptp->info, &ts, &sts);
  328. if (err)
  329. return err;
  330. /* Filter out disabled or unavailable clocks */
  331. if (sts.pre_ts.tv_sec < 0 || sts.post_ts.tv_sec < 0)
  332. return -EINVAL;
  333. extoff->ts[i][0].sec = sts.pre_ts.tv_sec;
  334. extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec;
  335. extoff->ts[i][1].sec = ts.tv_sec;
  336. extoff->ts[i][1].nsec = ts.tv_nsec;
  337. extoff->ts[i][2].sec = sts.post_ts.tv_sec;
  338. extoff->ts[i][2].nsec = sts.post_ts.tv_nsec;
  339. }
  340. return copy_to_user(arg, extoff, sizeof(*extoff)) ? -EFAULT : 0;
  341. }
  342. static long ptp_sys_offset(struct ptp_clock *ptp, void __user *arg)
  343. {
  344. struct ptp_sys_offset *sysoff __free(kfree) = NULL;
  345. struct ptp_clock_time *pct;
  346. struct timespec64 ts;
  347. sysoff = memdup_user(arg, sizeof(*sysoff));
  348. if (IS_ERR(sysoff))
  349. return PTR_ERR(sysoff);
  350. if (sysoff->n_samples > PTP_MAX_SAMPLES)
  351. return -EINVAL;
  352. pct = &sysoff->ts[0];
  353. for (unsigned int i = 0; i < sysoff->n_samples; i++) {
  354. struct ptp_clock_info *ops = ptp->info;
  355. int err;
  356. ktime_get_real_ts64(&ts);
  357. pct->sec = ts.tv_sec;
  358. pct->nsec = ts.tv_nsec;
  359. pct++;
  360. if (ops->gettimex64)
  361. err = ops->gettimex64(ops, &ts, NULL);
  362. else
  363. err = ops->gettime64(ops, &ts);
  364. if (err)
  365. return err;
  366. pct->sec = ts.tv_sec;
  367. pct->nsec = ts.tv_nsec;
  368. pct++;
  369. }
  370. ktime_get_real_ts64(&ts);
  371. pct->sec = ts.tv_sec;
  372. pct->nsec = ts.tv_nsec;
  373. return copy_to_user(arg, sysoff, sizeof(*sysoff)) ? -EFAULT : 0;
  374. }
  375. static long ptp_pin_getfunc(struct ptp_clock *ptp, unsigned int cmd, void __user *arg)
  376. {
  377. struct ptp_clock_info *ops = ptp->info;
  378. struct ptp_pin_desc pd;
  379. if (copy_from_user(&pd, arg, sizeof(pd)))
  380. return -EFAULT;
  381. if (cmd == PTP_PIN_GETFUNC2 && !mem_is_zero(pd.rsv, sizeof(pd.rsv)))
  382. return -EINVAL;
  383. if (pd.index >= ops->n_pins)
  384. return -EINVAL;
  385. scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux)
  386. pd = ops->pin_config[array_index_nospec(pd.index, ops->n_pins)];
  387. return copy_to_user(arg, &pd, sizeof(pd)) ? -EFAULT : 0;
  388. }
  389. static long ptp_pin_setfunc(struct ptp_clock *ptp, unsigned int cmd, void __user *arg)
  390. {
  391. struct ptp_clock_info *ops = ptp->info;
  392. struct ptp_pin_desc pd;
  393. unsigned int pin_index;
  394. if (copy_from_user(&pd, arg, sizeof(pd)))
  395. return -EFAULT;
  396. if (cmd == PTP_PIN_SETFUNC2 && !mem_is_zero(pd.rsv, sizeof(pd.rsv)))
  397. return -EINVAL;
  398. if (pd.index >= ops->n_pins)
  399. return -EINVAL;
  400. pin_index = array_index_nospec(pd.index, ops->n_pins);
  401. scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux)
  402. return ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan);
  403. }
  404. static long ptp_mask_clear_all(struct timestamp_event_queue *tsevq)
  405. {
  406. bitmap_clear(tsevq->mask, 0, PTP_MAX_CHANNELS);
  407. return 0;
  408. }
  409. static long ptp_mask_en_single(struct timestamp_event_queue *tsevq, void __user *arg)
  410. {
  411. unsigned int channel;
  412. if (copy_from_user(&channel, arg, sizeof(channel)))
  413. return -EFAULT;
  414. if (channel >= PTP_MAX_CHANNELS)
  415. return -EFAULT;
  416. set_bit(channel, tsevq->mask);
  417. return 0;
  418. }
  419. long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd,
  420. unsigned long arg)
  421. {
  422. struct ptp_clock *ptp = container_of(pccontext->clk, struct ptp_clock, clock);
  423. void __user *argptr;
  424. if (in_compat_syscall() && cmd != PTP_ENABLE_PPS && cmd != PTP_ENABLE_PPS2)
  425. arg = (unsigned long)compat_ptr(arg);
  426. argptr = (void __force __user *)arg;
  427. switch (cmd) {
  428. case PTP_CLOCK_GETCAPS:
  429. case PTP_CLOCK_GETCAPS2:
  430. return ptp_clock_getcaps(ptp, argptr);
  431. case PTP_EXTTS_REQUEST:
  432. case PTP_EXTTS_REQUEST2:
  433. if ((pccontext->fp->f_mode & FMODE_WRITE) == 0)
  434. return -EACCES;
  435. return ptp_extts_request(ptp, cmd, argptr);
  436. case PTP_PEROUT_REQUEST:
  437. case PTP_PEROUT_REQUEST2:
  438. if ((pccontext->fp->f_mode & FMODE_WRITE) == 0)
  439. return -EACCES;
  440. return ptp_perout_request(ptp, cmd, argptr);
  441. case PTP_ENABLE_PPS:
  442. case PTP_ENABLE_PPS2:
  443. if ((pccontext->fp->f_mode & FMODE_WRITE) == 0)
  444. return -EACCES;
  445. return ptp_enable_pps(ptp, !!arg);
  446. case PTP_SYS_OFFSET_PRECISE:
  447. case PTP_SYS_OFFSET_PRECISE2:
  448. return ptp_sys_offset_precise(ptp, argptr,
  449. ptp->info->getcrosststamp);
  450. case PTP_SYS_OFFSET_EXTENDED:
  451. case PTP_SYS_OFFSET_EXTENDED2:
  452. return ptp_sys_offset_extended(ptp, argptr,
  453. ptp->info->gettimex64);
  454. case PTP_SYS_OFFSET:
  455. case PTP_SYS_OFFSET2:
  456. return ptp_sys_offset(ptp, argptr);
  457. case PTP_PIN_GETFUNC:
  458. case PTP_PIN_GETFUNC2:
  459. return ptp_pin_getfunc(ptp, cmd, argptr);
  460. case PTP_PIN_SETFUNC:
  461. case PTP_PIN_SETFUNC2:
  462. if ((pccontext->fp->f_mode & FMODE_WRITE) == 0)
  463. return -EACCES;
  464. return ptp_pin_setfunc(ptp, cmd, argptr);
  465. case PTP_MASK_CLEAR_ALL:
  466. return ptp_mask_clear_all(pccontext->private_clkdata);
  467. case PTP_MASK_EN_SINGLE:
  468. return ptp_mask_en_single(pccontext->private_clkdata, argptr);
  469. case PTP_SYS_OFFSET_PRECISE_CYCLES:
  470. if (!ptp->has_cycles)
  471. return -EOPNOTSUPP;
  472. return ptp_sys_offset_precise(ptp, argptr,
  473. ptp->info->getcrosscycles);
  474. case PTP_SYS_OFFSET_EXTENDED_CYCLES:
  475. if (!ptp->has_cycles)
  476. return -EOPNOTSUPP;
  477. return ptp_sys_offset_extended(ptp, argptr,
  478. ptp->info->getcyclesx64);
  479. default:
  480. return -ENOTTY;
  481. }
  482. }
  483. __poll_t ptp_poll(struct posix_clock_context *pccontext, struct file *fp,
  484. poll_table *wait)
  485. {
  486. struct ptp_clock *ptp =
  487. container_of(pccontext->clk, struct ptp_clock, clock);
  488. struct timestamp_event_queue *queue;
  489. queue = pccontext->private_clkdata;
  490. if (!queue)
  491. return EPOLLERR;
  492. poll_wait(fp, &ptp->tsev_wq, wait);
  493. return queue_cnt(queue) ? EPOLLIN : 0;
  494. }
  495. #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
  496. ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags,
  497. char __user *buf, size_t cnt)
  498. {
  499. struct ptp_clock *ptp = container_of(pccontext->clk, struct ptp_clock, clock);
  500. struct timestamp_event_queue *queue;
  501. struct ptp_extts_event *event;
  502. ssize_t result;
  503. queue = pccontext->private_clkdata;
  504. if (!queue)
  505. return -EINVAL;
  506. if (cnt % sizeof(*event) != 0)
  507. return -EINVAL;
  508. if (cnt > EXTTS_BUFSIZE)
  509. cnt = EXTTS_BUFSIZE;
  510. if (wait_event_interruptible(ptp->tsev_wq, ptp->defunct || queue_cnt(queue)))
  511. return -ERESTARTSYS;
  512. if (ptp->defunct)
  513. return -ENODEV;
  514. event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
  515. if (!event)
  516. return -ENOMEM;
  517. scoped_guard(spinlock_irq, &queue->lock) {
  518. size_t qcnt = min((size_t)queue_cnt(queue), cnt / sizeof(*event));
  519. for (size_t i = 0; i < qcnt; i++) {
  520. event[i] = queue->buf[queue->head];
  521. /* Paired with READ_ONCE() in queue_cnt() */
  522. WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
  523. }
  524. cnt = qcnt * sizeof(*event);
  525. }
  526. result = cnt;
  527. if (copy_to_user(buf, event, cnt))
  528. result = -EFAULT;
  529. kfree(event);
  530. return result;
  531. }