pps_parport.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * pps_parport.c -- kernel parallel port PPS client
  4. *
  5. * Copyright (C) 2009 Alexander Gordeev <[email protected]>
  6. */
  7. /*
  8. * TODO:
  9. * implement echo over SEL pin
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/irqnr.h>
  16. #include <linux/time.h>
  17. #include <linux/slab.h>
  18. #include <linux/parport.h>
  19. #include <linux/pps_kernel.h>
  20. /* module parameters */
  21. #define CLEAR_WAIT_MAX 100
  22. #define CLEAR_WAIT_MAX_ERRORS 5
  23. static unsigned int clear_wait = 100;
  24. MODULE_PARM_DESC(clear_wait,
  25. "Maximum number of port reads when polling for signal clear,"
  26. " zero turns clear edge capture off entirely");
  27. module_param(clear_wait, uint, 0);
  28. static DEFINE_IDA(pps_client_index);
  29. /* internal per port structure */
  30. struct pps_client_pp {
  31. struct pardevice *pardev; /* parport device */
  32. struct pps_device *pps; /* PPS device */
  33. unsigned int cw; /* port clear timeout */
  34. unsigned int cw_err; /* number of timeouts */
  35. int index; /* device number */
  36. };
  37. static inline int signal_is_set(struct parport *port)
  38. {
  39. return (port->ops->read_status(port) & PARPORT_STATUS_ACK) != 0;
  40. }
  41. /* parport interrupt handler */
  42. static void parport_irq(void *handle)
  43. {
  44. struct pps_event_time ts_assert, ts_clear;
  45. struct pps_client_pp *dev = handle;
  46. struct parport *port = dev->pardev->port;
  47. unsigned int i;
  48. unsigned long flags;
  49. /* first of all we get the time stamp... */
  50. pps_get_ts(&ts_assert);
  51. if (dev->cw == 0)
  52. /* clear edge capture disabled */
  53. goto out_assert;
  54. /* try capture the clear edge */
  55. /* We have to disable interrupts here. The idea is to prevent
  56. * other interrupts on the same processor to introduce random
  57. * lags while polling the port. Reading from IO port is known
  58. * to take approximately 1us while other interrupt handlers can
  59. * take much more potentially.
  60. *
  61. * Interrupts won't be disabled for a long time because the
  62. * number of polls is limited by clear_wait parameter which is
  63. * kept rather low. So it should never be an issue.
  64. */
  65. local_irq_save(flags);
  66. /* check the signal (no signal means the pulse is lost this time) */
  67. if (!signal_is_set(port)) {
  68. local_irq_restore(flags);
  69. dev_err(dev->pps->dev, "lost the signal\n");
  70. goto out_assert;
  71. }
  72. /* poll the port until the signal is unset */
  73. for (i = dev->cw; i; i--)
  74. if (!signal_is_set(port)) {
  75. pps_get_ts(&ts_clear);
  76. local_irq_restore(flags);
  77. dev->cw_err = 0;
  78. goto out_both;
  79. }
  80. local_irq_restore(flags);
  81. /* timeout */
  82. dev->cw_err++;
  83. if (dev->cw_err >= CLEAR_WAIT_MAX_ERRORS) {
  84. dev_err(dev->pps->dev, "disabled clear edge capture after %d"
  85. " timeouts\n", dev->cw_err);
  86. dev->cw = 0;
  87. dev->cw_err = 0;
  88. }
  89. out_assert:
  90. /* fire assert event */
  91. pps_event(dev->pps, &ts_assert,
  92. PPS_CAPTUREASSERT, NULL);
  93. return;
  94. out_both:
  95. /* fire assert event */
  96. pps_event(dev->pps, &ts_assert,
  97. PPS_CAPTUREASSERT, NULL);
  98. /* fire clear event */
  99. pps_event(dev->pps, &ts_clear,
  100. PPS_CAPTURECLEAR, NULL);
  101. return;
  102. }
  103. static void parport_attach(struct parport *port)
  104. {
  105. struct pardev_cb pps_client_cb;
  106. int index;
  107. struct pps_client_pp *device;
  108. struct pps_source_info info = {
  109. .name = KBUILD_MODNAME,
  110. .path = "",
  111. .mode = PPS_CAPTUREBOTH | \
  112. PPS_OFFSETASSERT | PPS_OFFSETCLEAR | \
  113. PPS_ECHOASSERT | PPS_ECHOCLEAR | \
  114. PPS_CANWAIT | PPS_TSFMT_TSPEC,
  115. .owner = THIS_MODULE,
  116. .dev = NULL
  117. };
  118. if (clear_wait > CLEAR_WAIT_MAX) {
  119. pr_err("clear_wait value should be not greater then %d\n",
  120. CLEAR_WAIT_MAX);
  121. return;
  122. }
  123. device = kzalloc(sizeof(struct pps_client_pp), GFP_KERNEL);
  124. if (!device) {
  125. pr_err("memory allocation failed, not attaching\n");
  126. return;
  127. }
  128. index = ida_simple_get(&pps_client_index, 0, 0, GFP_KERNEL);
  129. memset(&pps_client_cb, 0, sizeof(pps_client_cb));
  130. pps_client_cb.private = device;
  131. pps_client_cb.irq_func = parport_irq;
  132. pps_client_cb.flags = PARPORT_FLAG_EXCL;
  133. device->pardev = parport_register_dev_model(port,
  134. KBUILD_MODNAME,
  135. &pps_client_cb,
  136. index);
  137. if (!device->pardev) {
  138. pr_err("couldn't register with %s\n", port->name);
  139. goto err_free;
  140. }
  141. if (parport_claim_or_block(device->pardev) < 0) {
  142. pr_err("couldn't claim %s\n", port->name);
  143. goto err_unregister_dev;
  144. }
  145. device->pps = pps_register_source(&info,
  146. PPS_CAPTUREBOTH | PPS_OFFSETASSERT | PPS_OFFSETCLEAR);
  147. if (IS_ERR(device->pps)) {
  148. pr_err("couldn't register PPS source\n");
  149. goto err_release_dev;
  150. }
  151. device->cw = clear_wait;
  152. port->ops->enable_irq(port);
  153. device->index = index;
  154. pr_info("attached to %s\n", port->name);
  155. return;
  156. err_release_dev:
  157. parport_release(device->pardev);
  158. err_unregister_dev:
  159. parport_unregister_device(device->pardev);
  160. err_free:
  161. ida_simple_remove(&pps_client_index, index);
  162. kfree(device);
  163. }
  164. static void parport_detach(struct parport *port)
  165. {
  166. struct pardevice *pardev = port->cad;
  167. struct pps_client_pp *device;
  168. /* FIXME: oooh, this is ugly! */
  169. if (!pardev || strcmp(pardev->name, KBUILD_MODNAME))
  170. /* not our port */
  171. return;
  172. device = pardev->private;
  173. port->ops->disable_irq(port);
  174. pps_unregister_source(device->pps);
  175. parport_release(pardev);
  176. parport_unregister_device(pardev);
  177. ida_simple_remove(&pps_client_index, device->index);
  178. kfree(device);
  179. }
  180. static struct parport_driver pps_parport_driver = {
  181. .name = KBUILD_MODNAME,
  182. .match_port = parport_attach,
  183. .detach = parport_detach,
  184. .devmodel = true,
  185. };
  186. module_parport_driver(pps_parport_driver);
  187. MODULE_AUTHOR("Alexander Gordeev <[email protected]>");
  188. MODULE_DESCRIPTION("parallel port PPS client");
  189. MODULE_LICENSE("GPL");