pps.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. /*
  34. * Local variables
  35. */
  36. static dev_t pps_devt;
  37. static struct class *pps_class;
  38. static DEFINE_MUTEX(pps_idr_lock);
  39. static DEFINE_IDR(pps_idr);
  40. /*
  41. * Char device methods
  42. */
  43. static unsigned int pps_cdev_poll(struct file *file, poll_table *wait)
  44. {
  45. struct pps_device *pps = file->private_data;
  46. poll_wait(file, &pps->queue, wait);
  47. return POLLIN | POLLRDNORM;
  48. }
  49. static int pps_cdev_fasync(int fd, struct file *file, int on)
  50. {
  51. struct pps_device *pps = file->private_data;
  52. return fasync_helper(fd, file, on, &pps->async_queue);
  53. }
  54. static long pps_cdev_ioctl(struct file *file,
  55. unsigned int cmd, unsigned long arg)
  56. {
  57. struct pps_device *pps = file->private_data;
  58. struct pps_kparams params;
  59. void __user *uarg = (void __user *) arg;
  60. int __user *iuarg = (int __user *) arg;
  61. int err;
  62. switch (cmd) {
  63. case PPS_GETPARAMS:
  64. dev_dbg(pps->dev, "PPS_GETPARAMS\n");
  65. spin_lock_irq(&pps->lock);
  66. /* Get the current parameters */
  67. params = pps->params;
  68. spin_unlock_irq(&pps->lock);
  69. err = copy_to_user(uarg, &params, sizeof(struct pps_kparams));
  70. if (err)
  71. return -EFAULT;
  72. break;
  73. case PPS_SETPARAMS:
  74. dev_dbg(pps->dev, "PPS_SETPARAMS\n");
  75. /* Check the capabilities */
  76. if (!capable(CAP_SYS_TIME))
  77. return -EPERM;
  78. err = copy_from_user(&params, uarg, sizeof(struct pps_kparams));
  79. if (err)
  80. return -EFAULT;
  81. if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
  82. dev_dbg(pps->dev, "capture mode unspecified (%x)\n",
  83. params.mode);
  84. return -EINVAL;
  85. }
  86. /* Check for supported capabilities */
  87. if ((params.mode & ~pps->info.mode) != 0) {
  88. dev_dbg(pps->dev, "unsupported capabilities (%x)\n",
  89. params.mode);
  90. return -EINVAL;
  91. }
  92. spin_lock_irq(&pps->lock);
  93. /* Save the new parameters */
  94. pps->params = params;
  95. /* Restore the read only parameters */
  96. if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
  97. /* section 3.3 of RFC 2783 interpreted */
  98. dev_dbg(pps->dev, "time format unspecified (%x)\n",
  99. params.mode);
  100. pps->params.mode |= PPS_TSFMT_TSPEC;
  101. }
  102. if (pps->info.mode & PPS_CANWAIT)
  103. pps->params.mode |= PPS_CANWAIT;
  104. pps->params.api_version = PPS_API_VERS;
  105. spin_unlock_irq(&pps->lock);
  106. break;
  107. case PPS_GETCAP:
  108. dev_dbg(pps->dev, "PPS_GETCAP\n");
  109. err = put_user(pps->info.mode, iuarg);
  110. if (err)
  111. return -EFAULT;
  112. break;
  113. case PPS_FETCH: {
  114. struct pps_fdata fdata;
  115. unsigned int ev;
  116. dev_dbg(pps->dev, "PPS_FETCH\n");
  117. err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
  118. if (err)
  119. return -EFAULT;
  120. ev = pps->last_ev;
  121. /* Manage the timeout */
  122. if (fdata.timeout.flags & PPS_TIME_INVALID)
  123. err = wait_event_interruptible(pps->queue,
  124. ev != pps->last_ev);
  125. else {
  126. unsigned long ticks;
  127. dev_dbg(pps->dev, "timeout %lld.%09d\n",
  128. (long long) fdata.timeout.sec,
  129. fdata.timeout.nsec);
  130. ticks = fdata.timeout.sec * HZ;
  131. ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ);
  132. if (ticks != 0) {
  133. err = wait_event_interruptible_timeout(
  134. pps->queue,
  135. ev != pps->last_ev,
  136. ticks);
  137. if (err == 0)
  138. return -ETIMEDOUT;
  139. }
  140. }
  141. /* Check for pending signals */
  142. if (err == -ERESTARTSYS) {
  143. dev_dbg(pps->dev, "pending signal caught\n");
  144. return -EINTR;
  145. }
  146. /* Return the fetched timestamp */
  147. spin_lock_irq(&pps->lock);
  148. fdata.info.assert_sequence = pps->assert_sequence;
  149. fdata.info.clear_sequence = pps->clear_sequence;
  150. fdata.info.assert_tu = pps->assert_tu;
  151. fdata.info.clear_tu = pps->clear_tu;
  152. fdata.info.current_mode = pps->current_mode;
  153. spin_unlock_irq(&pps->lock);
  154. err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata));
  155. if (err)
  156. return -EFAULT;
  157. break;
  158. }
  159. default:
  160. return -ENOTTY;
  161. break;
  162. }
  163. return 0;
  164. }
  165. static int pps_cdev_open(struct inode *inode, struct file *file)
  166. {
  167. struct pps_device *pps = container_of(inode->i_cdev,
  168. struct pps_device, cdev);
  169. file->private_data = pps;
  170. return 0;
  171. }
  172. static int pps_cdev_release(struct inode *inode, struct file *file)
  173. {
  174. return 0;
  175. }
  176. /*
  177. * Char device stuff
  178. */
  179. static const struct file_operations pps_cdev_fops = {
  180. .owner = THIS_MODULE,
  181. .llseek = no_llseek,
  182. .poll = pps_cdev_poll,
  183. .fasync = pps_cdev_fasync,
  184. .unlocked_ioctl = pps_cdev_ioctl,
  185. .open = pps_cdev_open,
  186. .release = pps_cdev_release,
  187. };
  188. static void pps_device_destruct(struct device *dev)
  189. {
  190. struct pps_device *pps = dev_get_drvdata(dev);
  191. /* release id here to protect others from using it while it's
  192. * still in use */
  193. mutex_lock(&pps_idr_lock);
  194. idr_remove(&pps_idr, pps->id);
  195. mutex_unlock(&pps_idr_lock);
  196. kfree(dev);
  197. kfree(pps);
  198. }
  199. int pps_register_cdev(struct pps_device *pps)
  200. {
  201. int err;
  202. dev_t devt;
  203. mutex_lock(&pps_idr_lock);
  204. /* Get new ID for the new PPS source */
  205. if (idr_pre_get(&pps_idr, GFP_KERNEL) == 0) {
  206. mutex_unlock(&pps_idr_lock);
  207. return -ENOMEM;
  208. }
  209. /* Now really allocate the PPS source.
  210. * After idr_get_new() calling the new source will be freely available
  211. * into the kernel.
  212. */
  213. err = idr_get_new(&pps_idr, pps, &pps->id);
  214. mutex_unlock(&pps_idr_lock);
  215. if (err < 0)
  216. return err;
  217. pps->id &= MAX_ID_MASK;
  218. if (pps->id >= PPS_MAX_SOURCES) {
  219. pr_err("%s: too many PPS sources in the system\n",
  220. pps->info.name);
  221. err = -EBUSY;
  222. goto free_idr;
  223. }
  224. devt = MKDEV(MAJOR(pps_devt), pps->id);
  225. cdev_init(&pps->cdev, &pps_cdev_fops);
  226. pps->cdev.owner = pps->info.owner;
  227. err = cdev_add(&pps->cdev, devt, 1);
  228. if (err) {
  229. pr_err("%s: failed to add char device %d:%d\n",
  230. pps->info.name, MAJOR(pps_devt), pps->id);
  231. goto free_idr;
  232. }
  233. pps->dev = device_create(pps_class, pps->info.dev, devt, pps,
  234. "pps%d", pps->id);
  235. if (IS_ERR(pps->dev))
  236. goto del_cdev;
  237. pps->dev->release = pps_device_destruct;
  238. pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
  239. MAJOR(pps_devt), pps->id);
  240. return 0;
  241. del_cdev:
  242. cdev_del(&pps->cdev);
  243. free_idr:
  244. mutex_lock(&pps_idr_lock);
  245. idr_remove(&pps_idr, pps->id);
  246. mutex_unlock(&pps_idr_lock);
  247. return err;
  248. }
  249. void pps_unregister_cdev(struct pps_device *pps)
  250. {
  251. device_destroy(pps_class, pps->dev->devt);
  252. cdev_del(&pps->cdev);
  253. }
  254. /*
  255. * Module stuff
  256. */
  257. static void __exit pps_exit(void)
  258. {
  259. class_destroy(pps_class);
  260. unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
  261. }
  262. static int __init pps_init(void)
  263. {
  264. int err;
  265. pps_class = class_create(THIS_MODULE, "pps");
  266. if (!pps_class) {
  267. pr_err("failed to allocate class\n");
  268. return -ENOMEM;
  269. }
  270. pps_class->dev_attrs = pps_attrs;
  271. err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
  272. if (err < 0) {
  273. pr_err("failed to allocate char device region\n");
  274. goto remove_class;
  275. }
  276. pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS);
  277. pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti "
  278. "<giometti@linux.it>\n", PPS_VERSION);
  279. return 0;
  280. remove_class:
  281. class_destroy(pps_class);
  282. return err;
  283. }
  284. subsys_initcall(pps_init);
  285. module_exit(pps_exit);
  286. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  287. MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
  288. MODULE_LICENSE("GPL");