pps-ldisc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * pps-ldisc.c -- PPS line discipline
  3. *
  4. *
  5. * Copyright (C) 2008 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/module.h>
  22. #include <linux/serial_core.h>
  23. #include <linux/tty.h>
  24. #include <linux/pps_kernel.h>
  25. #define PPS_TTY_MAGIC 0x0001
  26. static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status,
  27. struct timespec *ts)
  28. {
  29. int id = (long)tty->disc_data;
  30. struct timespec __ts;
  31. struct pps_ktime pps_ts;
  32. /* First of all we get the time stamp... */
  33. getnstimeofday(&__ts);
  34. /* Does caller give us a timestamp? */
  35. if (ts) { /* Yes. Let's use it! */
  36. pps_ts.sec = ts->tv_sec;
  37. pps_ts.nsec = ts->tv_nsec;
  38. } else { /* No. Do it ourself! */
  39. pps_ts.sec = __ts.tv_sec;
  40. pps_ts.nsec = __ts.tv_nsec;
  41. }
  42. /* Now do the PPS event report */
  43. pps_event(id, &pps_ts, status ? PPS_CAPTUREASSERT : PPS_CAPTURECLEAR,
  44. NULL);
  45. pr_debug("PPS %s at %lu on source #%d\n",
  46. status ? "assert" : "clear", jiffies, id);
  47. }
  48. static int (*alias_n_tty_open)(struct tty_struct *tty);
  49. static int pps_tty_open(struct tty_struct *tty)
  50. {
  51. struct pps_source_info info;
  52. struct tty_driver *drv = tty->driver;
  53. int index = tty->index + drv->name_base;
  54. int ret;
  55. info.owner = THIS_MODULE;
  56. info.dev = NULL;
  57. snprintf(info.name, PPS_MAX_NAME_LEN, "%s%d", drv->driver_name, index);
  58. snprintf(info.path, PPS_MAX_NAME_LEN, "/dev/%s%d", drv->name, index);
  59. info.mode = PPS_CAPTUREBOTH | \
  60. PPS_OFFSETASSERT | PPS_OFFSETCLEAR | \
  61. PPS_CANWAIT | PPS_TSFMT_TSPEC;
  62. ret = pps_register_source(&info, PPS_CAPTUREBOTH | \
  63. PPS_OFFSETASSERT | PPS_OFFSETCLEAR);
  64. if (ret < 0) {
  65. pr_err("cannot register PPS source \"%s\"\n", info.path);
  66. return ret;
  67. }
  68. tty->disc_data = (void *)(long)ret;
  69. /* Should open N_TTY ldisc too */
  70. ret = alias_n_tty_open(tty);
  71. if (ret < 0)
  72. pps_unregister_source((long)tty->disc_data);
  73. pr_info("PPS source #%d \"%s\" added\n", ret, info.path);
  74. return 0;
  75. }
  76. static void (*alias_n_tty_close)(struct tty_struct *tty);
  77. static void pps_tty_close(struct tty_struct *tty)
  78. {
  79. int id = (long)tty->disc_data;
  80. pps_unregister_source(id);
  81. alias_n_tty_close(tty);
  82. pr_info("PPS source #%d removed\n", id);
  83. }
  84. static struct tty_ldisc_ops pps_ldisc_ops;
  85. /*
  86. * Module stuff
  87. */
  88. static int __init pps_tty_init(void)
  89. {
  90. int err;
  91. /* Inherit the N_TTY's ops */
  92. n_tty_inherit_ops(&pps_ldisc_ops);
  93. /* Save N_TTY's open()/close() methods */
  94. alias_n_tty_open = pps_ldisc_ops.open;
  95. alias_n_tty_close = pps_ldisc_ops.close;
  96. /* Init PPS_TTY data */
  97. pps_ldisc_ops.owner = THIS_MODULE;
  98. pps_ldisc_ops.magic = PPS_TTY_MAGIC;
  99. pps_ldisc_ops.name = "pps_tty";
  100. pps_ldisc_ops.dcd_change = pps_tty_dcd_change;
  101. pps_ldisc_ops.open = pps_tty_open;
  102. pps_ldisc_ops.close = pps_tty_close;
  103. err = tty_register_ldisc(N_PPS, &pps_ldisc_ops);
  104. if (err)
  105. pr_err("can't register PPS line discipline\n");
  106. else
  107. pr_info("PPS line discipline registered\n");
  108. return err;
  109. }
  110. static void __exit pps_tty_cleanup(void)
  111. {
  112. int err;
  113. err = tty_unregister_ldisc(N_PPS);
  114. if (err)
  115. pr_err("can't unregister PPS line discipline\n");
  116. else
  117. pr_info("PPS line discipline removed\n");
  118. }
  119. module_init(pps_tty_init);
  120. module_exit(pps_tty_cleanup);
  121. MODULE_ALIAS_LDISC(N_PPS);
  122. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  123. MODULE_DESCRIPTION("PPS TTY device driver");
  124. MODULE_LICENSE("GPL");