pps.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. /* Return current parameters */
  63. err = copy_to_user(uarg, &pps->params,
  64. 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. pr_debug("PPS_FETCH: source %d\n", pps->id);
  110. err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
  111. if (err)
  112. return -EFAULT;
  113. pps->go = 0;
  114. /* Manage the timeout */
  115. if (fdata.timeout.flags & PPS_TIME_INVALID)
  116. err = wait_event_interruptible(pps->queue, pps->go);
  117. else {
  118. pr_debug("timeout %lld.%09d\n",
  119. (long long) fdata.timeout.sec,
  120. fdata.timeout.nsec);
  121. ticks = fdata.timeout.sec * HZ;
  122. ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ);
  123. if (ticks != 0) {
  124. err = wait_event_interruptible_timeout(
  125. pps->queue, pps->go, ticks);
  126. if (err == 0)
  127. return -ETIMEDOUT;
  128. }
  129. }
  130. /* Check for pending signals */
  131. if (err == -ERESTARTSYS) {
  132. pr_debug("pending signal caught\n");
  133. return -EINTR;
  134. }
  135. /* Return the fetched timestamp */
  136. spin_lock_irq(&pps->lock);
  137. fdata.info.assert_sequence = pps->assert_sequence;
  138. fdata.info.clear_sequence = pps->clear_sequence;
  139. fdata.info.assert_tu = pps->assert_tu;
  140. fdata.info.clear_tu = pps->clear_tu;
  141. fdata.info.current_mode = pps->current_mode;
  142. spin_unlock_irq(&pps->lock);
  143. err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata));
  144. if (err)
  145. return -EFAULT;
  146. break;
  147. default:
  148. return -ENOTTY;
  149. break;
  150. }
  151. return 0;
  152. }
  153. static int pps_cdev_open(struct inode *inode, struct file *file)
  154. {
  155. struct pps_device *pps = container_of(inode->i_cdev,
  156. struct pps_device, cdev);
  157. int found;
  158. found = pps_get_source(pps->id) != 0;
  159. if (!found)
  160. return -ENODEV;
  161. file->private_data = pps;
  162. return 0;
  163. }
  164. static int pps_cdev_release(struct inode *inode, struct file *file)
  165. {
  166. struct pps_device *pps = file->private_data;
  167. /* Free the PPS source and wake up (possible) deregistration */
  168. pps_put_source(pps);
  169. return 0;
  170. }
  171. /*
  172. * Char device stuff
  173. */
  174. static const struct file_operations pps_cdev_fops = {
  175. .owner = THIS_MODULE,
  176. .llseek = no_llseek,
  177. .poll = pps_cdev_poll,
  178. .fasync = pps_cdev_fasync,
  179. .unlocked_ioctl = pps_cdev_ioctl,
  180. .open = pps_cdev_open,
  181. .release = pps_cdev_release,
  182. };
  183. int pps_register_cdev(struct pps_device *pps)
  184. {
  185. int err;
  186. pps->devno = MKDEV(MAJOR(pps_devt), pps->id);
  187. cdev_init(&pps->cdev, &pps_cdev_fops);
  188. pps->cdev.owner = pps->info.owner;
  189. err = cdev_add(&pps->cdev, pps->devno, 1);
  190. if (err) {
  191. printk(KERN_ERR "pps: %s: failed to add char device %d:%d\n",
  192. pps->info.name, MAJOR(pps_devt), pps->id);
  193. return err;
  194. }
  195. pps->dev = device_create(pps_class, pps->info.dev, pps->devno, NULL,
  196. "pps%d", pps->id);
  197. if (err)
  198. goto del_cdev;
  199. dev_set_drvdata(pps->dev, pps);
  200. pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
  201. MAJOR(pps_devt), pps->id);
  202. return 0;
  203. del_cdev:
  204. cdev_del(&pps->cdev);
  205. return err;
  206. }
  207. void pps_unregister_cdev(struct pps_device *pps)
  208. {
  209. device_destroy(pps_class, pps->devno);
  210. cdev_del(&pps->cdev);
  211. }
  212. /*
  213. * Module stuff
  214. */
  215. static void __exit pps_exit(void)
  216. {
  217. class_destroy(pps_class);
  218. unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
  219. }
  220. static int __init pps_init(void)
  221. {
  222. int err;
  223. pps_class = class_create(THIS_MODULE, "pps");
  224. if (!pps_class) {
  225. printk(KERN_ERR "pps: failed to allocate class\n");
  226. return -ENOMEM;
  227. }
  228. pps_class->dev_attrs = pps_attrs;
  229. err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
  230. if (err < 0) {
  231. printk(KERN_ERR "pps: failed to allocate char device region\n");
  232. goto remove_class;
  233. }
  234. pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS);
  235. pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti "
  236. "<giometti@linux.it>\n", PPS_VERSION);
  237. return 0;
  238. remove_class:
  239. class_destroy(pps_class);
  240. return err;
  241. }
  242. subsys_initcall(pps_init);
  243. module_exit(pps_exit);
  244. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  245. MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
  246. MODULE_LICENSE("GPL");