xref: /utopia/UTPA2-700.0.x/modules/dscmb/drv/dscmb/drvDSCMB.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) 2006-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   drvDSCMB.c
98 /// @brief  Descrambler (DSCMB) Driver Interface
99 /// @author MStar Semiconductor,Inc.
100 ////////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 #include "MsCommon.h"
103 #include "MsVersion.h"
104 #ifdef MSOS_TYPE_LINUX_KERNEL
105 #include <linux/string.h>
106 #else
107 #include <string.h>
108 #include <limits.h>
109 #endif
110 #include "drvMMIO.h"
111 #include "drvTSP.h"
112 #include "drvDSCMB.h"
113 #include "regDSCMB.h"
114 #include "halDSCMB.h"
115 #include "asmCPU.h"
116 #include "utopia.h"
117 #include "drvDSCMB_v2.h"
118 #include "ULog.h"
119 
120 #if (!( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
121     #include "dscmb_private.h"
122 
123     #define DSCMB_UTOPIA2 1
124 #else
125     #define DSCMB_UTOPIA2 0
126 #endif
127 
128 // Threshold for KL calculation //
129 #define DSCMB_KL_WAIT_THRESHOLD 300
130 #define U32_MAX (0xFFFFFFFFUL)
131 
132 static MSIF_Version _drv_dscmb_version = {
133     .DDI = { DSCMB_DRV_VERSION, },
134 };
135 
136 static DSCMB_Info                   DscmbInfo;
137 
138 static DSCMB_Status _DscmbStatus;
139 MS_U32 _u32DscmbDGBLevel = 0;
140 
141 #if DSCMB_UTOPIA2
142     extern stDscmbTspMap (*DscmbTspMap)[MAX_NUM];
143     extern MS_U32 * _u32FreeSlot;
144 #else
145     stDscmbTspMap DscmbTspMap[ENG_NUM][MAX_NUM];
146     static MS_U32 _u32FreeSlot[ENG_NUM];
147 #endif
148 
149 static MS_S32 _s32DscMutexId = -1 ;
150 
151 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
152 static DSCMB_KLCfg KLCfgPrev;
153 #endif
154 
155 #if DSCMB_UTOPIA2
156 static void *pu32DscmbInst = NULL;
157 #endif
158 extern void DSCMBRegisterToUtopia(void);
_DSCMB_SlotAlloc(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Key_Type KeyType,MS_U32 * pu32SlotId)159 MS_BOOL _DSCMB_SlotAlloc(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Key_Type KeyType, MS_U32* pu32SlotId)
160 {
161 #if DSCMB_PARAMETER_TABLE
162     *pu32SlotId = (E_DSCMB_KEY_EVEN == KeyType)?(u32DscmbId * 2):(u32DscmbId * 2 + 1);
163 #else
164     MS_U32 u32Slot;
165     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
166     // Oberon/Uranus should judge if u32DscmbId has been visited
167     u32Slot = MAsm_CPU_GetTrailOne(_u32FreeSlot[u32EngId]);
168     if (REG_DSCMB_MAX_SLOT <= u32Slot)
169     {
170         DSCMB_ASSERT_AND_RETURN(0);
171         return FALSE;
172     }
173 
174     _u32FreeSlot[u32EngId] &= ~(1 << u32Slot);
175     *pu32SlotId = u32Slot;
176 #endif
177     return TRUE;
178 }
179 
_DSCMB_SlotFree(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Key_Type KeyType,MS_U32 u32SlotId)180 MS_BOOL _DSCMB_SlotFree(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Key_Type KeyType, MS_U32 u32SlotId)
181 {
182 #if DSCMB_PARAMETER_TABLE
183 #else
184     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
185     DSCMB_ASSERT_AND_RETURN(u32SlotId < REG_DSCMB_MAX_SLOT);
186     DSCMB_ASSERT_AND_RETURN(0 == ((_u32FreeSlot[u32EngId]) & (1 << u32SlotId)));
187 
188     _u32FreeSlot[u32EngId] |= (1 << u32SlotId);
189 #endif
190     return TRUE;
191 }
192 
193 //--------------------------------------------------------------------------------------------------
194 /// Initialize dscrambler driver and descrambler engine
195 /// @return TRUE - Success
196 /// @return FALSE - Failure
197 /// @note
198 /// It should be called before calling any other dscrambler DDI functions.
199 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB_Init(void)200 MS_BOOL _MDrv_DSCMB_Init(void)
201 {
202     MS_U32 i, j, eng;
203     stDscmbTspMap* pMap;
204     MS_PHY phyBankSize;
205     MS_VIRT u32Bank;
206 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
207     MS_VIRT u32OTPBank,u32PMBank;
208     MS_PHY phyOTPBankSize, phyPMBankSize;
209 #endif
210 
211     if (FALSE == MDrv_MMIO_GetBASE(&u32Bank, &phyBankSize, MS_MODULE_HW))
212     {
213         MS_DEBUG_MSG(ULOGE("DSCMB", "[%s][%d] Get MMIO base fail\n", __FUNCTION__, __LINE__));
214         return FALSE;
215     }
216 
217     HAL_DSCMB_SetBank(u32Bank);
218 
219 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
220     /////////// Set PM Bank & OTP Bank to HAL //////////////////////////////
221     extern MS_BOOL HAL_DSCMB_SetOTPBank(MS_U32) __attribute__((weak));
222     extern MS_BOOL HAL_DSCMB_SetPMBank(MS_U32) __attribute__((weak));
223     if(HAL_DSCMB_SetOTPBank)
224     {
225         if (FALSE == MDrv_MMIO_GetBASE(&u32OTPBank, &phyOTPBankSize, MS_MODULE_OTP))
226         {
227             MS_DEBUG_MSG(ULOGE("DSCMB", "[%s][%d] Get MMIO base fail\n", __FUNCTION__, __LINE__));
228             return FALSE;
229         }
230         HAL_DSCMB_SetOTPBank(u32OTPBank);
231     }
232 
233     if(HAL_DSCMB_SetPMBank)
234     {
235         if (FALSE == MDrv_MMIO_GetBASE(&u32PMBank, &phyPMBankSize, MS_MODULE_PM))
236         {
237             MS_DEBUG_MSG(ULOGE("DSCMB", "[%s][%d] Get MMIO base fail\n", __FUNCTION__, __LINE__));
238             return FALSE;
239         }
240         HAL_DSCMB_SetPMBank(u32PMBank);
241     }
242     //////////////////////////////////////////////////////////
243 #endif
244     HAL_DSCMB_Init();
245 
246     for(eng = 0; eng < ENG_NUM; eng++)
247     {
248         _u32FreeSlot[eng] = (MS_U32)((1ULL << REG_DSCMB_MAX_SLOT) - 1); // no more than 32 slots
249         memset((void*)&DscmbTspMap[eng], 0, sizeof(stDscmbTspMap));
250         for (i= 0; i< MAX_NUM; i++)
251         {
252             pMap = &DscmbTspMap[eng][i];
253             pMap->u32DscmbId = U32_MAX;
254             for (j= 0; j< MAX_DSCMB_PIDFLT_NUM; j++)
255             {
256                 pMap->u32PidFltId[j] = U32_MAX;
257             }
258             pMap->u32SlotIdClear = pMap->u32SlotIdEven = pMap->u32SlotIdOdd = U32_MAX;
259             pMap->bUsed = FALSE;
260             pMap->bDscmb = TRUE;
261             pMap->SlotType = E_SLOT_TYPE_NORMAL;
262         }
263     }
264 
265      _s32DscMutexId = MsOS_CreateMutex(E_MSOS_FIFO, (char*) "DSCMB_Mutex", MSOS_PROCESS_SHARED);
266 
267     return TRUE;
268 }
269 
MDrv_DSCMB_Init(void)270 MS_BOOL MDrv_DSCMB_Init(void)
271 {
272     return MDrv_DSCMB2_Init();
273 }
274 
MDrv_DSCMB2_Init(void)275 MS_BOOL MDrv_DSCMB2_Init(void)
276 {
277 #if DSCMB_UTOPIA2
278         MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
279         if(pu32DscmbInst == NULL)
280         {
281             DSCMBRegisterToUtopia();
282 
283             #ifdef MSOS_TYPE_LINUX_KERNEL
284             if(UTOPIA_STATUS_FAIL == UtopiaOpen(MODULE_DSCMB | KERNEL_MODE, &pu32DscmbInst, 0x0, NULL))
285             #else
286             if(UTOPIA_STATUS_FAIL == UtopiaOpen(MODULE_DSCMB, &pu32DscmbInst, 0x0, NULL))
287             #endif
288             {
289                 ULOGE("DSCMB", "UtopiaOpen DSCMB fail\n");
290                 return FALSE;
291             }
292         }
293 
294         u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_Init, NULL);
295         if(UTOPIA_STATUS_SUCCESS != u32Ret)
296         {
297             ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
298             return FALSE;
299         }
300 
301         return TRUE;
302 #else
303     return _MDrv_DSCMB_Init();
304 #endif
305 }
306 //--------------------------------------------------------------------------------------------------
307 /// Exit dscrambler driver and descrambler engine
308 /// @return TRUE - Success
309 /// @return FALSE - Failure
310 /// @note
311 /// It should be called after finish any other dscrambler DDI functions.
312 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB_Exit(void)313 MS_BOOL _MDrv_DSCMB_Exit(void)
314 {
315     if (_s32DscMutexId != -1)
316     {
317         MsOS_DeleteMutex(_s32DscMutexId);
318     }
319     return TRUE;
320 }
321 
MDrv_DSCMB_Exit(void)322 MS_BOOL MDrv_DSCMB_Exit(void)
323 {
324     return MDrv_DSCMB2_Exit();
325 }
326 
MDrv_DSCMB2_Exit(void)327 MS_BOOL MDrv_DSCMB2_Exit(void)
328 {
329 #if DSCMB_UTOPIA2
330         MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
331         if(pu32DscmbInst == NULL)
332         {
333             return FALSE;
334         }
335 
336         u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_Exit, NULL);
337         if(UTOPIA_STATUS_SUCCESS != u32Ret)
338     {
339             ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
340             return FALSE;
341     }
342 
343     return TRUE;
344 #else
345     return _MDrv_DSCMB_Exit();
346 #endif
347 }
348 
_DSCMB_TypeCasting(DSCMB_Type type)349 HAL_DSCMB_Type _DSCMB_TypeCasting(DSCMB_Type type)
350 {
351 #if (!( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
352     switch (type)
353     {
354         case E_DSCMB_TYPE_CSA:
355             return E_HAL_DSCMB_TYPE_CSA;
356             break;
357         case E_DSCMB_TYPE_CSA_CONF:
358             return E_HAL_DSCMB_TYPE_CSA_CONF;
359             break;
360         case E_DSCMB_TYPE_NSA_AS_ESA:
361             return E_HAL_DSCMB_TYPE_NSA_AS_ESA;
362             break;
363         case E_DSCMB_TYPE_DES:
364             return E_HAL_DSCMB_TYPE_DES;
365             break;
366         case E_DSCMB_TYPE_AES:
367             return E_HAL_DSCMB_TYPE_AES;
368             break;
369         case E_DSCMB_TYPE_AES_ECB:
370             return E_HAL_DSCMB_TYPE_AES_ECB;
371             break;
372         case E_DSCMB_TYPE_AES_SCTE52:
373             return E_HAL_DSCMB_TYPE_AES_SCTE52;
374             break;
375         case E_DSCMB_TYPE_AES_CTR:
376             return E_HAL_DSCMB_TYPE_AES_CTR;
377             break;
378         case E_DSCMB_TYPE_TDES_ECB:
379             return E_HAL_DSCMB_TYPE_TDES_ECB;
380             break;
381         case E_DSCMB_TYPE_TDES_SCTE52:
382             return E_HAL_DSCMB_TYPE_TDES_SCTE52;
383             break;
384         case E_DSCMB_TYPE_SYN_AES:
385             return E_HAL_DSCMB_TYPE_AES_LEADING_CLR_EBC;
386             break;
387         case E_DSCMB_TYPE_MULTI2:
388             return E_HAL_DSCMB_TYPE_MULTI2;
389             break;
390         case E_DSCMB_TYPE_OC:
391             return E_HAL_DSCMB_TYPE_OC;
392             break;
393         default:
394             return E_HAL_DSCMB_TYPE_INVALID;
395     }
396 #else
397     return E_HAL_DSCMB_TYPE_INVALID;
398 #endif
399 }
400 
_MDrv_DSCMB2_OptConfig(MS_U32 u32EngId,DSCMB_Option eOpt,void * pInput,void * pOutput,MS_BOOL bSet)401 MS_BOOL _MDrv_DSCMB2_OptConfig(MS_U32 u32EngId, DSCMB_Option eOpt, void* pInput, void* pOutput, MS_BOOL bSet)
402 {
403     HAL_DSCMB_Option eOption = 0;
404     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
405 
406     switch (eOpt)
407     {
408         case E_DSCMB_CFG_HDCP2_TX_CONTENT_CATEGORY_SUPPORT:
409             eOption = E_HAL_DSCMB_CFG_HDCP2_TX_CONTENT_CATEGORY_SUPPORT;
410             break;
411 
412         case E_DSCMB_CFG_SCMB_STATUS_PUSI_ONLY:
413             eOption = E_HAL_DSCMB_CFG_SCMB_STATUS_PUSI_ONLY;
414             break;
415 
416         default:
417             return FALSE;
418     }
419     return HAL_DSCMB_OptConfig(u32EngId, eOption, pInput, pOutput, bSet);
420 }
421 
422 //--------------------------------------------------------------------------------------------------
423 /// query dscrambler capability information
424 /// @param u32EngId \b IN: dscrambler engine index
425 /// @param eQueryType \b IN: type for query
426 /// @param pOutput \b OUT: result for the query type
427 /// @return TRUE - Success
428 /// @return FALSE - Failure
429 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_GetCap(MS_U32 u32EngId,DSCMB_Query_Type eQueryType,void * pInput,void * pOutput)430 MS_BOOL _MDrv_DSCMB2_GetCap(MS_U32 u32EngId, DSCMB_Query_Type eQueryType, void* pInput, void* pOutput)
431 {
432     HAL_DSCMB_Query_Type u32Cap;
433 
434     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
435     DSCMB_ASSERT_AND_RETURN(pOutput != NULL);
436 
437     switch (eQueryType)
438     {
439         case E_DSCMB_CAP_ENGINE_NUM:
440             u32Cap = E_HAL_DSCMB_CAP_ENGINE_NUM;
441             break;
442         case E_DSCMB_CAP_FLT_NUM:
443             u32Cap = E_HAL_DSCMB_CAP_FLT_NUM;
444             break;
445         case E_DSCMB_CAP_SUPPORT_ALGORITHM:
446             DSCMB_ASSERT_AND_RETURN(pInput != NULL);
447             *((HAL_DSCMB_Type *)pInput) = _DSCMB_TypeCasting(*((DSCMB_Type *)pInput));
448             u32Cap = E_HAL_DSCMB_CAP_SUPPORT_ALGORITHM;
449             break;
450         case E_DSCMB_CAP_SUPPORT_KEY_TYPE:
451             DSCMB_ASSERT_AND_RETURN(pInput != NULL);
452             if((*(DSCMB_Key_Type *)pInput) == E_DSCMB_KEY_CLEAR)
453                 return FALSE;
454             else
455                 return TRUE;
456             break;
457         case E_DSCMB_CAP_PIDFILTER_MAP_START:
458             u32Cap = E_HAL_DSCMB_CAP_PIDFILTER_MAP_START;
459             break;
460         case E_DSCMB_CAP_PIDFILTER_MAP_END:
461             u32Cap = E_HAL_DSCMB_CAP_PIDFILTER_MAP_END;
462             break;
463 #if ( defined(TSP_VERSION) && (TSP_VERSION == TSP_VER_3_0))
464         case E_DSCMB_CAP_SHARE_KEY_SLOT_MAX_NUM:
465             u32Cap = E_HAL_DSCMB_CAP_SHARE_KEY_SLOT_NUM;
466             break;
467         case E_DSCMB_CAP_SHARE_KEY_REGION_NUM:
468             u32Cap = E_HAL_DSCMB_CAP_SHARE_KEY_REGION_NUM;
469             break;
470         case E_DSCMB_CAP_SHARE_KEY_REGION_START:
471             DSCMB_ASSERT_AND_RETURN((pInput != NULL) && (*((MS_U32*)pInput) < E_HAL_DSCMB_CAP_SHARE_KEY_REGION_NUM));
472             u32Cap = E_HAL_DSCMB_CAP_SHARE_KEY_REGION_START;
473             break;
474         case E_DSCMB_CAP_SHARE_KEY_REGION_END:
475             DSCMB_ASSERT_AND_RETURN((pInput != NULL) && (*((MS_U32*)pInput) < E_HAL_DSCMB_CAP_SHARE_KEY_REGION_NUM));
476             u32Cap = E_HAL_DSCMB_CAP_SHARE_KEY_REGION_END;
477             break;
478 #endif
479         default:
480             return FALSE;
481             break;
482     }
483 
484     return HAL_DSCMB_GetCap(u32EngId, u32Cap, pInput, pOutput);
485 }
486 
MDrv_DSCMB2_OptConfig(MS_U32 u32EngId,DSCMB_Option eOpt,void * pInput,void * pOutput,MS_BOOL bSet)487 MS_BOOL MDrv_DSCMB2_OptConfig(MS_U32 u32EngId, DSCMB_Option eOpt, void* pInput, void* pOutput, MS_BOOL bSet)
488 {
489 #if DSCMB_UTOPIA2
490     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
491     DSCMB_OPTCONFIG pArgs;
492     if(pu32DscmbInst == NULL)
493     {
494         #ifdef MSOS_TYPE_LINUX_KERNEL
495         if(UTOPIA_STATUS_FAIL == UtopiaOpen(MODULE_DSCMB | KERNEL_MODE, (void**)&pu32DscmbInst, 0x0, NULL))
496         #else
497         if(UTOPIA_STATUS_FAIL == UtopiaOpen(MODULE_DSCMB, (void**)&pu32DscmbInst, 0x0, NULL))
498         #endif
499         {
500             ULOGE("DSCMB", "UtopiaOpen DSCMB fail\n");
501             return FALSE;
502         }
503     }
504 
505     pArgs.u32EngId = u32EngId;
506     pArgs.eOpt = eOpt;
507     pArgs.pInput = pInput;
508     pArgs.pOutput = pOutput;
509     pArgs.bSet = bSet;
510 
511     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_OptConfig, (void*)&pArgs);
512     if(UTOPIA_STATUS_SUCCESS != u32Ret)
513     {
514         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
515         return FALSE;
516     }
517 
518     return TRUE;
519 #else
520     return _MDrv_DSCMB2_OptConfig(u32EngId, eOpt, pInput, pOutput, bSet);
521 #endif
522 }
523 
MDrv_DSCMB2_GetCap(MS_U32 u32EngId,DSCMB_Query_Type eQueryType,void * pInput,void * pOutput)524 MS_BOOL MDrv_DSCMB2_GetCap(MS_U32 u32EngId, DSCMB_Query_Type eQueryType, void* pInput, void* pOutput)
525 {
526 #if DSCMB_UTOPIA2
527     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
528     DSCMB_QUERYCAP pArgs;
529     if(pu32DscmbInst == NULL)
530     {
531         #ifdef MSOS_TYPE_LINUX_KERNEL
532         if(UTOPIA_STATUS_FAIL == UtopiaOpen(MODULE_DSCMB | KERNEL_MODE, (void**)&pu32DscmbInst, 0x0, NULL))
533         #else
534         if(UTOPIA_STATUS_FAIL == UtopiaOpen(MODULE_DSCMB, (void**)&pu32DscmbInst, 0x0, NULL))
535         #endif
536         {
537             ULOGE("DSCMB", "UtopiaOpen DSCMB fail\n");
538             return FALSE;
539         }
540     }
541 
542     pArgs.u32EngId = u32EngId;
543     pArgs.eQueryType = eQueryType;
544     pArgs.pInput = pInput;
545     pArgs.pOutput = pOutput;
546 
547     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_QueryCap, (void*)&pArgs);
548     if(UTOPIA_STATUS_SUCCESS != u32Ret)
549     {
550         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
551         return FALSE;
552     }
553 
554     return TRUE;
555 #else
556     return _MDrv_DSCMB2_GetCap(u32EngId, eQueryType, pInput, pOutput);
557 #endif
558 }
559 
560 //--------------------------------------------------------------------------------------------------
561 /// Allocate a dscrambler filter
562 /// @return Descrambler filter ID - Success
563 /// @return DRV_DSCMB_FLT_NULL - Failure
564 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB2_FltAlloc(MS_U32 u32EngId)565 MS_U32 MDrv_DSCMB2_FltAlloc(MS_U32 u32EngId)
566 {
567 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
568     return MDrv_DSCMB2_FltAlloc_Ex(u32EngId, E_DSCMB_FLT_3_KEYS);
569 #else
570     return MDrv_DSCMB2_FltAlloc_Ex(u32EngId, E_DSCMB_FLT_2_KEYS);
571 #endif
572 }
573 //--------------------------------------------------------------------------------------------------
574 /// Allocate a dscrambler filter
575 /// @param eFltType \b IN: type for descramber filter
576 /// @return Descrambler filter ID - Success
577 /// @return DRV_DSCMB_FLT_NULL - Failure
578 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_FltAlloc_Ex(MS_U32 u32EngId,DSCMB_Flt_Type eFltType)579 MS_U32 _MDrv_DSCMB2_FltAlloc_Ex(MS_U32 u32EngId, DSCMB_Flt_Type eFltType)
580 {
581     MS_U32 i;
582     stDscmbTspMap* pMap;
583     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
584 
585 #if DSCMB_SHAREFLT_ENABLE
586     // check share key slot already exist or not
587     if((eFltType == E_DSCMB_FLT_2_KEYS_SHARE) && (DscmbTspMap[u32EngId][DSCMB_SHARE_SLOT_REV_IDX].bUsed == TRUE))
588     {
589         return DscmbTspMap[u32EngId][DSCMB_SHARE_SLOT_REV_IDX].u32DscmbId;
590         }
591 #else
592     if(eFltType == E_DSCMB_FLT_2_KEYS_SHARE)
593     {
594         return DRV_DSCMB_FLT_NULL;
595     }
596 #endif
597 
598     // try to find free slot
599     for (i= 0; i< MAX_NUM; i++)
600     {
601 #if DSCMB_SHAREFLT_ENABLE
602         if(eFltType == E_DSCMB_FLT_2_KEYS_SHARE)
603         { // share type direct allocate reserved slot.
604             i = DSCMB_SHARE_SLOT_REV_IDX;
605         }
606         else if((eFltType == E_DSCMB_FLT_2_KEYS) && (i == DSCMB_SHARE_SLOT_REV_IDX))
607         { // individual type not allocate reserved slot.
608             break;
609         }
610 #endif
611 
612         if (DscmbTspMap[u32EngId][i].bUsed == FALSE)
613         {
614             pMap = &DscmbTspMap[u32EngId][i];
615             pMap->bUsed = TRUE;
616             pMap->u32DscmbId = i;
617             pMap->u32PidFltIdNum = 0;
618 
619             if(eFltType == E_DSCMB_FLT_2_KEYS_SHARE)
620                 pMap->SlotType = E_SLOT_TYPE_SHARE;
621             else
622                 pMap->SlotType = E_SLOT_TYPE_NORMAL;
623 
624 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
625             if(eFltType == E_DSCMB_FLT_3_KEYS)
626             {
627                 if (FALSE == _DSCMB_SlotAlloc(u32EngId, i, E_DSCMB_KEY_CLEAR, &(pMap->u32SlotIdClear)))
628                 {
629                     DSCMB_ASSERT_NO_RETURN(0);
630                     return DRV_DSCMB_FLT_NULL;
631                 }
632             }
633 #endif
634             if (FALSE == _DSCMB_SlotAlloc(u32EngId, i, E_DSCMB_KEY_EVEN, &(pMap->u32SlotIdEven)))
635             {
636                 DSCMB_ASSERT_NO_RETURN(0);
637                 return DRV_DSCMB_FLT_NULL;
638             }
639             if (FALSE == _DSCMB_SlotAlloc(u32EngId, i, E_DSCMB_KEY_ODD, &(pMap->u32SlotIdOdd)))
640             {
641                 DSCMB_ASSERT_NO_RETURN(0);
642                 return DRV_DSCMB_FLT_NULL;
643             }
644             return DscmbTspMap[u32EngId][i].u32DscmbId;
645         }
646     }
647     return DRV_DSCMB_FLT_NULL;
648 }
649 
MDrv_DSCMB2_FltAlloc_Ex(MS_U32 u32EngId,DSCMB_Flt_Type eFltType)650 MS_U32 MDrv_DSCMB2_FltAlloc_Ex(MS_U32 u32EngId, DSCMB_Flt_Type eFltType)
651 {
652 #if DSCMB_UTOPIA2
653     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
654     DSCMB_FLTALLOC pArgs;
655     if(pu32DscmbInst == NULL)
656     {
657         return DRV_DSCMB_FLT_NULL;
658     }
659 
660     pArgs.u32EngId = u32EngId;
661     pArgs.eFltType = eFltType;
662     pArgs.u32DscmbId = 0xFF;
663 
664     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_FltAlloc, (void*)&pArgs);
665     if(UTOPIA_STATUS_SUCCESS != u32Ret)
666     {
667         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
668         return DRV_DSCMB_FLT_NULL;
669     }
670 
671     return pArgs.u32DscmbId;
672 
673 #else
674     return _MDrv_DSCMB2_FltAlloc_Ex(u32EngId, eFltType);
675 #endif
676 }
677 //--------------------------------------------------------------------------------------------------
678 /// Free a dscrambler filter
679 /// @param u32DscmbId \b IN: Descrambler filter ID obtained from MDrv_DSCMB_FltAlloc
680 /// @return TRUE - Success
681 /// @return FALSE - Failure
682 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_FltFree(MS_U32 u32EngId,MS_U32 u32DscmbId)683 MS_BOOL _MDrv_DSCMB2_FltFree(MS_U32 u32EngId, MS_U32 u32DscmbId)
684 {
685     stDscmbTspMap* pMap;
686     MS_U32 i;
687     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
688     DSCMB_ASSERT_AND_RETURN(MAX_NUM > u32DscmbId);
689     DSCMB_ASSERT_AND_RETURN(TRUE == DscmbTspMap[u32EngId][u32DscmbId].bUsed);
690 
691     pMap = &DscmbTspMap[u32EngId][u32DscmbId];
692     if (U32_MAX != pMap->u32DscmbId)
693     {
694         // can't free this slot, if there has the other filter connect to this one when use share key.
695         if((pMap->SlotType == E_SLOT_TYPE_SHARE) && (pMap->u32PidFltIdNum != 0))
696         {
697             //ULOGE("DSCMB", "[Share key slot] Can't free this slot due to the PidFltIdNum not equal to 0 !!\n");
698             return FALSE;
699         }
700 
701 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
702         if (U32_MAX != pMap->u32SlotIdClear)
703         {
704             _MDrv_DSCMB2_FltKeyReset(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_CLEAR);
705         }
706 #endif
707         if (U32_MAX != pMap->u32SlotIdOdd)
708         {
709             _MDrv_DSCMB2_FltKeyReset(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_ODD);
710         }
711         if (U32_MAX != pMap->u32SlotIdEven)
712         {
713             _MDrv_DSCMB2_FltKeyReset(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_EVEN);
714         }
715     }
716 
717     for (i= 0; i< MAX_DSCMB_PIDFLT_NUM; i++)
718     {
719         if (U32_MAX != pMap->u32PidFltId[i])
720         {
721 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
722             if (U32_MAX != pMap->u32SlotIdClear)
723             {
724                 if(FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_CLEAR, pMap->u32SlotIdClear, pMap->u32PidFltId[i]))
725                 {
726                     DSCMB_ASSERT_AND_RETURN(0);
727                     return FALSE;
728                 }
729             }
730 #endif
731             if (U32_MAX != pMap->u32SlotIdOdd)
732             {
733                 if(FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_ODD, pMap->u32SlotIdOdd, pMap->u32PidFltId[i]))
734                 {
735                     DSCMB_ASSERT_AND_RETURN(0);
736                     return FALSE;
737                 }
738             }
739             if (U32_MAX != pMap->u32SlotIdEven)
740             {
741                 if(FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_EVEN, pMap->u32SlotIdEven, pMap->u32PidFltId[i]))
742                 {
743                     DSCMB_ASSERT_AND_RETURN(0);
744                     return FALSE;
745                 }
746             }
747             pMap->u32PidFltId[i] = U32_MAX;
748         }
749     }
750 
751 
752 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
753     if (U32_MAX != pMap->u32SlotIdClear)
754     {
755         if (FALSE == _DSCMB_SlotFree(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_CLEAR, pMap->u32SlotIdClear))
756         {
757             DSCMB_ASSERT_AND_RETURN(0);
758         }
759     }
760 #endif
761     if (FALSE == _DSCMB_SlotFree(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_EVEN, pMap->u32SlotIdEven))
762     {
763         DSCMB_ASSERT_AND_RETURN(0);
764     }
765     if (FALSE == _DSCMB_SlotFree(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_ODD, pMap->u32SlotIdOdd))
766     {
767         DSCMB_ASSERT_AND_RETURN(0);
768     }
769 
770     if(pMap->SlotType == E_SLOT_TYPE_SHARE)
771     {
772         pMap->SlotType = E_SLOT_TYPE_NORMAL;
773     }
774 
775     pMap->bDscmb= TRUE;
776     pMap->bUsed = FALSE;
777     pMap->u32PidFltIdNum = 0;
778     pMap->u32SlotIdClear = pMap->u32SlotIdEven = pMap->u32SlotIdOdd = U32_MAX;
779     pMap->u32DscmbId = U32_MAX;
780 
781     return TRUE;
782 }
783 
MDrv_DSCMB2_FltFree(MS_U32 u32EngId,MS_U32 u32DscmbId)784 MS_BOOL MDrv_DSCMB2_FltFree(MS_U32 u32EngId, MS_U32 u32DscmbId)
785 {
786 #if DSCMB_UTOPIA2
787     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
788     DSCMB_FLTFREE pArgs;
789     memset(&pArgs, 0, sizeof(DSCMB_FLTFREE));
790 
791     if(pu32DscmbInst == NULL)
792     {
793         return FALSE;
794     }
795 
796     pArgs.u32EngId = u32EngId;
797     pArgs.u32DscmbId = u32DscmbId;
798 
799     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_FltFree, (void*)&pArgs);
800     if(UTOPIA_STATUS_SUCCESS != u32Ret)
801     {
802         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
803         return FALSE;
804     }
805 
806     return TRUE;
807 #else
808     return _MDrv_DSCMB2_FltFree(u32EngId, u32DscmbId);
809 #endif
810 }
811 
812 #if !(defined(MSOS_TYPE_OPTEE) || defined(MSOS_TYPE_NUTTX))
813 //--------------------------------------------------------------------------------------------------
814 /// Make connections between the Dscmb ID with the targeted Pid to be descrambled
815 /// @param u32DscmbId \b IN: Descrambler filter ID obtained from MDrv_DSCMB_FltAlloc
816 /// @param u32Pid \b IN: The targeted Pid to be descrambled
817 /// @return TRUE - Success
818 /// @return FALSE - Failure
819 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_FltConnectPid(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_U32 u32Pid)820 MS_BOOL _MDrv_DSCMB2_FltConnectPid(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_U32 u32Pid)
821 {
822     MS_BOOL bConnected = FALSE;
823     MS_U32 u32Max_TspFltNum = 0;
824     MS_U32 u32Min_TspFltNum = 0;
825     MS_U32 u32Fltid = 0;
826     MS_U32 u32Pid_Temp = 0x1FFF;
827     MS_U32 j = 0;
828 
829     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
830     DSCMB_ASSERT_AND_RETURN(MAX_NUM > u32DscmbId);
831     DSCMB_ASSERT_AND_RETURN(TRUE == DscmbTspMap[u32EngId][u32DscmbId].bUsed);
832 
833 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)) )
834     if(MDrv_TSP_GetCap(E_TSP_CAP_PIDFLT_NUM, &u32Max_TspFltNum) != E_TSP_OK)
835         DSCMB_ASSERT_AND_RETURN(0);
836 #else
837     MS_U32 i = 0;
838     MS_U32 u32ShareRegionNum = 0;
839     MS_U32 u32CapInput = 0;
840 
841     if(DscmbTspMap[u32EngId][u32DscmbId].SlotType == E_SLOT_TYPE_SHARE)
842     {
843         if(HAL_DSCMB_GetCap(u32EngId, E_HAL_DSCMB_CAP_SHARE_KEY_REGION_NUM, &u32CapInput, &u32ShareRegionNum) != TRUE)
844             DSCMB_ASSERT_AND_RETURN(0);
845     }
846     else
847         u32ShareRegionNum = 1;
848 
849     for (i = 0; i < u32ShareRegionNum; i++)
850     {
851         if(DscmbTspMap[u32EngId][u32DscmbId].SlotType == E_SLOT_TYPE_SHARE)
852         {// Search for share key region
853             if(HAL_DSCMB_GetCap(u32EngId, E_HAL_DSCMB_CAP_SHARE_KEY_REGION_START, &i, &u32Min_TspFltNum) != TRUE)
854                 DSCMB_ASSERT_AND_RETURN(0);
855             if(HAL_DSCMB_GetCap(u32EngId, E_HAL_DSCMB_CAP_SHARE_KEY_REGION_END, &i, &u32Max_TspFltNum) != TRUE)
856                 DSCMB_ASSERT_AND_RETURN(0);
857         }
858         else
859 #if ( defined(TSP_VERSION) && (TSP_VERSION == TSP_VER_3_0) )
860         {// Search for individual region.
861             if(MDrv_TSP_GetDscmbFltId_Range(u32EngId, &u32Min_TspFltNum, &u32Max_TspFltNum) != DRVTSP_OK)
862                 DSCMB_ASSERT_AND_RETURN(0);
863         }
864 #else
865         {// Search for individual region.
866             if(HAL_DSCMB_GetCap(u32EngId, E_HAL_DSCMB_CAP_PIDFILTER_MAP_START, NULL, &u32Min_TspFltNum) != TRUE)
867                 DSCMB_ASSERT_AND_RETURN(0);
868             if(HAL_DSCMB_GetCap(u32EngId, E_HAL_DSCMB_CAP_PIDFILTER_MAP_END, NULL, &u32Max_TspFltNum) != TRUE)
869                 DSCMB_ASSERT_AND_RETURN(0);
870         }
871 #endif
872 #endif
873 
874         for (j = u32Min_TspFltNum; j < u32Max_TspFltNum; j++)
875         {
876 // @TODO: it's not good to call driver by driver.
877 // Plan to add a new function for upper layer to assign pidfltid
878             if ((DRVTSP_OK == MDrv_TSP_PidFlt_GetPid(0, j, &u32Pid_Temp)) && (u32Pid_Temp == u32Pid)) // find the match pid filter index
879             {
880                 #if !(defined(TSP_VERSION) && (TSP_VERSION >= TSP_VER_2_0))
881                 u32Fltid = HAL_DSCMB_TransFltId2FltGroup0Id(j);
882                 #else
883                 u32Fltid = j;
884                 #endif
885                 if(_MDrv_DSCMB2_FltConnectFltId(u32EngId, u32DscmbId, u32Fltid) == FALSE)
886                 {
887                     DSCMB_ASSERT_AND_RETURN(0);
888                     return FALSE;
889                 }
890                 bConnected = TRUE;
891             }
892         }
893 #if !( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)) )
894     }
895 #endif
896 
897     return bConnected;
898 }
899 
MDrv_DSCMB2_FltConnectPid(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_U32 u32Pid)900 MS_BOOL MDrv_DSCMB2_FltConnectPid(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_U32 u32Pid)
901 {
902 #if DSCMB_UTOPIA2
903     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
904     DSCMB_CONNECTPID pArgs;
905     memset(&pArgs, 0, sizeof(DSCMB_CONNECTPID));
906     if(pu32DscmbInst == NULL)
907     {
908         return FALSE;
909     }
910 
911     pArgs.u32EngId = u32EngId;
912     pArgs.u32DscmbId = u32DscmbId;
913     pArgs.u32Pid = u32Pid;
914 
915     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_FltConnectPid, (void*)&pArgs);
916     if(UTOPIA_STATUS_SUCCESS != u32Ret)
917     {
918         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
919         return FALSE;
920     }
921 
922     return TRUE;
923 #else
924     return _MDrv_DSCMB2_FltConnectPid(u32EngId, u32DscmbId, u32Pid);
925 #endif
926 }
927 //--------------------------------------------------------------------------------------------------
928 /// Break connections between the Dscmb ID with the targeted Pid
929 /// @param u32DscmbId \b IN: Descrambler filter ID obtained from MDrv_DSCMB_FltAlloc
930 /// @param u32Pid \b IN: The targeted Pid to be descrambled
931 /// @return TRUE - Success
932 /// @return FALSE - Failure
933 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_FltDisconnectPid(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_U32 u32Pid)934 MS_BOOL _MDrv_DSCMB2_FltDisconnectPid(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_U32 u32Pid)
935 {
936     stDscmbTspMap* pMap;
937     MS_U32 u32Pid_Temp = 0x1FFF;
938     MS_U32 j = 0;
939     TSP_Result result;
940     MS_U32 u32Fltid = 0;
941 
942     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
943     DSCMB_ASSERT_AND_RETURN(TRUE == DscmbTspMap[u32EngId][u32DscmbId].bUsed);
944     pMap = &DscmbTspMap[u32EngId][u32DscmbId];
945 
946 
947     DSCMB_ASSERT_AND_RETURN(U32_MAX!= pMap->u32SlotIdEven);
948     DSCMB_ASSERT_AND_RETURN(U32_MAX!= pMap->u32SlotIdOdd);
949 
950     for (j= 0; j< MAX_DSCMB_PIDFLT_NUM; j++)
951     {
952         if (U32_MAX != pMap->u32PidFltId[j])
953         {
954             // just dis-connect the filter which PID matched when use share key
955             if(pMap->SlotType == E_SLOT_TYPE_SHARE)
956             {
957                 #if !(defined(TSP_VERSION) && (TSP_VERSION >= TSP_VER_2_0))
958                 u32Fltid = HAL_DSCMB_TransFltGroup0Id2Fltd(pMap->u32PidFltId[j]);
959                 #else
960                 u32Fltid = pMap->u32PidFltId[j];
961                 #endif
962                 result = MDrv_TSP_PidFlt_GetPid(0, u32Fltid, &u32Pid_Temp);
963                 if ((DRVTSP_OK == result) && (u32Pid_Temp != u32Pid))
964                     continue;
965                 else if(DRVTSP_FAIL == result)
966                 {
967                     ULOGE("DSCMB", "Can't find the filter which PID:[0x%x] by FltIdx:[%d]!!\n", (int)u32Pid, (int)pMap->u32PidFltId[j]);
968                     DSCMB_ASSERT_AND_RETURN(0);
969                 }
970             }
971 
972             pMap->u32PidFltIdNum--;
973 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
974             if (U32_MAX != pMap->u32SlotIdClear)
975             {
976                 if (FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_CLEAR, pMap->u32SlotIdClear, pMap->u32PidFltId[j]))
977                 {
978                     DSCMB_ASSERT_AND_RETURN(0);
979                     return FALSE;
980                 }
981             }
982 #endif
983             if (FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_EVEN, pMap->u32SlotIdEven, pMap->u32PidFltId[j]))
984             {
985                 DSCMB_ASSERT_AND_RETURN(0);
986                 return FALSE;
987             }
988             if (FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_ODD, pMap->u32SlotIdOdd, pMap->u32PidFltId[j]))
989             {
990                 DSCMB_ASSERT_AND_RETURN(0);
991                 return FALSE;
992             }
993         }
994     }
995 
996     for (j= 0; j< MAX_DSCMB_PIDFLT_NUM; j++)
997     {
998         pMap->u32PidFltId[j] = U32_MAX;
999     }
1000 
1001     return TRUE;
1002 }
1003 
MDrv_DSCMB2_FltDisconnectPid(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_U32 u32Pid)1004 MS_BOOL MDrv_DSCMB2_FltDisconnectPid(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_U32 u32Pid)
1005 {
1006 #if DSCMB_UTOPIA2
1007     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
1008     DSCMB_DISCONNECTPID pArgs;
1009     memset(&pArgs, 0, sizeof(DSCMB_DISCONNECTPID));
1010     if(pu32DscmbInst == NULL)
1011     {
1012         return FALSE;
1013     }
1014 
1015     pArgs.u32EngId = u32EngId;
1016     pArgs.u32DscmbId = u32DscmbId;
1017     pArgs.u32Pid = u32Pid;
1018     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_FltDisconnectPid, (void*)&pArgs);
1019 
1020     if(UTOPIA_STATUS_SUCCESS != u32Ret)
1021     {
1022         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
1023         return FALSE;
1024     }
1025 
1026     return TRUE;
1027 #else
1028     return _MDrv_DSCMB2_FltDisconnectPid(u32EngId, u32DscmbId, u32Pid);
1029 #endif
1030 }
1031 
1032 #if 1
1033 //--------------------------------------------------------------------------------------------------
1034 /// Break connections between the Dscmb ID with the targeted Pid
1035 /// @param u32DscmbId \b IN: Descrambler filter ID obtained from MDrv_DSCMB_FltAlloc
1036 /// @param u32Pid \b IN: The targeted Pid to be descrambled
1037 /// @return TRUE - Success
1038 /// @return FALSE - Failure
1039 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB2_FltDisconnectPid_Ex(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_U32 u32Pid)1040 MS_BOOL MDrv_DSCMB2_FltDisconnectPid_Ex(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_U32 u32Pid)
1041 {
1042     stDscmbTspMap* pMap;
1043     MS_U32 j,u32Flt_Pid = 0 ;
1044     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
1045     DSCMB_ASSERT_AND_RETURN(TRUE == DscmbTspMap[u32EngId][u32DscmbId].bUsed);
1046 
1047     pMap = &DscmbTspMap[u32EngId][u32DscmbId];
1048 
1049 
1050     DSCMB_ASSERT_AND_RETURN(U32_MAX!= pMap->u32SlotIdEven);
1051     DSCMB_ASSERT_AND_RETURN(U32_MAX!= pMap->u32SlotIdOdd);
1052 
1053     for (j= 0; j< MAX_DSCMB_PIDFLT_NUM; j++)
1054     {
1055         if ((U32_MAX != pMap->u32PidFltId[j]) )
1056         {
1057             u32Flt_Pid = 0 ;
1058             if ((DRVTSP_OK == MDrv_TSP_PidFlt_GetPid(0, j, &u32Flt_Pid) )&& ( u32Flt_Pid == u32Pid ))
1059             {
1060                 pMap->u32PidFltIdNum--;
1061 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
1062                 if (U32_MAX != pMap->u32SlotIdClear)
1063                 {
1064                     if (FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_CLEAR, pMap->u32SlotIdClear, pMap->u32PidFltId[j]))
1065                     {
1066                         DSCMB_ASSERT_AND_RETURN(0);
1067                         return FALSE;
1068                     }
1069                 }
1070 #endif
1071                 if (FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_EVEN, pMap->u32SlotIdEven, pMap->u32PidFltId[j]))
1072                 {
1073                     DSCMB_ASSERT_AND_RETURN(0);
1074                     return FALSE;
1075                 }
1076                 if (FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_ODD, pMap->u32SlotIdOdd, pMap->u32PidFltId[j]))
1077                 {
1078                     DSCMB_ASSERT_AND_RETURN(0);
1079                     return FALSE;
1080                 }
1081                 pMap->u32PidFltId[j] = U32_MAX;
1082             }
1083         }
1084     }
1085 
1086     return TRUE;
1087 }
1088 #endif
1089 
1090 #endif //MSOS_TYPE_NUTTX | MSOS_TYPE_OPTEE
1091 //--------------------------------------------------------------------------------------------------
1092 /// Make connections between the Dscmb ID with the targeted filter id to be descrambled
1093 /// @param u32DscmbId \b IN: Descrambler filter ID obtained from MDrv_DSCMB_FltAlloc
1094 /// @param u32Pid \b IN: The targeted filter id to be descrambled
1095 /// @return TRUE - Success
1096 /// @return FALSE - Failure
1097 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_FltConnectFltId(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_U32 u32FltId)1098 MS_BOOL _MDrv_DSCMB2_FltConnectFltId(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_U32 u32FltId)
1099 {
1100     stDscmbTspMap* pMap=NULL;
1101     MS_U32 i=0;
1102 
1103     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
1104     DSCMB_ASSERT_AND_RETURN(MAX_NUM > u32DscmbId);
1105     DSCMB_ASSERT_AND_RETURN(TRUE == DscmbTspMap[u32EngId][u32DscmbId].bUsed);
1106     DSCMB_ASSERT_AND_RETURN(MAX_DSCMB_PIDFLT_NUM > DscmbTspMap[u32EngId][u32DscmbId].u32PidFltIdNum);
1107 
1108     pMap = &DscmbTspMap[u32EngId][u32DscmbId];
1109 
1110     DSCMB_ASSERT_AND_RETURN(MAX_DSCMB_PIDFLT_NUM> pMap->u32PidFltIdNum);
1111 
1112 #if DSCMB_SHAREFLT_ENABLE
1113     // check dscmb idx & pid flt idx range is match or not
1114     if(HAL_DSCMB_CheckShareConnect(u32EngId, u32FltId, pMap->SlotType) == FALSE)
1115     {
1116         DSCMB_ASSERT_AND_RETURN(0);
1117         return FALSE;
1118     }
1119 #endif
1120 
1121     // check dscmb connected filters //
1122     for( i = 0 ; i < MAX_DSCMB_PIDFLT_NUM ; i ++)
1123     {
1124         if (pMap->u32PidFltId[i] == u32FltId)
1125         {
1126             //ULOGE("DSCMB", "[%s][%d] Dscmb %d is already connected to Filter %d \n", __FUNCTION__,__LINE__,u32DscmbId,u32FltId);
1127             return TRUE ;
1128         }
1129     }
1130 
1131 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
1132     // return FALSE if tsp filter try to connect to another dscmbid ( duplicate dscmb connction)
1133     MS_U32 j=0;
1134     MS_U32* pu32FltId = NULL;
1135 
1136     for (i = 0; i < MAX_NUM; i++)
1137     {
1138         if( (DscmbTspMap[u32EngId][i].bUsed) && (i != u32DscmbId) )
1139         {
1140             pu32FltId = DscmbTspMap[u32EngId][i].u32PidFltId;
1141 
1142             for (j = 0; j < MAX_DSCMB_PIDFLT_NUM ; j++)
1143             {
1144                 if(pu32FltId[j] == u32FltId)
1145                 {
1146                     MS_DEBUG_MSG( ULOGE("DSCMB", "[%s][%d] Duplicate descramble connection from dscmbid=%lu to dscmbid=%lu\n", __FUNCTION__, __LINE__ ,u32DscmbId, i));
1147                     ULOGE("DSCMB", "duplicate connection\n");
1148                     return FALSE;
1149                 }
1150             }
1151         }
1152     }
1153 
1154     if (U32_MAX != pMap->u32SlotIdClear)
1155     {
1156         if (FALSE == HAL_DSCMB_SlotConnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_CLEAR, pMap->u32SlotIdClear, u32FltId))
1157         {
1158             DSCMB_ASSERT_AND_RETURN(0);
1159             return FALSE;
1160         }
1161     }
1162 #endif
1163     if (FALSE == HAL_DSCMB_SlotConnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_EVEN, pMap->u32SlotIdEven, u32FltId))
1164     {
1165         DSCMB_ASSERT_AND_RETURN(0);
1166         return FALSE;
1167     }
1168     if (FALSE == HAL_DSCMB_SlotConnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_ODD, pMap->u32SlotIdOdd, u32FltId))
1169     {
1170         DSCMB_ASSERT_AND_RETURN(0);
1171         return FALSE;
1172     }
1173 
1174     for (i = 0; i < MAX_DSCMB_PIDFLT_NUM; i++)
1175     {
1176         if (pMap->u32PidFltId[i] == U32_MAX)
1177         {
1178             pMap->u32PidFltId[i] = u32FltId;
1179     pMap->u32PidFltIdNum++;
1180             break;
1181         }
1182     }
1183 
1184     return TRUE;
1185 }
1186 
MDrv_DSCMB2_FltConnectFltId(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_U32 u32FltId)1187 MS_BOOL MDrv_DSCMB2_FltConnectFltId(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_U32 u32FltId)
1188 {
1189 #if DSCMB_UTOPIA2
1190     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
1191     DSCMB_CONNECTFLT pArgs;
1192     memset(&pArgs, 0, sizeof(DSCMB_CONNECTFLT));
1193     if(pu32DscmbInst == NULL)
1194     {
1195         return FALSE;
1196     }
1197 
1198     pArgs.u32EngId = u32EngId;
1199     pArgs.u32DscmbId = u32DscmbId;
1200     pArgs.u32DmxFltId = u32FltId;
1201 
1202     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_FltConnectFltId, (void*)&pArgs);
1203     if(UTOPIA_STATUS_SUCCESS != u32Ret)
1204     {
1205         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
1206         return FALSE;
1207     }
1208 
1209     return TRUE;
1210 #else
1211     return _MDrv_DSCMB2_FltConnectFltId(u32EngId, u32DscmbId, u32FltId);
1212 #endif
1213 }
1214 
1215 //--------------------------------------------------------------------------------------------------
1216 /// Disconnect between the Dscmb ID with the targeted filter id to be descrambled
1217 /// @param u32DscmbId \b IN: Descrambler filter ID obtained from MDrv_DSCMB_FltAlloc
1218 /// @param u32Pid \b IN: The targeted filter id to be descrambled
1219 /// @return TRUE - Success
1220 /// @return FALSE - Failure
1221 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_FltDisconnectFltId(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_U32 u32FltId)1222 MS_BOOL _MDrv_DSCMB2_FltDisconnectFltId(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_U32 u32FltId)
1223 {
1224     stDscmbTspMap* pMap;
1225     MS_U32 j ;
1226     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
1227     DSCMB_ASSERT_AND_RETURN(MAX_NUM > u32DscmbId);
1228     DSCMB_ASSERT_AND_RETURN(TRUE == DscmbTspMap[u32EngId][u32DscmbId].bUsed);
1229 
1230     pMap = &DscmbTspMap[u32EngId][u32DscmbId];
1231 
1232     DSCMB_ASSERT_AND_RETURN(MAX_DSCMB_PIDFLT_NUM>= pMap->u32PidFltIdNum);
1233 
1234     for (j= 0; j< MAX_DSCMB_PIDFLT_NUM; j++)
1235     {
1236         if (pMap->u32PidFltId[j] == u32FltId)
1237         {
1238             pMap->u32PidFltIdNum--;
1239             pMap->u32PidFltId[j] = U32_MAX;
1240 
1241             if((pMap->SlotType == E_SLOT_TYPE_SHARE) && (pMap->u32PidFltIdNum != 0))
1242                 return TRUE;
1243 
1244 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
1245             if (U32_MAX != pMap->u32SlotIdClear)
1246             {
1247                 if (FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_CLEAR, pMap->u32SlotIdClear, u32FltId))
1248                 {
1249                     DSCMB_ASSERT_AND_RETURN(0);
1250                     return FALSE;
1251                 }
1252             }
1253 #endif
1254             if (FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_EVEN, pMap->u32SlotIdEven, u32FltId))
1255             {
1256                 DSCMB_ASSERT_AND_RETURN(0);
1257                 return FALSE;
1258             }
1259             if (FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_ODD, pMap->u32SlotIdOdd, u32FltId))
1260             {
1261                 DSCMB_ASSERT_AND_RETURN(0);
1262                 return FALSE;
1263             }
1264 
1265         }
1266 
1267     }
1268 
1269     return TRUE;
1270 }
1271 
MDrv_DSCMB2_FltDisconnectFltId(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_U32 u32FltId)1272 MS_BOOL MDrv_DSCMB2_FltDisconnectFltId(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_U32 u32FltId)
1273 {
1274 #if DSCMB_UTOPIA2
1275     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
1276     DSCMB_DISCONNECTFLT pArgs;
1277     memset(&pArgs, 0, sizeof(DSCMB_DISCONNECTFLT));
1278     if(pu32DscmbInst == NULL)
1279     {
1280         return FALSE;
1281     }
1282 
1283     pArgs.u32EngId = u32EngId;
1284     pArgs.u32DscmbId = u32DscmbId;
1285     pArgs.u32DmxFltId = u32FltId;
1286     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_FltDisconnectFltId, (void*)&pArgs);
1287     if(UTOPIA_STATUS_SUCCESS != u32Ret)
1288     {
1289         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
1290         return FALSE;
1291     }
1292 
1293     return TRUE;
1294 #else
1295     return _MDrv_DSCMB2_FltDisconnectFltId(u32EngId, u32DscmbId, u32FltId);
1296 #endif
1297 }
1298 //--------------------------------------------------------------------------------------------------
1299 /// Set descrambler filter as scrambling/decrambling mode
1300 /// Default filter mode is descrambling if this function is not specified
1301 /// This function should be invoked before MDrv_DSCMB_FltTypeSe
1302 /// @param u32DscmbId \b IN: Descrambler filter ID obtained from MDrv_DSCMB_FltAlloc
1303 /// @param DscmbType \b IN: The descrambler algorithm
1304 /// @return TRUE - Success
1305 /// @return FALSE - Failure
1306 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_FltDscmb(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_BOOL bDscmb)1307 MS_BOOL _MDrv_DSCMB2_FltDscmb(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_BOOL bDscmb)
1308 {
1309     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
1310     DscmbTspMap[u32EngId][u32DscmbId].bDscmb= (bDscmb)? TRUE: FALSE;
1311     return TRUE;
1312 }
1313 
MDrv_DSCMB2_FltDscmb(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_BOOL bDscmb)1314 MS_BOOL MDrv_DSCMB2_FltDscmb(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_BOOL bDscmb)
1315 {
1316 #if DSCMB_UTOPIA2
1317     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
1318     DSCMB_FLTDSCMB pArgs;
1319     memset(&pArgs, 0, sizeof(DSCMB_FLTDSCMB));
1320     if(pu32DscmbInst == NULL)
1321     {
1322         return FALSE;
1323     }
1324 
1325     pArgs.u32EngId = u32EngId;
1326     pArgs.u32DscmbId = u32DscmbId;
1327     pArgs.bDscmb = bDscmb;
1328 
1329     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_FltDscmb, (void*)&pArgs);
1330     if(UTOPIA_STATUS_SUCCESS != u32Ret)
1331     {
1332         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
1333         return FALSE;
1334     }
1335 
1336     return TRUE;
1337 #else
1338     return _MDrv_DSCMB2_FltDscmb(u32EngId, u32DscmbId, bDscmb);
1339 #endif
1340 }
1341 
1342 //--------------------------------------------------------------------------------------------------
1343 // [RESERVED] The low-level implementation of @ref MDrv_DSCMB_FltTypeSet
1344 // @param u32DscmbId \b IN: descrambler filter ID obtained from MDrv_DSCMB_FltAlloc
1345 // @param eKeyType \b IN: key type for this setting
1346 // @param u32SW0 \b IN: The descramlber switch configuration
1347 // @param u32SW1 \b IN: reserved
1348 // @param u32SW2 \b IN: reserved
1349 // @return TRUE - Success
1350 // @return FALSE - Failure
1351 // @sa MDrv_DSCMB_FltTypeSet
1352 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_SlotSwitchWrite(MS_U32 u32DscmbId,DSCMB_Key_Type eKeyType,MS_U32 u32SW0,MS_U32 u32SW1,MS_U32 u32SW2)1353 MS_BOOL MDrv_DSCMB_SlotSwitchWrite(MS_U32 u32DscmbId, DSCMB_Key_Type eKeyType, MS_U32 u32SW0, MS_U32 u32SW1, MS_U32 u32SW2)
1354 {
1355 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
1356     HAL_DSCMB_SlotSwitchWrite(u32DscmbId, eKeyType, u32SW0, REG_DSCMB_POS_SWITCH13);
1357     // u32SW1, u32SW2 are reserved
1358 
1359     return TRUE;
1360 #else
1361     return FALSE;
1362 #endif
1363 }
1364 
1365 //--------------------------------------------------------------------------------------------------
1366 /// Enable Record stream after stream is descrambled, only supported when CA venders do not prohibat this feature.
1367 /// @Note: Function can only be called before any record function is called.
1368 /// @param u32EngId \b IN: Descrambler filter ID obtained from MDrv_DSCMB_FltAlloc
1369 /// @param Enable \b IN: for enabe/disable record mechanism.
1370 /// @return TRUE - Success
1371 /// @return FALSE - Failure
1372 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB2_PVR_RecCtrl(MS_U32 u32EngId,MS_BOOL Enable)1373 MS_BOOL MDrv_DSCMB2_PVR_RecCtrl(MS_U32 u32EngId, MS_BOOL Enable)
1374 {
1375 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
1376     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
1377 
1378     HAL_DSCMB_PVR_RecCtrl( u32EngId,   Enable);
1379 
1380     return TRUE;
1381 #else
1382     return FALSE;
1383 #endif
1384 }
1385 //--------------------------------------------------------------------------------------------------
1386 /// Set descrambler algorithm by common predefined descramble type
1387 /// @param u32DscmbId \b IN: Descrambler filter ID obtained from MDrv_DSCMB_FltAlloc
1388 /// @param DscmbType \b IN: The descrambler algorithm
1389 /// @return TRUE - Success
1390 /// @return FALSE - Failure
1391 /// @note
1392 /// both dscmb engines operate on same algorithm
1393 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_FltTypeSet(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Type DscmbType)1394 MS_BOOL _MDrv_DSCMB2_FltTypeSet(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Type DscmbType)
1395 {
1396     HAL_DSCMB_Type hal_dscmb_type;
1397     stDscmbTspMap* pMap;
1398     MS_BOOL bSet = FALSE;
1399     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
1400 
1401     pMap = &DscmbTspMap[u32EngId][u32DscmbId];
1402     pMap->DscmbType = DscmbType;
1403 
1404     switch (DscmbType)
1405     {
1406     case E_DSCMB_TYPE_CSA:
1407         hal_dscmb_type = E_HAL_DSCMB_TYPE_CSA;
1408         break;
1409     case E_DSCMB_TYPE_CSA_CONF:
1410         hal_dscmb_type = E_HAL_DSCMB_TYPE_CSA_CONF;
1411         break;
1412     case E_DSCMB_TYPE_NSA_AS_ESA:
1413         hal_dscmb_type = E_HAL_DSCMB_TYPE_NSA_AS_ESA;
1414         break;
1415     case E_DSCMB_TYPE_DES:
1416         hal_dscmb_type = E_HAL_DSCMB_TYPE_DES;
1417         break;
1418     case E_DSCMB_TYPE_AES:
1419         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES;
1420         break;
1421     case E_DSCMB_TYPE_AES_ECB:
1422         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_ECB;
1423         break;
1424 #if (!( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
1425     case E_DSCMB_TYPE_AES_CTR:
1426         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_CTR;
1427         break;
1428 #endif
1429     case E_DSCMB_TYPE_AES_SCTE52:
1430         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_SCTE52;
1431         break;
1432     case E_DSCMB_TYPE_TDES_ECB:
1433         hal_dscmb_type = E_HAL_DSCMB_TYPE_TDES_ECB;
1434         break;
1435     case E_DSCMB_TYPE_TDES_SCTE52:
1436         hal_dscmb_type = E_HAL_DSCMB_TYPE_TDES_SCTE52;
1437         break;
1438     case E_DSCMB_TYPE_SYN_AES:
1439         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_LEADING_CLR_EBC;
1440         break;
1441 #if defined(DSCMB_MULTI2_ENABLE)
1442     case E_DSCMB_TYPE_MULTI2:
1443         hal_dscmb_type = E_HAL_DSCMB_TYPE_MULTI2;
1444         break;
1445 #endif
1446     case E_DSCMB_TYPE_OC:
1447         hal_dscmb_type = E_HAL_DSCMB_TYPE_OC;
1448         break;
1449     case E_DSCMB_TYPE_CSA3:
1450         hal_dscmb_type = E_HAL_DSCMB_TYPE_CSA3;
1451         break;
1452     default:
1453         DSCMB_ASSERT_AND_RETURN(0);
1454     }
1455 
1456 #if DSCMB_PARAMETER_TABLE
1457     bSet = HAL_DSCMB_FltTypeSet(u32EngId, 0, pMap->u32SlotIdEven, hal_dscmb_type, pMap->bDscmb);
1458     if(FALSE == bSet)
1459         DSCMB_ASSERT_AND_RETURN(0);
1460 #else
1461     MS_U32 i;
1462     for (i= 0; i< MAX_DSCMB_PIDFLT_NUM; i++)
1463     {
1464         if (U32_MAX != pMap->u32PidFltId[i])
1465         {
1466 #if defined(NDS_IMPL)
1467 //TODO: MDrv_DSCMB for NDS
1468 #endif
1469             bSet = HAL_DSCMB_FltTypeSet(u32EngId, pMap->u32PidFltId[i], hal_dscmb_type, pMap->bDscmb);
1470             if(FALSE == bSet)
1471                 DSCMB_ASSERT_AND_RETURN(0);
1472         }
1473     }
1474 #endif
1475     return bSet;
1476 }
1477 
MDrv_DSCMB2_FltTypeSet(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Type DscmbType)1478 MS_BOOL MDrv_DSCMB2_FltTypeSet(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Type DscmbType)
1479 {
1480 #if DSCMB_UTOPIA2
1481     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
1482     DSCMB_TYPESET pArgs;
1483     memset(&pArgs, 0, sizeof(DSCMB_TYPESET));
1484     if(pu32DscmbInst == NULL)
1485     {
1486         return FALSE;
1487     }
1488 
1489     pArgs.u32EngId = u32EngId;
1490     pArgs.u32DscmbId = u32DscmbId;
1491     pArgs.eType = DscmbType;
1492 
1493     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_FltTypeSet, (void*)&pArgs);
1494     if(UTOPIA_STATUS_SUCCESS != u32Ret)
1495     {
1496         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
1497         return FALSE;
1498     }
1499 
1500     return TRUE;
1501 #else
1502     return _MDrv_DSCMB2_FltTypeSet(u32EngId, u32DscmbId, DscmbType);
1503 #endif
1504 }
1505 
_MDrv_DSCMB2_FltKeyReset(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Key_Type eKeyType)1506 MS_BOOL _MDrv_DSCMB2_FltKeyReset(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Key_Type eKeyType)
1507 {
1508     DSCMB_Key_Type hal_dscmb_key_type;
1509     HAL_DSCMB_Type hal_dscmb_type;
1510     MS_U32 u32Slot;
1511     stDscmbTspMap* pMap;
1512 
1513     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
1514     pMap = &DscmbTspMap[u32EngId][u32DscmbId];
1515 
1516     switch (eKeyType)
1517     {
1518 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
1519     case E_DSCMB_KEY_CLEAR:
1520         hal_dscmb_key_type = E_DSCMB_KEY_CLEAR;
1521         u32Slot = pMap->u32SlotIdClear;
1522         break;
1523 #endif
1524     case E_DSCMB_KEY_ODD:
1525         hal_dscmb_key_type = E_DSCMB_KEY_ODD;
1526         u32Slot = pMap->u32SlotIdOdd;
1527         break;
1528     case E_DSCMB_KEY_EVEN:
1529         hal_dscmb_key_type = E_DSCMB_KEY_EVEN;
1530         u32Slot = pMap->u32SlotIdEven;
1531         break;
1532     default:
1533         return FALSE;
1534     }
1535 
1536     if (U32_MAX == u32Slot)
1537     {
1538         return FALSE;
1539     }
1540 
1541     switch (pMap->DscmbType)
1542     {
1543     case E_DSCMB_TYPE_CSA:
1544         hal_dscmb_type = E_HAL_DSCMB_TYPE_CSA;
1545         break;
1546     case E_DSCMB_TYPE_CSA_CONF:
1547         hal_dscmb_type = E_HAL_DSCMB_TYPE_CSA_CONF;
1548         break;
1549     case E_DSCMB_TYPE_NSA_AS_ESA:
1550         hal_dscmb_type = E_HAL_DSCMB_TYPE_NSA_AS_ESA;
1551         break;
1552     case E_DSCMB_TYPE_DES:
1553         hal_dscmb_type = E_HAL_DSCMB_TYPE_DES;
1554         break;
1555     case E_DSCMB_TYPE_AES:
1556         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES;
1557         break;
1558     case E_DSCMB_TYPE_AES_CTR:
1559         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_CTR;
1560         break;
1561     case E_DSCMB_TYPE_AES_ECB:
1562         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_ECB;
1563         break;
1564     case E_DSCMB_TYPE_AES_SCTE52:
1565         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_SCTE52;
1566         break;
1567     case E_DSCMB_TYPE_TDES_ECB:
1568         hal_dscmb_type = E_HAL_DSCMB_TYPE_TDES_ECB;
1569         break;
1570     case E_DSCMB_TYPE_TDES_SCTE52:
1571         hal_dscmb_type = E_HAL_DSCMB_TYPE_TDES_SCTE52;
1572         break;
1573     case E_DSCMB_TYPE_SYN_AES:
1574         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_LEADING_CLR_EBC;
1575         break;
1576     case E_DSCMB_TYPE_CSA3:
1577         hal_dscmb_type = E_HAL_DSCMB_TYPE_CSA3;
1578         break;
1579 #if defined(DSCMB_MULTI2_ENABLE)
1580     case E_DSCMB_TYPE_MULTI2:
1581         hal_dscmb_type = E_HAL_DSCMB_TYPE_MULTI2;
1582         break;
1583 #endif
1584     case E_DSCMB_TYPE_OC:
1585         hal_dscmb_type = E_HAL_DSCMB_TYPE_OC;
1586         break;
1587 
1588     default:
1589         DSCMB_ASSERT_AND_RETURN(0);
1590     }
1591 
1592 #if DSCMB_PARAMETER_TABLE
1593     HAL_DSCMB_FltKeyReset(u32EngId, 0, u32Slot, hal_dscmb_type, hal_dscmb_key_type);
1594 #else
1595     for (i= 0; i< MAX_DSCMB_PIDFLT_NUM; i++)
1596     {
1597         if (U32_MAX != pMap->u32PidFltId[i])
1598         {
1599             HAL_DSCMB_FltKeyReset(u32EngId, pMap->u32PidFltId[i], u32Slot, hal_dscmb_type, hal_dscmb_key_type);
1600         }
1601     }
1602 #endif
1603     return TRUE;
1604 }
1605 
MDrv_DSCMB2_FltKeyReset(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Key_Type eKeyType)1606 MS_BOOL MDrv_DSCMB2_FltKeyReset(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Key_Type eKeyType)
1607 {
1608 #if DSCMB_UTOPIA2
1609     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
1610     DSCMB_FLTKEYRESET pArgs;
1611     memset(&pArgs, 0, sizeof(DSCMB_FLTKEYRESET));
1612     if(pu32DscmbInst == NULL)
1613     {
1614         return FALSE;
1615     }
1616 
1617     pArgs.u32EngId = u32EngId;
1618     pArgs.u32DscmbId = u32DscmbId;
1619     pArgs.eKeyType = eKeyType;
1620     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_FltKeyReset, (void*)&pArgs);
1621 
1622     if(UTOPIA_STATUS_SUCCESS != u32Ret)
1623     {
1624         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
1625         return FALSE;
1626     }
1627 
1628     return TRUE;
1629 #else
1630     return _MDrv_DSCMB2_FltKeyReset(u32EngId, u32DscmbId, eKeyType);
1631 #endif
1632 }
1633 //--------------------------------------------------------------------------------------------------
1634 /// Set descrambler key of the Dscmb ID
1635 /// @param u32DscmbId \b IN: Descrambler filter ID obtained from MDrv_DSCMB_FltAlloc
1636 /// @param eKeyType \b IN: Descrambler key type
1637 /// @param pu8Key \b IN: pointer to the key
1638 /// @return TRUE - Success
1639 /// @return FALSE - Failure
1640 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_FltKeySet(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Key_Type eKeyType,MS_U8 * pu8Key)1641 MS_BOOL _MDrv_DSCMB2_FltKeySet(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Key_Type eKeyType, MS_U8* pu8Key)
1642 {
1643     DSCMB_Key_Type hal_dscmb_key_type;
1644     HAL_DSCMB_Type hal_dscmb_type;
1645     MS_U32 u32Slot;
1646     stDscmbTspMap* pMap;
1647     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
1648     pMap = &DscmbTspMap[u32EngId][u32DscmbId];
1649 
1650     switch (eKeyType)
1651     {
1652 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
1653     case E_DSCMB_KEY_CLEAR:
1654         hal_dscmb_key_type = E_DSCMB_KEY_CLEAR;
1655         u32Slot = pMap->u32SlotIdClear;
1656         break;
1657 #endif
1658     case E_DSCMB_KEY_ODD:
1659         hal_dscmb_key_type = E_DSCMB_KEY_ODD;
1660         u32Slot = pMap->u32SlotIdOdd;
1661         break;
1662     case E_DSCMB_KEY_EVEN:
1663         hal_dscmb_key_type = E_DSCMB_KEY_EVEN;
1664         u32Slot = pMap->u32SlotIdEven;
1665         break;
1666     default:
1667         return FALSE;
1668     }
1669 
1670     if (U32_MAX == u32Slot)
1671     {
1672         return FALSE;
1673     }
1674 
1675     switch (pMap->DscmbType)
1676     {
1677     case E_DSCMB_TYPE_CSA:
1678         hal_dscmb_type = E_HAL_DSCMB_TYPE_CSA;
1679         break;
1680     case E_DSCMB_TYPE_CSA_CONF:
1681         hal_dscmb_type = E_HAL_DSCMB_TYPE_CSA_CONF;
1682         break;
1683     case E_DSCMB_TYPE_NSA_AS_ESA:
1684         hal_dscmb_type = E_HAL_DSCMB_TYPE_NSA_AS_ESA;
1685         break;
1686     case E_DSCMB_TYPE_DES:
1687         hal_dscmb_type = E_HAL_DSCMB_TYPE_DES;
1688         break;
1689     case E_DSCMB_TYPE_AES:
1690         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES;
1691         break;
1692     case E_DSCMB_TYPE_AES_ECB:
1693         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_ECB;
1694         break;
1695     case E_DSCMB_TYPE_AES_SCTE52:
1696         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_SCTE52;
1697         break;
1698 #if (!( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
1699     case E_DSCMB_TYPE_AES_CTR:
1700         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_CTR;
1701         break;
1702 #endif
1703     case E_DSCMB_TYPE_TDES_ECB:
1704         hal_dscmb_type = E_HAL_DSCMB_TYPE_TDES_ECB;
1705         break;
1706     case E_DSCMB_TYPE_TDES_SCTE52:
1707         hal_dscmb_type = E_HAL_DSCMB_TYPE_TDES_SCTE52;
1708         break;
1709     case E_DSCMB_TYPE_SYN_AES:
1710         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_LEADING_CLR_EBC;
1711         break;
1712 #if defined(DSCMB_MULTI2_ENABLE)
1713     case E_DSCMB_TYPE_MULTI2:
1714         hal_dscmb_type = E_HAL_DSCMB_TYPE_MULTI2;
1715         break;
1716 #endif
1717     case E_DSCMB_TYPE_OC:
1718         hal_dscmb_type = E_HAL_DSCMB_TYPE_OC;
1719         break;
1720     case E_DSCMB_TYPE_CSA3:
1721         hal_dscmb_type = E_HAL_DSCMB_TYPE_CSA3;
1722         break;
1723     default:
1724         DSCMB_ASSERT_AND_RETURN(0);
1725     }
1726 
1727 #if DSCMB_PARAMETER_TABLE
1728     if(hal_dscmb_type == E_HAL_DSCMB_TYPE_AES_CTR)
1729     {
1730         MS_U32 u32TmpSlot = (hal_dscmb_key_type == E_DSCMB_KEY_ODD) ? pMap->u32SlotIdEven : pMap->u32SlotIdOdd ;
1731         DSCMB_Key_Type key_type = (hal_dscmb_key_type == E_DSCMB_KEY_ODD) ? E_DSCMB_KEY_EVEN : E_DSCMB_KEY_ODD;
1732         if(FALSE == HAL_DSCMB_FltKeySet(u32EngId, 0, u32TmpSlot, hal_dscmb_type, key_type, pu8Key))
1733             DSCMB_ASSERT_AND_RETURN(0);
1734     }
1735     if(FALSE == HAL_DSCMB_FltKeySet(u32EngId, 0, u32Slot, hal_dscmb_type, hal_dscmb_key_type, pu8Key)) // AES: IV: 0, 1, 2, 3.   CW: 4, 5, 6, 7
1736         DSCMB_ASSERT_AND_RETURN(0);
1737 #else
1738     MS_U32 i;
1739     for (i= 0; i< MAX_DSCMB_PIDFLT_NUM; i++)
1740     {
1741         if (U32_MAX != pMap->u32PidFltId[i])
1742         {
1743 #if (!( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
1744             // for HDCP2, set EVEN/ODD key at the same time
1745             if(hal_dscmb_type == E_HAL_DSCMB_TYPE_AES_CTR)
1746             {
1747                 MS_U32 u32TmpSlot = (hal_dscmb_key_type == E_DSCMB_KEY_ODD) ? pMap->u32SlotIdEven : pMap->u32SlotIdOdd ;
1748                 DSCMB_Key_Type key_type = (hal_dscmb_key_type == E_DSCMB_KEY_ODD) ? E_DSCMB_KEY_EVEN : E_DSCMB_KEY_ODD;
1749                 if(FALSE == HAL_DSCMB_FltKeySet(u32EngId, pMap->u32PidFltId[i], u32TmpSlot, hal_dscmb_type, key_type, pu8Key))
1750                     DSCMB_ASSERT_AND_RETURN(0);
1751             }
1752 #endif
1753             if(FALSE == HAL_DSCMB_FltKeySet(u32EngId, pMap->u32PidFltId[i], u32Slot, hal_dscmb_type, hal_dscmb_key_type, pu8Key)) // AES: IV: 0, 1, 2, 3.   CW: 4, 5, 6, 7
1754                 DSCMB_ASSERT_AND_RETURN(0);
1755         }
1756     }
1757 #endif
1758     return TRUE;
1759 }
1760 
MDrv_DSCMB2_FltKeySet(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Key_Type eKeyType,MS_U8 * pu8Key)1761 MS_BOOL MDrv_DSCMB2_FltKeySet(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Key_Type eKeyType, MS_U8* pu8Key)
1762 {
1763 #if DSCMB_UTOPIA2
1764     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
1765     DSCMB_FLTKEYSET pArgs;
1766     memset(&pArgs, 0, sizeof(DSCMB_FLTKEYSET));
1767     if(pu32DscmbInst == NULL)
1768     {
1769         return FALSE;
1770     }
1771 
1772     pArgs.u32EngId      = u32EngId;
1773     pArgs.u32DscmbId    = u32DscmbId;
1774     pArgs.eKeyType      = eKeyType;
1775     pArgs.pu8Key        = pu8Key;
1776 
1777     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_FltKeySet, (void*)&pArgs);
1778     if(UTOPIA_STATUS_SUCCESS != u32Ret)
1779     {
1780         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
1781         return FALSE;
1782     }
1783 
1784     return TRUE;
1785 #else
1786     return _MDrv_DSCMB2_FltKeySet(u32EngId, u32DscmbId, eKeyType, pu8Key);
1787 #endif
1788 }
1789 
1790 //--------------------------------------------------------------------------------------------------
1791 /// Set descrambler initial vector of the Dscmb ID
1792 /// @param u32DscmbId \b IN: Descrambler filter ID obtained from MDrv_DSCMB_FltAlloc
1793 /// @param eKeyType \b IN: Descrambler key type
1794 /// @param pu8IV \b IN: pointer to the key
1795 /// @return TRUE - Success
1796 /// @return FALSE - Failure
1797 /// @note
1798 /// This API is only valid for descrambler algorithm, E_DSCMB_TYPE_AES
1799 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_FltIVSet(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Key_Type eKeyType,MS_U8 * pu8IV)1800 MS_BOOL _MDrv_DSCMB2_FltIVSet(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Key_Type eKeyType, MS_U8* pu8IV)
1801 {
1802     DSCMB_Key_Type hal_dscmb_key_type;
1803     MS_U32 u32Slot;
1804     stDscmbTspMap* pMap;
1805     HAL_DSCMB_Type DscmbType = (HAL_DSCMB_Type)E_DSCMB_TYPE_AES;
1806     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
1807     pMap = &DscmbTspMap[u32EngId][u32DscmbId];
1808 
1809     switch (eKeyType)
1810     {
1811 #if ( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0)))
1812     case E_DSCMB_KEY_CLEAR:
1813         hal_dscmb_key_type = E_DSCMB_KEY_CLEAR;
1814         u32Slot = pMap->u32SlotIdClear;
1815         break;
1816 #endif
1817     case E_DSCMB_KEY_ODD:
1818         hal_dscmb_key_type = E_DSCMB_KEY_ODD;
1819         u32Slot = pMap->u32SlotIdOdd;
1820         break;
1821     case E_DSCMB_KEY_EVEN:
1822         hal_dscmb_key_type = E_DSCMB_KEY_EVEN;
1823         u32Slot = pMap->u32SlotIdEven;
1824         break;
1825     default:
1826         return FALSE;
1827     }
1828 
1829     if (U32_MAX == u32Slot)
1830     {
1831         return FALSE;
1832     }
1833 
1834     switch (pMap->DscmbType)
1835     {
1836     case E_DSCMB_TYPE_CSA:
1837     case E_DSCMB_TYPE_CSA_CONF:
1838     case E_DSCMB_TYPE_NSA_AS_ESA:
1839     case E_DSCMB_TYPE_DES:
1840     case E_DSCMB_TYPE_AES_ECB:
1841     case E_DSCMB_TYPE_TDES_ECB:
1842     default:
1843         DSCMB_ASSERT_AND_RETURN(0);
1844     case E_DSCMB_TYPE_AES_SCTE52:
1845         DscmbType = (HAL_DSCMB_Type)E_HAL_DSCMB_TYPE_AES_SCTE52;
1846         break;
1847     case E_DSCMB_TYPE_AES:
1848         DscmbType = (HAL_DSCMB_Type)E_HAL_DSCMB_TYPE_AES;
1849         break;
1850 #if defined(DSCMB_MULTI2_ENABLE)
1851     case E_DSCMB_TYPE_MULTI2:
1852         DscmbType = (HAL_DSCMB_Type)E_HAL_DSCMB_TYPE_MULTI2;
1853         break;
1854 #endif
1855     case E_DSCMB_TYPE_TDES_SCTE52:
1856         DscmbType = (HAL_DSCMB_Type)E_HAL_DSCMB_TYPE_TDES_SCTE52;
1857         break;
1858     case E_DSCMB_TYPE_OC:
1859         DscmbType = (HAL_DSCMB_Type)E_HAL_DSCMB_TYPE_OC;
1860         break;
1861 
1862     case E_DSCMB_TYPE_SYN_AES:
1863         break;
1864     }
1865 
1866 #if DSCMB_PARAMETER_TABLE
1867     if(FALSE == HAL_DSCMB_FltIVSet(u32EngId, 0, u32Slot, DscmbType, hal_dscmb_key_type, pu8IV)) // AES: IV: 0, 1, 2, 3.   CW: 4, 5, 6, 7
1868         DSCMB_ASSERT_AND_RETURN(0);
1869 #else
1870     MS_U32 i;
1871     for (i= 0; i< MAX_DSCMB_PIDFLT_NUM; i++)
1872     {
1873         if (U32_MAX != pMap->u32PidFltId[i])
1874         {
1875             if(FALSE == HAL_DSCMB_FltIVSet(u32EngId, pMap->u32PidFltId[i], u32Slot, DscmbType, hal_dscmb_key_type, pu8IV)) // AES: IV: 0, 1, 2, 3.   CW: 4, 5, 6, 7
1876                 DSCMB_ASSERT_AND_RETURN(0);
1877         }
1878     }
1879 #endif
1880     return TRUE;
1881 }
1882 
MDrv_DSCMB2_FltIVSet(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Key_Type eKeyType,MS_U8 * pu8IV)1883 MS_BOOL MDrv_DSCMB2_FltIVSet(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Key_Type eKeyType, MS_U8* pu8IV)
1884 {
1885 #if DSCMB_UTOPIA2
1886     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
1887     DSCMB_FLTIVSET pArgs;
1888     memset(&pArgs, 0, sizeof(DSCMB_FLTIVSET));
1889     if(pu32DscmbInst == NULL)
1890     {
1891         return FALSE;
1892     }
1893 
1894     pArgs.u32EngId      = u32EngId;
1895     pArgs.u32DscmbId    = u32DscmbId;
1896     pArgs.eKeyType      = eKeyType;
1897     pArgs.pu8IV         = pu8IV;
1898 
1899     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_FltIVSet, (void*)&pArgs);
1900     if(UTOPIA_STATUS_SUCCESS != u32Ret)
1901     {
1902         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
1903         return FALSE;
1904     }
1905 
1906     return TRUE;
1907 #else
1908     return _MDrv_DSCMB2_FltIVSet(u32EngId, u32DscmbId, eKeyType, pu8IV);
1909 #endif
1910 }
1911 
1912 //--------------------------------------------------------------------------------------------------
1913 /// Set rounds of descrambler algorithm MULTI2
1914 /// @param u32EngId \b IN: cipher engine index
1915 /// @param u32Round \b IN: # rounds to apply, multiple of 4 and maximum value is 252
1916 /// @return TRUE - Success
1917 /// @return FALSE - Failure
1918 /// @note
1919 /// This API is only valid for descrambler algorithm, E_DSCMB_TYPE_MULTI2
1920 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_Multi2_SetRound(MS_U32 u32EngId,MS_U32 u32Round)1921 MS_BOOL _MDrv_DSCMB2_Multi2_SetRound(MS_U32 u32EngId, MS_U32 u32Round)
1922 {
1923 #if (!( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
1924     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
1925     return HAL_DSCMB_Multi2_SetRound(u32EngId, u32Round);
1926 #else
1927     return FALSE;
1928 #endif
1929 }
1930 
MDrv_DSCMB2_Multi2_SetRound(MS_U32 u32EngId,MS_U32 u32Round)1931 MS_BOOL MDrv_DSCMB2_Multi2_SetRound(MS_U32 u32EngId, MS_U32 u32Round)
1932 {
1933 #if DSCMB_UTOPIA2
1934     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
1935     DSCMB_MULTI2_SETROUND pArgs;
1936     memset(&pArgs, 0, sizeof(DSCMB_MULTI2_SETROUND));
1937     if(pu32DscmbInst == NULL)
1938     {
1939         return FALSE;
1940     }
1941 
1942     pArgs.u32EngId      = u32EngId;
1943     pArgs.u32Round      = u32Round;
1944 
1945     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_Multi2_SetRound, (void*)&pArgs);
1946     if(UTOPIA_STATUS_SUCCESS != u32Ret)
1947     {
1948         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
1949         return FALSE;
1950     }
1951 
1952     return TRUE;
1953 #else
1954     return _MDrv_DSCMB2_Multi2_SetRound(u32EngId, u32Round);
1955 #endif
1956 }
1957 //--------------------------------------------------------------------------------------------------
1958 /// Set system key of descrambler algorithm MULTI2
1959 /// @param u32EngId \b IN: cipher engine index
1960 /// @param u32SysKey \b IN: the array of system key (u8SysKey[32]): there are 32 group system key(8-bit should be 32 group) should be set.
1961 /// @[0-31][32-63][64-95][96-127][128-159][160-191][192-223][224-255] and the bit order is MSB.
1962 /// @return TRUE - Success
1963 /// @return FALSE - Failure
1964 /// @note
1965 /// This API is only valid for descrambler algorithm, E_DSCMB_TYPE_MULTI2
1966 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_Multi2_SetSystemKey(MS_U32 u32EngId,MS_U8 * u8SysKey)1967 MS_BOOL _MDrv_DSCMB2_Multi2_SetSystemKey(MS_U32 u32EngId, MS_U8* u8SysKey)
1968 {
1969 #if (!( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
1970     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
1971     return HAL_DSCMB_Multi2_SetSystemKey(u32EngId, u8SysKey);
1972 #else
1973     return FALSE;
1974 #endif
1975 }
1976 
MDrv_DSCMB2_Multi2_SetSystemKey(MS_U32 u32EngId,MS_U8 * u8SysKey)1977 MS_BOOL MDrv_DSCMB2_Multi2_SetSystemKey(MS_U32 u32EngId, MS_U8* u8SysKey)
1978 {
1979 #if DSCMB_UTOPIA2
1980     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
1981     DSCMB_MULTI2_SETSYSKEY pArgs;
1982     memset(&pArgs, 0, sizeof(DSCMB_MULTI2_SETSYSKEY));
1983     if(pu32DscmbInst == NULL)
1984     {
1985         return FALSE;
1986     }
1987 
1988     pArgs.u32EngId       = u32EngId;
1989     pArgs.pu8SysKey      = u8SysKey;
1990 
1991     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_Multi2_SetSysKey, (void*)&pArgs);
1992     if(UTOPIA_STATUS_SUCCESS != u32Ret)
1993     {
1994         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
1995         return FALSE;
1996     }
1997 
1998     return TRUE;
1999 #else
2000     return _MDrv_DSCMB2_Multi2_SetSystemKey(u32EngId, u8SysKey);
2001 #endif
2002 }
2003 //--------------------------------------------------------------------------------------------------
2004 /// Set Riv of HDCP2, which is 64-bits number generated from transmitter
2005 /// @param u32EngId \b IN: cipher engine index
2006 /// @param pu8RIV \b IN: pointer to 64bit Riv buffer in BIG ENDIAN order
2007 /// @return TRUE - Success
2008 /// @return FALSE - Failure
2009 /// @note
2010 ///
2011 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_HDCP2_SetRIV(MS_U32 u32EngId,MS_U8 * pu8RIV)2012 MS_BOOL _MDrv_DSCMB2_HDCP2_SetRIV(MS_U32 u32EngId, MS_U8* pu8RIV)
2013 {
2014 #if (!( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2015     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
2016     return HAL_DSCMB_HDCP2_SetRiv(u32EngId, pu8RIV);
2017 #else
2018     return FALSE;
2019 #endif
2020 }
2021 
MDrv_DSCMB2_HDCP2_SetRIV_Ex(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_U8 * pu8RIV)2022 MS_BOOL MDrv_DSCMB2_HDCP2_SetRIV_Ex(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_U8* pu8RIV)
2023 {
2024     return MDrv_DSCMB2_HDCP2_SetRIV(u32EngId, pu8RIV);
2025 }
2026 
MDrv_DSCMB2_HDCP2_SetRIV(MS_U32 u32EngId,MS_U8 * pu8RIV)2027 MS_BOOL MDrv_DSCMB2_HDCP2_SetRIV(MS_U32 u32EngId, MS_U8* pu8RIV)
2028 {
2029 #if DSCMB_UTOPIA2
2030     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
2031     DSCMB_HDCP2_SETRIV pArgs;
2032     memset(&pArgs, 0, sizeof(DSCMB_HDCP2_SETRIV));
2033     if(pu32DscmbInst == NULL)
2034     {
2035         return FALSE;
2036     }
2037 
2038     pArgs.u32EngId      = u32EngId;
2039     pArgs.pu8Riv        = pu8RIV;
2040 
2041     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_HDCP2_SetRiv, (void*)&pArgs);
2042     if(UTOPIA_STATUS_SUCCESS != u32Ret)
2043     {
2044         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
2045         return FALSE;
2046     }
2047 
2048     return TRUE;
2049 #else
2050     return _MDrv_DSCMB2_HDCP2_SetRIV(u32EngId, pu8RIV);
2051 #endif
2052 }
2053 
2054 
2055 #if 0
2056 // corrupt
2057 MS_BOOL MDrv_DSCMB_EcmCorruptSet(MS_U8 u8CorruptDataByte, MS_U8 u8FromByte)
2058 {
2059     return HAL_DSCMB_EcmCorruptSet(u8CorruptDataByte, u8FromByte);
2060 }
2061 
2062 MS_BOOL MDrv_DSCMB_EcmCorruptEnable(MS_U32 u32PidIn, MS_BOOL bEnable)
2063 {
2064     MS_U32 i;
2065     MS_U32 u32Pid;
2066 
2067     for (i = 0; i< TSP_MAX_FLTNUM; i++)
2068     {
2069         if ((DRVTSP_OK == MDrv_TSP_PidFlt_GetPid(0, i, &u32Pid)) && (u32Pid == u32PidIn))
2070         {
2071             HAL_DSCMB_EcmCorruptEnable(i, bEnable);
2072         }
2073     }
2074     return FALSE;
2075 }
2076 #endif
2077 
2078 //--------------------------------------------------------------------------------------------------
2079 /// Get scrambler level of a pid filter ID
2080 /// @param u32PidFltId \b IN: Pid filter ID obtained from MApi_DMX_Open/MDrv_TSP_PidFlt_Alloc
2081 /// @param pScmbLevel \b OUT: The output scrambler level
2082 /// @return TRUE - Success
2083 /// @return FALSE - Failure
2084 //--------------------------------------------------------------------------------------------------
_MDrv_DSCMB2_PidFlt_ScmbStatus(MS_U32 u32EngId,MS_U32 u32PidFltId,SCMB_Level * pScmbLevel)2085 MS_BOOL _MDrv_DSCMB2_PidFlt_ScmbStatus(MS_U32 u32EngId, MS_U32 u32PidFltId, SCMB_Level* pScmbLevel)
2086 {
2087     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
2088     HAL_Scmb_Level HalScmbLevel = E_HAL_DSCMB_NONE;
2089     if (FALSE == HAL_DSCMB_PidFlt_ScmbStatus(u32EngId, u32PidFltId, &HalScmbLevel))
2090     {
2091         return FALSE;
2092     }
2093     *pScmbLevel = E_DSCMB_NONE;
2094     if (HalScmbLevel & E_HAL_DSCMB_TS)
2095     {
2096         *pScmbLevel|= E_DSCMB_TS;
2097     }
2098     if (HalScmbLevel & E_HAL_DSCMB_PES)
2099     {
2100         *pScmbLevel|= E_DSCMB_PES;
2101     }
2102     return TRUE;
2103 }
2104 
MDrv_DSCMB2_PidFlt_ScmbStatus(MS_U32 u32EngId,MS_U32 u32PidFltId,SCMB_Level * pScmbLevel)2105 MS_BOOL MDrv_DSCMB2_PidFlt_ScmbStatus(MS_U32 u32EngId, MS_U32 u32PidFltId, SCMB_Level* pScmbLevel)
2106 {
2107 #if DSCMB_UTOPIA2
2108     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
2109     DSCMB_DSCMBSTATUS pArgs;
2110     memset(&pArgs, 0, sizeof(DSCMB_DSCMBSTATUS));
2111     if(pu32DscmbInst == NULL)
2112     {
2113         return FALSE;
2114     }
2115 
2116     pArgs.u32EngId      = u32EngId;
2117     pArgs.u32DmxFltId   = u32PidFltId;
2118     pArgs.pScmbLevel    = pScmbLevel;
2119 
2120     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_PidFlt_ScmbStatus, (void*)&pArgs);
2121     if(UTOPIA_STATUS_SUCCESS != u32Ret)
2122     {
2123         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
2124         return FALSE;
2125     }
2126 
2127     return TRUE;
2128 #else
2129     return _MDrv_DSCMB2_PidFlt_ScmbStatus(u32EngId, u32PidFltId, pScmbLevel);
2130 #endif
2131 }
2132 
2133 //--------------------------------------------------------------------------------------------------
2134 /// Release key ladder key from ACPU
2135 /// @return TRUE - Success
2136 /// @return FALSE - Failure
2137 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_ResetAcpuAck(void)2138 MS_BOOL MDrv_DSCMB_KLadder_ResetAcpuAck(void)
2139 {
2140 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2141 
2142     MS_BOOL  bRet=FALSE;
2143     bRet = HAL_DSCMB_KLadder_Acpu_Ack(TRUE);
2144     bRet |= HAL_DSCMB_KLadder_Acpu_Ack(FALSE);
2145     return bRet;
2146 #else
2147     return FALSE;
2148 #endif
2149 }
2150 
2151 //--------------------------------------------------------------------------------------------------
2152 /// Read key ladder key from ACPU
2153 /// @return TRUE - Success
2154 /// @return FALSE - Failure
2155 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_ReadDataFromAcpu(MS_U8 * pu8Out)2156 MS_BOOL MDrv_DSCMB_KLadder_ReadDataFromAcpu(MS_U8* pu8Out)
2157 {
2158 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2159     return HAL_DSCMB_KLadder_Acpu_Data(pu8Out);
2160 #else
2161     return FALSE;
2162 #endif
2163 }
2164 
2165 //--------------------------------------------------------------------------------------------------
2166 /// Reset key ladder
2167 /// @return TRUE - Success
2168 /// @return FALSE - Failure
2169 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_Reset(void)2170 MS_BOOL MDrv_DSCMB_KLadder_Reset(void)
2171 {
2172 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2173     return HAL_DSCMB_KLadder_Reset();
2174 #else
2175     return FALSE;
2176 #endif
2177 }
2178 
2179 //--------------------------------------------------------------------------------------------------
2180 /// Set key ladder input
2181 /// @param u32Level \b IN: key ladder level
2182 /// @param pu8In \b IN: input of key ladder at u32Level level
2183 /// @param u32InLen \b IN: input length
2184 /// @return TRUE - Success
2185 /// @return FALSE - Failure
2186 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_SetInput(MS_U32 u32Level,MS_U8 * pu8In,MS_U32 u32InLen)2187 MS_BOOL MDrv_DSCMB_KLadder_SetInput(MS_U32 u32Level, MS_U8* pu8In, MS_U32 u32InLen)
2188 {
2189 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2190     return HAL_DSCMB_KLadder_SetInput(u32Level, pu8In, u32InLen);
2191 #else
2192     return FALSE;
2193 #endif
2194 }
2195 
2196 //--------------------------------------------------------------------------------------------------
2197 /// Set key ladder input
2198 /// @param KLSrc \b IN: key ladder key source
2199 /// @param pu8Key \b IN: The target key of key ladder
2200 /// @param u32KeyLen \b IN: the length of the target key of key ladder
2201 /// @return TRUE - Success
2202 /// @return FALSE - Failure
2203 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_SetKey(DSCMB_KLSrc KLSrc,MS_U8 * pu8Key,MS_U32 u32KeyLen)2204 MS_BOOL MDrv_DSCMB_KLadder_SetKey(DSCMB_KLSrc KLSrc, MS_U8* pu8Key, MS_U32 u32KeyLen)
2205 {
2206 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2207 
2208     HAL_DscmbKLadderSrc halKLSrc;
2209 
2210     switch (KLSrc)
2211     {
2212     case E_DSCMB_KL_SRC_ACPU:
2213         halKLSrc = E_HAL_DSCMB_KLAD_SRC_ACPU;
2214         break;
2215     case E_DSCMB_KL_SRC_SECRET_1:
2216         halKLSrc = E_HAL_DSCMB_KLAD_SRC_SECRET_KEY1;
2217         break;
2218     case E_DSCMB_KL_SRC_SECRET_2:
2219         halKLSrc = E_HAL_DSCMB_KLAD_SRC_SECRET_KEY2;
2220         break;
2221     case E_DSCMB_KL_SRC_SECRET_3:
2222         halKLSrc = E_HAL_DSCMB_KLAD_SRC_SECRET_KEY3;
2223         break;
2224     case E_DSCMB_KL_SRC_SECRET_4:
2225         halKLSrc = E_HAL_DSCMB_KLAD_SRC_SECRET_KEY4;
2226         break;
2227     case E_DSCMB_KL_SRC_VGK:
2228         halKLSrc = E_HAL_DSCMB_KLAD_SRC_VGK;
2229         break;
2230 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2231     case E_DSCMB_KL_SRC_PRIVATE_0:
2232         halKLSrc = E_HAL_DSCMB_KLAD_SRC_PRIVATE_KEY0;
2233         break;
2234     case E_DSCMB_KL_SRC_PRIVATE_1:
2235         halKLSrc = E_HAL_DSCMB_KLAD_SRC_PRIVATE_KEY1;
2236         break;
2237     case E_DSCMB_KL_SRC_PRIVATE_2:
2238         halKLSrc = E_HAL_DSCMB_KLAD_SRC_PRIVATE_KEY2;
2239         break;
2240 #endif
2241     default:
2242         DSCMB_ASSERT_AND_RETURN(0);
2243     }
2244     return HAL_DSCMB_KLadder_SetKey(halKLSrc, pu8Key, u32KeyLen);
2245 #else
2246     return FALSE;
2247 #endif
2248 }
2249 
2250 //--------------------------------------------------------------------------------------------------
2251 /// Set key ladder destination
2252 /// @param KLDst \b IN: key ladder key destination
2253 /// @return TRUE - Success
2254 /// @return FALSE - Failure
2255 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_SetDst(DSCMB_KLDst KLDst)2256 MS_BOOL MDrv_DSCMB_KLadder_SetDst(DSCMB_KLDst KLDst)
2257 {
2258 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2259 
2260     HAL_DscmbKLadderDst halKLDst;
2261 
2262     switch (KLDst)
2263     {
2264     case E_DSCMB_KL_DST_KT_ESA:
2265         halKLDst = E_HAL_DSCMB_KLAD_DST_KTAB_ESA;
2266         break;
2267     case E_DSCMB_KL_DST_KT_NSA:
2268         halKLDst = E_HAL_DSCMB_KLAD_DST_KTAB_NSA;
2269         break;
2270     case E_DSCMB_KL_DST_DMA_AES:
2271         halKLDst = E_HAL_DSCMB_KLAD_DST_AESDMA_AES;
2272         break;
2273     case E_DSCMB_KL_DST_DMA_TDES:
2274         halKLDst = E_HAL_DSCMB_KLAD_DST_AESDMA_TDES;
2275         break;
2276     case E_DSCMB_KL_DST_ACPU:
2277         halKLDst = E_HAL_DSCMB_KLAD_DST_ACPU;
2278         break;
2279 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2280     case E_DSCMB_KL_DST_PRIVATE0:
2281         halKLDst = E_HAL_DSCMB_KLAD_DST_PRIVATE0;
2282         break;
2283     case E_DSCMB_KL_DST_PRIVATE1:
2284         halKLDst = E_HAL_DSCMB_KLAD_DST_PRIVATE1;
2285         break;
2286     case E_DSCMB_KL_DST_PRIVATE2:
2287         halKLDst = E_HAL_DSCMB_KLAD_DST_PRIVATE2;
2288         break;
2289 #endif
2290     default:
2291         DSCMB_ASSERT_AND_RETURN(0);
2292     }
2293     return HAL_DSCMB_KLadder_SetDst(halKLDst);
2294 #else
2295     return FALSE;
2296 #endif
2297 }
2298 
2299 //--------------------------------------------------------------------------------------------------
2300 /// Set key ladder configurations
2301 /// @param pConfig \b IN: key ladder configurations
2302 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_SetConfig(DSCMB_KLCfg * pConfig)2303 void MDrv_DSCMB_KLadder_SetConfig(DSCMB_KLCfg* pConfig)
2304 {
2305 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2306 
2307     KLCfgPrev = *pConfig;
2308 
2309     HAL_DSCMB_KLadder_Decrypt(pConfig->bDecrypt);
2310     HAL_DSCMB_KLadder_ByteInv(pConfig->bInverse);
2311     HAL_DSCMB_KLadder_KeepRound(pConfig->bKeepRound);
2312     HAL_DSCMB_KLadder_Round(pConfig->u32Round);
2313     HAL_DSCMB_KLadder_EngMode(pConfig->u32EngMode);
2314 #else
2315     return;
2316 #endif
2317 }
2318 
2319 //--------------------------------------------------------------------------------------------------
2320 /// Get previous set configurations
2321 /// @param pConfig \b OUT: key ladder configurations
2322 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_GetConfig(DSCMB_KLCfg * pConfig)2323 void MDrv_DSCMB_KLadder_GetConfig(DSCMB_KLCfg* pConfig)
2324 {
2325 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2326     *pConfig = KLCfgPrev;
2327 #else
2328     return;
2329 #endif
2330 }
2331 
2332 
2333 //--------------------------------------------------------------------------------------------------
2334 /// Make key ladder start operating
2335 /// @return TRUE - Success
2336 /// @return FALSE - Failure
2337 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_Start(void)2338 MS_BOOL MDrv_DSCMB_KLadder_Start(void)
2339 {
2340 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2341     return HAL_DSCMB_KLadder_Start();
2342 #else
2343     return FALSE;
2344 #endif
2345 }
2346 
2347 //--------------------------------------------------------------------------------------------------
2348 /// Check if key ladder operation is complete
2349 /// @return TRUE - Success
2350 /// @return FALSE - Failure
2351 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_IsComplete(void)2352 MS_BOOL MDrv_DSCMB_KLadder_IsComplete(void)
2353 {
2354 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2355     return HAL_DSCMB_KLadder_IsComplete();
2356 #else
2357     return FALSE;
2358 #endif
2359 }
2360 
2361 //--------------------------------------------------------------------------------------------------
2362 /// Make key ladder stop operating
2363 /// @return TRUE - Success
2364 /// @return FALSE - Failure
2365 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_Stop(void)2366 MS_BOOL MDrv_DSCMB_KLadder_Stop(void)
2367 {
2368 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2369     return HAL_DSCMB_KLadder_Stop();
2370 #else
2371     return FALSE;
2372 #endif
2373 }
2374 
2375 //--------------------------------------------------------------------------------------------------
2376 /// Make key ladder start output
2377 /// @param u32DscmbId \b IN: Descrambler filter ID obtained from MDrv_DSCMB_FltAlloc
2378 /// @param eKeyType \b IN: Descrambler key type
2379 /// @return TRUE - Success
2380 /// @return FALSE - Failure
2381 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_Output_Start(MS_U32 u32DscmbId,DSCMB_Key_Type eKeyType)2382 MS_BOOL MDrv_DSCMB_KLadder_Output_Start(MS_U32 u32DscmbId, DSCMB_Key_Type eKeyType)
2383 {
2384 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2385     DSCMB_ASSERT_AND_RETURN(MAX_NUM > u32DscmbId);
2386     DSCMB_ASSERT_AND_RETURN(TRUE == DscmbTspMap[0][u32DscmbId].bUsed);
2387 
2388     stDscmbTspMap* pMap;
2389     MS_U32 j;
2390     MS_U32 i = 0;
2391     MS_U32 u32Slot;
2392     HAL_DSCMB_Type hal_dscmb_type;
2393     DSCMB_Key_Type hal_dscmb_key_type;
2394 
2395     pMap = &DscmbTspMap[0][u32DscmbId];
2396     // check if there exist a tsp filter connction
2397     // if no connection exist, return FALSE
2398     for (i = 0; i < MAX_DSCMB_PIDFLT_NUM ; i++) {
2399         if(pMap->u32PidFltId[i] != U32_MAX){
2400             break;
2401         }
2402     }
2403     if (i == MAX_DSCMB_PIDFLT_NUM)    return FALSE;
2404 
2405     switch (eKeyType)
2406     {
2407     case E_DSCMB_KEY_CLEAR:
2408         hal_dscmb_key_type = E_DSCMB_KEY_CLEAR;
2409         u32Slot = pMap->u32SlotIdClear;
2410         break;
2411     case E_DSCMB_KEY_ODD:
2412         hal_dscmb_key_type = E_DSCMB_KEY_ODD;
2413         u32Slot = pMap->u32SlotIdOdd;
2414         break;
2415     case E_DSCMB_KEY_EVEN:
2416         hal_dscmb_key_type = E_DSCMB_KEY_EVEN;
2417         u32Slot = pMap->u32SlotIdEven;
2418         break;
2419     default:
2420         return FALSE;
2421     }
2422 
2423     if (U32_MAX == u32Slot)
2424     {
2425         return FALSE;
2426     }
2427 
2428     switch (pMap->DscmbType)
2429     {
2430     case E_DSCMB_TYPE_CSA:
2431         hal_dscmb_type = E_HAL_DSCMB_TYPE_CSA;
2432         break;
2433     case E_DSCMB_TYPE_NSA_AS_ESA:
2434         hal_dscmb_type = E_HAL_DSCMB_TYPE_NSA_AS_ESA;
2435         break;
2436     case E_DSCMB_TYPE_DES:
2437         hal_dscmb_type = E_HAL_DSCMB_TYPE_DES;
2438         break;
2439     case E_DSCMB_TYPE_AES:
2440         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES;
2441         break;
2442     case E_DSCMB_TYPE_AES_ECB:
2443         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_ECB;
2444         break;
2445     case E_DSCMB_TYPE_AES_SCTE52:
2446         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_SCTE52;
2447         break;
2448     case E_DSCMB_TYPE_TDES_ECB:
2449         hal_dscmb_type = E_HAL_DSCMB_TYPE_TDES_ECB;
2450         break;
2451     case E_DSCMB_TYPE_TDES_SCTE52:
2452         hal_dscmb_type = E_HAL_DSCMB_TYPE_TDES_SCTE52;
2453         break;
2454     case E_DSCMB_TYPE_SYN_AES:
2455         hal_dscmb_type = E_HAL_DSCMB_TYPE_AES_LEADING_CLR_EBC;
2456         break;
2457     case E_DSCMB_TYPE_CSA3:
2458         hal_dscmb_type = E_HAL_DSCMB_TYPE_CSA3;
2459         break;
2460     default:
2461         DSCMB_ASSERT_AND_RETURN(0);
2462     }
2463 
2464 
2465     for (j= 0; j< MAX_DSCMB_PIDFLT_NUM; j++)
2466     {
2467         if (U32_MAX!= pMap->u32PidFltId[j])
2468         {
2469             HAL_DSCMB_KLadder_Output_Start(pMap->u32PidFltId[j], u32Slot, hal_dscmb_type, hal_dscmb_key_type);
2470             break;
2471         }
2472     }
2473     return TRUE;
2474 #else
2475     return FALSE;
2476 #endif
2477 }
2478 
2479 //--------------------------------------------------------------------------------------------------
2480 /// Check if key ladder output is complete
2481 /// @return TRUE - Success
2482 /// @return FALSE - Failure
2483 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_Output_IsComplete(void)2484 MS_BOOL MDrv_DSCMB_KLadder_Output_IsComplete(void)
2485 {
2486 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2487     return HAL_DSCMB_KLadder_Output_IsComplete();
2488 #else
2489     return FALSE;
2490 #endif
2491 }
2492 
2493 //--------------------------------------------------------------------------------------------------
2494 /// Make key ladder stop output
2495 /// @return TRUE - Success
2496 /// @return FALSE - Failure
2497 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_Output_Stop(void)2498 MS_BOOL MDrv_DSCMB_KLadder_Output_Stop(void)
2499 {
2500 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2501     return HAL_DSCMB_KLadder_Output_Stop();
2502 #else
2503     return FALSE;
2504 #endif
2505 }
2506 
2507 //--------------------------------------------------------------------------------------------------
2508 /// Get Dscmb driver information
2509 /// @return DSCMB_Info
2510 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_GetInfo(void)2511 const DSCMB_Info* MDrv_DSCMB_GetInfo(void)
2512 {
2513     return (&DscmbInfo);
2514 }
2515 
2516 //--------------------------------------------------------------------------------------------------
2517 /// Get Dscmb driver library information
2518 /// @param ppVersion \b OUT: Descrambler library version infomation
2519 /// @return TRUE - Success
2520 /// @return FALSE - Failure
2521 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_GetLibVer(const MSIF_Version ** ppVersion)2522 MS_BOOL MDrv_DSCMB_GetLibVer(const MSIF_Version **ppVersion)
2523 {
2524     return MDrv_DSCMB2_GetLibVer(ppVersion);
2525 }
2526 
MDrv_DSCMB2_GetLibVer(const MSIF_Version ** ppVersion)2527 MS_BOOL MDrv_DSCMB2_GetLibVer(const MSIF_Version **ppVersion)
2528 {
2529     *ppVersion = &_drv_dscmb_version;
2530     return TRUE;
2531 }
2532 
2533 //--------------------------------------------------------------------------------------------------
2534 /// Get Dscmb driver status
2535 /// @param pStatus \b OUT: Descrambler status
2536 /// @return TRUE - Success
2537 /// @return FALSE - Failure
2538 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_GetStatus(DSCMB_Status * pStatus)2539 MS_BOOL MDrv_DSCMB_GetStatus(DSCMB_Status *pStatus)
2540 {
2541     if (sizeof(DSCMB_Status))
2542     {
2543         memcpy(pStatus, &_DscmbStatus, sizeof(DSCMB_Status));
2544     }
2545     return TRUE;
2546 }
2547 
2548 //--------------------------------------------------------------------------------------------------
2549 /// Set Dscmb driver debug level
2550 /// @param u32Level \b IN: debug level
2551 /// @return TRUE - Success
2552 /// @return FALSE - Failure
2553 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_SetDBGLevel(MS_U32 u32Level)2554 MS_BOOL MDrv_DSCMB_SetDBGLevel(MS_U32 u32Level)
2555 {
2556     return MDrv_DSCMB2_SetDBGLevel(u32Level);
2557 }
2558 
MDrv_DSCMB2_SetDBGLevel(MS_U32 u32Level)2559 MS_BOOL MDrv_DSCMB2_SetDBGLevel(MS_U32 u32Level)
2560 {
2561     _u32DscmbDGBLevel = u32Level;
2562     return TRUE;
2563 }
2564 
2565 //--------------------------------------------------------------------------------------------------
2566 /// Get the status between descramble id and tsp filter id
2567 /// @param u32DscmbId \b IN: descramble id
2568 /// @param u32TspFltId \b IN: tsp filter id
2569 /// @return TRUE - Success
2570 /// @return FALSE - Failure
2571 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_GetConnectStatus(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_U32 u32TspFltId)2572 MS_BOOL MDrv_DSCMB_GetConnectStatus(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_U32 u32TspFltId)
2573 {
2574     return MDrv_DSCMB2_GetConnectStatus(u32EngId, u32DscmbId, u32TspFltId);
2575 }
2576 
MDrv_DSCMB2_GetConnectStatus(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_U32 u32TspFltId)2577 MS_BOOL MDrv_DSCMB2_GetConnectStatus(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_U32 u32TspFltId)
2578 {
2579     DSCMB_ASSERT_AND_RETURN(MAX_NUM > u32DscmbId);
2580     DSCMB_ASSERT_AND_RETURN(ENG_NUM > u32EngId);
2581     DSCMB_ASSERT_AND_RETURN(TRUE == DscmbTspMap[u32EngId][u32DscmbId].bUsed);
2582 
2583     MS_U32 i = 0;
2584     stDscmbTspMap* pMap =  &DscmbTspMap[u32EngId][u32DscmbId];
2585 
2586     for(i = 0 ; i < MAX_DSCMB_PIDFLT_NUM ; i++){
2587         if (pMap->u32PidFltId[i] == u32TspFltId) {
2588             return TRUE;
2589         }
2590     }
2591 
2592     return FALSE;
2593 }
2594 
2595 
2596 
2597 
2598 // Only for scramble
2599 //--------------------------------------------------------------------------------------------------
2600 /// Set the PID to be replaced
2601 /// @param u32PidPrim\b IN: The packet with this target PID(u32PidPrim) will be replaced in the field of PID by u32Pid2ndary
2602 /// @param u32Pid2ndary\b IN: The packet with this target PID(u32PidPrim) will be replaced in the field of PID by u32Pid2ndary
2603 /// @return TRUE - Success
2604 /// @return FALSE - Failure
2605 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_2ndarySet(MS_U32 u32PidPrim,MS_U32 u32Pid2ndary)2606 MS_BOOL MDrv_DSCMB_2ndarySet(MS_U32 u32PidPrim, MS_U32 u32Pid2ndary)
2607 {
2608 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2609     return HAL_DSCMB_2ndarySet(u32PidPrim, u32Pid2ndary);
2610 #else
2611     return FALSE;
2612 #endif
2613 }
2614 
2615 //--------------------------------------------------------------------------------------------------
2616 /// Reset the PID to be replaced
2617 /// @param u32PidPrim\b IN: Reset the replacement of PID (u32PidPrim) set by MDrv_DSCMB_2ndarySet
2618 /// @return TRUE - Success
2619 /// @return FALSE - Failure
2620 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_2ndaryReset(MS_U32 u32PidPrim)2621 MS_BOOL MDrv_DSCMB_2ndaryReset(MS_U32 u32PidPrim)
2622 {
2623 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2624     return HAL_DSCMB_2ndaryReset(u32PidPrim);
2625 #else
2626     return FALSE;
2627 #endif
2628 }
2629 
2630 
MDrv_DSCMB2_SetRecBuf(MS_U32 u32EngId,MS_U32 u32Start,MS_U32 u32Size)2631 MS_BOOL MDrv_DSCMB2_SetRecBuf(MS_U32 u32EngId , MS_U32 u32Start, MS_U32 u32Size)
2632 {
2633 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2634     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
2635     HAL_DSCMB_SetRecStart(u32Start , u32Start + (u32Size/2) );
2636     HAL_DSCMB_SetRecEnd(u32Start+(u32Size/2) , u32Start + u32Size );
2637     HAL_DSCMB_SetRecMid(u32Start , u32Start + (u32Size/2) );
2638 
2639     return TRUE ;
2640 #else
2641     return FALSE;
2642 #endif
2643 }
2644 
MDrv_DSCMB2_RecCtrl(MS_U32 u32EngId,MS_BOOL bEnable)2645 MS_BOOL  MDrv_DSCMB2_RecCtrl(MS_U32 u32EngId, MS_BOOL bEnable)
2646 {
2647 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2648 
2649     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
2650     HAL_DSCMB_RecCtrl( bEnable);
2651     return TRUE ;
2652 #else
2653     return FALSE;
2654 #endif
2655 }
2656 
MDrv_DSCMB2_GetRecWptr(MS_U32 u32EngId,MS_U32 * pu32Write)2657 MS_BOOL  MDrv_DSCMB2_GetRecWptr(MS_U32 u32EngId, MS_U32* pu32Write)
2658 {
2659 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2660 
2661     DSCMB_ASSERT_AND_RETURN(u32EngId < ENG_NUM);
2662     HAL_DSCMB_GetRecWptr( pu32Write);
2663     return TRUE ;
2664 #else
2665     return FALSE;
2666 #endif
2667 }
2668 
2669 
2670 
2671 
2672 //--------------------------------------------------------------------------------------------------
2673 /// Dscmb lock API for supporting multithreading
2674 /// @param u
2675 /// @return TRUE - Success
2676 /// @return FALSE - Failure
2677 //--------------------------------------------------------------------------------------------------
2678 
MDrv_DSCMB_KLadder_Lock(void)2679 MS_BOOL MDrv_DSCMB_KLadder_Lock(void  )
2680 {
2681 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2682     return MsOS_ObtainMutex(_s32DscMutexId, 0xFFFFFFFF);;
2683 #else
2684     return FALSE;
2685 #endif
2686 }
2687 
MDrv_DSCMB_KLadder_Unlock(void)2688 MS_BOOL MDrv_DSCMB_KLadder_Unlock(void)
2689 {
2690 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2691     return MsOS_ReleaseMutex(_s32DscMutexId);;
2692 #else
2693     return FALSE;
2694 #endif
2695 }
2696 
2697 //--------------------------------------------------------------------------------------------------
2698 /// An atomic Key ladder function for Key generation, including common KL use cases.
2699 /// Thread safe for cuncurrent usage.
2700 /// @param KLCfg\b IN: KL execution setting for target key generation
2701 /// @param ACPU_Out\b IN: A 16 bytes array pointer for Acpu key output.
2702 /// @param u32Status\b IN: Key ladder running status
2703 /// @return TRUE - Success
2704 /// @return FALSE - Failure
2705 //--------------------------------------------------------------------------------------------------
MDrv_DSCMB_KLadder_AtomicExec(DSCMB_KLCfg_All * KLCfg,MS_U8 * ACPU_Out,DSCMB_KL_Status * u32Status)2706 MS_BOOL MDrv_DSCMB_KLadder_AtomicExec(DSCMB_KLCfg_All* KLCfg , MS_U8 *ACPU_Out, DSCMB_KL_Status* u32Status )
2707 {
2708 #if (( defined(TSP_VERSION) && ((TSP_VERSION == TSP_VER_2_0) || (TSP_VERSION == TSP_VER_4_0))))
2709 
2710     MS_U32 i , u32Round= 0 , u32StartTime,u32EndTime ;
2711     MS_BOOL bKeepRound=FALSE;
2712     *u32Status = KL_STATUS_KEY_OK ;
2713 
2714     MDrv_DSCMB_KLadder_Lock(  );
2715 
2716     // Check DST and acpu data
2717     if ((E_DSCMB_KL_SRC_ACPU == KLCfg->eSrc) && (NULL == ACPU_Out))
2718     {
2719         *u32Status = KL_STATUS_ACPU_KEY_NULL;
2720         goto KL_Fail;
2721     }
2722 
2723     // Calculate KL round //
2724     if ((E_DSCMB_KL_64_BITS == KLCfg->eOutsize) && (E_DSCMB_KL_TDES == KLCfg->eAlgo))
2725     {
2726         u32Round = KLCfg->u32Level * 2 - 1;
2727 
2728         //[NOTE] special case for level 0 KL //
2729         if( KLCfg->u32Level == 0 ) u32Round = 0 ;
2730     }
2731     else if ((E_DSCMB_KL_128_BITS == KLCfg->eOutsize) && (E_DSCMB_KL_TDES == KLCfg->eAlgo))
2732     {
2733         u32Round = KLCfg->u32Level * 2 ;
2734     }
2735     else if ((E_DSCMB_KL_128_BITS == KLCfg->eOutsize) && (E_DSCMB_KL_AES == KLCfg->eAlgo))
2736     {
2737         u32Round = KLCfg->u32Level * 2 ;
2738     }
2739     else
2740     {
2741         *u32Status = KL_STATUS_INVALID_KEYROUND_PARAMETER;
2742         goto KL_Fail;
2743     }
2744 
2745     // Check KL level //
2746     if(KLCfg->u32Level >3)
2747     {
2748         //bKeepRound = TRUE ;
2749         *u32Status = KL_STATUS_INVALID_INPUT_LEVEL;
2750         goto KL_Fail;
2751     }
2752     else if(( 0 == KLCfg->u32Level ) && (E_DSCMB_KL_DST_DMA_AES != KLCfg->eDst) && (E_DSCMB_KL_DST_DMA_TDES != KLCfg->eDst))
2753     { // for AESDMA, it's possible to run KL with level 0 //
2754         *u32Status = KL_STATUS_INVALID_INPUT_LEVEL;
2755         goto KL_Fail;
2756     }
2757 
2758     // check Key destination //
2759     if ((E_DSCMB_KL_DST_ACPU == KLCfg->eDst) && (NULL == ACPU_Out))
2760     {
2761         *u32Status = KL_STATUS_ACPU_OUTKEY_NULL;
2762         goto KL_Fail;
2763     }
2764     else if ((E_DSCMB_KL_DST_KT_ESA== KLCfg->eDst) || (E_DSCMB_KL_DST_KT_NSA == KLCfg->eDst))
2765     {
2766         if ( FALSE == DscmbTspMap[KLCfg->u32EngID][KLCfg->u32DscID].bUsed )
2767         {
2768              *u32Status = KL_STATUS_IVALID_DSCMB_ID;
2769              goto KL_Fail;
2770         }
2771         if( 0 == DscmbTspMap[KLCfg->u32EngID][KLCfg->u32DscID].u32PidFltIdNum )
2772         {
2773              *u32Status = KL_STATUS_IVALID_DSCMB_ID;
2774              goto KL_Fail;
2775         }
2776 
2777     }
2778 
2779     if ((KLCfg->u32Level != 0) && (NULL == KLCfg->pu8KeyKLIn))
2780     {
2781              *u32Status = KL_STATUS_KL_INPUT_NULL;
2782              goto KL_Fail;
2783     }
2784 
2785     //ULOGD("DSCMB", " Round = %d , u32Status = %x \n",u32Round,*u32Status);
2786     // KL Reset and Reset ACPU Ack //
2787     HAL_DSCMB_KLadder_Reset();
2788     MDrv_DSCMB_KLadder_ResetAcpuAck();
2789 
2790     // Set KL config //
2791     HAL_DSCMB_KLadder_Decrypt(KLCfg->bDecrypt);
2792     HAL_DSCMB_KLadder_ByteInv(KLCfg->bInverse);
2793     HAL_DSCMB_KLadder_KeepRound(bKeepRound);
2794     HAL_DSCMB_KLadder_EngMode(KLCfg->eAlgo);
2795     HAL_DSCMB_KLadder_Round(u32Round);
2796 
2797 
2798     // Set KL Root Key //
2799     MDrv_DSCMB_KLadder_SetKey(KLCfg->eSrc,KLCfg->u8KeyACPU, 16  );
2800 
2801     // Set KL output Destination //
2802     MDrv_DSCMB_KLadder_SetDst(KLCfg->eDst);
2803 
2804     // Set KL Level input //
2805     for( i = 0 ; i < KLCfg->u32Level ; i ++ )
2806     {
2807         HAL_DSCMB_KLadder_SetInput(2-i, (MS_U8*)&(KLCfg->pu8KeyKLIn[i*16]), 16);
2808 
2809     #if 0
2810         ULOGD("DSCMB", "KL input Key[0-7] = %02X %02X %02X %02X %02X %02X %02X %02X\n",
2811             KLCfg->pu8KeyKLIn[16*i ],KLCfg->pu8KeyKLIn[16*i+1],KLCfg->pu8KeyKLIn[16*i+2],KLCfg->pu8KeyKLIn[i*16+3],
2812             KLCfg->pu8KeyKLIn[16*i+4],KLCfg->pu8KeyKLIn[i*16+5],KLCfg->pu8KeyKLIn[i*16+6],KLCfg->pu8KeyKLIn[i*16+7]);
2813         ULOGD("DSCMB", "KL input Key[8-15] = %02X %02X %02X %02X %02X %02X %02X %02X\n",
2814             KLCfg->pu8KeyKLIn[16*i +8],KLCfg->pu8KeyKLIn[16*i+9],KLCfg->pu8KeyKLIn[16*i+10],KLCfg->pu8KeyKLIn[i*16+11],
2815             KLCfg->pu8KeyKLIn[16*i+12],KLCfg->pu8KeyKLIn[i*16+13],KLCfg->pu8KeyKLIn[i*16+14],KLCfg->pu8KeyKLIn[i*16+15]);
2816     #endif
2817     }
2818 
2819     MDrv_DSCMB_KLadder_ResetAcpuAck();
2820 
2821     // KL start //
2822     u32StartTime = MsOS_GetSystemTime();
2823     MDrv_DSCMB_KLadder_Start();
2824 
2825     if (E_DSCMB_KL_DST_ACPU == KLCfg->eDst)
2826     {// KL flow for ACPU output //
2827 
2828         memset(ACPU_Out , 0x0 , 16 );
2829 
2830         while (!MDrv_DSCMB_KLadder_IsComplete())
2831         {
2832             u32EndTime = MsOS_GetSystemTime();
2833             if ((u32EndTime - u32StartTime) > DSCMB_KL_WAIT_THRESHOLD)
2834             {
2835                 *u32Status = KL_STATUS_WAIT_CW_READY_TIMEOUT;
2836                 goto KL_Fail;
2837             }
2838         };
2839         MDrv_DSCMB_KLadder_ReadDataFromAcpu(ACPU_Out);
2840         MDrv_DSCMB_KLadder_ResetAcpuAck();
2841     }
2842     else if ((E_DSCMB_KL_DST_DMA_AES == KLCfg->eDst) || (E_DSCMB_KL_DST_DMA_TDES == KLCfg->eDst))
2843     {// KL flow for output  to CryptoDMA  //
2844        while(!MDrv_DSCMB_KLadder_Output_IsComplete())
2845        {
2846             u32EndTime = MsOS_GetSystemTime();
2847             if ((u32EndTime - u32StartTime) > DSCMB_KL_WAIT_THRESHOLD)
2848             {
2849                 *u32Status = KL_STATUS_WAIT_KL_READY_TIMEOUT;
2850                 goto KL_Fail;
2851             }
2852        };
2853        //MDrv_DSCMB_KLadder_Stop();
2854     }
2855     else if ((E_DSCMB_KL_DST_KT_ESA== KLCfg->eDst) || (E_DSCMB_KL_DST_KT_NSA == KLCfg->eDst))
2856     {// KL flow for output  to KTE //
2857         while (!MDrv_DSCMB_KLadder_IsComplete())
2858         {
2859             u32EndTime = MsOS_GetSystemTime();
2860             if ((u32EndTime - u32StartTime) > DSCMB_KL_WAIT_THRESHOLD)
2861             {
2862                 *u32Status = KL_STATUS_WAIT_CW_READY_TIMEOUT;
2863                 goto KL_Fail;
2864             }
2865         };
2866         u32StartTime = MsOS_GetSystemTime();
2867 
2868         if(!MDrv_DSCMB_KLadder_Output_Start(KLCfg->u32DscID, KLCfg->eKeyType))
2869         {
2870              goto KL_Fail;
2871         }
2872 
2873         while(!MDrv_DSCMB_KLadder_Output_IsComplete())
2874         {
2875             u32EndTime = MsOS_GetSystemTime();
2876             if ((u32EndTime - u32StartTime) > DSCMB_KL_WAIT_THRESHOLD)
2877             {
2878                 *u32Status = KL_STATUS_WAIT_KL_READY_TIMEOUT;
2879                 goto KL_Fail;
2880             }
2881         };
2882         MDrv_DSCMB_KLadder_Output_Stop();
2883         //MDrv_DSCMB_KLadder_Stop();
2884 
2885     }
2886 
2887     if ( HAL_DSCMB_KLadder_Status() != 0 )
2888     {
2889         goto KL_Fail;
2890     }
2891 
2892     MDrv_DSCMB_KLadder_Stop();
2893 
2894     MDrv_DSCMB_KLadder_Unlock();
2895     return TRUE;
2896 
2897 KL_Fail:
2898 
2899     i = HAL_DSCMB_KLadder_Status();
2900     if ( i != 0 )
2901     {
2902         //ULOGD("DSCMB", "KL Satus = %x \n",i );
2903         *u32Status |= i ;
2904     }
2905 
2906     MDrv_DSCMB_KLadder_Stop();
2907 
2908     MDrv_DSCMB_KLadder_Unlock();
2909     //ULOGE("DSCMB", "[%s][%d]2. DSCMB KL Fail = %08lX \n",__FUNCTION__,__LINE__,*u32Status );
2910     DSCMB_ASSERT_AND_RETURN(FALSE);
2911 
2912 #else
2913     return FALSE;
2914 #endif
2915 }
2916 
MDrv_DSCMB_Suspend(void)2917 MS_BOOL MDrv_DSCMB_Suspend(void)
2918 {
2919     stDscmbTspMap* pMap;
2920     MS_U32 i;
2921     MS_U32 u32Cnt;
2922     MS_U32 u32EngId;
2923     MS_U32 u32DscmbId;
2924 
2925     if(DscmbTspMap == NULL)
2926         return FALSE;
2927 
2928     for(u32EngId=0; u32EngId<ENG_NUM; u32EngId++)
2929     {
2930         for(u32DscmbId=0; u32DscmbId<MAX_NUM; u32DscmbId++)
2931         {
2932             pMap = &DscmbTspMap[u32EngId][u32DscmbId];
2933 
2934             if(pMap->bUsed == FALSE)
2935                 continue;
2936 
2937             u32Cnt = pMap->u32PidFltIdNum;
2938 
2939             for (i = 0; i < MAX_DSCMB_PIDFLT_NUM; i++)
2940             {
2941                 if(u32Cnt == 0) break;
2942                 if (pMap->u32PidFltId[i] != U32_MAX)
2943                 {
2944                     if (FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_EVEN, pMap->u32SlotIdEven, pMap->u32PidFltId[i]))
2945                     {
2946                         DSCMB_ASSERT_AND_RETURN(0);
2947                         return FALSE;
2948                     }
2949                     if (FALSE == HAL_DSCMB_SlotDisconnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_ODD, pMap->u32SlotIdOdd, pMap->u32PidFltId[i]))
2950                     {
2951                         DSCMB_ASSERT_AND_RETURN(0);
2952                         return FALSE;
2953                     }
2954                     u32Cnt--;
2955                 }
2956             }
2957         }
2958     }
2959     return TRUE;
2960 }
MDrv_DSCMB_Resume(void)2961 MS_BOOL MDrv_DSCMB_Resume(void)
2962 {
2963     stDscmbTspMap* pMap;
2964     MS_U32 i;
2965     MS_U32 u32Cnt;
2966     MS_U32 u32EngId;
2967     MS_U32 u32DscmbId;
2968 
2969     if(DscmbTspMap == NULL)
2970         return FALSE;
2971 
2972     HAL_DSCMB_Init();
2973 
2974     for(u32EngId=0; u32EngId<ENG_NUM; u32EngId++)
2975     {
2976         for(u32DscmbId=0; u32DscmbId<MAX_NUM; u32DscmbId++)
2977         {
2978             pMap = &DscmbTspMap[u32EngId][u32DscmbId];
2979 
2980             if(pMap->bUsed == FALSE)
2981                 continue;
2982 
2983             u32Cnt = pMap->u32PidFltIdNum;
2984 
2985             for (i = 0; i < MAX_DSCMB_PIDFLT_NUM; i++)
2986             {
2987                 if(u32Cnt == 0) break;
2988                 if (pMap->u32PidFltId[i] != U32_MAX)
2989                 {
2990                     if (FALSE == HAL_DSCMB_SlotConnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_EVEN, pMap->u32SlotIdEven, pMap->u32PidFltId[i]))
2991                     {
2992                         DSCMB_ASSERT_AND_RETURN(0);
2993                         return FALSE;
2994                     }
2995                     if (FALSE == HAL_DSCMB_SlotConnectPidFltId(u32EngId, pMap->u32DscmbId, E_DSCMB_KEY_ODD, pMap->u32SlotIdOdd, pMap->u32PidFltId[i]))
2996                     {
2997                         DSCMB_ASSERT_AND_RETURN(0);
2998                         return FALSE;
2999                     }
3000                     u32Cnt--;
3001                 }
3002             }
3003         }
3004     }
3005     return TRUE;
3006 }
3007 
3008 
3009 //-------------------------------------------------------------------------------------------------
3010 /// STD general function
3011 /// @param u16PowerState              \b IN: DSCMB power state
3012 /// @return TRUE - Success
3013 /// @return FALSE - Failure
3014 /// @note
3015 //-------------------------------------------------------------------------------------------------
MApi_DSCMB_SetPowerState(EN_POWER_MODE u16PowerState)3016 MS_U32 MApi_DSCMB_SetPowerState(EN_POWER_MODE u16PowerState)
3017 {
3018     return MDrv_DSCMB2_SetPowerState(u16PowerState);
3019 }
3020 
MDrv_DSCMB2_SetPowerState(EN_POWER_MODE u16PowerState)3021 MS_U32 MDrv_DSCMB2_SetPowerState(EN_POWER_MODE u16PowerState)
3022 {
3023 #ifdef DSCMB_UTOPIA2
3024     MS_U32 u32Ret = UTOPIA_STATUS_SUCCESS;
3025     EN_POWER_MODE pArgs = u16PowerState;
3026     if(pu32DscmbInst == NULL)
3027     {
3028         #ifdef MSOS_TYPE_LINUX_KERNEL
3029         if(UTOPIA_STATUS_FAIL == UtopiaOpen(MODULE_DSCMB | KERNEL_MODE, (void**)&pu32DscmbInst, 0x0, NULL))
3030         #else
3031         if(UTOPIA_STATUS_FAIL == UtopiaOpen(MODULE_DSCMB, (void**)&pu32DscmbInst, 0x0, NULL))
3032         #endif
3033         {
3034             ULOGE("DSCMB", "UtopiaOpen DSCMB fail\n");
3035             return FALSE;
3036         }
3037     }
3038 
3039     u32Ret = UtopiaIoctl(pu32DscmbInst, E_MDRV_CMD_DSCMB_SetPowerState, (void*)&pArgs);
3040     if(UTOPIA_STATUS_SUCCESS != u32Ret)
3041     {
3042         ULOGE("DSCMB", "[%s]ERROR : UtopiaIoctl return value 0x%x\n", __FUNCTION__, (unsigned int)u32Ret);
3043         return FALSE;
3044     }
3045 
3046     return TRUE;
3047 #else
3048     return _MDrv_DSCMB2_SetPowerState(u16PowerState);
3049 #endif
3050 }
3051 
_MDrv_DSCMB2_SetPowerState(EN_POWER_MODE u16PowerState)3052 MS_U32 _MDrv_DSCMB2_SetPowerState(EN_POWER_MODE u16PowerState)
3053 {
3054     MS_U16 _ret = FALSE;
3055 
3056     switch (u16PowerState)
3057     {
3058         case E_POWER_SUSPEND:
3059             _ret = MDrv_DSCMB_Suspend();
3060             break;
3061 
3062         case E_POWER_RESUME:
3063             _ret = MDrv_DSCMB_Resume();
3064             break;
3065 
3066         case E_POWER_MECHANICAL:
3067         case E_POWER_SOFT_OFF:
3068         default:
3069             ULOGE("DSCMB", "[%s] %d Power state not support!!\n", __FUNCTION__, __LINE__);
3070             break;
3071     }
3072 
3073     return ((TRUE == _ret)? UTOPIA_STATUS_SUCCESS: UTOPIA_STATUS_FAIL);
3074 }
3075 
MDrv_DSCMB2_SetDefaultCAVid(MS_U32 u32EngId,MS_U32 u32CAVid)3076 MS_BOOL MDrv_DSCMB2_SetDefaultCAVid(MS_U32 u32EngId, MS_U32 u32CAVid)
3077 {
3078     return FALSE;
3079 }
3080 
MDrv_DSCMB2_EngSetAlgo(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Eng_Type eEngType,DSCMB_Algo_Cfg stConfig)3081 MS_BOOL MDrv_DSCMB2_EngSetAlgo(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Eng_Type eEngType, DSCMB_Algo_Cfg stConfig)
3082 {
3083     return FALSE;
3084 }
3085 
MDrv_DSCMB2_EngSetKey(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Eng_Type eEngType,DSCMB_Key_Type eKeyType,MS_U8 * pu8Key)3086 MS_BOOL MDrv_DSCMB2_EngSetKey(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Eng_Type eEngType, DSCMB_Key_Type eKeyType, MS_U8* pu8Key)
3087 {
3088     return FALSE;
3089 }
3090 
MDrv_DSCMB2_EngResetKey(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Eng_Type eEngType,DSCMB_Key_Type eKeyType)3091 MS_BOOL MDrv_DSCMB2_EngResetKey(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Eng_Type eEngType, DSCMB_Key_Type eKeyType)
3092 {
3093     return FALSE;
3094 }
3095 
MDrv_DSCMB2_EngSetIV(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Key_Type eKeyType,MS_U8 * pu8IV)3096 MS_BOOL MDrv_DSCMB2_EngSetIV(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Key_Type eKeyType, MS_U8* pu8IV)
3097 {
3098     return FALSE;
3099 }
3100 
MDrv_DSCMB2_EngSetSwitch(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Eng_Type eUppSwitch,DSCMB_Eng_Type eLowSwitch)3101 MS_BOOL MDrv_DSCMB2_EngSetSwitch(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Eng_Type eUppSwitch, DSCMB_Eng_Type eLowSwitch)
3102 {
3103     return FALSE;
3104 }
3105 
MDrv_DSCMB2_EngSetFSCB(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_FSCB eForceSCB)3106 MS_BOOL MDrv_DSCMB2_EngSetFSCB(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_FSCB eForceSCB )
3107 {
3108     return FALSE;
3109 }
3110 
MDrv_DSCMB2_KLadder_ETSI(DSCMB_KLCfg_All * KLCfg,MS_U8 * ACPU_Out,MS_U8 * pu8Nonce,MS_U8 * pu8Response,DSCMB_KL_Status * u32Status)3111 MS_BOOL MDrv_DSCMB2_KLadder_ETSI(DSCMB_KLCfg_All* KLCfg , MS_U8 *ACPU_Out, MS_U8 *pu8Nonce, MS_U8 * pu8Response, DSCMB_KL_Status* u32Status )
3112 {
3113     return FALSE;
3114 }
3115 
MDrv_DSCMB2_KLadder_AtomicExec(DSCMB_KLCfg_All * KLCfg,MS_U8 * ACPU_Out,DSCMB_KL_Status * u32Status)3116 MS_BOOL MDrv_DSCMB2_KLadder_AtomicExec(DSCMB_KLCfg_All* KLCfg , MS_U8 *ACPU_Out, DSCMB_KL_Status* u32Status )
3117 {
3118     return FALSE;
3119 }
3120 
MDrv_DSCMB2_FltSwitchType(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Flt_Type eFltType)3121 MS_BOOL MDrv_DSCMB2_FltSwitchType(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Flt_Type eFltType)
3122 {
3123     return FALSE;
3124 }
3125 
MDrv_DSCMB2_EngSetKeyFSCB(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Eng_Type eEngType,DSCMB_Key_Type eKeyType,DSCMB_FSCB eForceSCB)3126 MS_BOOL MDrv_DSCMB2_EngSetKeyFSCB(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Eng_Type eEngType, DSCMB_Key_Type eKeyType, DSCMB_FSCB eForceSCB)
3127 {
3128     return FALSE;
3129 }
3130 
MDrv_DSCMB2_EngSetRIV(MS_U32 u32EngId,MS_U32 u32DscmbId,MS_U8 * pu8RIV)3131 MS_BOOL MDrv_DSCMB2_EngSetRIV(MS_U32 u32EngId, MS_U32 u32DscmbId, MS_U8* pu8RIV)
3132 {
3133     return FALSE;
3134 }
3135 
MDrv_DSCMB2_EngEnableKey(MS_U32 u32EngId,MS_U32 u32DscmbId,DSCMB_Eng_Type eEngType,DSCMB_Key_Type eKeyType,MS_BOOL bEnable)3136 MS_BOOL MDrv_DSCMB2_EngEnableKey(MS_U32 u32EngId, MS_U32 u32DscmbId, DSCMB_Eng_Type eEngType, DSCMB_Key_Type eKeyType, MS_BOOL bEnable)
3137 {
3138     return FALSE;
3139 }
3140