75a65f3b24fc1f4a7764c04f39262a226454fcb8
[reactos.git] / reactos / drivers / network / ndis / ndis / time.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS NDIS library
4 * FILE: ndis/time.c
5 * PURPOSE: Time related routines
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Vizzini (vizzini@plasmic.com)
8 * REVISIONS:
9 * CSH 01/08-2000 Created
10 * Vizzini 08-Oct-2003 Formatting, commenting, and ASSERTs
11 *
12 * NOTES:
13 * - Although the standard kernel-mode M.O. is to trust the caller
14 * to not provide bad arguments, we have added lots of argument
15 * validation to assist in the effort to get third-party binaries
16 * working. It is easiest to track bugs when things break quickly
17 * and badly.
18 */
19
20 #include "ndissys.h"
21
22 \f
23 /*
24 * @implemented
25 */
26 VOID
27 EXPORT
28 NdisCancelTimer(
29 IN PNDIS_TIMER Timer,
30 OUT PBOOLEAN TimerCancelled)
31 /*
32 * FUNCTION: Cancels a scheduled NDIS timer
33 * ARGUMENTS:
34 * Timer: pointer to an NDIS_TIMER object to cancel
35 * TimerCancelled: boolean that returns cancellation status
36 * NOTES:
37 * - call at IRQL <= DISPATCH_LEVEL
38 */
39 {
40 ASSERT_IRQL(DISPATCH_LEVEL);
41 ASSERT(Timer);
42
43 *TimerCancelled = KeCancelTimer (&Timer->Timer);
44 }
45
46 \f
47 /*
48 * @implemented
49 */
50 #undef NdisGetCurrentSystemTime
51 VOID
52 EXPORT
53 NdisGetCurrentSystemTime (
54 IN OUT PLARGE_INTEGER pSystemTime)
55 /*
56 * FUNCTION: Retrieve the current system time
57 * ARGUMENTS:
58 * pSystemTime: pointer to the returned system time
59 * NOTES:
60 * - call at IRQL <= DISPATCH_LEVEL
61 */
62 {
63 ASSERT_IRQL(DISPATCH_LEVEL);
64 ASSERT(pSystemTime);
65
66 KeQuerySystemTime (pSystemTime);
67 }
68
69 \f
70 /*
71 * @implemented
72 */
73 VOID
74 EXPORT
75 NdisInitializeTimer(
76 IN OUT PNDIS_TIMER Timer,
77 IN PNDIS_TIMER_FUNCTION TimerFunction,
78 IN PVOID FunctionContext)
79 /*
80 * FUNCTION: Set up an NDIS_TIMER for later use
81 * ARGUMENTS:
82 * Timer: pointer to caller-allocated storage to receive an NDIS_TIMER
83 * TimerFunction: function pointer to routine to run when timer expires
84 * FunctionContext: context (param 2) to be passed to the timer function when it runs
85 * NOTES:
86 * - TimerFunction will be called at DISPATCH_LEVEL
87 * - call at IRQL = PASSIVE_LEVEL
88 */
89 {
90 PAGED_CODE();
91 ASSERT(Timer);
92
93 KeInitializeTimer (&Timer->Timer);
94
95 KeInitializeDpc (&Timer->Dpc, (PKDEFERRED_ROUTINE)TimerFunction, FunctionContext);
96 }
97
98 \f
99 /*
100 * @implemented
101 */
102 VOID
103 EXPORT
104 NdisMCancelTimer(
105 IN PNDIS_MINIPORT_TIMER Timer,
106 OUT PBOOLEAN TimerCancelled)
107 /*
108 * FUNCTION: cancel a scheduled NDIS_MINIPORT_TIMER
109 * ARGUMENTS:
110 * Timer: timer object to cancel
111 * TimerCancelled: status of cancel operation
112 * NOTES:
113 * - call at IRQL <= DISPATCH_LEVEL
114 */
115 {
116 ASSERT_IRQL(DISPATCH_LEVEL);
117 ASSERT(TimerCancelled);
118 ASSERT(Timer);
119
120 *TimerCancelled = KeCancelTimer (&Timer->Timer);
121 }
122
123 \f
124 /*
125 * @implemented
126 */
127 VOID
128 EXPORT
129 NdisMInitializeTimer(
130 IN OUT PNDIS_MINIPORT_TIMER Timer,
131 IN NDIS_HANDLE MiniportAdapterHandle,
132 IN PNDIS_TIMER_FUNCTION TimerFunction,
133 IN PVOID FunctionContext)
134 /*
135 * FUNCTION: Initialize an NDIS_MINIPORT_TIMER
136 * ARGUMENTS:
137 * Timer: Timer object to initialize
138 * MiniportAdapterHandle: Handle to the miniport, passed in to MiniportInitialize
139 * TimerFunction: function to be executed when the timer expires
140 * FunctionContext: argument passed to TimerFunction when it is called
141 * NOTES:
142 * - TimerFunction is called at IRQL = DISPATCH_LEVEL
143 * - call at IRQL = PASSIVE_LEVEL
144 */
145 {
146 PAGED_CODE();
147 ASSERT(Timer);
148 KeInitializeTimer (&Timer->Timer);
149
150 KeInitializeDpc (&Timer->Dpc, (PKDEFERRED_ROUTINE)TimerFunction, FunctionContext);
151 }
152
153 \f
154 /*
155 * @implemented
156 */
157 VOID
158 EXPORT
159 NdisMSetPeriodicTimer(
160 IN PNDIS_MINIPORT_TIMER Timer,
161 IN UINT MillisecondsPeriod)
162 /*
163 * FUNCTION: Set a timer to go off periodically
164 * ARGUMENTS:
165 * Timer: pointer to the timer object to set
166 * MillisecondsPeriod: period of the timer
167 * NOTES:
168 * - Minimum predictible interval is ~10ms
169 * - Must be called at IRQL <= DISPATCH_LEVEL
170 */
171 {
172 LARGE_INTEGER Timeout;
173
174 ASSERT_IRQL(DISPATCH_LEVEL);
175 ASSERT(Timer);
176
177 /* relative delays are negative, absolute are positive; resolution is 100ns */
178 Timeout.QuadPart = Int32x32To64(MillisecondsPeriod, -10000);
179
180 KeSetTimerEx (&Timer->Timer, Timeout, MillisecondsPeriod, &Timer->Dpc);
181 }
182
183 \f
184 /*
185 * @implemented
186 */
187 #undef NdisMSetTimer
188 VOID
189 EXPORT
190 NdisMSetTimer(
191 IN PNDIS_MINIPORT_TIMER Timer,
192 IN UINT MillisecondsToDelay)
193 /*
194 * FUNCTION: Set a NDIS_MINIPORT_TIMER so that it goes off
195 * ARGUMENTS:
196 * Timer: timer object to set
197 * MillisecondsToDelay: time to wait for the timer to expire
198 * NOTES:
199 * - Minimum predictible interval is ~10ms
200 * - Must be called at IRQL <= DISPATCH_LEVEL
201 */
202 {
203 LARGE_INTEGER Timeout;
204
205 ASSERT_IRQL(DISPATCH_LEVEL);
206 ASSERT(Timer);
207
208 /* relative delays are negative, absolute are positive; resolution is 100ns */
209 Timeout.QuadPart = Int32x32To64(MillisecondsToDelay, -10000);
210
211 KeSetTimer (&Timer->Timer, Timeout, &Timer->Dpc);
212 }
213
214 \f
215 /*
216 * @implemented
217 */
218 VOID
219 EXPORT
220 NdisSetTimer(
221 IN PNDIS_TIMER Timer,
222 IN UINT MillisecondsToDelay)
223 /*
224 * FUNCTION: Set an NDIS_TIMER so that it goes off
225 * ARGUMENTS:
226 * Timer: timer object to set
227 * MillisecondsToDelay: time to wait for the timer to expire
228 * NOTES:
229 * - Minimum predictible interval is ~10ms
230 * - Must be called at IRQL <= DISPATCH_LEVEL
231 */
232 {
233 LARGE_INTEGER Timeout;
234
235 ASSERT_IRQL(DISPATCH_LEVEL);
236 ASSERT(Timer);
237
238 NDIS_DbgPrint(MAX_TRACE, ("Called. Timer is: 0x%x, Timeout is: %ld\n", Timer, MillisecondsToDelay));
239
240 /* relative delays are negative, absolute are positive; resolution is 100ns */
241 Timeout.QuadPart = Int32x32To64(MillisecondsToDelay, -10000);
242
243 KeSetTimer (&Timer->Timer, Timeout, &Timer->Dpc);
244 }
245
246 /*
247 * @implemented
248 */
249 VOID
250 EXPORT
251 NdisSetTimerEx(
252 IN PNDIS_TIMER Timer,
253 IN UINT MillisecondsToDelay,
254 IN PVOID FunctionContext)
255 {
256 NDIS_DbgPrint(MAX_TRACE, ("Called. Timer is: 0x%x, Timeout is: %ld, FunctionContext is: 0x%x\n",
257 Timer, MillisecondsToDelay, FunctionContext));
258
259 Timer->Dpc.DeferredContext = FunctionContext;
260
261 NdisSetTimer(Timer, MillisecondsToDelay);
262 }
263
264 /* EOF */
265