xref: /utopia/UTPA2-700.0.x/modules/mbx/drv/mbx/linux/drvMBX.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 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file    drvMBX.c for Linux
98 /// @brief  MStar MailBox DDI
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 #define _GNU_SOURCE
102 
103 #define _DRV_MBX_C
104 
105 //=============================================================================
106 // Include Files
107 //=============================================================================
108 #include <errno.h>
109 #include <unistd.h>
110 #include <fcntl.h>
111 #include <sys/ioctl.h>
112 #include <string.h>
113 
114 #include "MsCommon.h"
115 #include "MsVersion.h"
116 #include "drvMBX.h"
117 #include "mdrv_mbx_io.h"
118 #include "mdrv_signals.h"
119 #include "drvDMD_VD_MBX.h"
120 #include "MsDevice.h"
121 #include "ULog.h"
122 
123 //=============================================================================
124 // Compile options
125 //=============================================================================
126 
127 
128 //=============================================================================
129 // Local Defines
130 //=============================================================================
131 #define MBXCLASS_IN_SIGNAL_MASK      0x0000FF00
132 #define MBXCLASS_IN_SIGNAL_SHIFT     0x8
133 #define TAG_MBX "MBX"
134 
135 //=============================================================================
136 // Debug Macros
137 //=============================================================================
138 
139 #define MBX_DBG_PRINT(dbgLevel, fmt, args...)  { \
140                                                     if(_u8DbgLevel&dbgLevel) \
141                                                     { \
142                                                         MBX_PRINT(fmt, ## args); \
143                                                     } \
144                                                    } \
145 
146 
147 
148 /*
149 #define MBX_DBG_PRINT(dbgLevel, fmt, args...)  { \
150                                                     { \
151                                                         MBX_PRINT(fmt, ## args); \
152                                                     } \
153                                                    }
154 */
155 
156 
157 //#define MBX_DEBUG
158 #ifdef MBX_DEBUG
159     #define MBX_PRINT(fmt, args...)      ULOGD(TAG_MBX, "[MailBox (Driver)][%05d] " fmt, __LINE__, ## args)
160     #define MBX_ASSERT(_cnd, _fmt, _args...)                    \
161                                     if (!(_cnd)) {              \
162                                         ULOGD(TAG_MBX, _fmt, ##_args);  \
163                                         while(1);               \
164                                     }
165 #else
166     #define MBX_PRINT(fmt, args...)   ULOGD(TAG_MBX, "[MailBox (Driver)][%05d] " fmt, __LINE__, ## args)
167     #define MBX_ASSERT(_cnd, _fmt, _args...)
168 #endif
169 
170 //=============================================================================
171 // Macros
172 //=============================================================================
173 //#define INVALID_MBXCLASS(mbxClass)  ((mbxClass<0) || (mbxClass >=E_MBX_CLASS_NUM))
174 #define INVALID_MBXCLASS(mbxClass)  (mbxClass >=E_MBX_CLASS_MAX)
175 
176 ///////////////////////////////////////////////////////////////////////////////////////////////////
177 // New Designed MBX Send/Recv will leads to DeadLock if we disable Interrupt in Send             //
178 // Because Send will Wait Ack from Recv, if Two side send at the same time dead lock will happen //
179 // So we can't disable interrupt when send                                                       //
180 // But this will request that we never send Mail in Interrupt otherwise Interrupt Hanlder may be //
181 // Blocked!                                                                                      //
182 ///////////////////////////////////////////////////////////////////////////////////////////////////
183 
184 #define DRV_MBX_InitLock()  {\
185                                     _mgrMBX.s32SendMutexMBX = MsOS_CreateMutex(E_MSOS_FIFO, "MBX_Send_Mutex", MSOS_PROCESS_SHARED);\
186                                     if(_mgrMBX.s32SendMutexMBX<0)\
187                                     {\
188                                         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "!!!Acquire Send Mutex for MBX Failed!\n");\
189                                         return E_MBX_UNKNOW_ERROR;\
190                                     }\
191                                     _mgrMBX.s32RecvMutexMBX = MsOS_CreateMutex(E_MSOS_FIFO, "MBX_Recv_Mutex", MSOS_PROCESS_SHARED);\
192                                     if(_mgrMBX.s32RecvMutexMBX<0)\
193                                     {\
194                                         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "!!!Acquire Recv Mutex for MBX Failed!\n");\
195                                         MsOS_DeleteMutex(_mgrMBX.s32SendMutexMBX);\
196                                         return E_MBX_UNKNOW_ERROR;\
197                                     }\
198                                }
199 #define DRV_MBX_DeInitLock()   {\
200                                     MsOS_DeleteMutex(_mgrMBX.s32SendMutexMBX);\
201                                     MsOS_DeleteMutex(_mgrMBX.s32RecvMutexMBX);\
202                                  }
203 
204 #ifdef MSOS_TYPE_LINUX
205     #define DRV_MBX_LockSend()   {\
206                                         {\
207                                             MsOS_ObtainMutex(_mgrMBX.s32SendMutexMBX, MSOS_WAIT_FOREVER);\
208                                         }\
209                                    }
210     #define DRV_MBX_UnLockSend()   {\
211                                         {\
212                                             MsOS_ReleaseMutex(_mgrMBX.s32SendMutexMBX);\
213                                         }\
214                                      }
215 
216     #define DRV_MBX_LockRecv()   {\
217                                         if(!_mgrMBX.bInInterruptHandler)\
218                                         {\
219                                             MsOS_ObtainMutex(_mgrMBX.s32RecvMutexMBX, MSOS_WAIT_FOREVER);\
220                                             _mgrMBX.u32IntLockerMBX = MsOS_DisableAllInterrupts();\
221                                         }\
222                                    }
223     #define DRV_MBX_UnLockRecv()   {\
224                                         if(!_mgrMBX.bInInterruptHandler)\
225                                         {\
226                                             MsOS_RestoreAllInterrupts(_mgrMBX.u32IntLockerMBX); \
227                                             MsOS_ReleaseMutex(_mgrMBX.s32RecvMutexMBX);\
228                                         }\
229                                      }
230 #endif
231 
232 //=============================================================================
233 // Local Variables
234 //=============================================================================
235 static MS_S32 _s32FdMBX = -1;
236 static MBX_MAIL_ARRIVE_NOTIFY_FUNC _mbxQStatusNotifierMBX[E_MBX_CLASS_MAX] = { NULL };
237 
238 static MS_U8 _u8DbgLevel = MBX_DBG_LEVEL_NONE;
239 
240 /// Version string
241 static MSIF_Version _drv_mbx_version = {
242     .DDI = { MBX_DRV_VERSION, },
243 };
244 //for Fastboot
245 static MBX_CPU_ID g_eHKCPU;
246 static MBX_ROLE_ID g_eHostRole;
247 static MS_U32 g_u32TimeoutMillSecs;
248 
249 typedef struct
250 {
251     MS_BOOL bInInterruptHandler;
252     MS_U32 u32IntLockerMBX;
253     MS_S32 s32SendMutexMBX;
254     MS_S32 s32RecvMutexMBX;
255 }MBX_Mgr;
256 
257 static MBX_Mgr _mgrMBX =
258 {
259     .bInInterruptHandler = FALSE, ////suppose two interrupt has the same priority, won't break each other.
260     .s32SendMutexMBX = -1,
261     .s32RecvMutexMBX = -1,
262 };
263 
264 //=============================================================================
265 // Global Variables
266 //=============================================================================
267 
268 //=============================================================================
269 // Local Function Prototypes
270 //=============================================================================
271 static void _MDrv_MBX_MsgArrived(int signo, siginfo_t *sig_info,void *unused);
272 
273 //=============================================================================
274 // Local Function
275 
276 //-------------------------------------------------------------------------------------------------
277 // Handle  the signal of "message arrived"
278 // @param  signo                  \b IN: the signal number
279 // @param  sig_info                   \b IN: the signal information attached
280 // @param  unused                  \b IN: unused
281 // @return @ref void
282 // @attention
283 // <b>[OBAMA] <em>If no callback with application, just skip the signal</em></b>
284 //-------------------------------------------------------------------------------------------------
_MDrv_MBX_MsgArrived(int signo,siginfo_t * sig_info,void * unused)285 void _MDrv_MBX_MsgArrived(int signo, siginfo_t *sig_info,void *unused)
286 {
287     MBX_Class msgClass;
288     //MBX_MSGQ_Status msgQueueStatus;
289     MBX_Msg mbxMsg;
290     MBX_Result mbxResult;
291     MS_BOOL bAddToQueue;
292 
293     int err = errno;
294 
295     //get message class:
296     if(SIGIO_MAILBOX != signo)
297     {
298         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[_MDrv_MBX_MsgArrived]Wrong Signal callback received! \n");
299     }
300 
301     //get message class:
302     msgClass = (MBX_Class)((sig_info->si_code&MBXCLASS_IN_SIGNAL_MASK)>>MBXCLASS_IN_SIGNAL_SHIFT);
303 
304     MBX_DBG_PRINT(MBX_DBG_LEVEL_MBXINT, "R Class %d\n", msgClass);
305     //MDrv_MBX_GetMsgQueueStatus(msgClass, &msgQueueStatus);
306     //MBX_DBG_PRINT(MBX_DBG_LEVEL_MBXINT, "R Normal Msg Count %d\n", msgQueueStatus.u32NormalMsgCount);
307 
308     if(NULL == _mbxQStatusNotifierMBX[msgClass])
309     {
310         MBX_DBG_PRINT(MBX_DBG_LEVEL_MBXINT, "[_MDrv_MBX_MsgArrived]Signal callback is NULL! \n");
311         errno = err;
312         return;
313     }
314 
315     //mbxResult = MDrv_MBX_GetMsgQueueStatus(msgClass, &msgQueueStatus);
316     //mbxResult = MDrv_MBX_RecvMsg (msgClass, &mbxMsg, 0, MBX_CHECK_ALL_MSG_CLASS);
317     mbxResult = MDrv_MBX_CheckMsg (msgClass, &mbxMsg, 0, MBX_CHECK_ALL_MSG_CLASS);
318     if(E_MBX_SUCCESS == mbxResult)
319     {
320         //_mbxQStatusNotifierMBX[msgClass](msgClass, &msgQueueStatus);
321         _mbxQStatusNotifierMBX[msgClass](&mbxMsg, &bAddToQueue);
322         if(!bAddToQueue)
323             MDrv_MBX_RemoveLatestMsg();
324     }
325     else
326     {
327         MBX_DBG_PRINT((MBX_DBG_LEVEL_MBXINT|MBX_DBG_LEVEL_MSGQ), "[_MDrv_MBX_MsgArrived]MDrv_MBX_GetMsgQueueStatus Failed! Result=%d \n", mbxResult);
328     }
329     errno = err;
330 }
331 
332 //=============================================================================
333 // Mailbox Driver Function
334 //=============================================================================
335 
336 //-------------------------------------------------------------------------------------------------
337 /// Init Mailbox driver
338 /// @param  eHKCPU    \b IN: The configged HK CPU ID\n
339 /// @param  eHostRole    \b IN: The configged Host ID which mbx driver will run on\n
340 /// @param  u32TimeoutMillSecs    \b IN: The configged timeout which mbx driver will send & wait\n
341 /// @return E_MBX_SUCCESS
342 /// @return E_MBX_ERR_NOT_INITIALIZED
343 /// @return E_MBX_ERR_NO_MORE_MEMORY
344 /// @return E_MBX_UNKNOW_ERROR
345 /// @attention
346 /// <b>[MXLIB] <em></em></b>
347 //-------------------------------------------------------------------------------------------------
_MDrv_MBX_Init_(MBX_CPU_ID eHKCPU,MBX_ROLE_ID eHostRole,MS_U32 u32TimeoutMillSecs)348 MBX_Result  _MDrv_MBX_Init_(MBX_CPU_ID eHKCPU, MBX_ROLE_ID eHostRole, MS_U32 u32TimeoutMillSecs)
349 {
350     MS_MBX_INIT_INFO stInitInfo;
351     struct sigaction action;
352 
353     MDrv_SYS_DMD_VD_MBX_Init();
354     //check input Parameter:
355     if((eHKCPU < E_MBX_CPU_AEON) || (eHKCPU >= E_MBX_CPU_MAX))
356     {
357         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_Init] Invalid HK CPU ID \n");
358         return E_MBX_ERR_INVALID_PARAM;
359     }
360 
361     //if((eHostRole < E_MBX_ROLE_HK) || (eHostRole >= E_MBX_ROLE_MAX)) // fix coverity issue
362     if(eHostRole >= E_MBX_ROLE_MAX)
363     {
364         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_Init] Invalid Host Role ID \n");
365         return E_MBX_ERR_INVALID_PARAM;
366     }
367 
368     if(u32TimeoutMillSecs == 0)
369     {
370         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_Init] Invalid Timeout Value \n");
371         return E_MBX_ERR_INVALID_PARAM;
372     }
373 	//for fastboot
374 	g_eHKCPU = eHKCPU;
375 	g_eHostRole = eHostRole;
376 	g_u32TimeoutMillSecs = u32TimeoutMillSecs;
377 
378     stInitInfo.eHKCPU = eHKCPU;
379     stInitInfo.eHostRole = eHostRole;
380     stInitInfo.u32TimeoutMillSecs = u32TimeoutMillSecs;
381     stInitInfo.mbxResult = E_MBX_SUCCESS;
382 
383     if (_s32FdMBX < 0)   //First time open
384     {
385         _s32FdMBX  = open("/dev/msmailbox", O_RDWR);
386 
387         if (_s32FdMBX > 0) //open device success
388         {
389             if(ioctl(_s32FdMBX, MDRV_MBX_IOC_INIT, &stInitInfo))
390             {
391                 MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_Init]MDRV_MBX_IOC_INIT Failed! \n");
392                 return E_MBX_UNKNOW_ERROR;
393             }
394 
395             if(stInitInfo.mbxResult != E_MBX_SUCCESS)
396             {
397                 MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_Init]MDrv_MBX_Init Failed! Result=%d \n", stInitInfo.mbxResult);
398                 return stInitInfo.mbxResult;
399             }
400 
401             DRV_MBX_InitLock();
402 
403             //handle signal related logics:
404             {
405                 if (fcntl(_s32FdMBX, F_SETOWN, getpid()) < 0)
406 					return E_MBX_UNKNOW_ERROR;
407                 if (fcntl(_s32FdMBX, F_SETSIG, SIGIO_MAILBOX) < 0)
408 					return E_MBX_UNKNOW_ERROR;
409                 if (fcntl(_s32FdMBX, F_SETFL, FASYNC) < 0) //register it right now
410 					return E_MBX_UNKNOW_ERROR;
411 
412                 // register the handler for signal "SIGIO"
413                 memset(&action, 0, sizeof(action));
414                 sigemptyset(&action.sa_mask);
415                 action.sa_sigaction = _MDrv_MBX_MsgArrived;
416                 action.sa_flags = SA_SIGINFO;
417                 sigaction(SIGIO_MAILBOX, &action, NULL);
418             }
419         }
420         else
421         {
422             MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_Init]Open Device Failed! \n");
423         }
424     }
425     else
426     {
427         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_Init]Device Already Opened! \n");
428     }
429 
430     return stInitInfo.mbxResult;
431 }
432 
433 extern MBX_Result  MApi_MBX_Init(MBX_CPU_ID eHKCPU, MBX_ROLE_ID eHostRole, MS_U32 u32TimeoutMillSecs);
434 
MDrv_MBX_Init(MBX_CPU_ID eHKCPU,MBX_ROLE_ID eHostRole,MS_U32 u32TimeoutMillSecs)435 MBX_Result  MDrv_MBX_Init(MBX_CPU_ID eHKCPU, MBX_ROLE_ID eHostRole, MS_U32 u32TimeoutMillSecs)
436 {
437 	return MApi_MBX_Init(eHKCPU, eHostRole, u32TimeoutMillSecs);
438 }
439 
440 //-------------------------------------------------------------------------------------------------
441 /// Deinitialize Mailbox driver
442 /// @param  bForceDiscardPendingMsg    \b IN: If Discard Pending Mail Message or not \n
443 ///              - # TRUE: Discard it and De-Init
444 ///              - # FALSE: if has pending mail message,
445 ///                     No Discard and Do not De-Init, return E_MBX_ERR_HAS_MSG_PENDING
446 /// @return E_MBX_SUCCESS
447 /// @return E_MBX_ERR_NOT_INITIALIZED
448 /// @return E_MBX_ERR_HAS_MSG_PENDING
449 /// @return E_MBX_UNKNOW_ERROR
450 /// @attention
451 /// <b>[MXLIB] <em> </em></b>
452 //-------------------------------------------------------------------------------------------------
MDrv_MBX_DeInit(MS_BOOL bForceDiscardPendingMsg)453 MBX_Result  MDrv_MBX_DeInit(MS_BOOL bForceDiscardPendingMsg)
454 {
455     MS_MBX_SET_BINFO stSetBInfo = { FALSE, E_MBX_UNKNOW_ERROR };
456 
457     if(!bForceDiscardPendingMsg) //if do not discard if still has mail message
458     {
459         stSetBInfo.bInfo = bForceDiscardPendingMsg;
460 
461         if (ioctl(_s32FdMBX, MDRV_MBX_IOC_DEINIT, &stSetBInfo))
462         {
463             MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_DeInit]MDRV_MBX_IOC_DEINIT Failed! \n");
464             return E_MBX_UNKNOW_ERROR;
465         }
466 
467         if(E_MBX_ERR_HAS_MSG_PENDING == stSetBInfo.mbxResult) //there has pending mail message!
468         {
469             MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_DeInit]DeInit Failed! Result=E_MBX_ERR_HAS_MSG_PENDING! \n");
470             return E_MBX_ERR_HAS_MSG_PENDING;
471         }
472     }
473 
474     //no pending Mail Message, or just discard pending mail message:
475     _s32FdMBX = close(_s32FdMBX);
476     _s32FdMBX = -1;
477 
478     return stSetBInfo.mbxResult;
479 }
480 
481 //-------------------------------------------------------------------------------------------------
482 /// Regisiter Message queue for special class
483 /// @param  eMsgClass \b IN: Mail Message Class, @ref MBX_Class
484 /// @param  u16MsgQueueSize \b IN: The mail message queue size,
485 ///                                The Max should not exceed @ref MAX_MBX_QUEUE_SIZE
486 /// @return E_MBX_SUCCESS
487 /// @return E_MBX_ERR_NOT_INITIALIZED
488 /// @return E_MBX_ERR_NO_MORE_MEMORY
489 /// @return E_MBX_ERR_SLOT_BUSY
490 /// @return E_MBX_ERR_SLOT_AREADY_OPENNED
491 /// @return E_MBX_ERR_INVALID_PARAM
492 /// @return E_MBX_UNKNOW_ERROR
493 /// @attention
494 /// <b>[MXLIB] <em> </em></b>
495 //-------------------------------------------------------------------------------------------------
MDrv_MBX_RegisterMSG(MBX_Class eMsgClass,MS_U16 u16MsgQueueSize)496 MBX_Result  MDrv_MBX_RegisterMSG(MBX_Class eMsgClass, MS_U16 u16MsgQueueSize)
497 {
498     MS_MBX_REGISTER_MSG stRegisterMsg;
499 
500     //parameter check:
501     if(INVALID_MBXCLASS(eMsgClass) || (u16MsgQueueSize>MAX_MBX_QUEUE_SIZE))
502     {
503         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_RegisterMSG]E_MBX_ERR_INVALID_PARAM! \n");
504         return E_MBX_ERR_INVALID_PARAM;
505     }
506 
507     DRV_MBX_LockRecv();
508     stRegisterMsg.eMsgClass= eMsgClass;
509     stRegisterMsg.u16MsgQueueSize = u16MsgQueueSize;
510 
511     if (ioctl(_s32FdMBX, MDRV_MBX_IOC_REGISTERMSG, &stRegisterMsg))
512     {
513         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_RegisterMSG]MDRV_MBX_IOC_REGISTERMSG Failed! \n");
514         DRV_MBX_UnLockRecv();
515         return E_MBX_UNKNOW_ERROR;
516     }
517 
518 	if (E_MBX_ERR_SLOT_AREADY_OPENNED == stRegisterMsg.mbxResult)
519 		stRegisterMsg.mbxResult = E_MBX_SUCCESS;
520 
521 
522     if(E_MBX_SUCCESS != stRegisterMsg.mbxResult)
523     {
524         MBX_DBG_PRINT(MBX_DBG_LEVEL_MSGQ, "[MDrv_MBX_RegisterMSG]MDrv_MBX_RegisterMSG Failed! Result=%d \n", stRegisterMsg.mbxResult);
525     }
526     DRV_MBX_UnLockRecv();
527 
528     return stRegisterMsg.mbxResult;
529 }
530 
531 //-------------------------------------------------------------------------------------------------
532 /// Regisiter Message queue for special class With Callback Handler Function
533 /// @param  eMsgClass \b IN: Mail Message Class, @ref MBX_Class
534 /// @param  u16MsgQueueSize \b IN: The mail message queue size,
535 ///                                The Max should not exceed @ref MAX_MBX_QUEUE_SIZE
536 /// @param  notifier \b IN: status change notify call function about this message queue, could be NULL
537 /// @return E_MBX_SUCCESS
538 /// @return E_MBX_ERR_NOT_IMPLEMENTED
539 /// @return E_MBX_ERR_NOT_INITIALIZED
540 /// @return E_MBX_ERR_NO_MORE_MEMORY
541 /// @return E_MBX_ERR_SLOT_BUSY
542 /// @return E_MBX_ERR_SLOT_AREADY_OPENNED
543 /// @return E_MBX_ERR_INVALID_PARAM
544 /// @return E_MBX_UNKNOW_ERROR
545 /// @attention
546 /// <b>[MXLIB] <em> </em></b>
547 //-------------------------------------------------------------------------------------------------
MDrv_MBX_RegisterMSGWithCallBack(MBX_Class eMsgClass,MS_U16 u16MsgQueueSize,MBX_MAIL_ARRIVE_NOTIFY_FUNC notifier)548 MBX_Result  MDrv_MBX_RegisterMSGWithCallBack(MBX_Class eMsgClass, MS_U16 u16MsgQueueSize, MBX_MAIL_ARRIVE_NOTIFY_FUNC notifier)
549 {
550     MBX_Result mbxResult = E_MBX_SUCCESS;
551 
552 //    _mbxQStatusNotifierMBX[eMsgClass] = notifier;
553 
554     mbxResult = MDrv_MBX_RegisterMSG(eMsgClass, u16MsgQueueSize);
555     if(mbxResult == E_MBX_SUCCESS)
556     {
557         _mbxQStatusNotifierMBX[eMsgClass] = notifier;
558     }
559 
560     return mbxResult;
561 }
562 
563 //-------------------------------------------------------------------------------------------------
564 /// UnRegisiter Message queue for special class
565 /// @param  eMsgClass \b IN: Mail Message Class, @ref MBX_Class
566 /// @param  bForceDiscardMsgQueue    \b IN: If Discard Pending Mail Message or not \n
567 ///              - # TRUE: Discard it and De-Init
568 ///              - # FALSE: if has pending mail message,
569 ///                     No Discard and Do not De-Init, return E_MBX_ERR_HAS_MSG_PENDING
570 /// @return E_MBX_SUCCESS
571 /// @return E_MBX_ERR_INVALID_PARAM
572 /// @return E_MBX_ERR_NOT_INITIALIZED
573 /// @return E_MBX_ERR_SLOT_NOT_OPENNED
574 /// @return E_MBX_ERR_HAS_MSG_PENDING
575 /// @return E_MBX_UNKNOW_ERROR
576 /// @attention
577 /// <b>[MXLIB] <em> </em></b>
578 //-------------------------------------------------------------------------------------------------
MDrv_MBX_UnRegisterMSG(MBX_Class eMsgClass,MS_BOOL bForceDiscardMsgQueue)579 MBX_Result  MDrv_MBX_UnRegisterMSG(MBX_Class eMsgClass, MS_BOOL bForceDiscardMsgQueue)
580 {
581     MS_MBX_UNREGISTER_MSG stUnRegisterMsg;
582 
583     //parameter check:
584     if(INVALID_MBXCLASS(eMsgClass))
585     {
586         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_UnRegisterMSG]E_MBX_ERR_INVALID_PARAM! \n");
587         return E_MBX_ERR_INVALID_PARAM;
588     }
589 
590     DRV_MBX_LockRecv();
591     stUnRegisterMsg.eMsgClass = eMsgClass;
592     stUnRegisterMsg.bForceDiscardMsgQueue = bForceDiscardMsgQueue;
593 
594     if (ioctl(_s32FdMBX, MDRV_MBX_IOC_UNREGISTERMSG, &stUnRegisterMsg))
595     {
596         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_UnRegisterMSG]MDRV_MBX_IOC_UNREGISTERMSG Failed! \n");
597         DRV_MBX_UnLockRecv();
598         return E_MBX_UNKNOW_ERROR;
599     }
600 
601     if(E_MBX_SUCCESS != stUnRegisterMsg.mbxResult)
602     {
603         MBX_DBG_PRINT(MBX_DBG_LEVEL_MSGQ, "[MDrv_MBX_UnRegisterMSG]MDrv_MBX_UnRegisterMSG Failed! Result=%d \n", stUnRegisterMsg.mbxResult);
604     }
605     DRV_MBX_UnLockRecv();
606 
607     return stUnRegisterMsg.mbxResult;
608 }
609 
610 //-------------------------------------------------------------------------------------------------
611 /// Clear Message queue for special class
612 /// @param  eMsgClass \b IN: Mail Message Class, @ref MBX_Class
613 /// @return E_MBX_SUCCESS
614 /// @return E_MBX_ERR_INVALID_PARAM
615 /// @return E_MBX_ERR_NOT_INITIALIZED
616 /// @return E_MBX_ERR_SLOT_NOT_OPENNED
617 /// @return E_MBX_UNKNOW_ERROR
618 /// @attention
619 /// <b>[MXLIB] <em> </em></b>
620 //-------------------------------------------------------------------------------------------------
MDrv_MBX_ClearMSG(MBX_Class eMsgClass)621 MBX_Result  MDrv_MBX_ClearMSG(MBX_Class eMsgClass)
622 {
623     MS_MBX_CLEAR_MSG stClearMsg;
624 
625     //parameter check:
626     if(INVALID_MBXCLASS(eMsgClass))
627     {
628         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_ClearMSG]E_MBX_ERR_INVALID_PARAM! \n");
629         return E_MBX_ERR_INVALID_PARAM;
630     }
631 
632     DRV_MBX_LockRecv();
633     stClearMsg.eMsgClass = eMsgClass;
634 
635     if (ioctl(_s32FdMBX, MDRV_mBX_IOC_CLEARMSG, &stClearMsg))
636     {
637         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_ClearMSG]MDRV_mBX_IOC_CLEARMSG Failed! \n");
638         DRV_MBX_UnLockRecv();
639         return E_MBX_UNKNOW_ERROR;
640     }
641 
642     if(E_MBX_SUCCESS != stClearMsg.mbxResult)
643     {
644         MBX_DBG_PRINT(MBX_DBG_LEVEL_MSGQ, "[MDrv_MBX_ClearMSG]MDrv_MBX_ClearMSG Failed! Result=%d \n", stClearMsg.mbxResult);
645     }
646     DRV_MBX_UnLockRecv();
647 
648     return stClearMsg.mbxResult;
649 }
650 
651 //-------------------------------------------------------------------------------------------------
652 /// Send message
653 /// @param  pMsg \b IN: Mail Message Class for sending, must not be NULL, @ref MBX_Msg
654 /// @param  bWaitPeerIdle \b IN: wait Peer CPU or not
655 /// @param  u32WaitMillSecs \b IN: if bWaitPeerIdle, indentify the timeout millsecons before return
656 /// @return E_MBX_SUCCESS
657 /// @return E_MBX_ERR_INVALID_PARAM
658 /// @return E_MBX_ERR_NOT_INITIALIZED
659 /// @return E_MBX_ERR_SLOT_NOT_OPENNED
660 /// @return E_MBX_ERR_INVALID_CPU_ID
661 /// @return E_MBX_ERR_PEER_CPU_BUSY
662 /// @return E_MBX_ERR_PEER_CPU_NOT_ALIVE
663 /// @return E_MBX_ERR_TIME_OUT
664 /// @return E_MBX_UNKNOW_ERROR
665 /// @attention
666 /// <b>[MXLIB] <em> </em></b>
667 //-------------------------------------------------------------------------------------------------
MDrv_MBX_SendMsg(MBX_Msg * pMsg)668 MBX_Result  MDrv_MBX_SendMsg(MBX_Msg *pMsg)
669 {
670     MS_MBX_SEND_MSG stSendMsg;
671 
672     //parameter check:
673     //if((NULL==pMsg) || INVALID_MBXCLASS(pMsg->u8MsgClass))
674     if((NULL==pMsg) || (pMsg->u8MsgClass>=E_MBX_CLASS_MAX))
675     {
676         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_SendMsg]E_MBX_ERR_INVALID_PARAM! \n");
677         return E_MBX_ERR_INVALID_PARAM;
678     }
679 
680     DRV_MBX_LockSend();
681     stSendMsg.pMsg = pMsg;
682 
683     if (ioctl(_s32FdMBX, MDRV_MBX_IOC_SENDMSG, &stSendMsg))
684     {
685         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_SendMsg]MDRV_MBX_IOC_SENDMSG Failed! \n");
686         DRV_MBX_UnLockSend();
687         return E_MBX_UNKNOW_ERROR;
688     }
689 
690     if(E_MBX_SUCCESS != stSendMsg.mbxResult)
691     {
692         MBX_DBG_PRINT(MBX_DBG_LEVEL_MBXHAL, "[MDrv_MBX_SendMsg]MDrv_MBX_SendMsg Failed! Result=%d \n", stSendMsg.mbxResult);
693     }
694     DRV_MBX_UnLockSend();
695 
696     return stSendMsg.mbxResult;
697 }
698 
MDrv_MBX_SendMsgLoopback(MBX_Msg * pMsg,MBX_ROLE_ID eSrcRoleId)699 MBX_Result  MDrv_MBX_SendMsgLoopback(MBX_Msg *pMsg, MBX_ROLE_ID eSrcRoleId)
700 {
701     MS_MBX_SEND_MSG stSendMsg;
702 
703     //parameter check:
704 
705     //if((NULL==pMsg) || INVALID_MBXCLASS(pMsg->u8MsgClass))
706     if((NULL==pMsg) || (pMsg->u8MsgClass>=E_MBX_CLASS_MAX))
707     {
708         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_SendMsg]E_MBX_ERR_INVALID_PARAM! \n");
709         return E_MBX_ERR_INVALID_PARAM;
710     }
711 
712     DRV_MBX_LockSend();
713     stSendMsg.pMsg = pMsg;
714     stSendMsg.mbxResult = (MBX_Result)eSrcRoleId;
715 
716     if (ioctl(_s32FdMBX, MDRV_MBX_IOC_SENDMSGLOOPBACK, &stSendMsg))
717     {
718         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_SendMsg]MDRV_MBX_IOC_SENDMSG Failed! \n");
719         DRV_MBX_UnLockSend();
720         return E_MBX_UNKNOW_ERROR;
721     }
722 
723     if(E_MBX_SUCCESS != stSendMsg.mbxResult)
724     {
725         MBX_DBG_PRINT(MBX_DBG_LEVEL_MBXHAL, "[MDrv_MBX_SendMsg]MDrv_MBX_SendMsg Failed! Result=%d \n", stSendMsg.mbxResult);
726     }
727     DRV_MBX_UnLockSend();
728 
729     return stSendMsg.mbxResult;
730 }
731 
732 
733 //-------------------------------------------------------------------------------------------------
734 /// Check the message queue status of a special message class
735 /// @param  eTargetClass \b IN: target class, @ref MBX_Class
736 /// @param  pMsgQueueStatus \b IN: return the queue status, @ref MBX_MSGQ_Status
737 /// @return E_MBX_SUCCESS
738 /// @return E_MBX_ERR_INVALID_PARAM
739 /// @return E_MBX_ERR_NOT_INITIALIZED
740 /// @return E_MBX_ERR_SLOT_NOT_OPENNED
741 /// @return E_MBX_UNKNOW_ERROR
742 /// @attention
743 /// <b>[MXLIB] <em> </em></b>
744 //-------------------------------------------------------------------------------------------------
MDrv_MBX_GetMsgQueueStatus(MBX_Class eTargetClass,MBX_MSGQ_Status * pMsgQueueStatus)745 MBX_Result  MDrv_MBX_GetMsgQueueStatus(MBX_Class eTargetClass, MBX_MSGQ_Status *pMsgQueueStatus)
746 {
747     MS_MBX_GET_MSGQSTATUS stGetMsgQStatus;
748 
749     //parameter check:
750     if((pMsgQueueStatus ==NULL) || INVALID_MBXCLASS(eTargetClass))
751     {
752         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_GetMsgQueueStatus]E_MBX_ERR_INVALID_PARAM! \n");
753         return E_MBX_ERR_INVALID_PARAM;
754     }
755 
756     DRV_MBX_LockRecv();
757     stGetMsgQStatus.eTargetClass = eTargetClass;
758     stGetMsgQStatus.pMsgQueueStatus = pMsgQueueStatus;
759 
760     if (ioctl(_s32FdMBX, MDRV_MBX_IOC_GETMSGQSTATUS, &stGetMsgQStatus))
761     {
762         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_GetMsgQueueStatus]MDRV_MBX_IOC_GETMSGQSTATUS Failed! \n");
763         DRV_MBX_UnLockRecv();
764         return E_MBX_UNKNOW_ERROR;
765     }
766 
767     if(E_MBX_SUCCESS != stGetMsgQStatus.mbxResult)
768     {
769         MBX_DBG_PRINT(MBX_DBG_LEVEL_MBXHAL, "[MDrv_MBX_SendMsg]MDrv_MBX_SendMsg Failed! Result=%d \n", stGetMsgQStatus.mbxResult);
770     }
771     DRV_MBX_UnLockRecv();
772 
773     return stGetMsgQStatus.mbxResult;
774 }
775 
776 //-------------------------------------------------------------------------------------------------
777 /// Recv message
778 /// @param  eTargetClass \b IN: target class, @ref MBX_Class
779 /// @param  pMsg \b IN: Mail Message Class for receive, must not be NULL, @ref MBX_Msg
780 /// @param  u32WaitMillSecs \b IN: if MBX_CHECK_BLOCK_RECV, indentify the timeout millsecons before return
781 /// @param  u32Flag \b IN: recv flag, has the following possible meaning bits
782 ///                  - # MBX_CHECK_ALL_MSG_CLASS: recv any message class, this means, the targetClass is useless if this bit is set.
783 ///                  - # MBX_CHECK_INSTANT_MSG: check INSTANT message.
784 ///                  - # MBX_CHECK_NORMAL_MSG: Check Normal message.
785 ///                  - # MBX_CHECK_BLOCK_RECV: block this function call until time out if no message available.
786 /// @return E_MBX_SUCCESS
787 /// @return E_MBX_ERR_INVALID_PARAM
788 /// @return E_MBX_ERR_NOT_INITIALIZED
789 /// @return E_MBX_ERR_SLOT_NOT_OPENNED
790 /// @return E_MBX_ERR_NO_MORE_MSG
791 /// @return E_MBX_ERR_TIME_OUT
792 /// @return E_MBX_UNKNOW_ERROR
793 /// @attention
794 /// <b>[MXLIB] <em> </em></b>
795 //-------------------------------------------------------------------------------------------------
MDrv_MBX_RecvMsg(MBX_Class eTargetClass,MBX_Msg * pMsg,MS_U32 u32WaitMillSecs,MS_U32 u32Flag)796 MBX_Result  MDrv_MBX_RecvMsg(MBX_Class eTargetClass, MBX_Msg *pMsg, MS_U32 u32WaitMillSecs, MS_U32 u32Flag)
797 {
798     MS_MBX_RECV_MSG stRecvMsg;
799 
800     //parameter check:
801     if((pMsg ==NULL) || INVALID_MBXCLASS(eTargetClass))
802     {
803         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_RecvMsg]E_MBX_ERR_INVALID_PARAM! \n");
804         return E_MBX_ERR_INVALID_PARAM;
805     }
806 
807     DRV_MBX_LockRecv();
808     stRecvMsg.eTargetClass = eTargetClass;
809     stRecvMsg.pMsg = pMsg;
810     stRecvMsg.u32WaitMillSecs = u32WaitMillSecs;
811     stRecvMsg.u32Flag = u32Flag;
812 
813     if (ioctl(_s32FdMBX, MDRV_MBX_IOC_RECVMSG, &stRecvMsg))
814     {
815         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_RecvMsg]MDRV_MBX_IOC_RECVMSG Failed! \n");
816         DRV_MBX_UnLockRecv();
817         return E_MBX_UNKNOW_ERROR;
818     }
819 
820     if(E_MBX_SUCCESS != stRecvMsg.mbxResult)
821     {
822         MBX_DBG_PRINT(MBX_DBG_LEVEL_MBXHAL, "[MDrv_MBX_RecvMsg]MDrv_MBX_RecvMsg Failed! Result=%d \n", stRecvMsg.mbxResult);
823     }
824     DRV_MBX_UnLockRecv();
825 
826     return stRecvMsg.mbxResult;
827 }
828 
829 //-------------------------------------------------------------------------------------------------
830 /// Check message
831 /// @param  eTargetClass \b IN: target class, @ref MBX_Class
832 /// @param  pMsg \b IN: Mail Message Class for receive, must not be NULL, @ref MBX_Msg
833 /// @param  u32WaitMillSecs \b IN: if MBX_CHECK_BLOCK_RECV, indentify the timeout millsecons before return
834 /// @param  u32Flag \b IN: recv flag, has the following possible meaning bits
835 ///                  - # MBX_CHECK_ALL_MSG_CLASS: recv any message class, this means, the targetClass is useless if this bit is set.
836 ///                  - # MBX_CHECK_INSTANT_MSG: check INSTANT message.
837 ///                  - # MBX_CHECK_NORMAL_MSG: Check Normal message.
838 ///                  - # MBX_CHECK_BLOCK_RECV: block this function call until time out if no message available.
839 /// @return E_MBX_SUCCESS
840 /// @return E_MBX_ERR_INVALID_PARAM
841 /// @return E_MBX_ERR_NOT_INITIALIZED
842 /// @return E_MBX_ERR_SLOT_NOT_OPENNED
843 /// @return E_MBX_ERR_NO_MORE_MSG
844 /// @return E_MBX_ERR_TIME_OUT
845 /// @return E_MBX_UNKNOW_ERROR
846 /// @attention
847 /// <b>[MXLIB] <em> </em></b>
848 //-------------------------------------------------------------------------------------------------
MDrv_MBX_CheckMsg(MBX_Class eTargetClass,MBX_Msg * pMsg,MS_U32 u32WaitMillSecs,MS_U32 u32Flag)849 MBX_Result  MDrv_MBX_CheckMsg(MBX_Class eTargetClass, MBX_Msg *pMsg, MS_U32 u32WaitMillSecs, MS_U32 u32Flag)
850 {
851     MS_MBX_RECV_MSG stRecvMsg;
852 
853     //parameter check:
854     if((pMsg ==NULL) || INVALID_MBXCLASS(eTargetClass))
855     {
856         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_CheckMsg]E_MBX_ERR_INVALID_PARAM! \n");
857         return E_MBX_ERR_INVALID_PARAM;
858     }
859 
860     DRV_MBX_LockRecv();
861     stRecvMsg.eTargetClass = eTargetClass;
862     stRecvMsg.pMsg = pMsg;
863     stRecvMsg.u32WaitMillSecs = u32WaitMillSecs;
864     stRecvMsg.u32Flag = u32Flag;
865 
866     if (ioctl(_s32FdMBX, MDRV_MBX_IOC_CHECKMSG, &stRecvMsg))
867     {
868         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_CheckMsg]MDRV_MBX_IOC_CHECKMSG Failed! \n");
869         DRV_MBX_UnLockRecv();
870         return E_MBX_UNKNOW_ERROR;
871     }
872 
873     if(E_MBX_SUCCESS != stRecvMsg.mbxResult)
874     {
875         MBX_DBG_PRINT(MBX_DBG_LEVEL_MBXHAL, "[MDrv_MBX_CheckMsg]MDrv_MBX_CheckMsg Failed! Result=%d \n", stRecvMsg.mbxResult);
876     }
877     DRV_MBX_UnLockRecv();
878 
879     return stRecvMsg.mbxResult;
880 }
881 
882 //-------------------------------------------------------------------------------------------------
883 /// Remove the latest received message from queue, this function is used right after MDrv_MBX_CheckMsg()
884 /// @param  none
885 /// @return E_MBX_SUCCESS
886 /// @return E_MBX_ERR_NOT_INITIALIZED
887 /// @return E_MBX_UNKNOW_ERROR
888 /// @attention
889 /// <b>[MXLIB] <em> </em></b>
890 //-------------------------------------------------------------------------------------------------
MDrv_MBX_RemoveLatestMsg(void)891 MBX_Result  MDrv_MBX_RemoveLatestMsg(void)
892 {
893     MS_MBX_SET_BINFO stSetBInfo;
894 
895     DRV_MBX_LockRecv();
896     stSetBInfo.bInfo = TRUE;
897 
898     if (ioctl(_s32FdMBX, MDRV_MBX_IOC_REMOVEMSG, &stSetBInfo))
899     {
900         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_RemoveLatestMsg]MDrv_MBX_RemoveLatestMsg Failed! \n");
901         DRV_MBX_UnLockRecv();
902         return E_MBX_UNKNOW_ERROR;
903     }
904 
905     if(E_MBX_SUCCESS != stSetBInfo.mbxResult)
906     {
907         MBX_DBG_PRINT(MBX_DBG_LEVEL_MBXHAL, "[MDrv_MBX_RemoveLatestMsg]MDrv_MBX_RemoveLatestMsg Failed! Result=%d \n", stSetBInfo.mbxResult);
908     }
909     DRV_MBX_UnLockRecv();
910 
911     return stSetBInfo.mbxResult;
912 }
913 
914 
915 //-------------------------------------------------------------------------------------------------
916 /// Enable Message Queue to recv message from peer CPUs, Enable receiving regisited messages in kernel
917 /// @param  bEnable \b IN: Enable or not
918 ///                  - # TRUE: enable
919 ///                  - # FALSE: disable
920 /// @return E_MBX_SUCCESS
921 /// @return E_MBX_ERR_NOT_INITIALIZED
922 /// @return E_MBX_UNKNOW_ERROR
923 /// @attention
924 /// <b>[MXLIB] <em> </em></b>
925 //-------------------------------------------------------------------------------------------------
MDrv_MBX_Enable(MS_BOOL bEnable)926 MBX_Result  MDrv_MBX_Enable(MS_BOOL bEnable)
927 {
928     MS_MBX_SET_BINFO stSetBInfo;
929 
930     stSetBInfo.bInfo = bEnable;
931 
932     if (ioctl(_s32FdMBX, MDRV_MBX_IOC_MBXENABLE, &stSetBInfo))
933     {
934         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_Enable]MDRV_MBX_IOC_MBXENABLE Failed! \n");
935         return E_MBX_UNKNOW_ERROR;
936     }
937 
938     if(E_MBX_SUCCESS != stSetBInfo.mbxResult)
939     {
940         MBX_DBG_PRINT(MBX_DBG_LEVEL_MBXHAL, "[MDrv_MBX_Enable]MDrv_MBX_Enable Failed! Result=%d \n", stSetBInfo.mbxResult);
941     }
942 
943     return stSetBInfo.mbxResult;
944 }
945 
946 //-------------------------------------------------------------------------------------------------
947 /// Set Information to dedicated Mailbox HW Regs.
948 /// @param  eTargetCPU \b IN: The correpsonded CPU ID for which MBX HW Group will be used for Set
949 /// @param  pU8Info \b IN: The Information which need to set
950 /// @param  u8Size \b IN: The Size(bytes) of pU8Info
951 /// @return E_MBX_SUCCESS
952 /// @return E_MBX_ERR_INVALID_PARAM
953 /// @return E_MBX_UNKNOW_ERROR
954 /// @attention
955 /// <b>[MXLIB] <em>It is used for speciall need when Init-Coprocesser</em></b>
956 //-------------------------------------------------------------------------------------------------
MDrv_MBX_SetInformation(MBX_ROLE_ID eTargetRole,MS_U8 * pU8Info,MS_U8 u8Size)957 MBX_Result  MDrv_MBX_SetInformation(MBX_ROLE_ID eTargetRole, MS_U8 *pU8Info, MS_U8 u8Size)
958 {
959     MS_MBX_CPROSYNC_INFORMATION stSetInfo;
960 
961     //Check if Valid Parameter:
962     if((eTargetRole != E_MBX_ROLE_CP) && (eTargetRole != E_MBX_ROLE_PM) && (eTargetRole != E_MBX_ROLE_FRC))//frcr2_integration###
963     {
964         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_SetInformation] Invalid Role ID \n");
965         return E_MBX_ERR_INVALID_PARAM;
966     }
967 
968     if(NULL == pU8Info)
969     {
970         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_SetInformation]Invalid pU8Info! \n");
971         return E_MBX_ERR_INVALID_PARAM;
972     }
973 
974     if(u8Size > MAX_MBX_INFORMATION_SIZE)
975     {
976         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_SetInformation]Invalid Information Size! \n");
977         return E_MBX_ERR_INVALID_PARAM;
978     }
979 
980     stSetInfo.eTargetRole = eTargetRole;
981     stSetInfo.pU8Info = pU8Info;
982     stSetInfo.u8Size = u8Size;
983 
984     if(_s32FdMBX < 0)   //First time open
985     {
986         _s32FdMBX  = open("/dev/msmailbox", O_RDWR);
987 
988         if (_s32FdMBX > 0) //open device success
989         {
990             if (ioctl(_s32FdMBX, MDRV_MBX_IOC_SETINFORMATION, &stSetInfo))
991             {
992                 MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_SetInformation]MDRV_MBX_IOC_SETINFORMATION Failed! \n");
993                 return E_MBX_UNKNOW_ERROR;
994             }
995         }
996         else //open Fail.
997         {
998             MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_SetInformation]Open Device Failed! \n");
999             return E_MBX_UNKNOW_ERROR;
1000         }
1001 
1002         _s32FdMBX = close(_s32FdMBX);
1003         _s32FdMBX = -1;
1004     }
1005     else
1006     {
1007         if (ioctl(_s32FdMBX, MDRV_MBX_IOC_SETINFORMATION, &stSetInfo))
1008         {
1009             MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_SetInformation]MDRV_MBX_IOC_SETINFORMATION Failed! \n");
1010             return E_MBX_UNKNOW_ERROR;
1011         }
1012     }
1013 
1014     if(E_MBX_SUCCESS != stSetInfo.mbxResult)
1015     {
1016         MBX_DBG_PRINT(MBX_DBG_LEVEL_MBXHAL, "[MDrv_MBX_SetInformation]MDrv_MBX_SetInformation Failed! Result=%d \n", stSetInfo.mbxResult);
1017     }
1018 
1019     return stSetInfo.mbxResult;
1020 }
1021 
1022 //-------------------------------------------------------------------------------------------------
1023 /// Get Information from dedicated Mailbox HW Regs.
1024 /// @param  eTargetCPU \b IN: The correpsonded CPU ID for which MBX HW Group will be used for Get
1025 /// @param  pU8Info \b OUT: The Information Where we get to put
1026 /// @param  u8Size \b IN: The Size(bytes) need to get
1027 /// @return E_MBX_SUCCESS
1028 /// @return E_MBX_ERR_INVALID_PARAM
1029 /// @return E_MBX_UNKNOW_ERROR
1030 /// @attention
1031 /// <b>[MXLIB] <em>It is used for speciall need when Init-Coprocesser.</em></b>
1032 //-------------------------------------------------------------------------------------------------
MDrv_MBX_GetInformation(MBX_ROLE_ID eTargetRole,MS_U8 * pU8Info,MS_U8 u8Size)1033 MBX_Result  MDrv_MBX_GetInformation(MBX_ROLE_ID eTargetRole, MS_U8 *pU8Info, MS_U8 u8Size)
1034 {
1035     MS_MBX_CPROSYNC_INFORMATION stGetInfo;
1036 
1037     //Check if Valid Parameter:
1038     if((eTargetRole != E_MBX_ROLE_CP) && (eTargetRole != E_MBX_ROLE_PM) && (eTargetRole != E_MBX_ROLE_FRC))//frcr2_integration###
1039     {
1040         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_SetInformation] Invalid Role ID \n");
1041         return E_MBX_ERR_INVALID_PARAM;
1042     }
1043 
1044     if(NULL == pU8Info)
1045     {
1046         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_GetInformation]Invalid pU8Info! \n");
1047         return E_MBX_ERR_INVALID_PARAM;
1048     }
1049 
1050     if(u8Size > MAX_MBX_INFORMATION_SIZE)
1051     {
1052         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_GetInformation]Invalid Information Size! \n");
1053         return E_MBX_ERR_INVALID_PARAM;
1054     }
1055 
1056     stGetInfo.eTargetRole = eTargetRole;
1057     stGetInfo.pU8Info = pU8Info;
1058     stGetInfo.u8Size = u8Size;
1059 
1060     if(_s32FdMBX < 0)   //First time open
1061     {
1062         _s32FdMBX = open("/dev/msmailbox", O_RDWR);
1063 
1064         if(_s32FdMBX > 0) //open device success
1065         {
1066             if(ioctl(_s32FdMBX, MDRV_MBX_IOC_GETINFORMATION, &stGetInfo))
1067             {
1068                 MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_GetInformation]MDRV_MBX_IOC_GETINFORMATION Failed! \n");
1069                 return E_MBX_UNKNOW_ERROR;
1070             }
1071         }
1072         else //open Fail.
1073         {
1074             MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_GetInformation]Open Device Failed! \n");
1075             return E_MBX_UNKNOW_ERROR;
1076         }
1077 
1078         _s32FdMBX = close(_s32FdMBX);
1079         _s32FdMBX = -1;
1080     }
1081     else
1082     {
1083         if(ioctl(_s32FdMBX, MDRV_MBX_IOC_GETINFORMATION, &stGetInfo))
1084         {
1085             MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_GetInformation]MDRV_MBX_IOC_GETINFORMATION Failed! \n");
1086             return E_MBX_UNKNOW_ERROR;
1087         }
1088     }
1089 
1090     if(E_MBX_SUCCESS != stGetInfo.mbxResult)
1091     {
1092         MBX_DBG_PRINT(MBX_DBG_LEVEL_MBXHAL, "[MDrv_MBX_GetInformation]MDrv_MBX_GetInformation Failed! Result=%d \n", stGetInfo.mbxResult);
1093     }
1094 
1095     return stGetInfo.mbxResult;
1096 }
1097 
1098 //-------------------------------------------------------------------------------------------------
1099 /// Set the Debug Lever to MBX Driver
1100 /// @param  u8DbgLevel    \b IN: The debug level. will be the value of belows \n
1101 ///              - # MBX_DBG_LEVEL_NONE: No any Debug Message \n
1102 ///              - # MBX_DBG_LEVEL_MBXHAL: Print HAL Related Message \n
1103 ///              - # MBX_DBG_LEVEL_MSGQ: Print MSGQ Related Message \n
1104 ///              - # MBX_DBG_LEVEL_MBXINT: Print Interrupt callback realted Message \n
1105 ///              - # MBX_DBG_LEVEL_CRITICAL: Print critical Message \n
1106 ///              - # MBX_DBG_LEVEL_ALL: Print All above kinds of Message
1107 /// @return void
1108 /// @attention
1109 /// <b>[MXLIB] <em> </em></b>
1110 //-------------------------------------------------------------------------------------------------
MDrv_MBX_SetDbgLevel(MS_U8 u8DbgLevel)1111 void MDrv_MBX_SetDbgLevel(MS_U8 u8DbgLevel)
1112 {
1113     _u8DbgLevel = u8DbgLevel;
1114 }
1115 
1116 //------------------------------------------------------------------------------
1117 /// Get MBX driver version
1118 /// @return -the pointer to the driver version
1119 //------------------------------------------------------------------------------
MDrv_MBX_GetLibVer(const MSIF_Version ** ppVersion)1120 MS_BOOL MDrv_MBX_GetLibVer(const MSIF_Version **ppVersion)
1121 {
1122     if (!ppVersion)
1123     {
1124         return FALSE;
1125     }
1126 
1127     *ppVersion = &_drv_mbx_version;
1128     return TRUE;
1129 }
1130 
1131 //------------------------------------------------------------------------------
1132 /// Get MBX driver Info
1133 /// @return -the pointer to the driver Ifno
1134 //------------------------------------------------------------------------------
MDrv_MBX_GetInfo(MBX_DrvInfo * pMbxDrvInfo)1135 MS_BOOL MDrv_MBX_GetInfo(MBX_DrvInfo *pMbxDrvInfo)
1136 {
1137     if (NULL == pMbxDrvInfo)
1138     {
1139         return FALSE;
1140     }
1141 
1142     pMbxDrvInfo->u8HWMBXGroupNum = 0x6;
1143     return TRUE;
1144 }
1145 
1146 //------------------------------------------------------------------------------
1147 /// Get MBX driver status
1148 /// @return -the pointer to the driver status
1149 //------------------------------------------------------------------------------
MDrv_MBX_GetStatus(MBX_DrvStatus * pMbxDrvStatus)1150 MS_BOOL MDrv_MBX_GetStatus(MBX_DrvStatus *pMbxDrvStatus)
1151 {
1152     MS_MBX_GET_DRVSTATUS stGetDrvStatus;
1153 
1154     if (NULL == pMbxDrvStatus)
1155     {
1156         return FALSE;
1157     }
1158 
1159     if (ioctl(_s32FdMBX, MDRV_MBX_IOC_GETDRVSTATUS, &stGetDrvStatus))
1160     {
1161         MBX_DBG_PRINT(MBX_DBG_LEVEL_CRITICAL, "[MDrv_MBX_GetStatus]MDRV_MBX_IOC_MBXGETDRVSTATUS Failed! \n");
1162         return FALSE;
1163     }
1164 
1165     pMbxDrvStatus->bEnabled = stGetDrvStatus.bEnabled;
1166     pMbxDrvStatus->s32RefCnt = stGetDrvStatus.s32RefCnt;
1167 
1168     return TRUE;
1169 }
1170 
MDrv_MBX_SetPowerState(EN_POWER_MODE u16PowerState)1171 MS_U16 MDrv_MBX_SetPowerState(EN_POWER_MODE u16PowerState)
1172 {
1173 	static EN_POWER_MODE _prev_u16PowerState = E_POWER_MECHANICAL;
1174 	MS_U16 u16Return = FALSE;
1175 
1176 	if (u16PowerState == E_POWER_SUSPEND)
1177 	{
1178 		_prev_u16PowerState = u16PowerState;
1179 		u16Return = 2;//SUSPEND_OK;
1180 	}
1181 	else if (u16PowerState == E_POWER_RESUME)
1182 	{
1183 
1184 		if (_prev_u16PowerState == E_POWER_SUSPEND)
1185 		{
1186 			 MDrv_MBX_Init(g_eHKCPU, g_eHostRole, g_u32TimeoutMillSecs);
1187 			_prev_u16PowerState = u16PowerState;
1188 			u16Return = 1;//RESUME_OK;
1189 		}
1190 		else
1191 		{
1192 			ULOGE(TAG_MBX, "[%s,%5d]It is not suspended yet. We shouldn't resume\n",__FUNCTION__,__LINE__);
1193 			u16Return = 3;//SUSPEND_FAILED;
1194 		}
1195 	}
1196 	else
1197 	{
1198 		ULOGE(TAG_MBX, "[%s,%5d]Do Nothing: %d\n",__FUNCTION__,__LINE__,u16PowerState);
1199 		u16Return = FALSE;
1200 	}
1201 
1202 	return u16Return;// for success
1203 }
1204 
MDrv_MBX_SetCallDrvFlag(MS_BOOL bEnable)1205 void MDrv_MBX_SetCallDrvFlag(MS_BOOL bEnable)
1206 {
1207 }
1208 
MDrv_MBX_GetCallDrvFlag(void)1209 MS_BOOL MDrv_MBX_GetCallDrvFlag(void)
1210 {
1211     return TRUE;
1212 }
1213 
1214