2004-08-16 Casper S. Hornstrup <chorns@users.sourceforge.net>
[reactos.git] / reactos / drivers / net / 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 * - Nearly this entire file is PAGED_CODE (with the exception of the
19 * MiniportTimerDpc() function)
20 */
21
22 #include "ndissys.h"
23
24 \f
25 VOID STDCALL
26 MiniportTimerDpc(
27 PKDPC Dpc,
28 PVOID DeferredContext,
29 PVOID SystemArgument1,
30 PVOID SystemArgument2)
31 /*
32 * FUNCTION: Scheduled by the SetTimer family of functions
33 * ARGUMENTS:
34 * Dpc: Pointer to the DPC Object being executed
35 * DeferredContext: Pointer to a NDIS_MINIPORT_TIMER object
36 * SystemArgument1: Unused.
37 * SystemArgument2: Unused.
38 * NOTES:
39 * - runs at IRQL = DISPATCH_LEVEL
40 */
41 {
42 PNDIS_MINIPORT_TIMER Timer;
43
44 Timer = (PNDIS_MINIPORT_TIMER)DeferredContext;
45
46 ASSERT(Timer->MiniportTimerFunction);
47
48 Timer->MiniportTimerFunction (NULL, Timer->MiniportTimerContext, NULL, NULL);
49 }
50
51 \f
52 /*
53 * @implemented
54 */
55 VOID
56 EXPORT
57 NdisCancelTimer(
58 IN PNDIS_TIMER Timer,
59 OUT PBOOLEAN TimerCancelled)
60 /*
61 * FUNCTION: Cancels a scheduled NDIS timer
62 * ARGUMENTS:
63 * Timer: pointer to an NDIS_TIMER object to cancel
64 * TimerCancelled: boolean that returns cancellation status
65 * NOTES:
66 * - call at IRQL <= DISPATCH_LEVEL
67 */
68 {
69 PAGED_CODE();
70 ASSERT(Timer);
71
72 *TimerCancelled = KeCancelTimer (&Timer->Timer);
73 }
74
75 \f
76 /*
77 * @implemented
78 */
79 VOID
80 EXPORT
81 NdisGetCurrentSystemTime (
82 IN OUT PLARGE_INTEGER pSystemTime)
83 /*
84 * FUNCTION: Retrieve the current system time
85 * ARGUMENTS:
86 * pSystemTime: pointer to the returned system time
87 * NOTES:
88 * - call at IRQL <= DISPATCH_LEVEL
89 */
90 {
91 PAGED_CODE();
92 ASSERT(pSystemTime);
93
94 KeQuerySystemTime (pSystemTime);
95 }
96
97 \f
98 /*
99 * @implemented
100 */
101 VOID
102 EXPORT
103 NdisInitializeTimer(
104 IN OUT PNDIS_TIMER Timer,
105 IN PNDIS_TIMER_FUNCTION TimerFunction,
106 IN PVOID FunctionContext)
107 /*
108 * FUNCTION: Set up an NDIS_TIMER for later use
109 * ARGUMENTS:
110 * Timer: pointer to caller-allocated storage to receive an NDIS_TIMER
111 * TimerFunction: function pointer to routine to run when timer expires
112 * FunctionContext: context (param 2) to be passed to the timer function when it runs
113 * NOTES:
114 * - TimerFunction will be called at DISPATCH_LEVEL
115 * - call at IRQL = PASSIVE_LEVEL
116 */
117 {
118 PAGED_CODE();
119 ASSERT(Timer);
120
121 KeInitializeTimer (&Timer->Timer);
122
123 KeInitializeDpc (&Timer->Dpc, (PKDEFERRED_ROUTINE)TimerFunction, FunctionContext);
124 }
125
126 \f
127 /*
128 * @implemented
129 */
130 VOID
131 EXPORT
132 NdisMCancelTimer(
133 IN PNDIS_MINIPORT_TIMER Timer,
134 OUT PBOOLEAN TimerCancelled)
135 /*
136 * FUNCTION: cancel a scheduled NDIS_MINIPORT_TIMER
137 * ARGUMENTS:
138 * Timer: timer object to cancel
139 * TimerCancelled: status of cancel operation
140 * NOTES:
141 * - call at IRQL <= DISPATCH_LEVEL
142 */
143 {
144 PAGED_CODE();
145 ASSERT(TimerCancelled);
146 ASSERT(Timer);
147
148 *TimerCancelled = KeCancelTimer (&Timer->Timer);
149 }
150
151 \f
152 /*
153 * @implemented
154 */
155 VOID
156 EXPORT
157 NdisMInitializeTimer(
158 IN OUT PNDIS_MINIPORT_TIMER Timer,
159 IN NDIS_HANDLE MiniportAdapterHandle,
160 IN PNDIS_TIMER_FUNCTION TimerFunction,
161 IN PVOID FunctionContext)
162 /*
163 * FUNCTION: Initialize an NDIS_MINIPORT_TIMER
164 * ARGUMENTS:
165 * Timer: Timer object to initialize
166 * MiniportAdapterHandle: Handle to the miniport, passed in to MiniportInitialize
167 * TimerFunction: function to be executed when the timer expires
168 * FunctionContext: argument passed to TimerFunction when it is called
169 * NOTES:
170 * - TimerFunction is called at IRQL = DISPATCH_LEVEL
171 * - call at IRQL = PASSIVE_LEVEL
172 */
173 {
174 PAGED_CODE();
175 ASSERT(Timer);
176 KeInitializeTimer (&Timer->Timer);
177
178 KeInitializeDpc (&Timer->Dpc, MiniportTimerDpc, (PVOID) Timer);
179
180 Timer->MiniportTimerFunction = TimerFunction;
181 Timer->MiniportTimerContext = FunctionContext;
182 Timer->Miniport = MiniportAdapterHandle;
183 }
184
185 \f
186 /*
187 * @implemented
188 */
189 VOID
190 EXPORT
191 NdisMSetPeriodicTimer(
192 IN PNDIS_MINIPORT_TIMER Timer,
193 IN UINT MillisecondsPeriod)
194 /*
195 * FUNCTION: Set a timer to go off periodically
196 * ARGUMENTS:
197 * Timer: pointer to the timer object to set
198 * MillisecondsPeriod: period of the timer
199 * NOTES:
200 * - Minimum predictible interval is ~10ms
201 * - Must be called at IRQL <= DISPATCH_LEVEL)
202 */
203 {
204 LARGE_INTEGER Timeout;
205
206 PAGED_CODE();
207 ASSERT(Timer);
208
209 /* relative delays are negative, absolute are positive; resolution is 100ns */
210 Timeout.QuadPart = MillisecondsPeriod * -10000;
211
212 KeSetTimerEx (&Timer->Timer, Timeout, MillisecondsPeriod, &Timer->Dpc);
213 }
214
215 \f
216 /*
217 * @implemented
218 */
219 VOID
220 EXPORT
221 NdisMSetTimer(
222 IN PNDIS_MINIPORT_TIMER Timer,
223 IN UINT MillisecondsToDelay)
224 /*
225 * FUNCTION: Set a NDIS_MINIPORT_TIMER so that it goes off
226 * ARGUMENTS:
227 * Timer: timer object to set
228 * MillisecondsToDelay: time to wait for the timer to expire
229 * NOTES:
230 * - Minimum predictible interval is ~10ms
231 * - Must be called at IRQL <= DISPATCH_LEVEL)
232 */
233 {
234 LARGE_INTEGER Timeout;
235
236 PAGED_CODE();
237 ASSERT(Timer);
238
239 /* relative delays are negative, absolute are positive; resolution is 100ns */
240 Timeout.QuadPart = MillisecondsToDelay * -10000;
241
242 KeSetTimer (&Timer->Timer, Timeout, &Timer->Dpc);
243 }
244
245 \f
246 /*
247 * @implemented
248 */
249 VOID
250 EXPORT
251 NdisSetTimer(
252 IN PNDIS_TIMER Timer,
253 IN UINT MillisecondsToDelay)
254 /*
255 * FUNCTION: Set an NDIS_TIMER so that it goes off
256 * ARGUMENTS:
257 * Timer: timer object to set
258 * MillisecondsToDelay: time to wait for the timer to expire
259 * NOTES:
260 * - Minimum predictible interval is ~10ms
261 * - Must be called at IRQL <= DISPATCH_LEVEL)
262 */
263 {
264 LARGE_INTEGER Timeout;
265
266 PAGED_CODE();
267 ASSERT(Timer);
268
269 /* relative delays are negative, absolute are positive; resolution is 100ns */
270 Timeout.QuadPart = MillisecondsToDelay * -10000;
271
272 KeSetTimer (&Timer->Timer, Timeout, &Timer->Dpc);
273 }
274
275 /* EOF */
276