pps.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/sched.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/idr.h>
  27. #include <linux/cdev.h>
  28. #include <linux/poll.h>
  29. #include <linux/pps_kernel.h>
  30. /*
  31. * Local variables
  32. */
  33. static dev_t pps_devt;
  34. static struct class *pps_class;
  35. /*
  36. * Char device methods
  37. */
  38. static unsigned int pps_cdev_poll(struct file *file, poll_table *wait)
  39. {
  40. struct pps_device *pps = file->private_data;
  41. poll_wait(file, &pps->queue, wait);
  42. return POLLIN | POLLRDNORM;
  43. }
  44. static int pps_cdev_fasync(int fd, struct file *file, int on)
  45. {
  46. struct pps_device *pps = file->private_data;
  47. return fasync_helper(fd, file, on, &pps->async_queue);
  48. }
  49. static long pps_cdev_ioctl(struct file *file,
  50. unsigned int cmd, unsigned long arg)
  51. {
  52. struct pps_device *pps = file->private_data;
  53. struct pps_kparams params;
  54. struct pps_fdata fdata;
  55. unsigned long ticks;
  56. void __user *uarg = (void __user *) arg;
  57. int __user *iuarg = (int __user *) arg;
  58. int err;
  59. switch (cmd) {
  60. case PPS_GETPARAMS:
  61. pr_debug("PPS_GETPARAMS: source %d\n", pps->id);
  62. spin_lock_irq(&pps->lock);
  63. /* Get the current parameters */
  64. params = pps->params;
  65. spin_unlock_irq(&pps->lock);
  66. err = copy_to_user(uarg, &params, sizeof(struct pps_kparams));
  67. if (err)
  68. return -EFAULT;
  69. break;
  70. case PPS_SETPARAMS:
  71. pr_debug("PPS_SETPARAMS: source %d\n", pps->id);
  72. /* Check the capabilities */
  73. if (!capable(CAP_SYS_TIME))
  74. return -EPERM;
  75. err = copy_from_user(&params, uarg, sizeof(struct pps_kparams));
  76. if (err)
  77. return -EFAULT;
  78. if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
  79. pr_debug("capture mode unspecified (%x)\n",
  80. params.mode);
  81. return -EINVAL;
  82. }
  83. /* Check for supported capabilities */
  84. if ((params.mode & ~pps->info.mode) != 0) {
  85. pr_debug("unsupported capabilities (%x)\n",
  86. params.mode);
  87. return -EINVAL;
  88. }
  89. spin_lock_irq(&pps->lock);
  90. /* Save the new parameters */
  91. pps->params = params;
  92. /* Restore the read only parameters */
  93. if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
  94. /* section 3.3 of RFC 2783 interpreted */
  95. pr_debug("time format unspecified (%x)\n",
  96. params.mode);
  97. pps->params.mode |= PPS_TSFMT_TSPEC;
  98. }
  99. if (pps->info.mode & PPS_CANWAIT)
  100. pps->params.mode |= PPS_CANWAIT;
  101. pps->params.api_version = PPS_API_VERS;
  102. spin_unlock_irq(&pps->lock);
  103. break;
  104. case PPS_GETCAP:
  105. pr_debug("PPS_GETCAP: source %d\n", pps->id);
  106. err = put_user(pps->info.mode, iuarg);
  107. if (err)
  108. return -EFAULT;
  109. break;
  110. case PPS_FETCH:
  111. pr_debug("PPS_FETCH: source %d\n", pps->id);
  112. err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
  113. if (err)
  114. return -EFAULT;
  115. pps->go = 0;
  116. /* Manage the timeout */
  117. if (fdata.timeout.flags & PPS_TIME_INVALID)
  118. err = wait_event_interruptible(pps->queue, pps->go);
  119. else {
  120. pr_debug("timeout %lld.%09d\n",
  121. (long long) fdata.timeout.sec,
  122. fdata.timeout.nsec);
  123. ticks = fdata.timeout.sec * HZ;
  124. ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ);
  125. if (ticks != 0) {
  126. err = wait_event_interruptible_timeout(
  127. pps->queue, pps->go, ticks);
  128. if (err == 0)
  129. return -ETIMEDOUT;
  130. }
  131. }
  132. /* Check for pending signals */
  133. if (err == -ERESTARTSYS) {
  134. pr_debug("pending signal caught\n");
  135. return -EINTR;
  136. }
  137. /* Return the fetched timestamp */
  138. spin_lock_irq(&pps->lock);
  139. fdata.info.assert_sequence = pps->assert_sequence;
  140. fdata.info.clear_sequence = pps->clear_sequence;
  141. fdata.info.assert_tu = pps->assert_tu;
  142. fdata.info.clear_tu = pps->clear_tu;
  143. fdata.info.current_mode = pps->current_mode;
  144. spin_unlock_irq(&pps->lock);
  145. err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata));
  146. if (err)
  147. return -EFAULT;
  148. break;
  149. default:
  150. return -ENOTTY;
  151. break;
  152. }
  153. return 0;
  154. }
  155. static int pps_cdev_open(struct inode *inode, struct file *file)
  156. {
  157. struct pps_device *pps = container_of(inode->i_cdev,
  158. struct pps_device, cdev);
  159. int found;
  160. found = pps_get_source(pps->id) != 0;
  161. if (!found)
  162. return -ENODEV;
  163. file->private_data = pps;
  164. return 0;
  165. }
  166. static int pps_cdev_release(struct inode *inode, struct file *file)
  167. {
  168. struct pps_device *pps = file->private_data;
  169. /* Free the PPS source and wake up (possible) deregistration */
  170. pps_put_source(pps);
  171. return 0;
  172. }
  173. /*
  174. * Char device stuff
  175. */
  176. static const struct file_operations pps_cdev_fops = {
  177. .owner = THIS_MODULE,
  178. .llseek = no_llseek,
  179. .poll = pps_cdev_poll,
  180. .fasync = pps_cdev_fasync,
  181. .unlocked_ioctl = pps_cdev_ioctl,
  182. .open = pps_cdev_open,
  183. .release = pps_cdev_release,
  184. };
  185. int pps_register_cdev(struct pps_device *pps)
  186. {
  187. int err;
  188. pps->devno = MKDEV(MAJOR(pps_devt), pps->id);
  189. cdev_init(&pps->cdev, &pps_cdev_fops);
  190. pps->cdev.owner = pps->info.owner;
  191. err = cdev_add(&pps->cdev, pps->devno, 1);
  192. if (err) {
  193. printk(KERN_ERR "pps: %s: failed to add char device %d:%d\n",
  194. pps->info.name, MAJOR(pps_devt), pps->id);
  195. return err;
  196. }
  197. pps->dev = device_create(pps_class, pps->info.dev, pps->devno, NULL,
  198. "pps%d", pps->id);
  199. if (IS_ERR(pps->dev))
  200. goto del_cdev;
  201. dev_set_drvdata(pps->dev, pps);
  202. pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
  203. MAJOR(pps_devt), pps->id);
  204. return 0;
  205. del_cdev:
  206. cdev_del(&pps->cdev);
  207. return err;
  208. }
  209. void pps_unregister_cdev(struct pps_device *pps)
  210. {
  211. device_destroy(pps_class, pps->devno);
  212. cdev_del(&pps->cdev);
  213. }
  214. /*
  215. * Module stuff
  216. */
  217. static void __exit pps_exit(void)
  218. {
  219. class_destroy(pps_class);
  220. unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
  221. }
  222. static int __init pps_init(void)
  223. {
  224. int err;
  225. pps_class = class_create(THIS_MODULE, "pps");
  226. if (!pps_class) {
  227. printk(KERN_ERR "pps: failed to allocate class\n");
  228. return -ENOMEM;
  229. }
  230. pps_class->dev_attrs = pps_attrs;
  231. err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
  232. if (err < 0) {
  233. printk(KERN_ERR "pps: failed to allocate char device region\n");
  234. goto remove_class;
  235. }
  236. pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS);
  237. pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti "
  238. "<giometti@linux.it>\n", PPS_VERSION);
  239. return 0;
  240. remove_class:
  241. class_destroy(pps_class);
  242. return err;
  243. }
  244. subsys_initcall(pps_init);
  245. module_exit(pps_exit);
  246. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  247. MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
  248. MODULE_LICENSE("GPL");