source: svn/zas_dstar/hal/os/src/module/lld_osutil.c @ 45

Last change on this file since 45 was 45, checked in by megakiss, 11 years ago
File size: 11.3 KB
Line 
1/****************************************************************************
2 *.Copyright (c) 2006 DST Technologies Inc.  All Rights Reserved.   
3 *
4 * Module:      LLD_OSUTIL
5 *
6 * Description: Unified APIs for Lowest Level Driver
7 *
8 * Notes:       This module implements APIs to interface with the following
9 *              device objects:
10 *              - OS specific Utilities
11 *
12 ***************************************************************************/
13#include "lld_os.h"
14#include "lld_local.h"
15#include <linux/interrupt.h>
16
17/*==============================
18 * Local defines
19 *=============================*/
20/*
21 * This returns 1 if the address is from user-address-space
22 * It's used because some of the functions can be called with
23 * args from user-space or from kernel-space memory
24 */
25#define FROM_USER_SPACE(addr) (((DS_U32) (addr)) >= PAGE_OFFSET ? 0 : 1)
26
27#define PUSH_PID        DS_U32 PidSave=current_filp; current_filp=KERNEL_PID;
28#define POP_PID         current_filp=PidSave;
29
30/*==============================
31 * Local prototypes
32 *=============================*/
33
34
35/*==============================
36 * External declarations
37 *=============================*/
38extern DS_U32 current_filp;
39
40/*^^***************************************************************************
41 * void lld_delay (milliseconds)
42 *
43 * Description:
44 *          Delays execution of caller for some milliseconds
45 *          This should NOT be used from interrupt handlers !
46 *
47 * Entry :  number of milliseconds to delay
48 *
49 * Return:  nothing
50 *
51 **************************************************************************^^*/
52void lld_delay (DS_U32 millisec) 
53{
54    PUSH_PID;
55#if KERNEL_2_4_0
56    current->state = TASK_INTERRUPTIBLE;
57    schedule_timeout ((HZ / 1000) * millisec);
58#elif 1
59    current->state = TASK_INTERRUPTIBLE;
60    schedule_timeout ((HZ / 1000) * millisec);
61#else   
62    current->timeout = (HZ / 1000) * millisec; 
63    current->state = TASK_INTERRUPTIBLE;
64    schedule ();
65    current->timeout = 0;
66#endif
67    POP_PID;
68}
69
70/*^^***************************************************************************
71 * void lld_os_disable_ints (void)
72 *
73 * Description:
74 *          disables system interrupts
75 *
76 * Entry :  none
77 *
78 * Return:  previous interrupt flag state
79 *
80 **************************************************************************^^*/
81DS_U32 lld_os_disable_ints (void)
82{
83    DS_U32 flags;   
84
85#if KERNEL_2_4_0
86    save_flags(flags);
87    cli ();
88#else
89    local_save_flags(flags);
90    local_irq_disable ();
91#endif
92    return (flags);
93}
94
95/*^^***************************************************************************
96 * void lld_os_restore_ints (DS_U32 flags)
97 *
98 * Description:
99 *          restores interrupt state
100 *
101 * Entry :  none
102 *
103 * Return:  none
104 *
105 **************************************************************************^^*/
106void lld_os_restore_ints (DS_U32 flags)
107{
108#if KERNEL_2_4_0
109    restore_flags(flags);
110#else
111    local_irq_restore(flags);
112#endif
113}
114       
115/*^^***************************************************************************
116 * void lld_os_enable_ints (void)
117 *
118 * Description:
119 *          Enable interrupts
120 *
121 * Entry :  none
122 *
123 * Return:  none
124 *
125 **************************************************************************^^*/
126void lld_os_enable_ints (void)
127{
128#if KERNEL_2_4_0
129    sti();
130#else
131    local_irq_enable();
132#endif
133}
134       
135/*^^***************************************************************************
136 * void* lld_os_malloc (DS_U32 cbsize)
137 *
138 * Description:
139 *          allocates a block of kernel memory
140 *
141 * Entry :  cbsize = size in bytes
142 *
143 * Return:  pointer to allocated kernel memory block
144 *
145 **************************************************************************^^*/
146void* lld_os_malloc (DS_U32 cbsize)
147{
148    return kmalloc (cbsize, GFP_KERNEL);
149}
150
151/*^^***************************************************************************
152 * void lld_os_free (void *pmemory)
153 *
154 * Description:
155 *          frees a previously allocated block of kernel memory
156 *
157 * Entry :  pointer to allocated kernel memory block
158 *
159 * Return:  none
160 *
161 **************************************************************************^^*/
162void lld_os_free (void *pmemory)
163{
164    kfree (pmemory);
165}
166
167/*^^***************************************************************************
168 * void lld_os_copy_from_user (DS_S8 *pdest, DS_S8* psrc, DS_U32 cbsize)
169 *
170 * Description:
171 *          copys the contents of user memory to kernel memory
172 *
173 * Entry :  pdest  = kernel mode destination pointer
174 *          psrc   = user mode source pointer
175 *          cbsize = size in bytes
176 *
177 * Return:  none
178 *
179 **************************************************************************^^*/
180void lld_os_copy_from_user (DS_S8 *pdest, DS_S8* psrc, DS_U32 cbsize)
181{
182    if (FROM_USER_SPACE(psrc))
183    {
184        copy_from_user (pdest, psrc, cbsize);
185    }
186    else
187    {
188        memcpy (pdest, psrc, cbsize);
189    }
190}
191
192/*^^***************************************************************************
193 * void lld_os_copy_to_user (DS_S8 *pdest, DS_S8* psrc, DS_U32 cbsize)
194 *
195 * Description:
196 *          copys the contents of kernel memory to user memory
197 *
198 * Entry :  pdest  = user mode destination pointer
199 *          psrc   = kernel mode source pointer
200 *          cbsize = size in bytes
201 *
202 * Return:  none
203 *
204 **************************************************************************^^*/
205void lld_os_copy_to_user (DS_S8 *pdest, DS_S8* psrc, DS_U32 cbsize)
206{
207    if (FROM_USER_SPACE(pdest))
208    {
209        copy_to_user (pdest, psrc, cbsize);
210    }
211    else
212    {
213        memcpy (pdest, psrc, cbsize);
214    }
215}
216
217/*^^***************************************************************************
218 * DS_BOOL lld_os_init_lock (DS_U32 lock, DS_U32 Init)
219 *
220 * Description:
221 *          initialize os specific semaphore type object
222 *
223 * Entry :  lock = Pointer to os specific semaphore id
224 *          Init = initialization value
225 *
226 * Return:  _TRUE_ or _FALSE_
227 *
228 **************************************************************************^^*/
229DS_BOOL lld_os_init_lock (DS_U32 lock, DS_U32 Init)
230{
231    sema_init ((struct semaphore*)lock, Init);
232    return (_TRUE_);
233}
234
235/*^^***************************************************************************
236 * DS_BOOL lld_os_get_lock (DS_U32 lock)
237 *
238 * Description:
239 *          locks an os specific semaphore type object
240 *
241 * Entry :  lock = os specific semaphore id
242 *
243 * Return:  _TRUE_ or _FALSE_
244 *
245 **************************************************************************^^*/
246DS_BOOL lld_os_get_lock (DS_U32 lock)
247{
248    PUSH_PID;
249    down((struct semaphore*)lock);
250    POP_PID;
251    return (_TRUE_);
252}
253
254/*^^***************************************************************************
255 * lld_os_release_lock
256 *
257 * Description:
258 *          unlocks an os specific semaphore type object
259 *
260 * Entry :  lock = os specific semaphore id
261 *
262 * Return:  _TRUE_ or _FALSE_
263 *
264 **************************************************************************^^*/
265DS_BOOL lld_os_release_lock (DS_U32 lock)
266{
267    up((struct semaphore*)lock);
268    return (_TRUE_);
269}
270
271/*^^***************************************************************************
272 * lld_os_get_usermode_process_id
273 *
274 * Description:
275 *          get current user mode process id.
276 *
277 * Entry :  none
278 *
279 * Return:  current user mode process id
280 *
281 **************************************************************************^^*/
282DS_U32 lld_os_get_usermode_process_id (void)
283{
284    return ((DS_U32)current_filp);
285}
286
287/*^^***************************************************************************
288 * DS_BOOL lld_os_signal_detected (void)
289 *
290 * Description:
291 *
292 * Entry:   None
293 *
294 * Return:  _TRUE_ if the current task received an OS signal (e.g. Ctrl-C)
295 *
296 * Notes:
297 *
298 **************************************************************************^^*/
299DS_BOOL lld_os_signal_detected (void)
300{
301    return (signal_pending(current));
302}
303
304/*^^***************************************************************************
305 * DS_BOOL lld_check_wait_q (WAIT_Q *WaitQPtr)
306 *
307 * Description:
308 *
309 * Entry:   WaitQPtr = Wait queue pointer
310 *
311 * Return:  _TRUE_ if the queue contains any sleeping tasks
312 *
313 * Notes:
314 *
315 **************************************************************************^^*/
316DS_BOOL lld_check_wait_q (WAIT_Q *WaitQPtr)
317{
318    return (waitqueue_active(WaitQPtr));
319}
320
321/*^^***************************************************************************
322 * void lld_wait_for_interrupt (WAIT_Q *WaitQPtr)
323 *
324 * Description:
325 *          Enable interrupts and wait on WaitQPtr for wake up
326 *
327 * Entry:   WaitQPtr = Wait queue pointer
328 *
329 * Return:  Interrupt occured
330 *
331 * Notes:   Interrupts can be disabled before calling this function
332 *
333 **************************************************************************^^*/
334void lld_wait_for_interrupt (WAIT_Q *WaitQPtr)
335{
336    DECLARE_WAITQUEUE(wait, current);
337    PUSH_PID;
338    add_wait_queue(WaitQPtr, &wait);
339    __set_current_state(TASK_INTERRUPTIBLE);
340    lld_os_enable_ints();
341    schedule();
342    remove_wait_queue(WaitQPtr, &wait);
343    set_current_state(TASK_RUNNING);
344    POP_PID;
345}
346
347/*^^***************************************************************************
348 * DS_U32 lld_init_wait_q (WAIT_Q *WaitQPtr)
349 *
350 * Description: Initialize wait queue
351 *
352 * Entry:   WaitQPtr = Wait queue pointer
353 *
354 * Return:  0
355 *
356 * Notes:
357 *
358 **************************************************************************^^*/
359DS_U32 lld_init_wait_q (WAIT_Q *WaitQPtr)
360{
361#if 1
362    init_waitqueue_head (WaitQPtr);
363#else
364    *WaitQPtr = NULL;
365#endif
366    return (0);
367}
368
369/*^^***************************************************************************
370 * DS_U32 lld_wait_on_q (WAIT_Q *WaitQPtr)
371 *
372 * Description: Wait on queue for wake up signal
373 *
374 * Entry:   WaitQPtr = Wait queue pointer
375 *
376 * Return:  Wake up signal received
377 *
378 * Notes:
379 *
380 **************************************************************************^^*/
381DS_U32 lld_wait_on_q (WAIT_Q *WaitQPtr)
382{
383    PUSH_PID;
384    interruptible_sleep_on (WaitQPtr);
385    POP_PID;
386    return (0);
387}
388
389/*^^***************************************************************************
390 * DS_U32 lld_timeout_wait_on_q (WAIT_Q *WaitQPtr, DS_U32 TimeOut)
391 *
392 * Description: Wait on queue with timeout
393 *
394 * Entry:   WaitQPtr = Wait queue pointer
395 *
396 * Return: 
397 *
398 * Notes:
399 *
400 **************************************************************************^^*/
401DS_U32 lld_timeout_wait_on_q (WAIT_Q *WaitQPtr, DS_U32 TimeOut)
402{
403    DS_U32 i;
404    PUSH_PID;
405    i = (DS_U32)(interruptible_sleep_on_timeout (WaitQPtr, TimeOut));
406    POP_PID;
407    return (i);
408}
409
410/*^^***************************************************************************
411 * DS_U32 lld_wakeup_wait_q (WAIT_Q *WaitQPtr)
412 *
413 * Description: Wake up waiting interruptible taks in a queue
414 *
415 * Entry:   WaitQPtr = Wait queue pointer
416 *
417 * Return: 
418 *
419 * Notes:
420 *
421 **************************************************************************^^*/
422DS_U32 lld_wakeup_wait_q (WAIT_Q *WaitQPtr)
423{
424    PUSH_PID;
425    wake_up_interruptible (WaitQPtr);
426    POP_PID;
427    return (0);
428}
429
430/*^^***************************************************************************
431 * DS_U32 lld_force_wakeup_wait_q (WAIT_Q *WaitQPtr)
432 *
433 * Description: Wake up any tasks in a queue
434 *
435 * Entry:   WaitQPtr = Wait queue pointer
436 *
437 * Return: 
438 *
439 * Notes:
440 *
441 **************************************************************************^^*/
442DS_U32 lld_force_wakeup_wait_q (WAIT_Q *WaitQPtr)
443{
444    PUSH_PID;
445    wake_up (WaitQPtr);
446    POP_PID;
447    return (0);
448}
Note: See TracBrowser for help on using the repository browser.