pps_parport.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /* the PPS echo function */
  116. static void pps_echo(struct pps_device *pps, int event, void *data)
  117. {
  118. dev_info(pps->dev, "echo %s %s\n",
  119. event & PPS_CAPTUREASSERT ? "assert" : "",
  120. event & PPS_CAPTURECLEAR ? "clear" : "");
  121. }
  122. static void parport_attach(struct parport *port)
  123. {
  124. struct pps_client_pp *device;
  125. struct pps_source_info info = {
  126. .name = KBUILD_MODNAME,
  127. .path = "",
  128. .mode = PPS_CAPTUREBOTH | \
  129. PPS_OFFSETASSERT | PPS_OFFSETCLEAR | \
  130. PPS_ECHOASSERT | PPS_ECHOCLEAR | \
  131. PPS_CANWAIT | PPS_TSFMT_TSPEC,
  132. .echo = pps_echo,
  133. .owner = THIS_MODULE,
  134. .dev = NULL
  135. };
  136. device = kzalloc(sizeof(struct pps_client_pp), GFP_KERNEL);
  137. if (!device) {
  138. pr_err("memory allocation failed, not attaching\n");
  139. return;
  140. }
  141. device->pardev = parport_register_device(port, KBUILD_MODNAME,
  142. NULL, NULL, parport_irq, PARPORT_FLAG_EXCL, device);
  143. if (!device->pardev) {
  144. pr_err("couldn't register with %s\n", port->name);
  145. goto err_free;
  146. }
  147. if (parport_claim_or_block(device->pardev) < 0) {
  148. pr_err("couldn't claim %s\n", port->name);
  149. goto err_unregister_dev;
  150. }
  151. device->pps = pps_register_source(&info,
  152. PPS_CAPTUREBOTH | PPS_OFFSETASSERT | PPS_OFFSETCLEAR);
  153. if (device->pps == NULL) {
  154. pr_err("couldn't register PPS source\n");
  155. goto err_release_dev;
  156. }
  157. device->cw = clear_wait;
  158. port->ops->enable_irq(port);
  159. pr_info("attached to %s\n", port->name);
  160. return;
  161. err_release_dev:
  162. parport_release(device->pardev);
  163. err_unregister_dev:
  164. parport_unregister_device(device->pardev);
  165. err_free:
  166. kfree(device);
  167. }
  168. static void parport_detach(struct parport *port)
  169. {
  170. struct pardevice *pardev = port->cad;
  171. struct pps_client_pp *device;
  172. /* FIXME: oooh, this is ugly! */
  173. if (strcmp(pardev->name, KBUILD_MODNAME))
  174. /* not our port */
  175. return;
  176. device = pardev->private;
  177. port->ops->disable_irq(port);
  178. pps_unregister_source(device->pps);
  179. parport_release(pardev);
  180. parport_unregister_device(pardev);
  181. kfree(device);
  182. }
  183. static struct parport_driver pps_parport_driver = {
  184. .name = KBUILD_MODNAME,
  185. .attach = parport_attach,
  186. .detach = parport_detach,
  187. };
  188. /* module staff */
  189. static int __init pps_parport_init(void)
  190. {
  191. int ret;
  192. pr_info(DRVDESC "\n");
  193. if (clear_wait > CLEAR_WAIT_MAX) {
  194. pr_err("clear_wait value should be not greater"
  195. " then %d\n", CLEAR_WAIT_MAX);
  196. return -EINVAL;
  197. }
  198. ret = parport_register_driver(&pps_parport_driver);
  199. if (ret) {
  200. pr_err("unable to register with parport\n");
  201. return ret;
  202. }
  203. return 0;
  204. }
  205. static void __exit pps_parport_exit(void)
  206. {
  207. parport_unregister_driver(&pps_parport_driver);
  208. }
  209. module_init(pps_parport_init);
  210. module_exit(pps_parport_exit);
  211. MODULE_AUTHOR("Alexander Gordeev <lasaine@lvk.cs.msu.su>");
  212. MODULE_DESCRIPTION(DRVDESC);
  213. MODULE_LICENSE("GPL");