xref: /utopia/UTPA2-700.0.x/modules/pws/drv/pws/drvPWS.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 ///////////////////////////////////////////////////////////////////////////////
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// file drvPWS.c
96 /// @author MStar Semiconductor Inc.
97 /// @brief power saving driver
98 ////////////////////////////////////////////////////////////////////////////////
99 //------------------------------------------------------------------------------
100 // Header Files
101 //------------------------------------------------------------------------------
102 #include <stdio.h>
103 #include <string.h>
104 #if defined (MSOS_TYPE_LINUX)
105 #include <unistd.h>
106 #include <sys/socket.h>
107 #include <sys/ioctl.h>
108 #include <net/if.h>
109 #endif
110 
111 #include "MsCommon.h"
112 #include "MsVersion.h"
113 #include "UFO.h"
114 #include "drvPWS.h"
115 #include "halPWS.h"
116 #include "drvMMIO.h"
117 #include "utopia_dapi.h"
118 #include "MsOS.h"
119 #include "utopia.h"
120 
121 #include "ULog.h"
122 
123 //------------------------------------------------------------------------------
124 // Local defines & local structures
125 //------------------------------------------------------------------------------
126 #define TAG_PWS "PWS"
127 
128 #define PWS_DBG_FUNC()             if (_pwsDbgLevel >= E_PWS_DBGLV_ALL) \
129                                         {ULOGD(TAG_PWS, "\t====   %s   ====\n", __FUNCTION__);}
130 #define PWS_DBG_INFO(x, args...)   if (_pwsDbgLevel >= E_PWS_DBGLV_INFO ) \
131                                         {ULOGI(TAG_PWS, x, ##args);}
132 #define PWS_DBG_ERR(x, args...)    if (_pwsDbgLevel >= E_PWS_DBGLV_ERR_ONLY) \
133                                         {ULOGE(TAG_PWS, x, ##args);}
134 #define DEBUG_INFO(y)   //y
135 
136 //------------------------------------------------------------------------------
137 // External funciton
138 //------------------------------------------------------------------------------
139 
140 //------------------------------------------------------------------------------
141 // Local & Global Variables
142 //------------------------------------------------------------------------------
143 //define mutex
144 static MS_S32 g_s32PWSMutex=-1;
145 #define PWS_MUTEX_CREATE()          g_s32PWSMutex = MsOS_CreateMutex(E_MSOS_FIFO, (char*)"PWSMUTEX" , MSOS_PROCESS_SHARED)
146 #define PWS_MUTEX_LOCK()            MsOS_ObtainMutex(g_s32PWSMutex,MSOS_WAIT_FOREVER)
147 #define PWS_MUTEX_UNLOCK()          MsOS_ReleaseMutex(g_s32PWSMutex)
148 #define PWS_MUTEX_DELETE()          MsOS_DeleteMutex(g_s32PWSMutex)
149 
150 static MS_BOOL PWS_Init = 0;
151 static E_PWS_Use PWS_UseStatus = E_PWS_API_UNUSED;
152 static MSIF_Version _api_pws_version = {
153     .DDI = { PWS_API_VERSION },
154 };
155 static E_PWS_DBG_LEVEL _pwsDbgLevel = E_PWS_DBGLV_ERR_ONLY;
156 static E_PWS_VIF_type pws_vif_init_type = E_PWS_VIF_NO_SAW;
157 //------------------------------------------------------------------------------
158 // Local Function
159 //------------------------------------------------------------------------------
MDrv_PWS_SetIOMapBase(void)160 MS_U8 MDrv_PWS_SetIOMapBase(void)
161 {
162     MS_VIRT u32BaseAddr = 0;
163     MS_PHY  u32BaseSize = 0;
164 
165     DEBUG_INFO(ULOGI(TAG_PWS, "%s\n",__FUNCTION__));
166 
167     if(!MDrv_MMIO_GetBASE(&u32BaseAddr, &u32BaseSize, MS_MODULE_PWS))
168     {
169         DEBUG_INFO(ULOGE(TAG_PWS, "Get IOMAP Base faill!\n"));
170         return 0;
171     }
172     DEBUG_INFO(ULOGI(TAG_PWS, "Get IOMAP ID:%u Base:%lx!\n", MS_MODULE_PWS, u32BaseAddr));
173     HAL_PWS_SetIOMapBase(u32BaseAddr);
174     return 1;
175 }
176 
177 //------------------------------------------------------------------------------
178 // Global Function
179 //------------------------------------------------------------------------------
MDrv_PWS_GetLibVer(const MSIF_Version ** ppVersion)180 E_PWS_Result MDrv_PWS_GetLibVer(const MSIF_Version **ppVersion)
181 {
182     DEBUG_INFO(ULOGI(TAG_PWS, "%s\n",__FUNCTION__));
183     if(!ppVersion)
184         return PWS_FAIL;
185 
186     *ppVersion = &_api_pws_version;
187     return PWS_SUCCESS;
188 }
189 
MDrv_PWS_SetDbgLevel(E_PWS_DBG_LEVEL eLEVEL)190 E_PWS_Result MDrv_PWS_SetDbgLevel(E_PWS_DBG_LEVEL eLEVEL)
191 {
192     DEBUG_INFO(ULOGI(TAG_PWS, "%s\n",__FUNCTION__));
193 
194     _pwsDbgLevel = eLEVEL;
195     return PWS_SUCCESS;
196 }
197 
MDrv_PWS_GetInfo(PWS_Info * pInfo)198 void MDrv_PWS_GetInfo(PWS_Info *pInfo)
199 {
200     DEBUG_INFO(ULOGI(TAG_PWS, "%s\n",__FUNCTION__));
201     pInfo->SourceList = HAL_PWS_GetSourceInfo();
202     pInfo->u32IOMap = HAL_PWS_GetIOMapBase();
203 }
204 
MDrv_PWS_GetStatus(PWS_Status * pStatus)205 void MDrv_PWS_GetStatus(PWS_Status *pStatus)
206 {
207     DEBUG_INFO(ULOGI(TAG_PWS, "%s\n",__FUNCTION__));
208     pStatus->bInit = PWS_Init;
209     pStatus->u8DbgLevel = _pwsDbgLevel;
210 }
211 
MDrv_PWS_GetSourceInfo(void)212 E_PWS_SouceInfo MDrv_PWS_GetSourceInfo(void)
213 {
214     DEBUG_INFO(ULOGI(TAG_PWS, "%s\n",__FUNCTION__));
215     return HAL_PWS_GetSourceInfo();
216 }
217 
MDrv_PWS_Read2Byte(MS_U32 u32RegAddr)218 MS_U16 MDrv_PWS_Read2Byte(MS_U32 u32RegAddr)
219 {
220     DEBUG_INFO(ULOGI(TAG_PWS, "%s\n",__FUNCTION__));
221     return HAL_PWS_Read2Byte(u32RegAddr);
222 }
223 
MDrv_PWS_Init(E_PWS_VIF_type eVifType)224 E_PWS_Result MDrv_PWS_Init(E_PWS_VIF_type eVifType)
225 {
226     E_PWS_Result ret = PWS_FAIL;
227     pws_vif_init_type = eVifType;
228     DEBUG_INFO(ULOGI(TAG_PWS, "%s\n",__FUNCTION__));
229 
230     if(!PWS_Init)
231     {
232         if (MDrv_PWS_SetIOMapBase() != TRUE)
233         {
234             DEBUG_INFO(ULOGE(TAG_PWS, "Get IO base fail\n"));
235             return PWS_FAIL;
236         }
237 
238         HAL_PWS_Init(eVifType);
239         ret = PWS_SUCCESS;
240     }
241 
242     #if ((defined(UFO_XC_DAC_POWERCONTROL)) && (!defined(CONFIG_MBOOT)))
243     HAL_PWS_MonitorPowerProc();
244     #endif
245 
246     PWS_Init = 1;
247     return ret;
248 }
249 
MDrv_PWS_IpPowerControl(E_PWS_IP_name eIpName,E_PWS_IpPowerCtrl pwr_type)250 E_PWS_Result MDrv_PWS_IpPowerControl(E_PWS_IP_name eIpName, E_PWS_IpPowerCtrl pwr_type)
251 {
252     if (eIpName > E_PWS_IP_MAX || pwr_type == E_PWS_IP_ON)
253     {
254         return PWS_FAIL;
255     }
256 
257     MDrv_PWS_Init(pws_vif_init_type);
258 
259     return HAL_PWS_IpPowerControl(eIpName, pwr_type);;
260 }
261 
MDrv_PWS_ScenePowerControl(E_PWS_SCENE_name eSceneName,E_PWS_IpPowerCtrl pwr_type)262 E_PWS_Result MDrv_PWS_ScenePowerControl(E_PWS_SCENE_name eSceneName, E_PWS_IpPowerCtrl pwr_type)
263 {
264 #if ((defined(UFO_XC_DAC_POWERCONTROL)) && (!defined(CONFIG_MBOOT)))
265 
266     MDrv_PWS_Init(pws_vif_init_type);
267 
268     return HAL_PWS_ScenePowerControl(eSceneName, pwr_type);
269 #else
270     return PWS_SUCCESS;
271 #endif
272 }
273 
MDrv_PWS_HandleSource(E_PWS_Operation operation,E_PWS_SouceInfo source,E_PWS_Sync sync_type)274 E_PWS_Result MDrv_PWS_HandleSource(E_PWS_Operation operation, E_PWS_SouceInfo source, E_PWS_Sync sync_type)
275 {
276     E_PWS_Result ret = PWS_FAIL;
277 
278     DEBUG_INFO(ULOGI(TAG_PWS, "%s\n",__FUNCTION__));
279     if(PWS_Init == 0)
280         return ret;
281     if(operation == PWS_INVAILD_OP)
282         return ret;
283     if((source == _NO_SOURCE_) || (source == _UNKNOWN_))
284         return ret;
285     if(PWS_UseStatus == E_PWS_USE_HANDLE_IP)
286         return ret;
287 
288     PWS_UseStatus = E_PWS_USE_HANDLE_SOURCE;
289     sync_type = sync_type;
290     ret = HAL_PWS_HandleSource(operation,source);
291 
292     return ret;
293 }
294 
MDrv_PWS_HandleIP(E_PWS_IpPowerCtrl IpPowerCtrl,const unsigned char * RegName)295 E_PWS_Result MDrv_PWS_HandleIP(E_PWS_IpPowerCtrl IpPowerCtrl,const unsigned char *RegName)
296 {
297     E_PWS_Result ret = PWS_FAIL;
298 
299     DEBUG_INFO(ULOGI(TAG_PWS, "%s\n",__FUNCTION__));
300     if(PWS_Init == 0)
301         return ret;
302     if(PWS_UseStatus == E_PWS_USE_HANDLE_SOURCE)
303         return ret;
304 
305     PWS_UseStatus = E_PWS_USE_HANDLE_IP;
306     ret = HAL_PWS_HandleIP(IpPowerCtrl,RegName);
307 
308     return ret;
309 }
310 
MDrv_PWS_RegisterCallback(MS_U8 index,E_PWS_CallbackCtrl cb_ctrl,unsigned char * RegName,P_PWS_Callback pfCallback)311 E_PWS_Result MDrv_PWS_RegisterCallback(MS_U8 index, E_PWS_CallbackCtrl cb_ctrl, unsigned char *RegName, P_PWS_Callback pfCallback)
312 {
313     E_PWS_Result ret = PWS_FAIL;
314 
315     DEBUG_INFO(ULOGI(TAG_PWS, "%s\n",__FUNCTION__));
316     if(index < PWS_Register_CB_size)
317     {
318         ret = HAL_PWS_RegisterCallback(index,cb_ctrl,RegName,pfCallback);
319     }
320     else
321     {
322         ret = PWS_FAIL;
323     }
324 
325     return ret;
326 }
327 
MDrv_PWS_CancelCallback(MS_U8 index)328 E_PWS_Result MDrv_PWS_CancelCallback(MS_U8 index)
329 {
330     E_PWS_Result ret = PWS_FAIL;
331 
332     DEBUG_INFO(ULOGI(TAG_PWS, "%s\n",__FUNCTION__));
333     if(index < PWS_Register_CB_size)
334     {
335         ret = HAL_PWS_CancelCallback(index);
336     }
337     else
338     {
339         ret = PWS_FAIL;
340     }
341 
342     return ret;
343 }
344 
345 #if defined (MSOS_TYPE_LINUX)
MDrv_PWS_PHYPower(E_PWS_PHY bSetFlag)346 E_PWS_Result MDrv_PWS_PHYPower(E_PWS_PHY bSetFlag)
347 {
348     struct ifreq ifr;
349     int sockfd;
350     const char s[] = "eth0" ;
351 	MS_U32 ret = PWS_SUCCESS;
352 
353     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
354     if(sockfd < 0)
355     {
356         DEBUG_INFO(ULOGE(TAG_PWS, "sockfd is negative %s\n",__FUNCTION__));
357         return PWS_FAIL;
358     }
359     memset(&ifr, 0, sizeof(ifr));
360     strncpy(ifr.ifr_name, s, 4);
361 
362     if(bSetFlag == PHY_OFF)
363     {
364         if(ioctl(sockfd, SIOCDEVOFF, &ifr))
365         {
366             DEBUG_INFO(ULOGE(TAG_PWS, "IOCTL FAIL %s\n",__FUNCTION__));
367             close(sockfd);
368             return PWS_FAIL;
369         }
370     }
371     else
372     {
373         if(ioctl(sockfd, SIOCDEVON, &ifr))
374         {
375             DEBUG_INFO(ULOGE(TAG_PWS, "IOCTL FAIL %s\n",__FUNCTION__));
376             close(sockfd);
377             return PWS_FAIL;
378         }
379     }
380 
381 	close(sockfd);
382     return ret;
383 }
384 #endif
385 
MDrv_PWS_SetPowerState(EN_POWER_MODE u16PowerState)386 MS_U32 MDrv_PWS_SetPowerState(EN_POWER_MODE u16PowerState)
387 {
388     static EN_POWER_MODE _prev_u16PowerState = E_POWER_MECHANICAL;
389     MS_U32 u32Return = UTOPIA_STATUS_FAIL;
390 
391     if(u16PowerState == E_POWER_SUSPEND)
392     {
393         _prev_u16PowerState = u16PowerState;
394         PWS_Init = false; //set PWS init state to false
395         u32Return = UTOPIA_STATUS_SUCCESS;//SUSPEND_OK;
396 
397     }
398     else if(u16PowerState == E_POWER_RESUME)
399     {
400         if(_prev_u16PowerState == E_POWER_SUSPEND)
401         {
402             MDrv_PWS_Init(pws_vif_init_type);
403             _prev_u16PowerState = u16PowerState;
404             u32Return = UTOPIA_STATUS_SUCCESS;//RESUME_OK;
405         }
406         else
407         {
408             ULOGE(TAG_PWS, "[%s,%5d]It is not suspended yet. We shouldn't resume\n",__FUNCTION__,__LINE__);
409             u32Return = UTOPIA_STATUS_FAIL;//SUSPEND_FAILED;
410         }
411         return u32Return;
412     }
413     else
414     {
415         ULOGE(TAG_PWS, "[%s,%5d]Do Nothing: %d\n",__FUNCTION__,__LINE__,u16PowerState);
416         u32Return = UTOPIA_STATUS_FAIL;
417     }
418 
419     return u32Return;// for success
420 }
421 
422 
423 enum
424 {
425     PWS_POOL_ID_PWS0=0
426 } ePWS_PoolID;
427 
428 //--------------------------------------------------------------------------------------------------
429 // Utopia2.0 will call this function to register PWS module
430 //--------------------------------------------------------------------------------------------------
PWSRegisterToUtopia(FUtopiaOpen ModuleType)431 void PWSRegisterToUtopia(FUtopiaOpen ModuleType)
432 {
433     // 1. create a module(module_name, SHM_size), and register to utopia2.0
434     void* pUtopiaModule = NULL;
435     UtopiaModuleCreate(MODULE_PWS, 0, &pUtopiaModule);
436     UtopiaModuleRegister(pUtopiaModule);
437 
438     // register func for module, after register here, then ap call UtopiaOpen/UtopiaIoctl/UtopiaClose can call to these registered standard func
439     UtopiaModuleSetupFunctionPtr(pUtopiaModule, (FUtopiaOpen)PWSOpen, (FUtopiaClose)PWSClose, (FUtopiaIOctl)PWSIoctl);
440 
441     // 2. Resource register
442     void* psResource = NULL;
443 
444     // start func to add resources of a certain Resource_Pool
445     UtopiaModuleAddResourceStart(pUtopiaModule, PWS_POOL_ID_PWS0);
446 
447     // create a resouce and regiter it to a certain Resource_Pool, resource can alloc private for internal use
448     UtopiaResourceCreate("pws0", sizeof(PWS_RESOURCE_PRIVATE), &psResource);
449     UtopiaResourceRegister(pUtopiaModule, psResource, PWS_POOL_ID_PWS0);
450 
451     //UtopiaResourceCreate("pws1", sizeof(PWS_RESOURCE_PRIVATE), &psResource);
452     //UtopiaResourceRegister(pUtopiaModule, psResource, PWS_POOL_ID_PWS0);
453 
454     // end func to add resources of a certain Resource_Pool(this will set the ResourceSemaphore of this ResourcePool)
455     UtopiaModuleAddResourceEnd(pUtopiaModule, PWS_POOL_ID_PWS0);
456 
457     //MDrv_PWS_Init(E_PWS_VIF_DUAL_SAW);
458     ULOGD(TAG_PWS, "\033[35mFunction = %s, Line = %d, PWS Register finished\033[m\n", __PRETTY_FUNCTION__, __LINE__);
459 }
460 
461 //--------------------------------------------------------------------------------------------------
462 // Utopia2.0 will call this function to get a instance to use PWS
463 // @ \b in: 32ModuleVersion => this is for checking if API version is the same
464 //--------------------------------------------------------------------------------------------------
PWSOpen(void ** ppInstance,MS_U32 u32ModuleVersion,void * pAttribute)465 MS_U32 PWSOpen(void** ppInstance, MS_U32 u32ModuleVersion, void* pAttribute)
466 {
467     MS_VIRT u32RetAddr;
468     MS_U32 u32RetShmID, u32RetShmSize;
469     MS_U32 u32Ret, u32RetTmp;
470 
471     //Check DRV_REVISION
472     if(u32ModuleVersion != PWS_VERSION)
473     {
474         //printf("\033[35mFunction = %s, Line = %d, ERROR, DRV_VERSION is not PWS_VERSION\033[m\n", __PRETTY_FUNCTION__, __LINE__);
475         //return FALSE;
476     }
477 
478     // for multi-process safe, check if already other Instance exist
479     PWS_MUTEX_LOCK();
480     u32Ret = MsOS_SHM_GetId((MS_U8*)"PWS_Instance", sizeof(MS_U32), &u32RetShmID, &u32RetAddr, &u32RetShmSize, MSOS_SHM_QUERY);
481     if(u32Ret == 0)
482     {
483         // first create, set value as 0
484         u32RetTmp = MsOS_SHM_GetId((MS_U8*)"PWS_Instance", sizeof(MS_U32), &u32RetShmID, &u32RetAddr, &u32RetShmSize, MSOS_SHM_CREATE);
485 
486         if (u32RetTmp == 0)
487         {
488             ULOGE(TAG_PWS, "[%s error] something wrong in MsOS_SHM_GetId\n", __FUNCTION__);
489             ULOGE(TAG_PWS, "is SHM_SIZE reached?\n");
490             ULOGE(TAG_PWS, "is MAX_SHM_CLIENT_NUM reached?\n");
491 
492             ULOGE(TAG_PWS, "Edit while(1) to return for Coverity Warning;");
493             return FALSE;
494         }
495 
496         *(MS_U32 *)u32RetAddr = 0;
497     }
498 
499     if(*(MS_U32 *)u32RetAddr == 1)
500     {
501         ULOGE(TAG_PWS, "\033[35mFunction = %s, Line = %d, [Multi-process Safe] A Instance is existed!!\033[m\n", __PRETTY_FUNCTION__, __LINE__);
502         PWS_MUTEX_UNLOCK();
503         return FALSE;
504     }
505     else
506     {
507         *(MS_U32 *)u32RetAddr = 1;
508         PWS_MUTEX_UNLOCK();
509     }
510 
511     PWS_INSTANT_PRIVATE *pPWSPri = NULL;
512     ULOGD(TAG_PWS, "\033[35mFunction = %s, Line = %d, [PWS INFO] OPEN INSTANCE...\033[m\n", __PRETTY_FUNCTION__, __LINE__); // joe.liu
513 
514     // instance is allocated here, also can allocate private for internal use, ex, PWS_INSTANT_PRIVATE, ppInstance will point to a pointer of the created UTOPIA_INSTANCE
515     UtopiaInstanceCreate(sizeof(PWS_INSTANT_PRIVATE), ppInstance);
516     // set the pPWSPri point to the private of UTOPIA_INSTANCE
517     UtopiaInstanceGetPrivate(*ppInstance, (void**)&pPWSPri);
518 
519     // setup func in private and assign the calling func in func ptr in instance private
520     pPWSPri->fpPWSGetLibVer = (IOCTL_PWS_GETLIBVER)MDrv_PWS_GetLibVer;
521     pPWSPri->fpPWSSetDbgLevel = (IOCTL_PWS_SETDBGLEVEL)MDrv_PWS_SetDbgLevel;
522     pPWSPri->fpPWSHandleSource = (IOCTL_PWS_HANDLESOURCE)MDrv_PWS_HandleSource;
523 #if defined (MSOS_TYPE_LINUX)
524     pPWSPri->fpPWSPHYPower = (IOCTL_PWS_PHYPOWER)MDrv_PWS_PHYPower;
525 #endif
526 
527     return TRUE;
528 }
529 
PWSIoctl(void * pInstance,MS_U32 u32Cmd,void * pArgs)530 MS_U32 PWSIoctl(void* pInstance, MS_U32 u32Cmd, void* pArgs)
531 {
532     void* pModule = NULL;
533     UtopiaInstanceGetModule(pInstance, &pModule);
534     void* pResource = NULL;
535 
536     PWS_INSTANT_PRIVATE* psPWSInstPri = NULL;
537     UtopiaInstanceGetPrivate(pInstance, (void**)&psPWSInstPri);
538 
539     PPWS_GETLIBVER_PARAM pGetLibVerParam = NULL;
540     PPWS_SETDBGLEVEL_PARAM pSetDbgLevelParam = NULL;
541     PPWS_HANDLESOURCE_PARAM pHandleSourceParam = NULL;
542 #if defined (MSOS_TYPE_LINUX)
543     PPWS_PHYPOWER_PARAM pPhyPowerParm = NULL;
544 #endif
545 
546     MS_U32 u32Ret;
547 
548     switch(u32Cmd)
549     {
550         case MDrv_CMD_PWS_GetLibVer:
551             ULOGD(TAG_PWS, "PWSIoctl - MDrv_CMD_PWS_GetLibVer\n");
552             pGetLibVerParam = (PPWS_GETLIBVER_PARAM)pArgs;
553             // only print, do not need to get resource
554             u32Ret = psPWSInstPri->fpPWSGetLibVer(pGetLibVerParam->ppVersion);
555             return u32Ret;
556         case MDrv_CMD_PWS_SetDbgLevel:
557             ULOGD(TAG_PWS,"PWSIoctl - MDrv_CMD_PWS_SetDbgLevel\n");
558             pSetDbgLevelParam = (PPWS_SETDBGLEVEL_PARAM)pArgs;
559             // to check if enough resource?
560             if(UtopiaResourceObtain(pModule, PWS_POOL_ID_PWS0, &pResource) != 0)
561             {
562                 ULOGE(TAG_PWS,"UtopiaResourceObtainToInstant fail\n");
563                 return 0xFFFFFFFF;
564             }
565             u32Ret = (MS_U32)psPWSInstPri->fpPWSSetDbgLevel(pSetDbgLevelParam->DbgLevel);
566             UtopiaResourceRelease(pResource);
567             return u32Ret;
568         case MDrv_CMD_PWS_HandleSource:
569             ULOGD(TAG_PWS,"PWSIoctl - MDrv_CMD_PWS_HandleSource\n");
570             pHandleSourceParam = (PPWS_HANDLESOURCE_PARAM)pArgs;
571             // to check if enough resource?
572             if(UtopiaResourceObtain(pModule, PWS_POOL_ID_PWS0, &pResource) != 0)
573             {
574                 ULOGE(TAG_PWS,"UtopiaResourceObtainToInstant fail\n");
575                 return 0xFFFFFFFF;
576             }
577             u32Ret = (MS_U32)psPWSInstPri->fpPWSHandleSource(pHandleSourceParam->operation, pHandleSourceParam->source, pHandleSourceParam->sync_type);
578             UtopiaResourceRelease(pResource);
579             return u32Ret;
580 #if defined (MSOS_TYPE_LINUX)
581         case MDrv_CMD_PWS_PHYPower:
582             ULOGD(TAG_PWS,"PWSIoctl - MDrv_CMD_PWS_PHYPower\n");
583             pPhyPowerParm = (PPWS_PHYPOWER_PARAM)pArgs;
584             // to check if enough resource?
585             if(UtopiaResourceObtain(pModule, PWS_POOL_ID_PWS0, &pResource) != 0)
586             {
587                 ULOGE(TAG_PWS,"UtopiaResourceObtainToInstant fail\n");
588                 return 0xFFFFFFFF;
589             }
590             u32Ret = (MS_U32)psPWSInstPri->fpPWSPHYPower(pPhyPowerParm->bSetFlag);
591             UtopiaResourceRelease(pResource);
592             return u32Ret;
593 #endif
594         default:
595             break;
596     };
597 
598 	return 0; // FIXME: error code
599 }
600 
PWSClose(void * pInstance)601 MS_U32 PWSClose(void* pInstance)
602 {
603     MS_VIRT u32RetAddr;
604     MS_U32 u32RetShmID, u32RetShmSize;
605     MS_U32 u32Ret;
606 
607 	u32RetAddr = 0 ; //fix coverity isue
608     UtopiaInstanceDelete(pInstance);
609 
610     u32Ret = MsOS_SHM_GetId((MS_U8*)"PWS_Instance", sizeof(MS_U32), &u32RetShmID, &u32RetAddr, &u32RetShmSize, MSOS_SHM_QUERY);
611 
612     if( (u32Ret == 0) || (*(MS_U32 *)u32RetAddr == 0) )
613     {
614         ULOGE(TAG_PWS,"\033[35mFunction = %s, Line = %d, [Multi-process Safe] u32IsInstanceCreated value is incorrect, which is %u!!\033[m\n", __PRETTY_FUNCTION__, __LINE__, *(unsigned int *)u32RetAddr);
615         return FALSE;
616     }
617     else
618     {
619         ULOGD(TAG_PWS,"\033[35mFunction = %s, Line = %d, [Multi-process Safe] Release a Instance!!\033[m\n", __PRETTY_FUNCTION__, __LINE__);
620         PWS_MUTEX_LOCK();
621         *(MS_U32 *)u32RetAddr = 0;
622         PWS_MUTEX_UNLOCK();
623         return TRUE;
624     }
625 }
626 
627 
628