/**************************************************************************** * Copyright (c) 2006 DST Technologies Inc. All Rights Reserved. * * Module: OS LOCAL * * Description: Local APIs for use by the HAP. * (Linux) * * Notes: This module implement APIs to interface between the kernel and user level. * ***************************************************************************/ /*========================== * Includes *=========================*/ #include #include #include #include #include #include #include #include #include "oem_opt.h" #include "os_prive.h" #include "os.h" #include "module/dstmodule.h" #ifdef DMALLOC #include #endif /*========================== * Local defines *=========================*/ typedef struct { DS_U32 InitCount; DS_U32 Count; DS_U32 Name; DS_U32 Pids[MAX_NOF_PROCS]; DS_U32 Locks[MAX_NOF_PROCS]; DS_U32 Q[4]; }SEM; typedef struct { DS_U32 Status; DS_U32 Name; DS_U32 Pids[MAX_NOF_PROCS]; DS_U32 Q[4]; }EVNT; /*========================== * External declarations *=========================*/ extern DS_BOOL Os_QueueInit (DS_BOOL Init); extern DS_BOOL Os_TimersInit (DS_BOOL Init); /*================================= * Global variables (per process) *================================*/ /*================================= * local variables (per process) *================================*/ static DS_S32 ModHandle=INVALID_KHANDLE; /* Per-process kernel handle */ /*^^*************************************************************************** * DS_U32 Os_GetOsType (void) * * Description: * Get OS ID * * Entry : None * * Return: Os ID * * Notes : **************************************************************************^^*/ DS_U32 Os_GetOsType (void) { return (OS_LINUX); } /*^^*************************************************************************** * DS_BOOL Os_Initialize (DS_BOOL Init) * * Description: Initialize OSLIB data structures per process * * Entry : Init = _TRUE_ to init * _FALSE_ to free * * Return: _TRUE_ if Oslib internal data structures were initialized * * Notes : This function must be called once for each process * **************************************************************************^^*/ DS_BOOL Os_Initialize (DS_BOOL Init) { DS_BOOL RetVal = _TRUE_; if (Init) { ModHandle = Os_KmodGetHandle (0); if (ModHandle == INVALID_KHANDLE) { RetVal = _FALSE_; } else { RetVal = RetVal && Os_QueueInit (_TRUE_); RetVal = RetVal && Os_TimersInit (_TRUE_); } if (!RetVal) { Init = _FALSE_; } } if (!Init) { RetVal = _FALSE_; if (ModHandle != INVALID_KHANDLE) { RetVal = _TRUE_; RetVal = RetVal && Os_TimersInit (_FALSE_); RetVal = RetVal && Os_QueueInit (_FALSE_); close (ModHandle); ModHandle = INVALID_KHANDLE; } } return (RetVal); } /*^^*************************************************************************** * DS_S32 Os_KmodGetHandle (DS_U32 ChipIndex) * * Description: * Gets a file handle for ioctl and mmap operations. * If file is already open, returns existing handle and increases * reference count. * * Entry : ChipIndex * * Return: Valid Linux file handle to kernel module device * INVALID_KHANDLE on error * * Notes : * **************************************************************************^^*/ DS_S32 Os_KmodGetHandle (DS_U32 ChipIndex) { DS_S8 DevName[32]; if (ModHandle != INVALID_KHANDLE) { return (ModHandle); } sprintf (DevName, "/tmp/%s%d", DST_DEV_NAME, (int)0); ModHandle = open (DevName, O_RDWR); if (ModHandle < 0) { perror (DevName); INFO_MSG ("\nCan not open module handle for device %s ", DevName); return (INVALID_KHANDLE); } return (ModHandle); } /*^^*************************************************************************** * void Os_KmodReleaseHandle (DS_S32 Handle) * * Description: * Decreases the reference count. When count reaches zero, * the file is closed. * * Entry : Handle * * Return: None * * Notes : * **************************************************************************^^*/ void Os_KmodReleaseHandle (DS_S32 Handle) { return; } /*^^*************************************************************************** * DS_U32 Os_PerformIoctl (DS_U32 Func, void *RetBuf) * * Description: Perform IOCTL function * * Entry : * * Return: oxffffffff if ioctl error * * Notes : * **************************************************************************^^*/ DS_U32 Os_PerformIoctl (DS_U32 Func, void *RetBuf) { DS_S32 i, FileH; FileH = Os_KmodGetHandle (0); if (FileH == INVALID_KHANDLE) { return (0xffffffff); } i = ioctl (FileH, Func, RetBuf); Os_KmodReleaseHandle (FileH); return (i); } /*^^*************************************************************************** * void Os_CheckResources (void) * * Description: Check OS resources in use by the application * * Entry : None * * Return: None * * Notes : * **************************************************************************^^*/ void Os_CheckResources (void) { DS_U32 i, j; EVNT Ev; SEM Sem; OBJ_OPER ObjOp; DBG_MSG ("\nList of semaphores still in the system:"); for (i=1; i<=MAX_NOF_SEMS; i++) { ObjOp.Opcode = OBJ_OPCODE_INFO; ObjOp.Prm1 = i; ObjOp.Prm2 = (DS_U32)(&Sem); Os_PerformIoctl (DSTHWIOC_SEM_OP, &ObjOp); if ((ObjOp.RetCode == OBJ_OK) && (Sem.InitCount != 0xffffffff)) { DBG_MSG ("\nName = %08lx", Sem.Name); DBG_MSG ("\nOwner Pids = "); for (j=0; j