pps.c 12 KB

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