pps.c 7.1 KB

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