pps-ldisc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/module.h>
  23. #include <linux/serial_core.h>
  24. #include <linux/tty.h>
  25. #include <linux/pps_kernel.h>
  26. #include <linux/bug.h>
  27. #define PPS_TTY_MAGIC 0x0001
  28. static void pps_tty_dcd_change(struct tty_struct *tty, unsigned int status)
  29. {
  30. struct pps_device *pps;
  31. struct pps_event_time ts;
  32. pps_get_ts(&ts);
  33. pps = pps_lookup_dev(tty);
  34. /*
  35. * This should never fail, but the ldisc locking is very
  36. * convoluted, so don't crash just in case.
  37. */
  38. if (WARN_ON_ONCE(pps == NULL))
  39. return;
  40. /* Now do the PPS event report */
  41. pps_event(pps, &ts, status ? PPS_CAPTUREASSERT :
  42. PPS_CAPTURECLEAR, NULL);
  43. dev_dbg(pps->dev, "PPS %s at %lu\n",
  44. status ? "assert" : "clear", jiffies);
  45. }
  46. static int (*alias_n_tty_open)(struct tty_struct *tty);
  47. static int pps_tty_open(struct tty_struct *tty)
  48. {
  49. struct pps_source_info info;
  50. struct tty_driver *drv = tty->driver;
  51. int index = tty->index + drv->name_base;
  52. struct pps_device *pps;
  53. int ret;
  54. info.owner = THIS_MODULE;
  55. info.dev = NULL;
  56. snprintf(info.name, PPS_MAX_NAME_LEN, "%s%d", drv->driver_name, index);
  57. snprintf(info.path, PPS_MAX_NAME_LEN, "/dev/%s%d", drv->name, index);
  58. info.mode = PPS_CAPTUREBOTH | \
  59. PPS_OFFSETASSERT | PPS_OFFSETCLEAR | \
  60. PPS_CANWAIT | PPS_TSFMT_TSPEC;
  61. pps = pps_register_source(&info, PPS_CAPTUREBOTH | \
  62. PPS_OFFSETASSERT | PPS_OFFSETCLEAR);
  63. if (pps == NULL) {
  64. pr_err("cannot register PPS source \"%s\"\n", info.path);
  65. return -ENOMEM;
  66. }
  67. pps->lookup_cookie = tty;
  68. /* Now open the base class N_TTY ldisc */
  69. ret = alias_n_tty_open(tty);
  70. if (ret < 0) {
  71. pr_err("cannot open tty ldisc \"%s\"\n", info.path);
  72. goto err_unregister;
  73. }
  74. dev_info(pps->dev, "source \"%s\" added\n", info.path);
  75. return 0;
  76. err_unregister:
  77. pps_unregister_source(pps);
  78. return ret;
  79. }
  80. static void (*alias_n_tty_close)(struct tty_struct *tty);
  81. static void pps_tty_close(struct tty_struct *tty)
  82. {
  83. struct pps_device *pps = pps_lookup_dev(tty);
  84. alias_n_tty_close(tty);
  85. if (WARN_ON(!pps))
  86. return;
  87. dev_info(pps->dev, "removed\n");
  88. pps_unregister_source(pps);
  89. }
  90. static struct tty_ldisc_ops pps_ldisc_ops;
  91. /*
  92. * Module stuff
  93. */
  94. static int __init pps_tty_init(void)
  95. {
  96. int err;
  97. /* Inherit the N_TTY's ops */
  98. n_tty_inherit_ops(&pps_ldisc_ops);
  99. /* Save N_TTY's open()/close() methods */
  100. alias_n_tty_open = pps_ldisc_ops.open;
  101. alias_n_tty_close = pps_ldisc_ops.close;
  102. /* Init PPS_TTY data */
  103. pps_ldisc_ops.owner = THIS_MODULE;
  104. pps_ldisc_ops.magic = PPS_TTY_MAGIC;
  105. pps_ldisc_ops.name = "pps_tty";
  106. pps_ldisc_ops.dcd_change = pps_tty_dcd_change;
  107. pps_ldisc_ops.open = pps_tty_open;
  108. pps_ldisc_ops.close = pps_tty_close;
  109. err = tty_register_ldisc(N_PPS, &pps_ldisc_ops);
  110. if (err)
  111. pr_err("can't register PPS line discipline\n");
  112. else
  113. pr_info("PPS line discipline registered\n");
  114. return err;
  115. }
  116. static void __exit pps_tty_cleanup(void)
  117. {
  118. int err;
  119. err = tty_unregister_ldisc(N_PPS);
  120. if (err)
  121. pr_err("can't unregister PPS line discipline\n");
  122. else
  123. pr_info("PPS line discipline removed\n");
  124. }
  125. module_init(pps_tty_init);
  126. module_exit(pps_tty_cleanup);
  127. MODULE_ALIAS_LDISC(N_PPS);
  128. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  129. MODULE_DESCRIPTION("PPS TTY device driver");
  130. MODULE_LICENSE("GPL");