pps.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 unsigned int 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 POLLIN | POLLRDNORM;
  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 long pps_cdev_ioctl(struct file *file,
  56. unsigned int cmd, unsigned long arg)
  57. {
  58. struct pps_device *pps = file->private_data;
  59. struct pps_kparams params;
  60. void __user *uarg = (void __user *) arg;
  61. int __user *iuarg = (int __user *) arg;
  62. int err;
  63. switch (cmd) {
  64. case PPS_GETPARAMS:
  65. dev_dbg(pps->dev, "PPS_GETPARAMS\n");
  66. spin_lock_irq(&pps->lock);
  67. /* Get the current parameters */
  68. params = pps->params;
  69. spin_unlock_irq(&pps->lock);
  70. err = copy_to_user(uarg, &params, sizeof(struct pps_kparams));
  71. if (err)
  72. return -EFAULT;
  73. break;
  74. case PPS_SETPARAMS:
  75. dev_dbg(pps->dev, "PPS_SETPARAMS\n");
  76. /* Check the capabilities */
  77. if (!capable(CAP_SYS_TIME))
  78. return -EPERM;
  79. err = copy_from_user(&params, uarg, sizeof(struct pps_kparams));
  80. if (err)
  81. return -EFAULT;
  82. if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
  83. dev_dbg(pps->dev, "capture mode unspecified (%x)\n",
  84. params.mode);
  85. return -EINVAL;
  86. }
  87. /* Check for supported capabilities */
  88. if ((params.mode & ~pps->info.mode) != 0) {
  89. dev_dbg(pps->dev, "unsupported capabilities (%x)\n",
  90. params.mode);
  91. return -EINVAL;
  92. }
  93. spin_lock_irq(&pps->lock);
  94. /* Save the new parameters */
  95. pps->params = params;
  96. /* Restore the read only parameters */
  97. if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
  98. /* section 3.3 of RFC 2783 interpreted */
  99. dev_dbg(pps->dev, "time format unspecified (%x)\n",
  100. params.mode);
  101. pps->params.mode |= PPS_TSFMT_TSPEC;
  102. }
  103. if (pps->info.mode & PPS_CANWAIT)
  104. pps->params.mode |= PPS_CANWAIT;
  105. pps->params.api_version = PPS_API_VERS;
  106. spin_unlock_irq(&pps->lock);
  107. break;
  108. case PPS_GETCAP:
  109. dev_dbg(pps->dev, "PPS_GETCAP\n");
  110. err = put_user(pps->info.mode, iuarg);
  111. if (err)
  112. return -EFAULT;
  113. break;
  114. case PPS_FETCH: {
  115. struct pps_fdata fdata;
  116. unsigned int ev;
  117. dev_dbg(pps->dev, "PPS_FETCH\n");
  118. err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
  119. if (err)
  120. return -EFAULT;
  121. ev = pps->last_ev;
  122. /* Manage the timeout */
  123. if (fdata.timeout.flags & PPS_TIME_INVALID)
  124. err = wait_event_interruptible(pps->queue,
  125. ev != pps->last_ev);
  126. else {
  127. unsigned long ticks;
  128. dev_dbg(pps->dev, "timeout %lld.%09d\n",
  129. (long long) fdata.timeout.sec,
  130. fdata.timeout.nsec);
  131. ticks = fdata.timeout.sec * HZ;
  132. ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ);
  133. if (ticks != 0) {
  134. err = wait_event_interruptible_timeout(
  135. pps->queue,
  136. ev != pps->last_ev,
  137. ticks);
  138. if (err == 0)
  139. return -ETIMEDOUT;
  140. }
  141. }
  142. /* Check for pending signals */
  143. if (err == -ERESTARTSYS) {
  144. dev_dbg(pps->dev, "pending signal caught\n");
  145. return -EINTR;
  146. }
  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. static int pps_cdev_open(struct inode *inode, struct file *file)
  194. {
  195. struct pps_device *pps = container_of(inode->i_cdev,
  196. struct pps_device, cdev);
  197. file->private_data = pps;
  198. kobject_get(&pps->dev->kobj);
  199. return 0;
  200. }
  201. static int pps_cdev_release(struct inode *inode, struct file *file)
  202. {
  203. struct pps_device *pps = container_of(inode->i_cdev,
  204. struct pps_device, cdev);
  205. kobject_put(&pps->dev->kobj);
  206. return 0;
  207. }
  208. /*
  209. * Char device stuff
  210. */
  211. static const struct file_operations pps_cdev_fops = {
  212. .owner = THIS_MODULE,
  213. .llseek = no_llseek,
  214. .poll = pps_cdev_poll,
  215. .fasync = pps_cdev_fasync,
  216. .unlocked_ioctl = pps_cdev_ioctl,
  217. .open = pps_cdev_open,
  218. .release = pps_cdev_release,
  219. };
  220. static void pps_device_destruct(struct device *dev)
  221. {
  222. struct pps_device *pps = dev_get_drvdata(dev);
  223. cdev_del(&pps->cdev);
  224. /* Now we can release the ID for re-use */
  225. pr_debug("deallocating pps%d\n", pps->id);
  226. mutex_lock(&pps_idr_lock);
  227. idr_remove(&pps_idr, pps->id);
  228. mutex_unlock(&pps_idr_lock);
  229. kfree(dev);
  230. kfree(pps);
  231. }
  232. int pps_register_cdev(struct pps_device *pps)
  233. {
  234. int err;
  235. dev_t devt;
  236. mutex_lock(&pps_idr_lock);
  237. /*
  238. * Get new ID for the new PPS source. After idr_alloc() calling
  239. * the new source will be freely available into the kernel.
  240. */
  241. err = idr_alloc(&pps_idr, pps, 0, PPS_MAX_SOURCES, GFP_KERNEL);
  242. if (err < 0) {
  243. if (err == -ENOSPC) {
  244. pr_err("%s: too many PPS sources in the system\n",
  245. pps->info.name);
  246. err = -EBUSY;
  247. }
  248. goto out_unlock;
  249. }
  250. pps->id = err;
  251. mutex_unlock(&pps_idr_lock);
  252. devt = MKDEV(MAJOR(pps_devt), pps->id);
  253. cdev_init(&pps->cdev, &pps_cdev_fops);
  254. pps->cdev.owner = pps->info.owner;
  255. err = cdev_add(&pps->cdev, devt, 1);
  256. if (err) {
  257. pr_err("%s: failed to add char device %d:%d\n",
  258. pps->info.name, MAJOR(pps_devt), pps->id);
  259. goto free_idr;
  260. }
  261. pps->dev = device_create(pps_class, pps->info.dev, devt, pps,
  262. "pps%d", pps->id);
  263. if (IS_ERR(pps->dev)) {
  264. err = PTR_ERR(pps->dev);
  265. goto del_cdev;
  266. }
  267. /* Override the release function with our own */
  268. pps->dev->release = pps_device_destruct;
  269. pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
  270. MAJOR(pps_devt), pps->id);
  271. return 0;
  272. del_cdev:
  273. cdev_del(&pps->cdev);
  274. free_idr:
  275. mutex_lock(&pps_idr_lock);
  276. idr_remove(&pps_idr, pps->id);
  277. out_unlock:
  278. mutex_unlock(&pps_idr_lock);
  279. return err;
  280. }
  281. void pps_unregister_cdev(struct pps_device *pps)
  282. {
  283. pr_debug("unregistering pps%d\n", pps->id);
  284. pps->lookup_cookie = NULL;
  285. device_destroy(pps_class, pps->dev->devt);
  286. }
  287. /*
  288. * Look up a pps device by magic cookie.
  289. * The cookie is usually a pointer to some enclosing device, but this
  290. * code doesn't care; you should never be dereferencing it.
  291. *
  292. * This is a bit of a kludge that is currently used only by the PPS
  293. * serial line discipline. It may need to be tweaked when a second user
  294. * is found.
  295. *
  296. * There is no function interface for setting the lookup_cookie field.
  297. * It's initialized to NULL when the pps device is created, and if a
  298. * client wants to use it, just fill it in afterward.
  299. *
  300. * The cookie is automatically set to NULL in pps_unregister_source()
  301. * so that it will not be used again, even if the pps device cannot
  302. * be removed from the idr due to pending references holding the minor
  303. * number in use.
  304. */
  305. struct pps_device *pps_lookup_dev(void const *cookie)
  306. {
  307. struct pps_device *pps;
  308. unsigned id;
  309. rcu_read_lock();
  310. idr_for_each_entry(&pps_idr, pps, id)
  311. if (cookie == pps->lookup_cookie)
  312. break;
  313. rcu_read_unlock();
  314. return pps;
  315. }
  316. EXPORT_SYMBOL(pps_lookup_dev);
  317. /*
  318. * Module stuff
  319. */
  320. static void __exit pps_exit(void)
  321. {
  322. class_destroy(pps_class);
  323. unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
  324. }
  325. static int __init pps_init(void)
  326. {
  327. int err;
  328. pps_class = class_create(THIS_MODULE, "pps");
  329. if (IS_ERR(pps_class)) {
  330. pr_err("failed to allocate class\n");
  331. return PTR_ERR(pps_class);
  332. }
  333. pps_class->dev_groups = pps_groups;
  334. err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
  335. if (err < 0) {
  336. pr_err("failed to allocate char device region\n");
  337. goto remove_class;
  338. }
  339. pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS);
  340. pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti "
  341. "<giometti@linux.it>\n", PPS_VERSION);
  342. return 0;
  343. remove_class:
  344. class_destroy(pps_class);
  345. return err;
  346. }
  347. subsys_initcall(pps_init);
  348. module_exit(pps_exit);
  349. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  350. MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
  351. MODULE_LICENSE("GPL");