pps-ktimer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * pps-ktimer.c -- kernel timer test client
  3. *
  4. *
  5. * Copyright (C) 2005-2006 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/time.h>
  25. #include <linux/timer.h>
  26. #include <linux/pps_kernel.h>
  27. /*
  28. * Global variables
  29. */
  30. static int source;
  31. static struct timer_list ktimer;
  32. /*
  33. * The kernel timer
  34. */
  35. static void pps_ktimer_event(unsigned long ptr)
  36. {
  37. struct timespec __ts;
  38. struct pps_ktime ts;
  39. /* First of all we get the time stamp... */
  40. getnstimeofday(&__ts);
  41. pr_info("PPS event at %lu\n", jiffies);
  42. /* ... and translate it to PPS time data struct */
  43. ts.sec = __ts.tv_sec;
  44. ts.nsec = __ts.tv_nsec;
  45. pps_event(source, &ts, PPS_CAPTUREASSERT, NULL);
  46. mod_timer(&ktimer, jiffies + HZ);
  47. }
  48. /*
  49. * The echo function
  50. */
  51. static void pps_ktimer_echo(int source, int event, void *data)
  52. {
  53. pr_info("echo %s %s for source %d\n",
  54. event & PPS_CAPTUREASSERT ? "assert" : "",
  55. event & PPS_CAPTURECLEAR ? "clear" : "",
  56. source);
  57. }
  58. /*
  59. * The PPS info struct
  60. */
  61. static struct pps_source_info pps_ktimer_info = {
  62. .name = "ktimer",
  63. .path = "",
  64. .mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
  65. PPS_ECHOASSERT |
  66. PPS_CANWAIT | PPS_TSFMT_TSPEC,
  67. .echo = pps_ktimer_echo,
  68. .owner = THIS_MODULE,
  69. };
  70. /*
  71. * Module staff
  72. */
  73. static void __exit pps_ktimer_exit(void)
  74. {
  75. del_timer_sync(&ktimer);
  76. pps_unregister_source(source);
  77. pr_info("ktimer PPS source unregistered\n");
  78. }
  79. static int __init pps_ktimer_init(void)
  80. {
  81. int ret;
  82. ret = pps_register_source(&pps_ktimer_info,
  83. PPS_CAPTUREASSERT | PPS_OFFSETASSERT);
  84. if (ret < 0) {
  85. printk(KERN_ERR "cannot register ktimer source\n");
  86. return ret;
  87. }
  88. source = ret;
  89. setup_timer(&ktimer, pps_ktimer_event, 0);
  90. mod_timer(&ktimer, jiffies + HZ);
  91. pr_info("ktimer PPS source registered at %d\n", source);
  92. return 0;
  93. }
  94. module_init(pps_ktimer_init);
  95. module_exit(pps_ktimer_exit);
  96. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  97. MODULE_DESCRIPTION("dummy PPS source by using a kernel timer (just for debug)");
  98. MODULE_LICENSE("GPL");