pps.c 6.9 KB

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