- Fix a multiplication error, which lead to a garbage DueTime being passed to KeSetTi...
[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 VOID NTAPI
24 MiniportTimerDpc(
25 PKDPC Dpc,
26 PVOID DeferredContext,
27 PVOID SystemArgument1,
28 PVOID SystemArgument2)
29 /*
30 * FUNCTION: Scheduled by the SetTimer family of functions
31 * ARGUMENTS:
32 * Dpc: Pointer to the DPC Object being executed
33 * DeferredContext: Pointer to a NDIS_MINIPORT_TIMER object
34 * SystemArgument1: Unused.
35 * SystemArgument2: Unused.
36 * NOTES:
37 * - runs at IRQL = DISPATCH_LEVEL
38 */
39 {
40 PNDIS_MINIPORT_TIMER Timer;
41
42 Timer = (PNDIS_MINIPORT_TIMER)DeferredContext;
43
44 ASSERT(Timer->MiniportTimerFunction);
45
46 Timer->MiniportTimerFunction (NULL, Timer->MiniportTimerContext, NULL, NULL);
47 }
48
49 \f
50 /*
51 * @implemented
52 */
53 VOID
54 EXPORT
55 NdisCancelTimer(
56 IN PNDIS_TIMER Timer,
57 OUT PBOOLEAN TimerCancelled)
58 /*
59 * FUNCTION: Cancels a scheduled NDIS timer
60 * ARGUMENTS:
61 * Timer: pointer to an NDIS_TIMER object to cancel
62 * TimerCancelled: boolean that returns cancellation status
63 * NOTES:
64 * - call at IRQL <= DISPATCH_LEVEL
65 */
66 {
67 ASSERT_IRQL(DISPATCH_LEVEL);
68 ASSERT(Timer);
69
70 *TimerCancelled = KeCancelTimer (&Timer->Timer);
71 }
72
73 \f
74 /*
75 * @implemented
76 */
77 #undef NdisGetCurrentSystemTime
78 VOID
79 EXPORT
80 NdisGetCurrentSystemTime (
81 IN OUT PLARGE_INTEGER pSystemTime)
82 /*
83 * FUNCTION: Retrieve the current system time
84 * ARGUMENTS:
85 * pSystemTime: pointer to the returned system time
86 * NOTES:
87 * - call at IRQL <= DISPATCH_LEVEL
88 */
89 {
90 ASSERT_IRQL(DISPATCH_LEVEL);
91 ASSERT(pSystemTime);
92
93 KeQuerySystemTime (pSystemTime);
94 }
95
96 \f
97 /*
98 * @implemented
99 */
100 VOID
101 EXPORT
102 NdisInitializeTimer(
103 IN OUT PNDIS_TIMER Timer,
104 IN PNDIS_TIMER_FUNCTION TimerFunction,
105 IN PVOID FunctionContext)
106 /*
107 * FUNCTION: Set up an NDIS_TIMER for later use
108 * ARGUMENTS:
109 * Timer: pointer to caller-allocated storage to receive an NDIS_TIMER
110 * TimerFunction: function pointer to routine to run when timer expires
111 * FunctionContext: context (param 2) to be passed to the timer function when it runs
112 * NOTES:
113 * - TimerFunction will be called at DISPATCH_LEVEL
114 * - call at IRQL = PASSIVE_LEVEL
115 */
116 {
117 PAGED_CODE();
118 ASSERT(Timer);
119
120 KeInitializeTimer (&Timer->Timer);
121
122 KeInitializeDpc (&Timer->Dpc, (PKDEFERRED_ROUTINE)TimerFunction, FunctionContext);
123 }
124
125 \f
126 /*
127 * @implemented
128 */
129 VOID
130 EXPORT
131 NdisMCancelTimer(
132 IN PNDIS_MINIPORT_TIMER Timer,
133 OUT PBOOLEAN TimerCancelled)
134 /*
135 * FUNCTION: cancel a scheduled NDIS_MINIPORT_TIMER
136 * ARGUMENTS:
137 * Timer: timer object to cancel
138 * TimerCancelled: status of cancel operation
139 * NOTES:
140 * - call at IRQL <= DISPATCH_LEVEL
141 */
142 {
143 ASSERT_IRQL(DISPATCH_LEVEL);
144 ASSERT(TimerCancelled);
145 ASSERT(Timer);
146
147 *TimerCancelled = KeCancelTimer (&Timer->Timer);
148 }
149
150 \f
151 /*
152 * @implemented
153 */
154 VOID
155 EXPORT
156 NdisMInitializeTimer(
157 IN OUT PNDIS_MINIPORT_TIMER Timer,
158 IN NDIS_HANDLE MiniportAdapterHandle,
159 IN PNDIS_TIMER_FUNCTION TimerFunction,
160 IN PVOID FunctionContext)
161 /*
162 * FUNCTION: Initialize an NDIS_MINIPORT_TIMER
163 * ARGUMENTS:
164 * Timer: Timer object to initialize
165 * MiniportAdapterHandle: Handle to the miniport, passed in to MiniportInitialize
166 * TimerFunction: function to be executed when the timer expires
167 * FunctionContext: argument passed to TimerFunction when it is called
168 * NOTES:
169 * - TimerFunction is called at IRQL = DISPATCH_LEVEL
170 * - call at IRQL = PASSIVE_LEVEL
171 */
172 {
173 PAGED_CODE();
174 ASSERT(Timer);
175 KeInitializeTimer (&Timer->Timer);
176
177 KeInitializeDpc (&Timer->Dpc, MiniportTimerDpc, (PVOID) Timer);
178
179 Timer->MiniportTimerFunction = TimerFunction;
180 Timer->MiniportTimerContext = FunctionContext;
181 Timer->Miniport = MiniportAdapterHandle;
182 }
183
184 \f
185 /*
186 * @implemented
187 */
188 VOID
189 EXPORT
190 NdisMSetPeriodicTimer(
191 IN PNDIS_MINIPORT_TIMER Timer,
192 IN UINT MillisecondsPeriod)
193 /*
194 * FUNCTION: Set a timer to go off periodically
195 * ARGUMENTS:
196 * Timer: pointer to the timer object to set
197 * MillisecondsPeriod: period of the timer
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(MillisecondsPeriod, -10000);
210
211 KeSetTimerEx (&Timer->Timer, Timeout, MillisecondsPeriod, &Timer->Dpc);
212 }
213
214 \f
215 /*
216 * @implemented
217 */
218 #undef NdisMSetTimer
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 ASSERT_IRQL(DISPATCH_LEVEL);
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 ASSERT_IRQL(DISPATCH_LEVEL);
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