xref: /utopia/UTPA2-700.0.x/modules/dscmb/drv/nds/nds_cam.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94 
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file    drvNDS_CAM.c
98 /// @brief  NDS CAM Driver Interface
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 //-------------------------------------------------------------------------------------------------
103 //  Include Files
104 //-------------------------------------------------------------------------------------------------
105 #include "MsCommon.h"
106 #ifdef MSOS_TYPE_LINUX
107 #include "string.h"
108 #endif
109 
110 #include "drvDSCMB.h"
111 
112 #include "regNDS.h"
113 #include "drvNDS.h"
114 #include "halNDS.h"
115 
116 //#include "../rasp/halRASP.h"
117 
118 #include "../dscmb/drvDSCMB.h" //FIXME: move up or embedded
119 
120 #include "ddiNDS_HDI.h"
121 
122 #include "nds.h"
123 
124 
125 //-------------------------------------------------------------------------------------------------
126 //  Driver Compiler Options
127 //-------------------------------------------------------------------------------------------------
128 #define CAM_MAX_DSCMB_NUM           6
129 #define CAM_MAX_CONNECTION          2
130 
131 
132 //-------------------------------------------------------------------------------------------------
133 //  Local Defines
134 //-------------------------------------------------------------------------------------------------
135 
136 
137 //-------------------------------------------------------------------------------------------------
138 //  Local Structurs
139 //-------------------------------------------------------------------------------------------------
140 typedef struct
141 {
142     MS_U32                          u32DscmbId;
143     MS_U16                          u16Pid;
144     MS_U8                           u8Status;
145 } CAM_Dscmb;
146 
147 
148 //-------------------------------------------------------------------------------------------------
149 //  Global Variables
150 //-------------------------------------------------------------------------------------------------
151 
152 
153 //-------------------------------------------------------------------------------------------------
154 //  Local Variables
155 //-------------------------------------------------------------------------------------------------
156 
157 static CAM_Dscmb                    _Dscmb[CAM_MAX_CONNECTION][CAM_MAX_DSCMB_NUM];
158 static MS_U8                        _u8DscmbType[CAM_MAX_CONNECTION];
159 
160 #define DSCMB_TYPE_NULL             0xFF
161 
162 
163 
164 //static MS_U32 u32ChipPropLen = 0;
165 
166 static MS_U8 u8ChipType[] = // chip type descriptor
167 {
168     CHIP_TYPE_DSCR_TAG, 4,
169     ((NDS_CHIP_TYPE >> 24) & 0xFF), ((NDS_CHIP_TYPE >> 16) & 0xFF), ((NDS_CHIP_TYPE >> 8) & 0xFF), ((NDS_CHIP_TYPE >> 0) & 0xFF),
170 };
171 
172 static MS_U8 u8ChipId[] =  // chip id descriptor
173 {
174     CHIP_ID_DSCR_TAG, 4,
175     ((NDS_CHIP_ID >> 24) & 0xFF), ((NDS_CHIP_ID >> 16) & 0xFF), ((NDS_CHIP_ID >> 8) & 0xFF), ((NDS_CHIP_ID >> 0) & 0xFF), // default
176 };
177 
178 static MS_U8 _u8DefaultKey[] =
179 {
180     0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87, 0x78, 0x69, 0x5A, 0x4B, 0x3C, 0x2D, 0x1E, 0x0F
181 };
182 
183 static MS_U8 u8HashKey[] =  // Hash of Key descriptor
184 {
185     HASH_KEY_DSCR_TAG, 16,
186     NDS_HASH_KEY,
187 //    0xec, 0x04, 0x6d, 0x48, 0x0f, 0xc4, 0xd2, 0x81, 0x32, 0x3e, 0x1f, 0x5d, 0xbe, 0xed, 0x3f, 0x8b, // Hash of Key descriptor
188 };
189 
190 static MS_U8 u8KLDepth[] =  // Hash of Key descriptor
191 {
192     KEY_LADDER_DEPTH_DSCR_TAG, 1,
193     NDS_KL_DEPTH, // key lader depth descriptor
194 };
195 
196 static MS_U8 u8ChipEnFlags[] =  // Chip Enabled Flag Descriptor
197 {
198     ENABLE_FLAGS_DSCR_TAG, 1,
199     NDS_ENFLAGS_DEFAULT,
200 };
201 
202 
203 
204 #define NDS_CHIP_PROP_LENGTH        (sizeof(u8ChipType)+ sizeof(u8ChipId)+ sizeof(u8HashKey)+ sizeof(u8KLDepth)+ sizeof(u8ChipEnFlags))
205 // static MS_U8 u8ChipProp[NDS_CHIP_PROP_LENGTH];
206 
207 
208 //-------------------------------------------------------------------------------------------------
209 //  Debug Functions
210 //-------------------------------------------------------------------------------------------------
211 
212 
213 //-------------------------------------------------------------------------------------------------
214 //  Local Functions
215 //-------------------------------------------------------------------------------------------------
216 /*
217 unsigned long GetXConnection(void)
218 {
219     return 0;
220 }
221 */
222 
_NDS_CamMapInit(CAM_Dscmb * pDscmb)223 static MS_BOOL _NDS_CamMapInit(CAM_Dscmb* pDscmb)
224 {
225     pDscmb->u32DscmbId = DRV_DSCMB_FLT_NULL;
226     pDscmb->u16Pid = 0x1FFF;
227     pDscmb->u8Status = 0;
228     return TRUE;
229 }
230 
231 
232 //-------------------------------------------------------------------------------------------------
233 //  Global Functions
234 //-------------------------------------------------------------------------------------------------
235 
NDS_CAM_Init(void)236 NDS_Result NDS_CAM_Init(void)
237 {
238     MS_U32 i, j;
239 
240     for (i = 0; i < CAM_MAX_CONNECTION; i++)
241     {
242         for (j = 0; j < CAM_MAX_DSCMB_NUM; j++)
243         {
244             _NDS_CamMapInit(&_Dscmb[i][j]);
245         }
246     }
247 
248     MDrv_DSCMB_KLadder_SetInput(1, _u8DefaultKey, 16);
249     MDrv_DSCMB_KLadder_SetInput(2, _u8DefaultKey, 16);
250 
251 //    _NDS_SC_Ctrl_Reg |= CAM_UART_CTRL_AUTO_FLOWCTRL;//0x14;
252 //    CAM_REG(REG_CAM_UART_CTRL) = _NDS_SC_Ctrl_Reg;
253 
254     return E_NDS_OK;
255 }
256 
257 
NDS_CAM_Exit(void)258 NDS_Result NDS_CAM_Exit(void)
259 {
260     return E_NDS_OK;
261 }
262 
263 
264 // CA module hardware indeitifcation --------------------------------------------------------------
265 
266 // 5.2
XHDICA_GetLogicalStreamId(int request_type,NDS_ULONG x_connection,NDS_BYTE * hw_module_id)267 NDS_STATUS XHDICA_GetLogicalStreamId (int request_type,
268                                       NDS_ULONG x_connection,
269                                       NDS_BYTE *hw_module_id)
270 {
271     NDS_FUNC("[%s][%d] \n", __FUNCTION__, __LINE__);
272 
273     if ((request_type < HDICA_GET_EMM_STREAM) || (request_type > HDICA_GET_DESCRAMBLER_STREAM))
274     {
275         return CA_REQUEST_NOT_SUPPORTED_BY_DRIVER;
276     }
277 
278     hw_module_id = 0;
279     return CA_OK;
280 }
281 
282 
283 // Smart card driver ------------------------------------------------------------------------------
284 
285 // nds_sc.c
286 
287 // 6.3.1
HDICA_SetCardId(NDS_ULONG card_id)288 NDS_STATUS HDICA_SetCardId(NDS_ULONG card_id)
289 {
290     NDS_FUNC("[%s][%d]--card_id[0x%lx] \n", __FUNCTION__, __LINE__, card_id);
291 
292     CAM_REG(REG_CAM_CA_CARDID_0) = 0xFF & card_id;
293     CAM_REG(REG_CAM_CA_CARDID_1) = 0xFF & (card_id>>8);
294     CAM_REG(REG_CAM_CA_CARDID_2) = 0xFF & (card_id>>16);
295     CAM_REG(REG_CAM_CA_CARDID_3) = 0xFF & (card_id>>24);
296 
297     return CA_OK;
298 }
299 
300 
301 // 6.3.2
HDICA_SetIrdId(NDS_ULONG ird_id)302 NDS_STATUS HDICA_SetIrdId(NDS_ULONG ird_id)
303 {
304     NDS_FUNC("[%s][%d]--ird_id[0x%lx] \n", __FUNCTION__, __LINE__, ird_id);
305 
306     CAM_REG(REG_CAM_CA_IRDID_0) = 0xFF & ird_id;
307     CAM_REG(REG_CAM_CA_IRDID_1) = 0xFF & (ird_id>>8);
308     CAM_REG(REG_CAM_CA_IRDID_2) = 0xFF & (ird_id>>16);
309     CAM_REG(REG_CAM_CA_IRDID_3) = 0xFF & (ird_id>>24);
310 
311     return CA_OK;
312 }
313 
314 // ECM filtering and reception --------------------------------------------------------------------
315 
316 // nds_flt.c
317 
318 // 8.2.6
XHDICA_SetCaValue(NDS_ULONG x_connection,NDS_BYTE ca_value)319 NDS_STATUS XHDICA_SetCaValue( NDS_ULONG  x_connection, NDS_BYTE ca_value )
320 {
321     return (HDICA_SetCaValue(ca_value));
322 }
323 
324 
325 // 8.2.7
HDICA_SetCaValue(NDS_BYTE ca_value)326 NDS_STATUS HDICA_SetCaValue (NDS_BYTE ca_value)
327 {
328     NDS_FUNC("[%s]-[%d] ca_value[0x%x]\n", __FUNCTION__, __LINE__, ca_value);
329 
330     //printf("[%s]-[%d] ca_value[0x%x]\n", __FUNCTION__, __LINE__, ca_value);
331 
332     CAM_REG(REG_CAM_CA_SBOX) = ca_value;
333     return CA_OK;
334 }
335 
336 
XHDICA_DefineDescramblerType(NDS_ULONG x_connection,NDS_BYTE descrambler_type)337 NDS_STATUS XHDICA_DefineDescramblerType(NDS_ULONG x_connection, NDS_BYTE descrambler_type)
338 {
339     NDS_FUNC("[%s]-[%d] descrambler_type[0x%x]\n", __FUNCTION__, __LINE__, descrambler_type);
340 
341     switch (descrambler_type)
342     {
343         case CSA:
344         case NSA:
345         case DES:
346         case AES:
347             break;
348     default:
349         descrambler_type = DSCMB_TYPE_NULL;
350         break;
351     }
352     _u8DscmbType[x_connection] = descrambler_type;
353     return CA_OK;
354 }
355 
XHDICA_DefineDescramblerEs(NDS_ULONG x_connection,NDS_BYTE descrambler_num,NDS_USHORT pid,NDS_BYTE filter_control)356 NDS_STATUS XHDICA_DefineDescramblerEs(NDS_ULONG x_connection,
357                                       NDS_BYTE descrambler_num,
358                                       NDS_USHORT pid,
359                                       NDS_BYTE filter_control)
360 {
361     CAM_Dscmb* pDscmb = NULL;
362     NDS_STATUS ret = CA_OK;
363     DSCMB_Type DscmbType;
364 
365     NDS_FUNC("[%s]-[%d] descrambler_num[%d] pid[0x%x] con[0x%x]\n", __FUNCTION__, __LINE__, descrambler_num, pid, filter_control);
366 //    printf("[%s]-[%d] descrambler_num[%d] pid[0x%x] con[0x%x]\n", __FUNCTION__, __LINE__, descrambler_num, pid, filter_control);
367 
368     if (pid < 2 || pid > 0x1FFE)
369     {
370         NDS_DBG("[%s]-[%d]\n", __FUNCTION__, __LINE__);
371         return CA_REQUEST_NOT_SUPPORTED_BY_DRIVER;
372     }
373 
374     if (CAM_MAX_DSCMB_NUM <= descrambler_num)
375     {
376         NDS_DBG("[%s]-[%d]\n", __FUNCTION__, __LINE__);
377         return CA_REQUEST_NOT_SUPPORTED_BY_DRIVER;
378     }
379 
380     // HDICA_DESCRAMBLE_PES_PACKET_LEVEL
381     if (HDICA_DESCRAMBLE_PES_PACKET_LEVEL & filter_control)
382     {
383         ret = CA_REQUEST_NOT_SUPPORTED_BY_DRIVER;
384         NDS_ASSERT(FALSE, ret, "NDS_ASSERT [%s]-[%d]\n", __FUNCTION__, __LINE__);
385         goto XHDICA_DefineDescramblerEs_exit;
386     }
387 
388     pDscmb = &(_Dscmb[x_connection][descrambler_num]);
389     if (!(HDICA_DESCRAMBLER_CONTROL_ENABLE_DESCRAMBLING & filter_control))
390     {
391         if (pDscmb->u32DscmbId != DRV_DSCMB_FLT_NULL)
392         {
393 //            MS_U8        CWFF[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
394 
395             MDrv_DSCMB_FltKeyReset(pDscmb->u32DscmbId, E_DSCMB_KEY_CLEAR);
396             MDrv_DSCMB_FltKeyReset(pDscmb->u32DscmbId, E_DSCMB_KEY_EVEN);
397             MDrv_DSCMB_FltKeyReset(pDscmb->u32DscmbId, E_DSCMB_KEY_ODD);
398             MDrv_DSCMB_FltDisconnectPid(pDscmb->u32DscmbId, pid);
399             MDrv_DSCMB_FltFree(pDscmb->u32DscmbId);
400             _NDS_CamMapInit(pDscmb);
401         }
402         goto XHDICA_DefineDescramblerEs_exit;
403     }
404 
405 
406     switch (_u8DscmbType[x_connection])
407     {
408     case CSA:
409         DscmbType = E_DSCMB_TYPE_CSA;
410         break;
411     case NSA:
412         DscmbType = E_DSCMB_TYPE_NSA_AS_ESA;
413         break;
414     case DES:
415         DscmbType = E_DSCMB_TYPE_DES;
416         break;
417     case AES:
418         DscmbType = E_DSCMB_TYPE_SYN_AES;
419         break;
420     default:
421         ret = CA_REQUEST_NOT_SUPPORTED_BY_DRIVER;
422         NDS_ASSERT(FALSE, ret, "NDS_ASSERT [%s]-[%d]\n", __FUNCTION__, __LINE__);
423         goto XHDICA_DefineDescramblerEs_exit;
424     }
425     // HDICA_DESCRAMBLER_CONTROL_ENABLE_DESCRAMBLING || HDICA_DESCRAMBLE_TRANSPORT_PACKET_LEVEL
426     if ((DRV_DSCMB_FLT_NULL == pDscmb->u32DscmbId) && (DRV_DSCMB_FLT_NULL == (pDscmb->u32DscmbId = MDrv_DSCMB_FltAlloc())))
427     {
428         ret = CA_GLOBAL_HARDWARE_FAILURE;
429         NDS_ASSERT(FALSE, ret, "NDS_ASSERT [%s]-[%d]\n", __FUNCTION__, __LINE__);
430         goto XHDICA_DefineDescramblerEs_exit;
431     }
432     MDrv_DSCMB_FltConnectPid(pDscmb->u32DscmbId, pid);
433     MDrv_DSCMB_FltTypeSet(pDscmb->u32DscmbId, DscmbType);
434     pDscmb->u16Pid = (MS_U16)pid;
435     pDscmb->u8Status |= (MS_U8)filter_control;
436 
437 XHDICA_DefineDescramblerEs_exit:
438     return ret;
439 }
440 
441 // Legacy interface
XHDICA_SetEvenKey(NDS_ULONG x_connection,NDS_BYTE descrambler_num,NDS_BYTE * key_ptr)442 NDS_STATUS XHDICA_SetEvenKey(NDS_ULONG x_connection, NDS_BYTE descrambler_num, NDS_BYTE * key_ptr)
443 {
444     CAM_Dscmb* pDscmb = NULL;
445 
446     NDS_FUNC("[%s]-[%d] \n", __FUNCTION__, __LINE__);
447 
448     if (CAM_MAX_DSCMB_NUM <= descrambler_num)
449     {
450         NDS_DBG("[%s]-[%d]\n", __FUNCTION__, __LINE__);
451         return CA_REQUEST_NOT_SUPPORTED_BY_DRIVER;
452     }
453 
454     pDscmb = &(_Dscmb[x_connection][descrambler_num]);
455     if (pDscmb->u32DscmbId == DRV_DSCMB_FLT_NULL)
456     {
457         return CA_INFO_NOT_AVAILABLE;
458     }
459     MDrv_DSCMB_FltKeySet(pDscmb->u32DscmbId, E_DSCMB_KEY_EVEN, key_ptr);
460     return CA_OK;
461 }
462 
463 // Legacy interface
XHDICA_SetOddKey(NDS_ULONG x_connection,NDS_BYTE descrambler_num,NDS_BYTE * key_ptr)464 NDS_STATUS XHDICA_SetOddKey(NDS_ULONG x_connection, NDS_BYTE descrambler_num, NDS_BYTE * key_ptr)
465 {
466     CAM_Dscmb* pDscmb = NULL;
467 
468     NDS_FUNC("[%s]-[%d] \n", __FUNCTION__, __LINE__);
469 
470     if (CAM_MAX_DSCMB_NUM <= descrambler_num)
471     {
472         NDS_DBG("[%s]-[%d]\n", __FUNCTION__, __LINE__);
473         return CA_REQUEST_NOT_SUPPORTED_BY_DRIVER;
474     }
475 
476     pDscmb = &(_Dscmb[x_connection][descrambler_num]);
477     if (pDscmb->u32DscmbId == DRV_DSCMB_FLT_NULL)
478     {
479         return CA_INFO_NOT_AVAILABLE;
480     }
481     MDrv_DSCMB_FltKeySet(pDscmb->u32DscmbId, E_DSCMB_KEY_ODD, key_ptr);
482     return CA_OK;
483 }
484 
XHDICA_GetSecurityChipNumber(NDS_BYTE * scn)485 NDS_STATUS XHDICA_GetSecurityChipNumber (NDS_BYTE *scn)
486 {
487     NDS_FUNC("[%s]-[%d] \n", __FUNCTION__, __LINE__);
488 
489     *scn = 0xFF;
490     return CA_OK;
491 }
492 
493 #if 0 //[FIXME] if enable Rasp
494 NDS_STATUS XHDICA_ECMCorruptionSettings(NDS_BYTE CorruptDataByte, NDS_BYTE FromByte)
495 {
496     NDS_FUNC("[%s]-[%d] \n", __FUNCTION__, __LINE__);
497 
498     MS_U32 EngId =  0;
499 
500     //[FIXME] Not Xconection ?
501     HAL_RASP_SetCorptData(EngId , FromByte , 187 , CorruptDataByte);
502 
503 //    MDrv_DSCMB_EcmCorruptSet((MS_U8)CorruptDataByte, (MS_U8)FromByte);
504     return CA_FEATURE_NOT_SUPPORTED;
505 
506 }
507 
508 
509 
510 NDS_STATUS XHDICA_EnableECMCorruption(NDS_ULONG x_connection, NDS_USHORT pid, NDS_BYTE enable_corruption)
511 {
512     // @FIXME: complete _NDS_EMM_ECM_Flt_CorruptEnable implementation later
513 
514     MS_U32 EngId = 0, i ;
515     MS_U16 FltPid = 0  ;
516     MS_BOOL enable ;
517 
518 
519     NDS_FUNC("[%s]-[%d] \n", __FUNCTION__, __LINE__);
520 
521     if (enable_corruption == 1)
522        enable = TRUE ;
523     else
524        enable = FALSE ;
525 
526 
527     for( i = 0 ; i < RASP_EVENT_NUM ; i++)
528     {
529         HAL_RASP_GetPidflt(EngId, i , &FltPid);
530         if (FltPid == pid) break ;
531     }
532     if (RASP_EVENT_NUM  == i) return CA_DRIVER_CAN_NOT_PERFORM_FUNCTION_NOW ;
533 
534     HAL_RASP_SetCorptFlt(EngId,i,enable);
535 
536 
537     return CA_OK;
538 }
539 #endif
540 
541 
XHDICA_SecondaryScramblingKey(NDS_ULONG x_connection,NDS_BYTE operation,NDS_BYTE * key_ptr)542 NDS_STATUS XHDICA_SecondaryScramblingKey(NDS_ULONG x_connection, NDS_BYTE operation, NDS_BYTE * key_ptr)
543 {
544 // nsk packet
545     // @FIXME: Richard don't know how to deal with secondary key
546     //TODO:
547     NDS_ASSERT(FALSE, CA_FEATURE_NOT_SUPPORTED, "NDS_ASSERT [%s]-[%d]\n", __FUNCTION__, __LINE__);
548 
549     if (operation == CA_OP_BASE_KEY)
550     {}
551     else if (operation == CA_OP_WRITING)
552     {}
553     else
554     {}
555 
556     //TODO:
557     return CA_FEATURE_NOT_SUPPORTED;
558 }
559 
560 
_NDS_CamGetHashKey(MS_U8 * pu8KeyData)561 static void _NDS_CamGetHashKey(MS_U8 *pu8KeyData)
562 {
563     DSCMB_KLCfg KLConfig;
564 
565     NDS_DBG("[%s]-[%d] \n", __FUNCTION__, __LINE__);
566 
567     KLConfig.bDecrypt = TRUE;
568     KLConfig.bInverse = FALSE;
569     KLConfig.bKeepRound = FALSE;
570     KLConfig.u32Round = 6;
571     KLConfig.u32EngMode = E_DSCMB_KL_TDES; // @FIXME: only TDES HASH key?
572 
573     MDrv_DSCMB_KLadder_SetConfig(&KLConfig);
574     MDrv_DSCMB_KLadder_SetKey(E_DSCMB_KL_SRC_VGK, NULL, 0);
575     MDrv_DSCMB_KLadder_SetDst(E_DSCMB_KL_DST_ACPU);
576 
577     MDrv_DSCMB_KLadder_SetInput(0, _u8DefaultKey, 16);
578     MDrv_DSCMB_KLadder_SetInput(1, _u8DefaultKey, 16);
579     MDrv_DSCMB_KLadder_SetInput(2, _u8DefaultKey, 16);
580 
581     MDrv_DSCMB_KLadder_Start();
582 
583     // (3) polling status
584     while (1)
585     {
586         if (MDrv_DSCMB_KLadder_IsComplete())
587         {
588             break;
589         }
590     }
591 
592     //Read key
593     MDrv_DSCMB_KLadder_ReadDataFromAcpu(pu8KeyData);
594     //_verSC_PrintData(pu8KeyData, 18);
595 
596     //ACK --release key
597     MDrv_DSCMB_KLadder_ResetAcpuAck();
598 
599     // (5) Poll key ladder done
600     while (1)
601     {
602         if (MDrv_DSCMB_KLadder_Output_IsComplete())
603         // if (kl_done == ((reg_kl_status & 0x1)))
604         {
605             break;
606         }
607     }
608 
609     MDrv_DSCMB_KLadder_Output_Stop();
610 
611     // (6) Clear key ladder control
612     MDrv_DSCMB_KLadder_Stop();
613 }
614 
XHDICA_GetChipProperties(NDS_ULONG hw_module_id,unsigned int chip_prop_length,unsigned int * actual_chip_prop_length,NDS_BYTE * chip_properties,void * auxiliary_data)615 NDS_STATUS XHDICA_GetChipProperties(NDS_ULONG hw_module_id,
616                                                                 unsigned int chip_prop_length,
617                                                                 unsigned int *actual_chip_prop_length,
618                                                                 NDS_BYTE *chip_properties,
619                                                                 void *auxiliary_data)
620 {
621     NDS_FUNC("[%s]-[%d] \n", __FUNCTION__, __LINE__);
622 
623     MS_U8* u8Ptr;
624     // @FIXME: return no data for capacity query at this stage
625     //TODO:
626     *actual_chip_prop_length = NDS_CHIP_PROP_LENGTH;
627     if (0 == chip_prop_length)
628     {
629         return CA_OK;
630     }
631     NDS_ASSERT((chip_prop_length>= NDS_CHIP_PROP_LENGTH), CA_INSUFFICIENT_BUFFER, "NDS_ASSERT [%s]-[%d]\n", __FUNCTION__, __LINE__);
632 
633     u8Ptr = chip_properties;
634     memcpy(u8Ptr, u8ChipType, sizeof(u8ChipType));
635     u8Ptr += sizeof(u8ChipType);
636 
637     // otp_cfg_did2
638     u8ChipId[2] = (CFG_REG(REG_CFG_DID2_H) >> 8) & 0xFF;
639     u8ChipId[3] = (CFG_REG(REG_CFG_DID2_H) >> 0) & 0xFF;
640     u8ChipId[4] = (CFG_REG(REG_CFG_DID2_L) >> 8) & 0xFF;
641     u8ChipId[5] = (CFG_REG(REG_CFG_DID2_L) >> 0) & 0xFF;
642     memcpy(u8Ptr, u8ChipId, sizeof(u8ChipId));
643     u8Ptr += sizeof(u8ChipId);
644 
645     _NDS_CamGetHashKey(&u8HashKey[2]);
646     memcpy(u8Ptr, u8HashKey, sizeof(u8HashKey));
647     u8Ptr += sizeof(u8HashKey);
648 
649     memcpy(u8Ptr, u8KLDepth, sizeof(u8KLDepth));
650     u8Ptr += sizeof(u8KLDepth);
651 
652     //(0<< 3) // Jtag password enabled , 0xBF221400+0x11[10-15]
653     //(0<< 4) // Boot Room enabled, 0xBF221400+0x0E[4-9] ==> 0x3F0
654     if ( (CFG_REG(REG_CFG_MODE) & CFG_MODE_JTAG_PWD) && !(CFG_REG(REG_CFG_MODE) & CFG_MODE_JTAG_CLOSE) )
655     {
656         u8ChipEnFlags[2] |= NDS_ENFLAGS_JTAG_PWD;
657     }
658 /*    if (((*((volatile unsigned int*)( 0xBF221400+(0x11<< 2))) >> 10) > 0) && ((*((volatile unsigned int*)( 0xBF221400+(0x11<< 2))) >> 10) <= 0x07))
659         u8DscrEnableFlags[2] |= (1<<3);
660 */
661     if (IS_CFG_BOOT_OTP())
662     {
663         u8ChipEnFlags[2] |= NDS_ENFLAGS_BOOTROM;
664     }
665 /*    if ((*((volatile unsigned int*)( 0xBF221400+(0x0E<< 2))) >> 4) > 0x3F)
666         u8ChipEnFlags[2] |= (1<<4);
667 */
668     memcpy(u8Ptr, u8ChipEnFlags, sizeof(u8ChipEnFlags));
669     u8Ptr += sizeof(u8ChipEnFlags);
670     return CA_OK;
671 }
672 
673 typedef struct
674 {
675     MS_U8* pu8Key;
676     MS_U8 u8Level;
677     MS_U8 u8KeyLen;
678 } _EncyptKeyDscr;
679 
_XHDICA_GetKeyLength(DSCMB_Type DscmbType)680 static MS_U32 _XHDICA_GetKeyLength(DSCMB_Type DscmbType)
681 {
682     switch (DscmbType)
683     {
684     case E_DSCMB_TYPE_CSA:
685         return 8;
686     case E_DSCMB_TYPE_NSA_AS_ESA:
687         return 16;
688     case E_DSCMB_TYPE_DES:
689         return 8;
690     case E_DSCMB_TYPE_AES:
691         return 16;
692     default:
693         return 0;
694     }
695     return 0;
696 }
697 
698 
XHDICA_SetControlWord(NDS_ULONG x_connection,NDS_USHORT descrambler_num,unsigned int control_data_length,NDS_BYTE * control_data,unsigned int polarity_control)699 NDS_STATUS XHDICA_SetControlWord( NDS_ULONG x_connection,
700                                   NDS_USHORT descrambler_num,
701                                   unsigned int control_data_length,
702                                   NDS_BYTE *control_data,
703                                   unsigned int polarity_control )
704 {
705     // New interface
706     CAM_Dscmb *         pDscmb = NULL;
707     MS_U32              u32Len;
708     DSCMB_Key_Type      DscmbKeyType;
709     MS_BOOL             bEncyptedCW = FALSE;
710     MS_U32              u32KLScheme  = E_DSCMB_KL_TDES;
711     _EncyptKeyDscr      enKeyDscr[4];
712     MS_U32              nKeyDscr = 0;
713     DSCMB_KLCfg         KLConfig;
714     MS_U32              u32CWLength;
715     MS_U8 *             pu8EnCW = NULL;
716     MS_U32              u32EnCWLen = 0;
717     MS_U32              u32Round;
718 
719     NDS_FUNC("[%s]-[%d] \n", __FUNCTION__, __LINE__);
720 
721     pDscmb = &(_Dscmb[x_connection][descrambler_num]);
722     if (pDscmb->u32DscmbId == DRV_DSCMB_FLT_NULL)
723     {
724         return CA_INFO_NOT_AVAILABLE;
725     }
726 
727     u32CWLength = _XHDICA_GetKeyLength((DSCMB_Type)_u8DscmbType[x_connection]);
728     switch (polarity_control)
729     {
730     case POLARITY_CLEAR:
731         DscmbKeyType = E_DSCMB_KEY_CLEAR;
732         break;
733     case POLARITY_ODD:
734         DscmbKeyType = E_DSCMB_KEY_ODD;
735         break;
736     case POLARITY_EVEN:
737         DscmbKeyType = E_DSCMB_KEY_EVEN;
738         break;
739     default:
740         return CA_REQUEST_NOT_SUPPORTED_BY_DRIVER;
741     }
742 
743     nKeyDscr = 0;
744     while (control_data_length)
745     {
746         u32Len = (control_data[1] + 2);
747         switch (control_data[0]) // tag
748         {
749         case CLEAR_CW_DSCR_TAG: // 0x01
750             NDS_ASSERT((u32CWLength == control_data[1]), CA_REQUEST_NOT_SUPPORTED_BY_DRIVER, "NDS_ASSERT [%s]-[%d]\n", __FUNCTION__, __LINE__);
751             //if (descrambler_num == 0)
752                 //_verSC_PrintData(&(control_data[2]), 8);
753             MDrv_DSCMB_FltKeySet(pDscmb->u32DscmbId, DscmbKeyType, &(control_data[2]));
754             break;
755         case ENCRYPTED_CW_DSCR_TAG: // 0x02
756             bEncyptedCW = TRUE;
757             pu8EnCW = &(control_data[2]);
758             u32EnCWLen = control_data[1];
759 //            NDS_ASSERT((u32KeyLength == control_data[1]), CA_REQUEST_NOT_SUPPORTED_BY_DRIVER, "NDS_ASSERT [%s]-[%d]\n", __FUNCTION__, __LINE__);
760             MDrv_DSCMB_KLadder_SetKey(E_DSCMB_KL_SRC_VGK, NULL, 0);//just ACPU need set key
761             MDrv_DSCMB_KLadder_SetInput(0, pu8EnCW, u32EnCWLen);
762             break;
763         case ENCRYPTED_KEY_DSCR_TAG: // 0x03
764             bEncyptedCW = TRUE;
765             enKeyDscr[nKeyDscr].u8Level = control_data[2];
766             //MS_ASSERT(1== enKeyDscr[nKeyDscr].u8Level); // must be level 1,2
767             enKeyDscr[nKeyDscr].u8KeyLen = control_data[3];
768             //MS_ASSERT(16 == enKeyDscr[nKeyDscr].u8KeyLen); // must be 128, 64 bits
769             enKeyDscr[nKeyDscr].pu8Key = &(control_data[4]);
770             MDrv_DSCMB_KLadder_SetInput(enKeyDscr[nKeyDscr].u8Level, enKeyDscr[nKeyDscr].pu8Key, enKeyDscr[nKeyDscr].u8KeyLen);
771             nKeyDscr++;
772             break;
773         case ENCRYPTION_SCHEME_DSCR_TAG: // 0x04
774             switch ((control_data[2]<< 8) | control_data[3])
775             {
776 //#if NDS_KL_SCHEME_TDES_SUPPORTED
777             case 0x0002:
778                 u32KLScheme = E_DSCMB_KL_TDES;
779                 break;
780 //#endif
781 #if NDS_KL_SCHEME_AES_SUPPORTED
782             case 0x0010:
783                 u32KLScheme = E_DSCMB_KL_AES;
784                 break;
785 #endif
786             default:
787                 u32KLScheme = E_DSCMB_KL_TDES;
788                 return CA_SCHEME_NOT_SUPPORTED;
789             }
790 
791             break;
792         default:
793             printf("[%s][%d] Unsupported tag 0x%02x\n", __FUNCTION__, __LINE__, control_data[0]);
794             break;
795         }
796         if (control_data_length >= u32Len)
797         {
798             control_data_length -= u32Len;
799             control_data += u32Len;
800         }
801         else
802         {
803             NDS_ASSERT(FALSE, CA_REQUEST_NOT_SUPPORTED_BY_DRIVER, "NDS_ASSERT [%s]-[%d]\n", __FUNCTION__, __LINE__);
804             break;
805         }
806     }
807 
808     if (bEncyptedCW)
809     {
810         //MS_ASSERT(u32EnCWLen == u32KeyLength); // Given CW length must be
811         if (8 == u32CWLength) // CSA
812         {
813             u32Round = 5;
814         }
815         else if (16 == u32CWLength) // NSA, Syn_AES
816         {
817             u32Round = 6;
818         }
819         else
820         {
821             NDS_ASSERT(FALSE, CA_REQUEST_NOT_SUPPORTED_BY_DRIVER, "NDS_ASSERT [%s]-[%d]\n", __FUNCTION__, __LINE__);
822             return CA_REQUEST_NOT_SUPPORTED_BY_DRIVER;
823         }
824 
825         /// (1) put key
826 //        MDrv_DSCMB_KLadder_SetKey(E_DSCMB_KLAD_SRC_VGK, NULL, 0);
827 
828         /// (1.5) put input
829         /// Level 0
830     // reg_dcw_in_l = 0x3c, 0x2d, 0x1e, 0x0f;
831         // reg_dcw_in_h = 0x78, 0x69, 0x5a, 0x4b;;
832         // reg_dcw1_in_l = 0xb4, 0xa5, 0x96, 0x87;
833         // reg_dcw1_in_h = 0xf0, 0xe1, 0xd2, 0xc3;
834 //MDrv_DSCMB_KLadder_SetInput(1, _u8NDS_DefaultIn, 16);//spec form 082.pdf for initaliataion(1~2)
835 //MDrv_DSCMB_KLadder_SetInput(2, _u8NDS_DefaultIn, 16);
836 
837         /// Level 1 for encryption key
838         // reg_dcw2_in = enKeyDscr[0].pu8Key;
839         // reg_dcw3_in = enKeyDscr[0].pu8Key;
840 //        MDrv_DSCMB_KLadder_SetInput(1, enKeyDscr[0].pu8Key, 16);
841 
842         // Level 2 for encryption CW
843         // reg_dcw2_in = pu8EnCW;
844         // reg_dcw3_in = pu8EnCW;
845 //        MDrv_DSCMB_KLadder_SetInput(2, pu8EnCW, 16);
846 
847         // (2) key ladder set up
848         KLConfig.bDecrypt = TRUE;
849         KLConfig.bInverse = FALSE;
850         KLConfig.bKeepRound = FALSE;
851         KLConfig.u32Round = (MS_U32) u32Round;
852         KLConfig.u32EngMode = u32KLScheme;
853 
854         MDrv_DSCMB_KLadder_SetConfig(&KLConfig);
855         MDrv_DSCMB_KLadder_SetDst(E_DSCMB_KL_DST_KT_ESA);
856 #if 0 // TEST
857         vgk_test[16] = { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
858                          0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF  };
859         MDrv_DSCMB_KLadder_SetKey(E_DSCMB_KL_SRC_ACPU, vgk_test, 16);
860         MDrv_DSCMB_KLadder_SetDst(E_DSCMB_KL_DST_ACPU);
861 #endif
862 
863         MDrv_DSCMB_KLadder_Start();
864 
865         // (3) polling status
866         while (1)
867         {
868             if (MDrv_DSCMB_KLadder_IsComplete())
869             {
870                 break;
871             }
872         }
873 
874         MDrv_DSCMB_KLadder_Output_Start(pDscmb->u32DscmbId, DscmbKeyType);
875 
876         // (5) Poll key ladder done
877         while (1)
878         {
879             if (MDrv_DSCMB_KLadder_Output_IsComplete())
880             // if (kl_done == ((reg_kl_status & 0x1)))
881             {
882                 break;
883             }
884         }
885         MDrv_DSCMB_KLadder_Output_Stop();
886 
887         // (6) Clear key ladder control
888         MDrv_DSCMB_KLadder_Stop();
889     }
890     return CA_OK;
891 }
892 
893 
XHDICA_GetSerializationInfo(unsigned int info_type,unsigned int serializaion_data_len,unsigned int * actual_serialization_data_len,NDS_BYTE * serialization_data)894 NDS_STATUS XHDICA_GetSerializationInfo(unsigned int info_type,
895                                                             unsigned int serializaion_data_len,
896                                                             unsigned int *actual_serialization_data_len,
897                                                             NDS_BYTE *serialization_data)
898 {
899     NDS_FUNC("[%s]-[%d] \n", __FUNCTION__, __LINE__);
900 
901     *actual_serialization_data_len = 0;
902     return CA_INFO_NOT_AVAILABLE;
903 }
904 
XHDICA_GetResponseToChallenge(NDS_BYTE * nonce,NDS_BYTE nonce_length,int descrambling_key_length,NDS_BYTE * descrambling_key_descrip,NDS_BYTE * response,NDS_BYTE * response_length)905 NDS_STATUS XHDICA_GetResponseToChallenge( NDS_BYTE *nonce,
906                                           NDS_BYTE nonce_length,
907                                           int descrambling_key_length,
908                                           NDS_BYTE *descrambling_key_descrip,
909                                           NDS_BYTE *response,
910                                           NDS_BYTE *response_length)
911 {
912     DSCMB_KLCfg         KLConfig;
913     MS_U32              u32KLScheme;
914     MS_U32              u32Len=0;
915 
916     MDrv_DSCMB_KLadder_SetInput(0, nonce, nonce_length);//CW
917     MDrv_DSCMB_KLadder_SetInput(1, _u8DefaultKey, 16);
918     MDrv_DSCMB_KLadder_SetInput(2, _u8DefaultKey, 16);
919 
920     NDS_FUNC("[%s]-[%d] \n", __FUNCTION__, __LINE__);
921     while (descrambling_key_length)
922     {
923         u32Len = (descrambling_key_descrip[1] + 2);
924         switch (descrambling_key_descrip[0]) // tag
925         {
926         case CLEAR_CW_DSCR_TAG:
927             break;
928         case ENCRYPTED_CW_DSCR_TAG:
929             break;
930         case ENCRYPTED_KEY_DSCR_TAG:
931             MDrv_DSCMB_KLadder_SetInput((MS_U32)descrambling_key_descrip[2],
932                                         &descrambling_key_descrip[4],
933                                         (MS_U32)descrambling_key_descrip[3]);
934             break;
935         case ENCRYPTION_SCHEME_DSCR_TAG:
936             switch ((descrambling_key_descrip[2]<< 8) | descrambling_key_descrip[3])
937             {
938 //#if NDS_KL_SCHEME_TDES_SUPPORTED
939             case 0x0002:
940                 u32KLScheme = E_DSCMB_KL_TDES;
941                 break;
942 //#endif
943 #if NDS_KL_SCHEME_AES_SUPPORTED
944             case 0x0010:
945                 u32KLScheme = E_DSCMB_KL_AES;
946                 break;
947 #endif
948             default:
949                 return CA_SCHEME_NOT_SUPPORTED;
950             }
951             break;
952         default:
953             printf("[%s][%d] Unsupported tag 0x%02x\n", __FUNCTION__, __LINE__, descrambling_key_descrip[0]);
954             break;
955         }
956         if (descrambling_key_length >= u32Len)
957         {
958             descrambling_key_length -= u32Len;
959             descrambling_key_descrip+= u32Len;
960         }
961         else
962         {
963             NDS_ASSERT(FALSE, CA_REQUEST_NOT_SUPPORTED_BY_DRIVER, "NDS_ASSERT [%s]-[%d]\n", __FUNCTION__, __LINE__);
964             break;
965         }
966     }
967 
968     KLConfig.bDecrypt = TRUE;
969     KLConfig.bInverse = FALSE;
970     KLConfig.bKeepRound = FALSE;
971     KLConfig.u32Round = 6;
972     KLConfig.u32EngMode = E_DSCMB_KL_TDES;
973 
974     MDrv_DSCMB_KLadder_SetConfig(&KLConfig);
975     MDrv_DSCMB_KLadder_SetKey(E_DSCMB_KL_SRC_VGK, NULL, 0);
976     MDrv_DSCMB_KLadder_SetDst(E_DSCMB_KL_DST_ACPU);
977 
978     MDrv_DSCMB_KLadder_Start();
979 
980     // (3) polling status
981     while (1)
982     {
983         if (MDrv_DSCMB_KLadder_IsComplete())
984         {
985             break;
986         }
987     }
988     //Read key
989     MDrv_DSCMB_KLadder_ReadDataFromAcpu(response);
990     *response_length = 16;
991     //_verSC_PrintData(response, 16);
992 
993     //ACK --release key
994     MDrv_DSCMB_KLadder_ResetAcpuAck();
995 
996     // (5) Poll key ladder done
997     while (1)
998     {
999         if (MDrv_DSCMB_KLadder_Output_IsComplete())
1000         // if (kl_done == ((reg_kl_status & 0x1)))
1001         {
1002             break;
1003         }
1004     }
1005     MDrv_DSCMB_KLadder_Output_Stop();
1006 
1007     // (6) Clear key ladder control
1008     MDrv_DSCMB_KLadder_Stop();
1009 
1010     return CA_OK;
1011 }
1012 
XHDICA_EncryptData(void * encrypt_in_params,unsigned int * out_data_buf_len,NDS_BYTE * out_data_buf)1013 NDS_STATUS XHDICA_EncryptData( void *encrypt_in_params,
1014                                unsigned int *out_data_buf_len,
1015                                NDS_BYTE *out_data_buf)
1016 {
1017     DSCMB_KLCfg KLConfig;
1018     MS_U8   u8Tag;
1019     MS_U16  u16InLen;
1020     MS_U8 pu8InData[16];
1021     MS_U8   u8Flag;
1022     MS_U8* pu8InParams = (MS_U8*)encrypt_in_params;
1023     MS_U32  i=0;
1024     u8Tag = ((pu8InParams[0])&0xFF);
1025     u16InLen = ((MS_U16)(((MS_U8*)(pu8InParams))[2]<<8)|(((MS_U8*)(pu8InParams))[3]));//((pu8InParams[3]) & 0xFF);
1026     u8Flag = ((pu8InParams[4+u16InLen])&0xFF);
1027 
1028     NDS_FUNC("[%s]-[%d] \n", __FUNCTION__, __LINE__);
1029 
1030     if (u8Tag != ENCRYPT_IN_PARAMS_DSCR_TAG)
1031         return CA_REQUEST_NOT_SUPPORTED_BY_DRIVER;
1032 
1033     if (u8Flag & 0x01)//flag:1
1034     {
1035         if (u16InLen > 16)
1036         {
1037             *out_data_buf_len = (16 * (u16InLen/16));
1038             if (u16InLen%16)
1039                 *out_data_buf_len+=16;
1040         }
1041         else
1042             *out_data_buf_len = 16;
1043     }
1044     else
1045     {
1046         if (out_data_buf == NULL)
1047         {
1048             return CA_REQUEST_NOT_SUPPORTED_BY_DRIVER;
1049         }
1050         for (i=0;i<((*out_data_buf_len)/16);i++)
1051         {
1052             memset(pu8InData, 0, sizeof(pu8InData));
1053             memcpy(pu8InData, &pu8InParams[4+(i*16)], (u16InLen>16)?16:u16InLen);
1054             if (u16InLen > 16)
1055             {
1056                 u16InLen -=16;
1057             }
1058             MDrv_DSCMB_KLadder_SetInput(0, pu8InData, sizeof(pu8InData));//CW
1059             MDrv_DSCMB_KLadder_SetInput(1, _u8DefaultKey, sizeof(_u8DefaultKey));
1060             MDrv_DSCMB_KLadder_SetInput(2, _u8DefaultKey, sizeof(_u8DefaultKey));
1061 
1062             KLConfig.bDecrypt = TRUE;
1063             KLConfig.bInverse = FALSE;
1064             KLConfig.bKeepRound = FALSE;
1065             KLConfig.u32Round = 6;
1066             KLConfig.u32EngMode = E_DSCMB_KL_TDES;
1067 
1068             MDrv_DSCMB_KLadder_SetConfig(&KLConfig);
1069             MDrv_DSCMB_KLadder_SetKey(E_DSCMB_KL_SRC_VGK, NULL, 0);
1070             MDrv_DSCMB_KLadder_SetDst(E_DSCMB_KL_DST_ACPU);
1071 
1072             MDrv_DSCMB_KLadder_Start();
1073 
1074             // (3) polling status
1075             while (1)
1076             {
1077                 if (MDrv_DSCMB_KLadder_IsComplete())
1078                 {
1079                     break;
1080                 }
1081             }
1082             //Read key
1083             MDrv_DSCMB_KLadder_ReadDataFromAcpu(&out_data_buf[i*16]);
1084             //_verSC_PrintData(&out_data_buf[i*16], 16);
1085 
1086             //ACK --release key
1087             MDrv_DSCMB_KLadder_ResetAcpuAck();
1088 
1089             // (5) Poll key ladder done
1090             while (1)
1091             {
1092                 if (MDrv_DSCMB_KLadder_Output_IsComplete())
1093                 // if (kl_done == ((reg_kl_status & 0x1)))
1094                 {
1095                     break;
1096                 }
1097             }
1098             MDrv_DSCMB_KLadder_Output_Stop();
1099 
1100             // (6) Clear key ladder control
1101             MDrv_DSCMB_KLadder_Stop();
1102         }
1103     }
1104     return CA_OK;
1105 }
1106 
1107 
1108