pps_parport.c 6.0 KB

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