1 /****************************************************************************
3 (c) SYSTEC electronic GmbH, D-07973 Greiz, August-Bebel-Str. 29
4 www.systec-electronic.com
8 Description: source file for EPL User Timermodule for Linux kernel module
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions
16 1. Redistributions of source code must retain the above copyright
17 notice, this list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
23 3. Neither the name of SYSTEC electronic GmbH nor the names of its
24 contributors may be used to endorse or promote products derived
25 from this software without prior written permission. For written
26 permission, please contact info@systec-electronic.com.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 POSSIBILITY OF SUCH DAMAGE.
43 If a provision of this License is or becomes illegal, invalid or
44 unenforceable in any jurisdiction, that shall not affect:
45 1. the validity or enforceability in that jurisdiction of any other
46 provision of this License; or
47 2. the validity or enforceability in other jurisdictions of that or
48 any other provision of this License.
50 -------------------------------------------------------------------------
52 $RCSfile: EplTimeruLinuxKernel.c,v $
56 $Revision: 1.6 $ $Date: 2008/04/17 21:36:32 $
63 -------------------------------------------------------------------------
67 2006/09/12 d.k.: start of the implementation
69 ****************************************************************************/
71 #include "user/EplTimeru.h"
72 #include <linux/timer.h>
74 /***************************************************************************/
77 /* G L O B A L D E F I N I T I O N S */
80 /***************************************************************************/
82 //---------------------------------------------------------------------------
84 //---------------------------------------------------------------------------
86 //---------------------------------------------------------------------------
88 //---------------------------------------------------------------------------
91 struct timer_list m_Timer;
92 tEplTimerArg TimerArgument;
96 //---------------------------------------------------------------------------
98 //---------------------------------------------------------------------------
100 //---------------------------------------------------------------------------
101 // local function prototypes
102 //---------------------------------------------------------------------------
103 static void PUBLIC EplTimeruCbMs(unsigned long ulParameter_p);
105 /***************************************************************************/
108 /* C L A S S <Epl Userspace-Timermodule for Linux Kernel> */
111 /***************************************************************************/
113 // Description: Epl Userspace-Timermodule for Linux Kernel
116 /***************************************************************************/
118 //=========================================================================//
120 // P U B L I C F U N C T I O N S //
122 //=========================================================================//
124 //---------------------------------------------------------------------------
126 // Function: EplTimeruInit
128 // Description: function inits first instance
132 // Returns: tEplKernel = errorcode
136 //---------------------------------------------------------------------------
138 tEplKernel PUBLIC EplTimeruInit()
142 Ret = EplTimeruAddInstance();
148 //---------------------------------------------------------------------------
150 // Function: EplTimeruAddInstance
152 // Description: function inits additional instance
156 // Returns: tEplKernel = errorcode
160 //---------------------------------------------------------------------------
162 tEplKernel PUBLIC EplTimeruAddInstance()
166 Ret = kEplSuccessful;
172 //---------------------------------------------------------------------------
174 // Function: EplTimeruDelInstance
176 // Description: function deletes instance
177 // -> under Linux nothing to do
178 // -> no instance table needed
182 // Returns: tEplKernel = errorcode
186 //---------------------------------------------------------------------------
188 tEplKernel PUBLIC EplTimeruDelInstance()
192 Ret = kEplSuccessful;
198 //---------------------------------------------------------------------------
200 // Function: EplTimeruSetTimerMs
202 // Description: function creates a timer and returns the corresponding handle
204 // Parameters: pTimerHdl_p = pointer to a buffer to fill in the handle
205 // ulTime_p = time for timer in ms
206 // Argument_p = argument for timer
208 // Returns: tEplKernel = errorcode
212 //---------------------------------------------------------------------------
214 tEplKernel PUBLIC EplTimeruSetTimerMs(tEplTimerHdl* pTimerHdl_p,
215 unsigned long ulTime_p,
216 tEplTimerArg Argument_p)
218 tEplKernel Ret = kEplSuccessful;
219 tEplTimeruData* pData;
221 // check pointer to handle
222 if(pTimerHdl_p == NULL)
224 Ret = kEplTimerInvalidHandle;
228 pData = (tEplTimeruData*) EPL_MALLOC(sizeof (tEplTimeruData));
231 Ret = kEplNoResource;
235 init_timer(&pData->m_Timer);
236 pData->m_Timer.function = EplTimeruCbMs;
237 pData->m_Timer.data = (unsigned long) pData;
238 pData->m_Timer.expires = jiffies + ulTime_p * HZ / 1000;
240 EPL_MEMCPY(&pData->TimerArgument, &Argument_p, sizeof(tEplTimerArg));
242 add_timer(&pData->m_Timer);
244 *pTimerHdl_p = (tEplTimerHdl) pData;
251 //---------------------------------------------------------------------------
253 // Function: EplTimeruModifyTimerMs
255 // Description: function changes a timer and returns the corresponding handle
257 // Parameters: pTimerHdl_p = pointer to a buffer to fill in the handle
258 // ulTime_p = time for timer in ms
259 // Argument_p = argument for timer
261 // Returns: tEplKernel = errorcode
265 //---------------------------------------------------------------------------
267 tEplKernel PUBLIC EplTimeruModifyTimerMs(tEplTimerHdl* pTimerHdl_p,
268 unsigned long ulTime_p,
269 tEplTimerArg Argument_p)
271 tEplKernel Ret = kEplSuccessful;
272 tEplTimeruData* pData;
274 // check pointer to handle
275 if(pTimerHdl_p == NULL)
277 Ret = kEplTimerInvalidHandle;
281 // check handle itself, i.e. was the handle initialized before
282 if (*pTimerHdl_p == 0)
284 Ret = EplTimeruSetTimerMs(pTimerHdl_p, ulTime_p, Argument_p);
287 pData = (tEplTimeruData*) *pTimerHdl_p;
288 if ((tEplTimeruData*)pData->m_Timer.data != pData)
290 Ret = kEplTimerInvalidHandle;
294 mod_timer(&pData->m_Timer, (jiffies + ulTime_p * HZ / 1000));
296 // copy the TimerArg after the timer is restarted,
297 // so that a timer occured immediately before mod_timer
298 // won't use the new TimerArg and
299 // therefore the old timer cannot be distinguished from the new one.
300 // But if the new timer is too fast, it may get lost.
301 EPL_MEMCPY(&pData->TimerArgument, &Argument_p, sizeof(tEplTimerArg));
303 // check if timer is really running
304 if (timer_pending(&pData->m_Timer) == 0)
305 { // timer is not running
307 add_timer(&pData->m_Timer);
310 // set handle to pointer of tEplTimeruData
311 // *pTimerHdl_p = (tEplTimerHdl) pData;
318 //---------------------------------------------------------------------------
320 // Function: EplTimeruDeleteTimer
322 // Description: function deletes a timer
324 // Parameters: pTimerHdl_p = pointer to a buffer to fill in the handle
326 // Returns: tEplKernel = errorcode
330 //---------------------------------------------------------------------------
332 tEplKernel PUBLIC EplTimeruDeleteTimer(tEplTimerHdl* pTimerHdl_p)
334 tEplKernel Ret = kEplSuccessful;
335 tEplTimeruData* pData;
337 // check pointer to handle
338 if(pTimerHdl_p == NULL)
340 Ret = kEplTimerInvalidHandle;
344 // check handle itself, i.e. was the handle initialized before
345 if (*pTimerHdl_p == 0)
347 Ret = kEplSuccessful;
350 pData = (tEplTimeruData*) *pTimerHdl_p;
351 if ((tEplTimeruData*)pData->m_Timer.data != pData)
353 Ret = kEplTimerInvalidHandle;
357 /* if (del_timer(&pData->m_Timer) == 1)
362 // try to delete the timer
363 del_timer(&pData->m_Timer);
364 // free memory in any case
367 // uninitialize handle
376 //---------------------------------------------------------------------------
378 // Function: EplTimeruIsTimerActive
380 // Description: checks if the timer referenced by the handle is currently
383 // Parameters: TimerHdl_p = handle of the timer to check
385 // Returns: BOOL = TRUE, if active;
390 //---------------------------------------------------------------------------
392 BOOL PUBLIC EplTimeruIsTimerActive(tEplTimerHdl TimerHdl_p)
394 BOOL fActive = FALSE;
395 tEplTimeruData* pData;
397 // check handle itself, i.e. was the handle initialized before
399 { // timer was not created yet, so it is not active
402 pData = (tEplTimeruData*) TimerHdl_p;
403 if ((tEplTimeruData*)pData->m_Timer.data != pData)
408 // check if timer is running
409 if (timer_pending(&pData->m_Timer) == 0)
410 { // timer is not running
421 //=========================================================================//
423 // P R I V A T E F U N C T I O N S //
425 //=========================================================================//
427 //---------------------------------------------------------------------------
429 // Function: EplTimeruCbMs
431 // Description: function to process timer
435 // Parameters: lpParameter = pointer to structur of type tEplTimeruData
443 //---------------------------------------------------------------------------
444 static void PUBLIC EplTimeruCbMs(unsigned long ulParameter_p)
446 tEplKernel Ret = kEplSuccessful;
447 tEplTimeruData* pData;
449 tEplTimerEventArg TimerEventArg;
451 pData = (tEplTimeruData*) ulParameter_p;
453 // call event function
454 TimerEventArg.m_TimerHdl = (tEplTimerHdl)pData;
455 TimerEventArg.m_ulArg = pData->TimerArgument.m_ulArg;
457 EplEvent.m_EventSink = pData->TimerArgument.m_EventSink;
458 EplEvent.m_EventType = kEplEventTypeTimer;
459 EPL_MEMSET(&EplEvent.m_NetTime, 0x00, sizeof(tEplNetTime));
460 EplEvent.m_pArg = &TimerEventArg;
461 EplEvent.m_uiSize = sizeof(TimerEventArg);
463 Ret = EplEventuPost(&EplEvent);
465 // d.k. do not free memory, user has to call EplTimeruDeleteTimer()