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