fsl_smartcard.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef _FSL_SMARTCARD_H_
  35. #define _FSL_SMARTCARD_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup smartcard
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @name Driver version */
  45. /*@{*/
  46. /*! @brief Smart card driver version 2.2.0.
  47. */
  48. #define FSL_SMARTCARD_DRIVER_VERSION (MAKE_VERSION(2, 2, 0))
  49. /*@}*/
  50. /*! @brief Smart card global define which specify number of clock cycles until initial 'TS' character has to be received
  51. */
  52. #define SMARTCARD_INIT_DELAY_CLOCK_CYCLES (42000u)
  53. /*! @brief Smart card global define which specify number of clock cycles during which ATR string has to be received */
  54. #define SMARTCARD_EMV_ATR_DURATION_ETU (20150u)
  55. /*! @brief Smart card specification initial TS character definition of direct convention */
  56. #define SMARTCARD_TS_DIRECT_CONVENTION (0x3Bu)
  57. /*! @brief Smart card specification initial TS character definition of inverse convention */
  58. #define SMARTCARD_TS_INVERSE_CONVENTION (0x3Fu)
  59. /*! @brief Smart card Error codes. */
  60. typedef enum _smartcard_status
  61. {
  62. kStatus_SMARTCARD_Success = MAKE_STATUS(kStatusGroup_SMARTCARD, 0), /*!< Transfer ends successfully */
  63. kStatus_SMARTCARD_TxBusy = MAKE_STATUS(kStatusGroup_SMARTCARD, 1), /*!< Transmit in progress */
  64. kStatus_SMARTCARD_RxBusy = MAKE_STATUS(kStatusGroup_SMARTCARD, 2), /*!< Receiving in progress */
  65. kStatus_SMARTCARD_NoTransferInProgress = MAKE_STATUS(kStatusGroup_SMARTCARD, 3), /*!< No transfer in progress */
  66. kStatus_SMARTCARD_Timeout = MAKE_STATUS(kStatusGroup_SMARTCARD, 4), /*!< Transfer ends with time-out */
  67. kStatus_SMARTCARD_Initialized =
  68. MAKE_STATUS(kStatusGroup_SMARTCARD, 5), /*!< Smart card driver is already initialized */
  69. kStatus_SMARTCARD_PhyInitialized =
  70. MAKE_STATUS(kStatusGroup_SMARTCARD, 6), /*!< Smart card PHY drive is already initialized */
  71. kStatus_SMARTCARD_CardNotActivated = MAKE_STATUS(kStatusGroup_SMARTCARD, 7), /*!< Smart card is not activated */
  72. kStatus_SMARTCARD_InvalidInput =
  73. MAKE_STATUS(kStatusGroup_SMARTCARD, 8), /*!< Function called with invalid input arguments */
  74. kStatus_SMARTCARD_OtherError = MAKE_STATUS(kStatusGroup_SMARTCARD, 9) /*!< Some other error occur */
  75. } smartcard_status_t;
  76. /*! @brief Control codes for the Smart card protocol timers and misc. */
  77. typedef enum _smartcard_control
  78. {
  79. kSMARTCARD_EnableADT = 0x0u,
  80. kSMARTCARD_DisableADT = 0x1u,
  81. kSMARTCARD_EnableGTV = 0x2u,
  82. kSMARTCARD_DisableGTV = 0x3u,
  83. kSMARTCARD_ResetWWT = 0x4u,
  84. kSMARTCARD_EnableWWT = 0x5u,
  85. kSMARTCARD_DisableWWT = 0x6u,
  86. kSMARTCARD_ResetCWT = 0x7u,
  87. kSMARTCARD_EnableCWT = 0x8u,
  88. kSMARTCARD_DisableCWT = 0x9u,
  89. kSMARTCARD_ResetBWT = 0xAu,
  90. kSMARTCARD_EnableBWT = 0xBu,
  91. kSMARTCARD_DisableBWT = 0xCu,
  92. kSMARTCARD_EnableInitDetect = 0xDu,
  93. kSMARTCARD_EnableAnack = 0xEu,
  94. kSMARTCARD_DisableAnack = 0xFu,
  95. kSMARTCARD_ConfigureBaudrate = 0x10u,
  96. kSMARTCARD_SetupATRMode = 0x11u,
  97. kSMARTCARD_SetupT0Mode = 0x12u,
  98. kSMARTCARD_SetupT1Mode = 0x13u,
  99. kSMARTCARD_EnableReceiverMode = 0x14u,
  100. kSMARTCARD_DisableReceiverMode = 0x15u,
  101. kSMARTCARD_EnableTransmitterMode = 0x16u,
  102. kSMARTCARD_DisableTransmitterMode = 0x17u,
  103. kSMARTCARD_ResetWaitTimeMultiplier = 0x18u,
  104. } smartcard_control_t;
  105. /*! @brief Defines Smart card interface voltage class values */
  106. typedef enum _smartcard_card_voltage_class
  107. {
  108. kSMARTCARD_VoltageClassUnknown = 0x0u,
  109. kSMARTCARD_VoltageClassA5_0V = 0x1u,
  110. kSMARTCARD_VoltageClassB3_3V = 0x2u,
  111. kSMARTCARD_VoltageClassC1_8V = 0x3u
  112. } smartcard_card_voltage_class_t;
  113. /*! @brief Defines Smart card I/O transfer states */
  114. typedef enum _smartcard_transfer_state
  115. {
  116. kSMARTCARD_IdleState = 0x0u,
  117. kSMARTCARD_WaitingForTSState = 0x1u,
  118. kSMARTCARD_InvalidTSDetecetedState = 0x2u,
  119. kSMARTCARD_ReceivingState = 0x3u,
  120. kSMARTCARD_TransmittingState = 0x4u,
  121. } smartcard_transfer_state_t;
  122. /*! @brief Defines Smart card reset types */
  123. typedef enum _smartcard_reset_type
  124. {
  125. kSMARTCARD_ColdReset = 0x0u,
  126. kSMARTCARD_WarmReset = 0x1u,
  127. kSMARTCARD_NoColdReset = 0x2u,
  128. kSMARTCARD_NoWarmReset = 0x3u,
  129. } smartcard_reset_type_t;
  130. /*! @brief Defines Smart card transport protocol types */
  131. typedef enum _smartcard_transport_type
  132. {
  133. kSMARTCARD_T0Transport = 0x0u,
  134. kSMARTCARD_T1Transport = 0x1u
  135. } smartcard_transport_type_t;
  136. /*! @brief Defines Smart card data parity types */
  137. typedef enum _smartcard_parity_type
  138. {
  139. kSMARTCARD_EvenParity = 0x0u,
  140. kSMARTCARD_OddParity = 0x1u
  141. } smartcard_parity_type_t;
  142. /*! @brief Defines data Convention format */
  143. typedef enum _smartcard_card_convention
  144. {
  145. kSMARTCARD_DirectConvention = 0x0u,
  146. kSMARTCARD_InverseConvention = 0x1u
  147. } smartcard_card_convention_t;
  148. /*! @brief Defines Smart card interface IC control types */
  149. typedef enum _smartcard_interface_control
  150. {
  151. kSMARTCARD_InterfaceSetVcc = 0x00u,
  152. kSMARTCARD_InterfaceSetClockToResetDelay = 0x01u,
  153. kSMARTCARD_InterfaceReadStatus = 0x02u
  154. } smartcard_interface_control_t;
  155. /*! @brief Defines transfer direction.*/
  156. typedef enum _smartcard_direction
  157. {
  158. kSMARTCARD_Receive = 0u,
  159. kSMARTCARD_Transmit = 1u
  160. } smartcard_direction_t;
  161. /*! @brief Smart card interface interrupt callback function type */
  162. typedef void (*smartcard_interface_callback_t)(void *smartcardContext, void *param);
  163. /*! @brief Smart card transfer interrupt callback function type */
  164. typedef void (*smartcard_transfer_callback_t)(void *smartcardContext, void *param);
  165. /*! @brief Time Delay function used to passive waiting using RTOS [us] */
  166. typedef void (*smartcard_time_delay_t)(uint32_t us);
  167. /*! @brief Defines card-specific parameters for Smart card driver */
  168. typedef struct _smartcard_card_params
  169. {
  170. /* ISO7816/EMV4.3 specification variables */
  171. uint16_t Fi; /*!< 4 bits Fi - clock rate conversion integer */
  172. uint8_t fMax; /*!< Maximum Smart card frequency in MHz */
  173. uint8_t WI; /*!< 8 bits WI - work wait time integer */
  174. uint8_t Di; /*!< 4 bits DI - baud rate divisor */
  175. uint8_t BWI; /*!< 4 bits BWI - block wait time integer */
  176. uint8_t CWI; /*!< 4 bits CWI - character wait time integer */
  177. uint8_t BGI; /*!< 4 bits BGI - block guard time integer */
  178. uint8_t GTN; /*!< 8 bits GTN - extended guard time integer */
  179. uint8_t IFSC; /*!< Indicates IFSC value of the card */
  180. uint8_t modeNegotiable; /*!< Indicates if the card acts in negotiable or a specific mode. */
  181. uint8_t currentD; /*!< 4 bits DI - current baud rate divisor*/
  182. /* Driver-specific variables */
  183. uint8_t status; /*!< Indicates smart card status */
  184. bool t0Indicated; /*!< Indicates ff T=0 indicated in TD1 byte */
  185. bool t1Indicated; /*!< Indicates if T=1 indicated in TD2 byte */
  186. bool atrComplete; /*!< Indicates whether the ATR received from the card was complete or not */
  187. bool atrValid; /*!< Indicates whether the ATR received from the card was valid or not */
  188. bool present; /*!< Indicates if a smart card is present */
  189. bool active; /*!< Indicates if the smart card is activated */
  190. bool faulty; /*!< Indicates whether smart card/interface is faulty */
  191. smartcard_card_convention_t convention; /*!< Card convention, kSMARTCARD_DirectConvention for direct convention,
  192. kSMARTCARD_InverseConvention for inverse convention */
  193. } smartcard_card_params_t;
  194. /*! @brief Smart card defines the state of the EMV timers in the Smart card driver */
  195. typedef struct _smartcard_timers_state
  196. {
  197. volatile bool adtExpired; /*!< Indicates whether ADT timer expired */
  198. volatile bool wwtExpired; /*!< Indicates whether WWT timer expired */
  199. volatile bool cwtExpired; /*!< Indicates whether CWT timer expired */
  200. volatile bool bwtExpired; /*!< Indicates whether BWT timer expired */
  201. volatile bool initCharTimerExpired; /*!< Indicates whether reception timer
  202. for initialization character (TS) after the RST has expired */
  203. } smartcard_timers_state_t;
  204. /*! @brief Defines user specified configuration of Smart card interface */
  205. typedef struct _smartcard_interface_config
  206. {
  207. uint32_t smartCardClock; /*!< Smart card interface clock [Hz] */
  208. uint32_t clockToResetDelay; /*!< Indicates clock to RST apply delay [smart card clock cycles] */
  209. uint8_t clockModule; /*!< Smart card clock module number */
  210. uint8_t clockModuleChannel; /*!< Smart card clock module channel number */
  211. uint8_t clockModuleSourceClock; /*!< Smart card clock module source clock [e.g., BusClk] */
  212. smartcard_card_voltage_class_t vcc; /*!< Smart card voltage class */
  213. uint8_t controlPort; /*!< Smart card PHY control port instance */
  214. uint8_t controlPin; /*!< Smart card PHY control pin instance */
  215. uint8_t irqPort; /*!< Smart card PHY Interrupt port instance */
  216. uint8_t irqPin; /*!< Smart card PHY Interrupt pin instance */
  217. uint8_t resetPort; /*!< Smart card reset port instance */
  218. uint8_t resetPin; /*!< Smart card reset pin instance */
  219. uint8_t vsel0Port; /*!< Smart card PHY Vsel0 control port instance */
  220. uint8_t vsel0Pin; /*!< Smart card PHY Vsel0 control pin instance */
  221. uint8_t vsel1Port; /*!< Smart card PHY Vsel1 control port instance */
  222. uint8_t vsel1Pin; /*!< Smart card PHY Vsel1 control pin instance */
  223. uint8_t dataPort; /*!< Smart card PHY data port instance */
  224. uint8_t dataPin; /*!< Smart card PHY data pin instance */
  225. uint8_t dataPinMux; /*!< Smart card PHY data pin mux option */
  226. uint8_t tsTimerId; /*!< Numerical identifier of the External HW timer for Initial character detection */
  227. } smartcard_interface_config_t;
  228. /*! @brief Defines user transfer structure used to initialize transfer */
  229. typedef struct _smartcard_xfer
  230. {
  231. smartcard_direction_t direction; /*!< Direction of communication. (RX/TX) */
  232. uint8_t *buff; /*!< The buffer of data. */
  233. size_t size; /*!< The number of transferred units. */
  234. } smartcard_xfer_t;
  235. /*!
  236. * @brief Runtime state of the Smart card driver.
  237. */
  238. typedef struct _smartcard_context
  239. {
  240. /* Xfer part */
  241. void *base; /*!< Smart card module base address */
  242. smartcard_direction_t direction; /*!< Direction of communication. (RX/TX) */
  243. uint8_t *xBuff; /*!< The buffer of data being transferred.*/
  244. volatile size_t xSize; /*!< The number of bytes to be transferred. */
  245. volatile bool xIsBusy; /*!< True if there is an active transfer. */
  246. uint8_t txFifoEntryCount; /*!< Number of data word entries in transmit FIFO. */
  247. uint8_t rxFifoThreshold; /*!< The max value of the receiver FIFO threshold. */
  248. /* Smart card Interface part */
  249. smartcard_interface_callback_t interfaceCallback; /*!< Callback to invoke after interface IC raised interrupt.*/
  250. smartcard_transfer_callback_t transferCallback; /*!< Callback to invoke after transfer event occur.*/
  251. void *interfaceCallbackParam; /*!< Interface callback parameter pointer.*/
  252. void *transferCallbackParam; /*!< Transfer callback parameter pointer.*/
  253. smartcard_time_delay_t timeDelay; /*!< Function which handles time delay defined by user or RTOS. */
  254. smartcard_reset_type_t resetType; /*!< Indicates whether a Cold reset or Warm reset was requested. */
  255. smartcard_transport_type_t tType; /*!< Indicates current transfer protocol (T0 or T1) */
  256. /* Smart card State part */
  257. volatile smartcard_transfer_state_t transferState; /*!< Indicates the current transfer state */
  258. smartcard_timers_state_t timersState; /*!< Indicates the state of different protocol timers used in driver */
  259. smartcard_card_params_t
  260. cardParams; /*!< Smart card parameters(ATR and current) and interface slots states(ATR and current) */
  261. uint8_t IFSD; /*!< Indicates the terminal IFSD */
  262. smartcard_parity_type_t parity; /*!< Indicates current parity even/odd */
  263. volatile bool rxtCrossed; /*!< Indicates whether RXT thresholds has been crossed */
  264. volatile bool txtCrossed; /*!< Indicates whether TXT thresholds has been crossed */
  265. volatile bool wtxRequested; /*!< Indicates whether WTX has been requested or not*/
  266. volatile bool parityError; /*!< Indicates whether a parity error has been detected */
  267. uint8_t statusBytes[2]; /*!< Used to store Status bytes SW1, SW2 of the last executed card command response */
  268. /* Configuration part */
  269. smartcard_interface_config_t interfaceConfig; /*!< Smart card interface configuration structure */
  270. bool abortTransfer; /*!< Used to abort transfer. */
  271. } smartcard_context_t;
  272. /*! @}*/
  273. #endif /* _FSL_SMARTCARD_H_*/