pps.c 11 KB

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