source: svn/trunk/zas_dstar/hal/os/src/os_local.c @ 2

Last change on this file since 2 was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
File size: 6.9 KB
Line 
1/****************************************************************************
2 * Copyright (c) 2006 DST Technologies Inc.  All Rights Reserved.   
3 *
4 * Module:      OS LOCAL
5 *
6 * Description: Local APIs for use by the HAP.
7 *              (Linux)
8 *
9 * Notes:       This module implement APIs to interface between the kernel and user level.
10 *
11 ***************************************************************************/
12/*==========================
13 * Includes
14 *=========================*/
15#include <stdio.h>
16#include <string.h>
17#include <fcntl.h>
18#include <unistd.h>
19#include <sys/mman.h>
20#include <sys/types.h>
21#include <sys/ioctl.h>
22#include <errno.h>
23#include "oem_opt.h"
24#include "os_prive.h"
25#include "os.h"
26#include "module/dstmodule.h"
27
28#ifdef DMALLOC
29#include <dmalloc.h>
30#endif
31
32/*==========================
33 * Local defines
34 *=========================*/
35typedef struct
36{
37    DS_U32  InitCount;
38    DS_U32  Count;
39    DS_U32  Name;
40    DS_U32  Pids[MAX_NOF_PROCS];
41    DS_U32  Locks[MAX_NOF_PROCS];
42    DS_U32  Q[4];
43}SEM;
44
45typedef struct
46{
47    DS_U32  Status;
48    DS_U32  Name;
49    DS_U32  Pids[MAX_NOF_PROCS];
50    DS_U32  Q[4];
51}EVNT;
52
53/*==========================
54 * External declarations
55 *=========================*/
56extern DS_BOOL Os_QueueInit (DS_BOOL Init);
57extern DS_BOOL Os_TimersInit (DS_BOOL Init);
58
59/*=================================
60 * Global variables (per process)
61 *================================*/
62
63/*=================================
64 * local variables (per process)
65 *================================*/
66static DS_S32    ModHandle=INVALID_KHANDLE;  /* Per-process kernel handle    */
67
68/*^^***************************************************************************
69 * DS_U32 Os_GetOsType (void)
70 *
71 * Description:
72 *          Get OS ID
73 *
74 * Entry :  None
75 *
76 * Return:  Os ID
77 *
78 * Notes :
79 **************************************************************************^^*/
80DS_U32 Os_GetOsType (void)
81{
82    return (OS_LINUX);
83}
84
85/*^^***************************************************************************
86 * DS_BOOL Os_Initialize (DS_BOOL Init)
87 *
88 * Description: Initialize OSLIB data structures per process
89 *
90 * Entry :  Init    = _TRUE_  to init
91 *                    _FALSE_ to free
92 *
93 * Return:  _TRUE_ if Oslib internal data structures were initialized
94 *
95 * Notes :  This function must be called once for each process
96 *
97 **************************************************************************^^*/
98DS_BOOL Os_Initialize (DS_BOOL Init)
99{
100    DS_BOOL RetVal = _TRUE_;
101
102    if (Init)
103    {
104        ModHandle = Os_KmodGetHandle (0);
105        if (ModHandle == INVALID_KHANDLE)
106        {
107            RetVal = _FALSE_;
108        }
109        else
110        {
111            RetVal = RetVal && Os_QueueInit (_TRUE_);
112            RetVal = RetVal && Os_TimersInit (_TRUE_);
113        }
114        if (!RetVal)
115        {
116            Init = _FALSE_;
117        }
118    }
119
120    if (!Init)
121    {
122        RetVal = _FALSE_;
123        if (ModHandle != INVALID_KHANDLE)
124        {
125            RetVal = _TRUE_;
126            RetVal = RetVal && Os_TimersInit (_FALSE_);
127            RetVal = RetVal && Os_QueueInit (_FALSE_);
128            close (ModHandle);
129            ModHandle = INVALID_KHANDLE;
130        }
131    }
132    return (RetVal);
133}
134
135/*^^***************************************************************************
136 * DS_S32 Os_KmodGetHandle (DS_U32 ChipIndex)
137 *
138 * Description:
139 *          Gets a file handle for ioctl and mmap operations.
140 *          If file is already open, returns existing handle and increases
141 *          reference count.
142 *
143 * Entry :  ChipIndex   
144 *
145 * Return:  Valid Linux file handle to kernel module device
146 *          INVALID_KHANDLE on error
147 *
148 * Notes :
149 *
150 **************************************************************************^^*/
151DS_S32 Os_KmodGetHandle (DS_U32 ChipIndex)
152{
153    DS_S8  DevName[32];
154
155    if (ModHandle != INVALID_KHANDLE)
156    {
157        return (ModHandle);
158    }
159
160    sprintf (DevName, "/tmp/%s%d", DST_DEV_NAME, (int)0);
161    ModHandle = open (DevName, O_RDWR);
162    if (ModHandle < 0)
163    {
164        perror (DevName);
165        INFO_MSG ("\nCan not open module handle for device %s ", DevName);
166            return (INVALID_KHANDLE);
167    }
168
169        return (ModHandle);
170}
171
172/*^^***************************************************************************
173 * void Os_KmodReleaseHandle (DS_S32 Handle)
174 *
175 * Description:
176 *          Decreases the reference count.  When count reaches zero,
177 *          the file is closed.
178 *
179 * Entry :  Handle
180 *
181 * Return:  None
182 *
183 * Notes :
184 *
185 **************************************************************************^^*/
186void Os_KmodReleaseHandle (DS_S32 Handle)
187{
188    return;
189}
190
191/*^^***************************************************************************
192 * DS_U32 Os_PerformIoctl (DS_U32 Func, void *RetBuf)
193 *
194 * Description: Perform IOCTL function
195 *
196 * Entry :
197 *
198 * Return:  oxffffffff if ioctl error
199 *
200 * Notes : 
201 *
202 **************************************************************************^^*/
203DS_U32 Os_PerformIoctl (DS_U32 Func, void *RetBuf)
204{
205    DS_S32 i, FileH;
206
207    FileH = Os_KmodGetHandle (0);
208    if (FileH == INVALID_KHANDLE)
209    {
210        return (0xffffffff);
211    }
212    i = ioctl (FileH, Func, RetBuf);
213    Os_KmodReleaseHandle (FileH);
214    return (i);
215}
216
217/*^^***************************************************************************
218 * void Os_CheckResources (void)
219 *
220 * Description: Check OS resources in use by the application
221 *
222 * Entry :  None
223 *
224 * Return:  None
225 *
226 * Notes : 
227 *
228 **************************************************************************^^*/
229void Os_CheckResources (void)
230{
231    DS_U32 i, j;
232    EVNT Ev;
233    SEM  Sem;
234    OBJ_OPER ObjOp;
235
236    DBG_MSG ("\nList of semaphores still in the system:");
237    for (i=1; i<=MAX_NOF_SEMS; i++)
238    {
239        ObjOp.Opcode = OBJ_OPCODE_INFO;
240        ObjOp.Prm1   = i;
241        ObjOp.Prm2   = (DS_U32)(&Sem);
242        Os_PerformIoctl (DSTHWIOC_SEM_OP, &ObjOp);
243        if ((ObjOp.RetCode == OBJ_OK) && (Sem.InitCount != 0xffffffff))
244        {
245            DBG_MSG ("\nName       = %08lx", Sem.Name);
246            DBG_MSG ("\nOwner Pids = ");
247            for (j=0; j<MAX_NOF_PROCS; j++)
248            {
249                if (Sem.Pids[j] != 0xffffffff)
250                {
251                    DBG_MSG ("%08lx ", Sem.Pids[j]);
252                }
253            }
254            DBG_MSG ("\n");
255        }
256    }
257
258    DBG_MSG ("\n\nList of evenst still in the system:");
259    for (i=1; i<=MAX_NOF_EVENTS; i++)
260    {
261        ObjOp.Opcode = OBJ_OPCODE_INFO;
262        ObjOp.Prm1   = i;
263        ObjOp.Prm2   = (DS_U32)(&Ev);
264        Os_PerformIoctl (DSTHWIOC_EVENT_OP, &ObjOp);
265        if ((ObjOp.RetCode == OBJ_OK) && (Ev.Status != 0xffffffff))
266        {
267            DBG_MSG ("\nName       = %08lx", Ev.Name);
268            DBG_MSG ("\nOwner Pids = ");
269            for (j=0; j<MAX_NOF_PROCS; j++)
270            {
271                if (Ev.Pids[j] != 0xffffffff)
272                {
273                    DBG_MSG ("%08lx ", Ev.Pids[j]);
274                }
275            }
276            DBG_MSG ("\n");
277        }
278    }
279    DBG_MSG ("\n\nVideo memory allocation:\n");
280}
Note: See TracBrowser for help on using the repository browser.