pps_parport.c 6.5 KB

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