xref: /utopia/UTPA2-700.0.x/modules/dscmb/api/nsk2hdi/drvNDSSC.h (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   drvSC.h
98 /// @brief  SmartCard Driver Interface
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 #ifndef __DRV_SC_H__
103 #define __DRV_SC_H__
104 
105 
106 #ifdef __cplusplus
107 extern "C"
108 {
109 #endif
110 
111 #include "MsDevice.h"
112 
113 //-------------------------------------------------------------------------------------------------
114 //  Driver Capability
115 //-------------------------------------------------------------------------------------------------
116 
117 
118 //-------------------------------------------------------------------------------------------------
119 //  Macro and Define
120 //-------------------------------------------------------------------------------------------------
121 #define MSIF_SC_LIB_CODE            {'S','C'}    //Lib code
122 #define MSIF_SC_LIBVER              {'0','1'}            //LIB version
123 #define MSIF_SC_BUILDNUM            {'0','1'}            //Build Number
124 #define MSIF_SC_CHANGELIST          {'0','0','0','0','0','0','0','0'} //P4 ChangeList Number
125 
126 #define SC_DRV_VERSION              /* Character String for DRV/API version             */  \
127     MSIF_TAG,                       /* 'MSIF'                                           */  \
128     MSIF_CLASS,                     /* '00'                                             */  \
129     MSIF_CUS,                       /* 0x0000                                           */  \
130     MSIF_MOD,                       /* 0x0000                                           */  \
131     MSIF_CHIP,                                                                              \
132     MSIF_CPU,                                                                               \
133     MSIF_SC_LIB_CODE,               /* IP__                                             */  \
134     MSIF_SC_LIBVER,                 /* 0.0 ~ Z.Z                                        */  \
135     MSIF_SC_BUILDNUM,               /* 00 ~ 99                                          */  \
136     MSIF_SC_CHANGELIST,             /* CL#                                              */  \
137     MSIF_OS
138 
139 #define SC_ATR_LEN_MAX              33                                  ///< Maximum length of ATR
140 #define SC_ATR_LEN_MIN              2                                   ///< Minimum length of ATR
141 #define SC_HIST_LEN_MAX             15                                  ///< Maximum length of ATR history
142 #define SC_PPS_LEN_MAX              6                                   ///< Maximum length of PPS
143 #define SC_FIFO_SIZE                512                                 // Rx fifo size
144 
145 ///SC_Param.u8UartMode
146 #define SC_UART_CHAR_7              (0x02)
147 #define SC_UART_CHAR_8              (0x03)
148 #define SC_UART_STOP_1              (0x00)
149 #define SC_UART_STOP_2              (0x04)
150 #define SC_UART_PARITY_NO           (0x00)
151 #define SC_UART_PARITY_ODD          (0x08)
152 #define SC_UART_PARITY_EVEN         (0x08|0x10)
153 
154 #define SC_PROC_LOCK                (0xFE66)
155 #define SC_PROC_UNLOCK              (0x0)
156 
157 //-------------------------------------------------------------------------------------------------
158 //  Type and Structure
159 //-------------------------------------------------------------------------------------------------
160 
161 typedef struct _SC_Status
162 {
163     MS_BOOL                         bCardIn;                            ///< Card status
164 
165 } SC_Status;
166 
167 typedef enum _SC_DbgLv
168 {
169     E_SC_DBGLV_NONE,                                                    //no debug message
170     E_SC_DBGLV_ERR_ONLY,                                                //show error only
171     E_SC_DBGLV_REG_DUMP,                                                //show error & reg dump
172     E_SC_DBGLV_INFO,                                                    //show error & informaiton
173     E_SC_DBGLV_ALL,                                                     //show error, information & funciton name
174 } SC_DbgLv;
175 
176 /// SmartCard DDI error code
177 typedef enum
178 {
179     E_SC_FAIL,                                                          ///< Function fail
180     E_SC_OK,                                                            ///< No Error
181     E_SC_PPSFAIL,                                                       ///< Do PPS fail
182     E_SC_INVCONV,                                                       ///< Inverse convention
183     E_SC_CARDIN,                                                        ///< Card in
184     E_SC_CARDOUT,                                                       ///< Card out
185     E_SC_NODATA,                                                        ///< No data
186     E_SC_TIMEOUT,                                                       ///< Timeout
187     E_SC_OVERFLOW,                                                      ///< Rx data fifo overflow
188     E_SC_CRCERROR,                                                      ///< ATR checksum error
189     E_SC_DATAERROR,                                                     ///< Data error
190     E_SC_PARMERROR,                                                     ///< Parameter error
191 } SC_Result;
192 
193 /// SmartCard event
194 typedef enum
195 {
196     E_SC_EVENT_DATA,                                                    ///< Rx data valid
197     E_SC_EVENT_IN,                                                      ///< Card in
198     E_SC_EVENT_OUT,                                                     ///< Card out
199 
200 } SC_Event;
201 
202 /// SmartCard CLK setting
203 typedef enum
204 {
205     E_SC_CLK_3M,                                                        ///< 3 MHz
206     E_SC_CLK_4P5M,                                                      ///< 4.5 MHz
207     E_SC_CLK_6M,                                                        ///< 6 MHz
208     E_SC_CLK_13M,                                                        ///< 6 MHz
209 } SC_ClkCtrl;
210 
211 /// SmartCard VCC control mode
212 typedef enum
213 {
214     E_SC_VCC_CTRL_8024_ON,                                              ///< by external 8024 on
215     E_SC_VCC_CTRL_LOW,                                                  ///< by direct Vcc (low active)
216     E_SC_VCC_CTRL_HIGH,                                                 ///< by direct Vcc (high active)
217     E_SC_OCP_VCC_HIGH,
218     E_SC_VCC_VCC_ONCHIP_8024,
219 } SC_VccCtrl;
220 
221 // smart card 3V/5V control
222 typedef enum
223 {
224     E_SC_VOLTAGE_3_POINT_3V,                                              ///<3.3V
225     E_SC_VOLTAGE_5V,                                                      ///< 5V
226     E_SC_VOLTAGE_OFF,                                                     ///< vcc off
227     E_SC_VOLTAGE_MAX
228 } SC_VoltageCtrl;
229 
230 typedef enum
231 {
232     E_SC_IO_USE_SMIO,                                                       ///< Use SM_IO pin
233     E_SC_IO_USE_C4,                                                         ///< Use C4 pin
234     E_SC_IO_USE_C8,                                                         ///< use C8 pin
235     E_SC_C4C8_MAX
236 } SC_IOC4C8;
237 
238 // smart card Reset Type
239 typedef enum
240 {
241     E_SC_WARM_RESET,                                                      ///< warm reset
242     E_SC_COLD_RESET                                                      ///< cold reset
243 } SC_ResetType;
244 
245 typedef void(*P_SC_En5V_Callback)(MS_BOOL bEnable);
246 
247 /// SmartCard configuration
248 typedef struct
249 {
250     MS_U8                           u8Protocal;                         ///< T=
251     SC_ClkCtrl                      eCardClk;                           ///< Clock
252     MS_U8                           u8UartMode;                         ///< Uart Mode
253     MS_U16                          u16ClkDiv;                          ///< Div
254     SC_VccCtrl                      eVccCtrl;
255     MS_U16                          u16Bonding;                         ///Chip Bonding type
256     P_SC_En5V_Callback              pfOCPControl;
257     MS_U8                           u8Convention;                       ///< Convention
258     SC_VoltageCtrl                  eVoltage;
259     MS_U8                           eResetType;                         ///< Reset Type
260 } SC_Param;
261 
262 /// SmartCard event callback
263 typedef void (*P_SC_Callback)(MS_U8 u8SCID, SC_Event eEvent);
264 
265 typedef struct _Smart_Dev
266 {
267 
268 } SC_Dev;
269 
270 
271 /// SmartCard Info
272 typedef struct
273 {
274     // SmsartCard Protocol
275     MS_U8                           u8Protocol;                         ///T= Protocol
276     MS_BOOL                         bSpecMode;                          ///Special mode
277     MS_U8                           pu8Atr[SC_ATR_LEN_MAX];             ///Atr buffer
278     MS_U16                          u16AtrLen;                          ///Atr length
279     MS_U8                           pu8Hist[SC_HIST_LEN_MAX];           ///History buffer
280     MS_U16                          u16HistLen;                         ///History length
281     MS_U8                           u8Fi;                               ///Fi
282     MS_U8                           u8Di;                               ///Di
283 
284     // Device Setting
285     MS_BOOL                         bInited;
286     MS_BOOL                         bOpened;                           ///Open
287     MS_BOOL                         bCardIn;                            ///Status care in
288     MS_BOOL                         blast_cardin;
289     SC_ClkCtrl                      eCardClk;                           ///< Clock
290     MS_U8                           u8UartMode;                         ///< Uart Mode
291     SC_VccCtrl                      eVccCtrl;
292     MS_U16                          u16ClkDiv;                         ///< Div
293     MS_U16                          u16Bonding; //@TODO: how to take care of bonding?????
294     P_SC_En5V_Callback              pfEn5V;
295     MS_U8                           u8Convention;
296 
297     MS_U8                           u8FifoRx[SC_FIFO_SIZE];
298     MS_U16                          u16FifoRxRead;
299     MS_U16                          u16FifoRxWrite;
300 
301     MS_U8                           u8FifoTx[SC_FIFO_SIZE];
302     MS_U16                          u16FifoTxRead;
303     MS_U16                          u16FifoTxWrite;
304     P_SC_Callback                   pfNotify;                           ///Call back funtcion
305 
306     MS_BOOL                         bLastCardIn;
307     MS_S32                          s32DevFd;
308     SC_VoltageCtrl                  eVoltage;
309     SC_ResetType                    eResetType;
310 } SC_Info;
311 
312 
313 /// SmartCard Caps
314 typedef struct
315 {
316     MS_U8                           u8DevNum;                           ///SmartCard Device Number
317 
318 } SC_Caps;
319 
320 ///Define SC Command Index
321 typedef enum
322 {
323     //MIPS-->51 Command Index
324     SC_CMDIDX_RAW_EXCHANGE = 0x01,                  /// SC Command Index is Raw Data Exchange
325     SC_CMDIDX_GET_ATR      = 0x02,                  /// SC Command Index is Get ATR
326     SC_CMDIDX_SEND         = 0x03,                  /// SC Command Index is Send Data
327     SC_CMDIDX_RECV         = 0x04,                  /// SC Command Index is Receive Data
328 
329     SC_CMDIDX_ACK_51ToMIPS = 0x30,                  /// SC Command Index is ACK 51 To MIPS
330 
331     //51->MIPS Command Index
332     SC_CMDIDX_ACK_MIPSTo51 = 0xA0,                  /// SC Command Index is ACK MIPS To 51
333 
334 } SC_CmdIndex;
335 
336 
337 ///Define SC Acknowledge Flags
338 typedef enum
339 {
340     E_SC_ACKFLG_NULL                = 0,            /// Ack flag for NULL
341     E_SC_ACKFLG_WAIT_RAW_EXCHANGE   = (1<<0),       /// Ack flag for Raw Exchange
342     E_SC_ACKFLG_WAIT_GET_ATR        = (1<<1),       /// Ack flag for ATR
343     E_SC_ACKFLG_WAIT_SEND           = (1<<2),       /// Ack flag for Sending
344     E_SC_ACKFLG_WAIT_RECV           = (1<<3),       /// Ack flag for Receiving
345 
346 } SC_AckFlags;
347 
348 typedef struct __attribute__ ((packed, aligned (4096)))
349 {
350     MS_U8  u8SCID;
351     MS_U16 u16DataLen;       /* Send and reply data length*/
352     MS_U16 u16ReplyMaxLen;   /* Maximum length of reply data */
353     MS_U8  u8Data[259];      /* T=0, 256 data bytes plus the two status words SW1 and SW2
354                                                 T=1, 254 INF bytes plus NAD, PCB, LEN, and two EDC bytes
355                                                 Send and reply data buffer*/
356 } SC_DataCfg;
357 
358 /// SmartCard Caps
359 typedef struct
360 {
361     MS_U32   u32DataBuffAddr;                            ///SmartCard Data Buffer Address, 4K alignment
362     MS_U32   u32FwBuffAddr;                              ///SmartCard Firmware Buffer Address, 64K alignment
363 } SC_BuffAddr;
364 
365 //-------------------------------------------------------------------------------------------------
366 //  Function and Variable
367 //-------------------------------------------------------------------------------------------------
368 
369 SC_Result       MDrv_SC_Init(MS_U8 u8SCID);
370 SC_Result       MDrv_SC_Open(MS_U8 u8SCID, MS_U8 u8Protocol, SC_Param* pParam, P_SC_Callback pfSmartNotify);
371 SC_Result       MDrv_SC_Config(MS_U8 u8SCID, SC_Param* pParam);
372 SC_Result       MDrv_SC_Close(MS_U8 u8SCID);
373 SC_Result       MDrv_SC_Reset(MS_U8 u8SCID, SC_Param* pParam);
374 SC_Result       MDrv_SC_Activate(MS_U8 u8SCID);
375 SC_Result       MDrv_SC_Deactivate(MS_U8 u8SCID);
376 SC_Result       MDrv_SC_Reset_ATR(MS_U8 u8SCID, SC_Param* pParam, MS_U8 *pu8Atr, MS_U16 *pu16AtrLen, MS_U8 *pu8His, MS_U16 *pu16HisLen);
377 SC_Result       MDrv_SC_PPS(MS_U8 u8SCID);
378 SC_Result       MDrv_SC_Send(MS_U8 u8SCID, MS_U8 *pu8SendData, MS_U16 u16SendDataLen, MS_U32 u32TimeoutMs);
379 SC_Result       MDrv_SC_Recv(MS_U8 u8SCID, MS_U8 *pu8ReadData, MS_U16 *u16ReadDataLen, MS_U32 u32TimeoutMs);
380 SC_Result       MDrv_SC_T0_SendRecv(MS_U8 u8SCID, MS_U8 *pu8SendData, MS_U16 u16SendLen, MS_U8 *pu8RecvData, MS_U16 *pu16RecvLen);
381 SC_Result       MDrv_SC_T1_SendRecv(MS_U8 u8SCID, MS_U8 *pu8SendData, MS_U16 *u16SendDataLen, MS_U8 *pu8ReadData, MS_U16 *u16ReadDataLen);
382 SC_Result       MDrv_SC_T14_SendRecv(MS_U8 u8SCID, MS_U8 *pu8SendData, MS_U16 u16SendLen, MS_U8 *pu8RecvData, MS_U16 *pu16RecvLen);
383 SC_Result       MDrv_SC_Exit(MS_U8 u8SCID);
384 
385 SC_Result       MDrv_SC_GetATR(MS_U8 u8SCID, MS_U32 u32TimeOut,MS_U8 *pu8Atr, MS_U16 *pu16AtrLen, MS_U8 *pu8His, MS_U16 *pu16HisLen);
386 const SC_Info*  MDrv_SC_GetInfo(MS_U8 u8SCID);
387 SC_Result       MDrv_SC_GetLibVer(const MSIF_Version **ppVersion);
388 SC_Result       MDrv_SC_GetStatus(MS_U8 u8SCID, SC_Status *pStatus);
389 void            MDrv_SC_SetDbgLevel(SC_DbgLv eLevel);
390 SC_Result       MDrv_SC_SetPPS(MS_U8 u8SCID, MS_U8 u8SCProtocol, MS_U8 u8Di,MS_U8 u8Fi);
391 SC_Result       MDrv_SC_ClearState(MS_U8 u8SCID);
392 SC_Result       MDrv_SC_PowerOff(void);
393 SC_Result       MDrv_SC_SetGuardTime(MS_U8 u8SCID, MS_U8 u8GuardTime);
394 SC_Result       MDrv_SC_SetWaitingTime(MS_U8 u8SCID, MS_U16 u16WaitingTime);
395 SC_Result       MDrv_SC_SetBlockGuardTime(MS_U8 u8SCID, MS_U8 u8GuardTime);
396 SC_Result       MDrv_SC_SetBlockWaitingTime(MS_U8 u8SCID, MS_U32 u32WaitingTime);
397 
398 SC_Result       MDrv_SC_Task_Proc(void);
399 MS_BOOL         MDrv_SC_ISR_Proc(MS_U8 u8SCID);
400 SC_Result       MDrv_SC_RawExchange(MS_U8 u8SCID, MS_U8 *pu8SendData, MS_U16 *u16SendDataLen, MS_U8 *pu8ReadData, MS_U16 *u16ReadDataLen);
401 SC_Result       MDrv_SC_RawExchangeTimeout(MS_U8 u8SCID, MS_U8 *pu8SendData, MS_U16 *u16SendDataLen, MS_U8 *pu8ReadData, MS_U16 *u16ReadDataLen, MS_U32 u32TimeoutMs);
402 SC_Result       MDrv_SC_SetBuffAddr(SC_BuffAddr *pScBuffAddr);
403 MS_BOOL         MDrv_SC_CardVoltage_Config(MS_U8 u8SCID, SC_VoltageCtrl eVoltage);
404 void            MDrv_SC_EnableTimeout(MS_BOOL bTimeout);
405 MS_U32          MDrv_SC_SetPowerState(EN_POWER_MODE u16PowerState);
406 MS_BOOL         MDrv_SC_IOC4C8Switch(SC_IOC4C8 eIOSelect);
407 MS_BOOL MDrv_SC_SAR_DeactiveSeq(MS_BOOL bEnable);
408 void MDrv_SC_T0_SetTimeOut(MS_U32 u32TimeOut);
409 void MDrv_SC_C4_SignalLevel(MS_U8 u8Level);
410 void MDrv_SC_C7_SignalLevel(MS_U8 u8Level);
411 void MDrv_SC_C8_SignalLevel(MS_U8 u8Level);
412 SC_Result MDrv_SC_GetSCParam(MS_U8 u8SCID, SC_Param *pParam);
413 SC_Result MDrv_SC_GetWaitingTime(MS_U8 u8SCID);
414 #ifdef __cplusplus
415 }
416 #endif
417 
418 #endif // __DRV_SC_H__
419