pps.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PPS core file
  4. *
  5. * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/sched.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/idr.h>
  14. #include <linux/mutex.h>
  15. #include <linux/cdev.h>
  16. #include <linux/poll.h>
  17. #include <linux/pps_kernel.h>
  18. #include <linux/slab.h>
  19. #include "kc.h"
  20. /*
  21. * Local variables
  22. */
  23. static int pps_major;
  24. static struct class *pps_class;
  25. static DEFINE_MUTEX(pps_idr_lock);
  26. static DEFINE_IDR(pps_idr);
  27. /*
  28. * Char device methods
  29. */
  30. static __poll_t pps_cdev_poll(struct file *file, poll_table *wait)
  31. {
  32. struct pps_device *pps = file->private_data;
  33. poll_wait(file, &pps->queue, wait);
  34. if (pps->last_fetched_ev == pps->last_ev)
  35. return 0;
  36. return EPOLLIN | EPOLLRDNORM;
  37. }
  38. static int pps_cdev_fasync(int fd, struct file *file, int on)
  39. {
  40. struct pps_device *pps = file->private_data;
  41. return fasync_helper(fd, file, on, &pps->async_queue);
  42. }
  43. static int pps_cdev_pps_fetch(struct pps_device *pps, struct pps_fdata *fdata)
  44. {
  45. unsigned int ev = pps->last_ev;
  46. int err = 0;
  47. /* Manage the timeout */
  48. if (fdata->timeout.flags & PPS_TIME_INVALID)
  49. err = wait_event_interruptible(pps->queue,
  50. ev != pps->last_ev);
  51. else {
  52. unsigned long ticks;
  53. dev_dbg(&pps->dev, "timeout %lld.%09d\n",
  54. (long long) fdata->timeout.sec,
  55. fdata->timeout.nsec);
  56. ticks = fdata->timeout.sec * HZ;
  57. ticks += fdata->timeout.nsec / (NSEC_PER_SEC / HZ);
  58. if (ticks != 0) {
  59. err = wait_event_interruptible_timeout(
  60. pps->queue,
  61. ev != pps->last_ev,
  62. ticks);
  63. if (err == 0)
  64. return -ETIMEDOUT;
  65. }
  66. }
  67. /* Check for pending signals */
  68. if (err == -ERESTARTSYS) {
  69. dev_dbg(&pps->dev, "pending signal caught\n");
  70. return -EINTR;
  71. }
  72. return 0;
  73. }
  74. static long pps_cdev_ioctl(struct file *file,
  75. unsigned int cmd, unsigned long arg)
  76. {
  77. struct pps_device *pps = file->private_data;
  78. struct pps_kparams params;
  79. void __user *uarg = (void __user *) arg;
  80. int __user *iuarg = (int __user *) arg;
  81. int err;
  82. switch (cmd) {
  83. case PPS_GETPARAMS:
  84. dev_dbg(&pps->dev, "PPS_GETPARAMS\n");
  85. spin_lock_irq(&pps->lock);
  86. /* Get the current parameters */
  87. params = pps->params;
  88. spin_unlock_irq(&pps->lock);
  89. err = copy_to_user(uarg, &params, sizeof(struct pps_kparams));
  90. if (err)
  91. return -EFAULT;
  92. break;
  93. case PPS_SETPARAMS:
  94. dev_dbg(&pps->dev, "PPS_SETPARAMS\n");
  95. /* Check the capabilities */
  96. if (!capable(CAP_SYS_TIME))
  97. return -EPERM;
  98. err = copy_from_user(&params, uarg, sizeof(struct pps_kparams));
  99. if (err)
  100. return -EFAULT;
  101. if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
  102. dev_dbg(&pps->dev, "capture mode unspecified (%x)\n",
  103. params.mode);
  104. return -EINVAL;
  105. }
  106. /* Check for supported capabilities */
  107. if ((params.mode & ~pps->info.mode) != 0) {
  108. dev_dbg(&pps->dev, "unsupported capabilities (%x)\n",
  109. params.mode);
  110. return -EINVAL;
  111. }
  112. spin_lock_irq(&pps->lock);
  113. /* Save the new parameters */
  114. pps->params = params;
  115. /* Restore the read only parameters */
  116. if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
  117. /* section 3.3 of RFC 2783 interpreted */
  118. dev_dbg(&pps->dev, "time format unspecified (%x)\n",
  119. params.mode);
  120. pps->params.mode |= PPS_TSFMT_TSPEC;
  121. }
  122. if (pps->info.mode & PPS_CANWAIT)
  123. pps->params.mode |= PPS_CANWAIT;
  124. pps->params.api_version = PPS_API_VERS;
  125. /*
  126. * Clear unused fields of pps_kparams to avoid leaking
  127. * uninitialized data of the PPS_SETPARAMS caller via
  128. * PPS_GETPARAMS
  129. */
  130. pps->params.assert_off_tu.flags = 0;
  131. pps->params.clear_off_tu.flags = 0;
  132. spin_unlock_irq(&pps->lock);
  133. break;
  134. case PPS_GETCAP:
  135. dev_dbg(&pps->dev, "PPS_GETCAP\n");
  136. err = put_user(pps->info.mode, iuarg);
  137. if (err)
  138. return -EFAULT;
  139. break;
  140. case PPS_FETCH: {
  141. struct pps_fdata fdata;
  142. dev_dbg(&pps->dev, "PPS_FETCH\n");
  143. err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
  144. if (err)
  145. return -EFAULT;
  146. err = pps_cdev_pps_fetch(pps, &fdata);
  147. if (err)
  148. return err;
  149. /* Return the fetched timestamp and save last fetched event */
  150. spin_lock_irq(&pps->lock);
  151. pps->last_fetched_ev = pps->last_ev;
  152. fdata.info.assert_sequence = pps->assert_sequence;
  153. fdata.info.clear_sequence = pps->clear_sequence;
  154. fdata.info.assert_tu = pps->assert_tu;
  155. fdata.info.clear_tu = pps->clear_tu;
  156. fdata.info.current_mode = pps->current_mode;
  157. spin_unlock_irq(&pps->lock);
  158. err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata));
  159. if (err)
  160. return -EFAULT;
  161. break;
  162. }
  163. case PPS_KC_BIND: {
  164. struct pps_bind_args bind_args;
  165. dev_dbg(&pps->dev, "PPS_KC_BIND\n");
  166. /* Check the capabilities */
  167. if (!capable(CAP_SYS_TIME))
  168. return -EPERM;
  169. if (copy_from_user(&bind_args, uarg,
  170. sizeof(struct pps_bind_args)))
  171. return -EFAULT;
  172. /* Check for supported capabilities */
  173. if ((bind_args.edge & ~pps->info.mode) != 0) {
  174. dev_err(&pps->dev, "unsupported capabilities (%x)\n",
  175. bind_args.edge);
  176. return -EINVAL;
  177. }
  178. /* Validate parameters roughly */
  179. if (bind_args.tsformat != PPS_TSFMT_TSPEC ||
  180. (bind_args.edge & ~PPS_CAPTUREBOTH) != 0 ||
  181. bind_args.consumer != PPS_KC_HARDPPS) {
  182. dev_err(&pps->dev, "invalid kernel consumer bind"
  183. " parameters (%x)\n", bind_args.edge);
  184. return -EINVAL;
  185. }
  186. err = pps_kc_bind(pps, &bind_args);
  187. if (err < 0)
  188. return err;
  189. break;
  190. }
  191. default:
  192. return -ENOTTY;
  193. }
  194. return 0;
  195. }
  196. #ifdef CONFIG_COMPAT
  197. static long pps_cdev_compat_ioctl(struct file *file,
  198. unsigned int cmd, unsigned long arg)
  199. {
  200. struct pps_device *pps = file->private_data;
  201. void __user *uarg = (void __user *) arg;
  202. cmd = _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(void *));
  203. if (cmd == PPS_FETCH) {
  204. struct pps_fdata_compat compat;
  205. struct pps_fdata fdata;
  206. int err;
  207. dev_dbg(&pps->dev, "PPS_FETCH\n");
  208. err = copy_from_user(&compat, uarg, sizeof(struct pps_fdata_compat));
  209. if (err)
  210. return -EFAULT;
  211. memcpy(&fdata.timeout, &compat.timeout,
  212. sizeof(struct pps_ktime_compat));
  213. err = pps_cdev_pps_fetch(pps, &fdata);
  214. if (err)
  215. return err;
  216. /* Return the fetched timestamp and save last fetched event */
  217. spin_lock_irq(&pps->lock);
  218. pps->last_fetched_ev = pps->last_ev;
  219. compat.info.assert_sequence = pps->assert_sequence;
  220. compat.info.clear_sequence = pps->clear_sequence;
  221. compat.info.current_mode = pps->current_mode;
  222. memcpy(&compat.info.assert_tu, &pps->assert_tu,
  223. sizeof(struct pps_ktime_compat));
  224. memcpy(&compat.info.clear_tu, &pps->clear_tu,
  225. sizeof(struct pps_ktime_compat));
  226. spin_unlock_irq(&pps->lock);
  227. return copy_to_user(uarg, &compat,
  228. sizeof(struct pps_fdata_compat)) ? -EFAULT : 0;
  229. }
  230. return pps_cdev_ioctl(file, cmd, arg);
  231. }
  232. #else
  233. #define pps_cdev_compat_ioctl NULL
  234. #endif
  235. static struct pps_device *pps_idr_get(unsigned long id)
  236. {
  237. struct pps_device *pps;
  238. mutex_lock(&pps_idr_lock);
  239. pps = idr_find(&pps_idr, id);
  240. if (pps)
  241. get_device(&pps->dev);
  242. mutex_unlock(&pps_idr_lock);
  243. return pps;
  244. }
  245. static int pps_cdev_open(struct inode *inode, struct file *file)
  246. {
  247. struct pps_device *pps = pps_idr_get(iminor(inode));
  248. if (!pps)
  249. return -ENODEV;
  250. file->private_data = pps;
  251. return 0;
  252. }
  253. static int pps_cdev_release(struct inode *inode, struct file *file)
  254. {
  255. struct pps_device *pps = file->private_data;
  256. WARN_ON(pps->id != iminor(inode));
  257. put_device(&pps->dev);
  258. return 0;
  259. }
  260. /*
  261. * Char device stuff
  262. */
  263. static const struct file_operations pps_cdev_fops = {
  264. .owner = THIS_MODULE,
  265. .poll = pps_cdev_poll,
  266. .fasync = pps_cdev_fasync,
  267. .compat_ioctl = pps_cdev_compat_ioctl,
  268. .unlocked_ioctl = pps_cdev_ioctl,
  269. .open = pps_cdev_open,
  270. .release = pps_cdev_release,
  271. };
  272. static void pps_device_destruct(struct device *dev)
  273. {
  274. struct pps_device *pps = dev_get_drvdata(dev);
  275. pr_debug("deallocating pps%d\n", pps->id);
  276. kfree(pps);
  277. }
  278. int pps_register_cdev(struct pps_device *pps)
  279. {
  280. int err;
  281. mutex_lock(&pps_idr_lock);
  282. /*
  283. * Get new ID for the new PPS source. After idr_alloc() calling
  284. * the new source will be freely available into the kernel.
  285. */
  286. err = idr_alloc(&pps_idr, pps, 0, PPS_MAX_SOURCES, GFP_KERNEL);
  287. if (err < 0) {
  288. if (err == -ENOSPC) {
  289. pr_err("%s: too many PPS sources in the system\n",
  290. pps->info.name);
  291. err = -EBUSY;
  292. }
  293. kfree(pps);
  294. goto out_unlock;
  295. }
  296. pps->id = err;
  297. pps->dev.class = pps_class;
  298. pps->dev.parent = pps->info.dev;
  299. pps->dev.devt = MKDEV(pps_major, pps->id);
  300. dev_set_drvdata(&pps->dev, pps);
  301. dev_set_name(&pps->dev, "pps%d", pps->id);
  302. pps->dev.release = pps_device_destruct;
  303. err = device_register(&pps->dev);
  304. if (err)
  305. goto free_idr;
  306. pr_debug("source %s got cdev (%d:%d)\n", pps->info.name, pps_major,
  307. pps->id);
  308. get_device(&pps->dev);
  309. mutex_unlock(&pps_idr_lock);
  310. return 0;
  311. free_idr:
  312. idr_remove(&pps_idr, pps->id);
  313. put_device(&pps->dev);
  314. out_unlock:
  315. mutex_unlock(&pps_idr_lock);
  316. return err;
  317. }
  318. void pps_unregister_cdev(struct pps_device *pps)
  319. {
  320. pr_debug("unregistering pps%d\n", pps->id);
  321. pps->lookup_cookie = NULL;
  322. device_destroy(pps_class, pps->dev.devt);
  323. /* Now we can release the ID for re-use */
  324. mutex_lock(&pps_idr_lock);
  325. idr_remove(&pps_idr, pps->id);
  326. put_device(&pps->dev);
  327. mutex_unlock(&pps_idr_lock);
  328. }
  329. /*
  330. * Look up a pps device by magic cookie.
  331. * The cookie is usually a pointer to some enclosing device, but this
  332. * code doesn't care; you should never be dereferencing it.
  333. *
  334. * This is a bit of a kludge that is currently used only by the PPS
  335. * serial line discipline. It may need to be tweaked when a second user
  336. * is found.
  337. *
  338. * There is no function interface for setting the lookup_cookie field.
  339. * It's initialized to NULL when the pps device is created, and if a
  340. * client wants to use it, just fill it in afterward.
  341. *
  342. * The cookie is automatically set to NULL in pps_unregister_source()
  343. * so that it will not be used again, even if the pps device cannot
  344. * be removed from the idr due to pending references holding the minor
  345. * number in use.
  346. *
  347. * Since pps_idr holds a reference to the device, the returned
  348. * pps_device is guaranteed to be valid until pps_unregister_cdev() is
  349. * called on it. But after calling pps_unregister_cdev(), it may be
  350. * freed at any time.
  351. */
  352. struct pps_device *pps_lookup_dev(void const *cookie)
  353. {
  354. struct pps_device *pps;
  355. unsigned id;
  356. rcu_read_lock();
  357. idr_for_each_entry(&pps_idr, pps, id)
  358. if (cookie == pps->lookup_cookie)
  359. break;
  360. rcu_read_unlock();
  361. return pps;
  362. }
  363. EXPORT_SYMBOL(pps_lookup_dev);
  364. /*
  365. * Module stuff
  366. */
  367. static void __exit pps_exit(void)
  368. {
  369. class_destroy(pps_class);
  370. __unregister_chrdev(pps_major, 0, PPS_MAX_SOURCES, "pps");
  371. }
  372. static int __init pps_init(void)
  373. {
  374. pps_class = class_create("pps");
  375. if (IS_ERR(pps_class)) {
  376. pr_err("failed to allocate class\n");
  377. return PTR_ERR(pps_class);
  378. }
  379. pps_class->dev_groups = pps_groups;
  380. pps_major = __register_chrdev(0, 0, PPS_MAX_SOURCES, "pps",
  381. &pps_cdev_fops);
  382. if (pps_major < 0) {
  383. pr_err("failed to allocate char device region\n");
  384. goto remove_class;
  385. }
  386. pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS);
  387. pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti "
  388. "<giometti@linux.it>\n", PPS_VERSION);
  389. return 0;
  390. remove_class:
  391. class_destroy(pps_class);
  392. return pps_major;
  393. }
  394. subsys_initcall(pps_init);
  395. module_exit(pps_exit);
  396. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  397. MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
  398. MODULE_LICENSE("GPL");