timer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*********************************************************************
  2. *
  3. * Filename: timer.c
  4. * Version:
  5. * Description:
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sat Aug 16 00:59:29 1997
  9. * Modified at: Wed Dec 8 12:50:34 1999
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1997, 1999 Dag Brattli <dagb@cs.uit.no>,
  13. * All Rights Reserved.
  14. * Copyright (c) 2000-2002 Jean Tourrilhes <jt@hpl.hp.com>
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * Neither Dag Brattli nor University of Tromsø admit liability nor
  22. * provide warranty for any of this software. This material is
  23. * provided "AS-IS" and at no charge.
  24. *
  25. ********************************************************************/
  26. #include <linux/delay.h>
  27. #include <net/irda/timer.h>
  28. #include <net/irda/irda.h>
  29. #include <net/irda/irda_device.h>
  30. #include <net/irda/irlap.h>
  31. #include <net/irda/irlmp.h>
  32. extern int sysctl_slot_timeout;
  33. static void irlap_slot_timer_expired(void* data);
  34. static void irlap_query_timer_expired(void* data);
  35. static void irlap_final_timer_expired(void* data);
  36. static void irlap_wd_timer_expired(void* data);
  37. static void irlap_backoff_timer_expired(void* data);
  38. static void irlap_media_busy_expired(void* data);
  39. void irlap_start_slot_timer(struct irlap_cb *self, int timeout)
  40. {
  41. irda_start_timer(&self->slot_timer, timeout, (void *) self,
  42. irlap_slot_timer_expired);
  43. }
  44. void irlap_start_query_timer(struct irlap_cb *self, int S, int s)
  45. {
  46. int timeout;
  47. /* Calculate when the peer discovery should end. Normally, we
  48. * get the end-of-discovery frame, so this is just in case
  49. * we miss it.
  50. * Basically, we multiply the number of remaining slots by our
  51. * slot time, plus add some extra time to properly receive the last
  52. * discovery packet (which is longer due to extra discovery info),
  53. * to avoid messing with for incoming connections requests and
  54. * to accommodate devices that perform discovery slower than us.
  55. * Jean II */
  56. timeout = msecs_to_jiffies(sysctl_slot_timeout) * (S - s)
  57. + XIDEXTRA_TIMEOUT + SMALLBUSY_TIMEOUT;
  58. /* Set or re-set the timer. We reset the timer for each received
  59. * discovery query, which allow us to automatically adjust to
  60. * the speed of the peer discovery (faster or slower). Jean II */
  61. irda_start_timer( &self->query_timer, timeout, (void *) self,
  62. irlap_query_timer_expired);
  63. }
  64. void irlap_start_final_timer(struct irlap_cb *self, int timeout)
  65. {
  66. irda_start_timer(&self->final_timer, timeout, (void *) self,
  67. irlap_final_timer_expired);
  68. }
  69. void irlap_start_wd_timer(struct irlap_cb *self, int timeout)
  70. {
  71. irda_start_timer(&self->wd_timer, timeout, (void *) self,
  72. irlap_wd_timer_expired);
  73. }
  74. void irlap_start_backoff_timer(struct irlap_cb *self, int timeout)
  75. {
  76. irda_start_timer(&self->backoff_timer, timeout, (void *) self,
  77. irlap_backoff_timer_expired);
  78. }
  79. void irlap_start_mbusy_timer(struct irlap_cb *self, int timeout)
  80. {
  81. irda_start_timer(&self->media_busy_timer, timeout,
  82. (void *) self, irlap_media_busy_expired);
  83. }
  84. void irlap_stop_mbusy_timer(struct irlap_cb *self)
  85. {
  86. /* If timer is activated, kill it! */
  87. del_timer(&self->media_busy_timer);
  88. /* If we are in NDM, there is a bunch of events in LAP that
  89. * that be pending due to the media_busy condition, such as
  90. * CONNECT_REQUEST and SEND_UI_FRAME. If we don't generate
  91. * an event, they will wait forever...
  92. * Jean II */
  93. if (self->state == LAP_NDM)
  94. irlap_do_event(self, MEDIA_BUSY_TIMER_EXPIRED, NULL, NULL);
  95. }
  96. void irlmp_start_watchdog_timer(struct lsap_cb *self, int timeout)
  97. {
  98. irda_start_timer(&self->watchdog_timer, timeout, (void *) self,
  99. irlmp_watchdog_timer_expired);
  100. }
  101. void irlmp_start_discovery_timer(struct irlmp_cb *self, int timeout)
  102. {
  103. irda_start_timer(&self->discovery_timer, timeout, (void *) self,
  104. irlmp_discovery_timer_expired);
  105. }
  106. void irlmp_start_idle_timer(struct lap_cb *self, int timeout)
  107. {
  108. irda_start_timer(&self->idle_timer, timeout, (void *) self,
  109. irlmp_idle_timer_expired);
  110. }
  111. void irlmp_stop_idle_timer(struct lap_cb *self)
  112. {
  113. /* If timer is activated, kill it! */
  114. del_timer(&self->idle_timer);
  115. }
  116. /*
  117. * Function irlap_slot_timer_expired (data)
  118. *
  119. * IrLAP slot timer has expired
  120. *
  121. */
  122. static void irlap_slot_timer_expired(void *data)
  123. {
  124. struct irlap_cb *self = (struct irlap_cb *) data;
  125. IRDA_ASSERT(self != NULL, return;);
  126. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  127. irlap_do_event(self, SLOT_TIMER_EXPIRED, NULL, NULL);
  128. }
  129. /*
  130. * Function irlap_query_timer_expired (data)
  131. *
  132. * IrLAP query timer has expired
  133. *
  134. */
  135. static void irlap_query_timer_expired(void *data)
  136. {
  137. struct irlap_cb *self = (struct irlap_cb *) data;
  138. IRDA_ASSERT(self != NULL, return;);
  139. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  140. irlap_do_event(self, QUERY_TIMER_EXPIRED, NULL, NULL);
  141. }
  142. /*
  143. * Function irda_final_timer_expired (data)
  144. *
  145. *
  146. *
  147. */
  148. static void irlap_final_timer_expired(void *data)
  149. {
  150. struct irlap_cb *self = (struct irlap_cb *) data;
  151. IRDA_ASSERT(self != NULL, return;);
  152. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  153. irlap_do_event(self, FINAL_TIMER_EXPIRED, NULL, NULL);
  154. }
  155. /*
  156. * Function irda_wd_timer_expired (data)
  157. *
  158. *
  159. *
  160. */
  161. static void irlap_wd_timer_expired(void *data)
  162. {
  163. struct irlap_cb *self = (struct irlap_cb *) data;
  164. IRDA_ASSERT(self != NULL, return;);
  165. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  166. irlap_do_event(self, WD_TIMER_EXPIRED, NULL, NULL);
  167. }
  168. /*
  169. * Function irda_backoff_timer_expired (data)
  170. *
  171. *
  172. *
  173. */
  174. static void irlap_backoff_timer_expired(void *data)
  175. {
  176. struct irlap_cb *self = (struct irlap_cb *) data;
  177. IRDA_ASSERT(self != NULL, return;);
  178. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  179. irlap_do_event(self, BACKOFF_TIMER_EXPIRED, NULL, NULL);
  180. }
  181. /*
  182. * Function irtty_media_busy_expired (data)
  183. *
  184. *
  185. */
  186. static void irlap_media_busy_expired(void *data)
  187. {
  188. struct irlap_cb *self = (struct irlap_cb *) data;
  189. IRDA_ASSERT(self != NULL, return;);
  190. irda_device_set_media_busy(self->netdev, FALSE);
  191. /* Note : the LAP event will be send in irlap_stop_mbusy_timer(),
  192. * to catch other cases where the flag is cleared (for example
  193. * after a discovery) - Jean II */
  194. }