Home | History | Annotate | Download | only in int
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2009-2013 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 
     20 /******************************************************************************
     21  *
     22  *  This file contains the Near Field Communication (NFC) Reader/Writer mode
     23  *  related internal function / definitions.
     24  *
     25  ******************************************************************************/
     26 
     27 #ifndef RW_INT_H_
     28 #define RW_INT_H_
     29 
     30 #include "tags_defs.h"
     31 #include "tags_int.h"
     32 #include "rw_api.h"
     33 
     34 /* Proprietary definitions for HR0 and HR1 */
     35 #define RW_T1T_HR0_HI_NIB                       0xF0    /* HI NIB Tag                                               */
     36 #define RW_T1T_IS_JEWEL64                       0x20    /* Jewel 64 Tag                                             */
     37 #define RW_T1T_IS_JEWEL                         0x00    /* Jewel Tag                                                */
     38 #define RW_T1T_IS_TOPAZ                         0x10    /* TOPAZ Tag                                                */
     39 #define RW_T1T_IS_TOPAZ96                       0x11    /* TOPAZ96 Tag                                              */
     40 #define RW_T1T_IS_TOPAZ512                      0x12    /* TOPAZ512 Tag                                             */
     41 #define RW_T1T_HR1_MIN                          0x49    /* Supports dynamic commands on static tag if HR1 > 0x49    */
     42 
     43 #define RW_T1T_MAX_MEM_TLVS                     0x05    /* Maximum supported Memory control TLVS in the tag         */
     44 #define RW_T1T_MAX_LOCK_TLVS                    0x05    /* Maximum supported Lock control TLVS in the tag           */
     45 #define RW_T1T_MAX_LOCK_BYTES                   0x1E    /* Maximum supported dynamic lock bytes                     */
     46 
     47 /* State of the Tag as interpreted by RW */
     48 #define RW_T1_TAG_ATTRB_UNKNOWN                 0x00    /* TAG State is unknown to RW                               */
     49 #define RW_T1_TAG_ATTRB_INITIALIZED             0x01    /* TAG is in INITIALIZED state                              */
     50 #define RW_T1_TAG_ATTRB_INITIALIZED_NDEF        0x02    /* TAG is in INITIALIZED state and has NDEF tlv with len=0  */
     51 #define RW_T1_TAG_ATTRB_READ_ONLY               0x03    /* TAG is in READ ONLY state                                */
     52 #define RW_T1_TAG_ATTRB_READ_WRITE              0x04    /* TAG is in READ WRITE state                               */
     53 
     54 #define RW_T1T_LOCK_NOT_UPDATED                 0x00    /* Lock not yet set as part of SET TAG RO op                */
     55 #define RW_T1T_LOCK_UPDATE_INITIATED            0x01    /* Sent command to set the Lock bytes                       */
     56 #define RW_T1T_LOCK_UPDATED                     0x02    /* Lock bytes are set                                       */
     57 typedef UINT8 tRW_T1T_LOCK_STATUS;
     58 
     59 /* States */
     60 #define RW_T1T_STATE_NOT_ACTIVATED              0x00    /* Tag not activated and or response not received for RID   */
     61 #define RW_T1T_STATE_IDLE                       0x01    /* T1 Tag activated and ready to perform rw operation on Tag*/
     62 #define RW_T1T_STATE_READ                       0x02    /* waiting rsp for read command sent to tag                 */
     63 #define RW_T1T_STATE_WRITE                      0x03    /* waiting rsp for write command sent to tag                */
     64 #define RW_T1T_STATE_TLV_DETECT                 0x04    /* performing TLV detection procedure                       */
     65 #define RW_T1T_STATE_READ_NDEF                  0x05    /* performing read NDEF procedure                           */
     66 #define RW_T1T_STATE_WRITE_NDEF                 0x06    /* performing update NDEF procedure                         */
     67 #define RW_T1T_STATE_SET_TAG_RO                 0x07    /* Setting Tag as read only tag                             */
     68 #define RW_T1T_STATE_CHECK_PRESENCE             0x08    /* Check if Tag is still present                            */
     69 #define RW_T1T_STATE_FORMAT_TAG                 0x09    /* Format T1 Tag                                            */
     70 
     71 /* Sub states */
     72 #define RW_T1T_SUBSTATE_NONE                    0x00    /* Default substate                                         */
     73 
     74 /* Sub states in RW_T1T_STATE_TLV_DETECT state */
     75 #define RW_T1T_SUBSTATE_WAIT_TLV_DETECT         0x01    /* waiting for the detection of a tlv in a tag              */
     76 #define RW_T1T_SUBSTATE_WAIT_FIND_LEN_FIELD_LEN 0x02    /* waiting for finding the len field is 1 or 3 bytes long   */
     77 #define RW_T1T_SUBSTATE_WAIT_READ_TLV_LEN0      0x03    /* waiting for extracting len field value                   */
     78 #define RW_T1T_SUBSTATE_WAIT_READ_TLV_LEN1      0x04    /* waiting for extracting len field value                   */
     79 #define RW_T1T_SUBSTATE_WAIT_READ_TLV_VALUE     0x05    /* waiting for extracting value field in the TLV            */
     80 #define RW_T1T_SUBSTATE_WAIT_READ_LOCKS         0x06    /* waiting for reading dynamic locks in the TLV             */
     81 
     82 /* Sub states in RW_T1T_STATE_WRITE_NDEF state */
     83 #define RW_T1T_SUBSTATE_WAIT_READ_NDEF_BLOCK    0x07    /* waiting for response of reading a block that will be partially updated */
     84 #define RW_T1T_SUBSTATE_WAIT_INVALIDATE_NDEF    0x08    /* waiting for response of invalidating NDEF Msg                          */
     85 #define RW_T1T_SUBSTATE_WAIT_NDEF_WRITE         0x09    /* waiting for response of writing a part of NDEF Msg                     */
     86 #define RW_T1T_SUBSTATE_WAIT_NDEF_UPDATED       0x0A    /* waiting for response of writing last part of NDEF Msg                  */
     87 #define RW_T1T_SUBSTATE_WAIT_VALIDATE_NDEF      0x0B    /* waiting for response of validating NDEF Msg                            */
     88 
     89 /* Sub states in RW_T1T_STATE_SET_TAG_RO state */
     90 #define RW_T1T_SUBSTATE_WAIT_SET_CC_RWA_RO      0x0C    /* waiting for response of setting CC-RWA to read only      */
     91 #define RW_T1T_SUBSTATE_WAIT_SET_ST_LOCK_BITS   0x0D    /* waiting for response of setting all static lock bits     */
     92 #define RW_T1T_SUBSTATE_WAIT_SET_DYN_LOCK_BITS  0x0E    /* waiting for response of setting all dynamic lock bits    */
     93 
     94 /* Sub states in RW_T1T_STATE_FORMAT_TAG state */
     95 #define RW_T1T_SUBSTATE_WAIT_SET_CC             0x0F    /* waiting for response to format/set capability container  */
     96 #define RW_T1T_SUBSTATE_WAIT_SET_NULL_NDEF      0x10    /* waiting for response to format/set NULL NDEF             */
     97 
     98 
     99 typedef struct
    100 {
    101     UINT16  offset;                                     /* Offset of the lock byte in the Tag                   */
    102     UINT8   num_bits;                                   /* Number of lock bits in the lock byte                 */
    103     UINT8   bytes_locked_per_bit;                       /* No. of tag bytes gets locked by a bit in this byte   */
    104 } tRW_T1T_LOCK_INFO;
    105 
    106 typedef struct
    107 {
    108     UINT16  offset;                                     /* Reserved bytes offset taken from Memory control TLV  */
    109     UINT8   num_bytes;                                  /* Number of reserved bytes as per the TLV              */
    110 }tRW_T1T_RES_INFO;
    111 
    112 typedef struct
    113 {
    114     UINT8               tlv_index;                      /* Index of Lock control tlv that points to this address*/
    115     UINT8               byte_index;                     /* Index of Lock byte pointed by the TLV                */
    116     UINT8               lock_byte;                      /* Value in the lock byte                               */
    117     tRW_T1T_LOCK_STATUS lock_status;                    /* Indicates if it is modifed to set tag as Read only   */
    118     BOOLEAN             b_lock_read;                    /* Is the lock byte is already read from tag            */
    119 } tRW_T1T_LOCK;
    120 
    121 typedef struct
    122 {
    123     UINT8               addr;                           /* ADD/ADD8/ADDS field value                            */
    124     UINT8               op_code;                        /* Command sent                                         */
    125     UINT8               rsp_len;                        /* expected length of the response                      */
    126     UINT8               pend_retx_rsp;                  /* Number of pending rsps to retransmission on prev cmd */
    127 } tRW_T1T_PREV_CMD_RSP_INFO;
    128 
    129 #if (defined (RW_NDEF_INCLUDED) && (RW_NDEF_INCLUDED == TRUE))
    130 #define T1T_BUFFER_SIZE             T1T_STATIC_SIZE     /* Buffer 0-E block, for easier tlv operation           */
    131 #else
    132 #define T1T_BUFFER_SIZE             T1T_UID_LEN         /* Buffer UID                                           */
    133 #endif
    134 
    135 /* RW Type 1 Tag control blocks */
    136 typedef struct
    137 {
    138     UINT8               hr[T1T_HR_LEN];                     /* Header ROM byte 0 - 0x1y,Header ROM byte 1 - 0x00    */
    139     UINT8               mem[T1T_SEGMENT_SIZE];              /* Tag contents of block 0 or from block 0-E            */
    140     tT1T_CMD_RSP_INFO   *p_cmd_rsp_info;                    /* Pointer to Command rsp info of last sent command     */
    141     UINT8               state;                              /* Current state of RW module                           */
    142     UINT8               tag_attribute;                      /* Present state of the Tag as interpreted by RW        */
    143     BT_HDR              *p_cur_cmd_buf;                     /* Buffer to hold cur sent command for retransmission   */
    144     UINT8               addr;                               /* ADD/ADD8/ADDS value                                  */
    145     tRW_T1T_PREV_CMD_RSP_INFO prev_cmd_rsp_info;            /* Information about previous sent command if retx      */
    146     TIMER_LIST_ENT      timer;                              /* timer to set timelimit for the response to command   */
    147     BOOLEAN             b_update;                           /* Tag header updated                                   */
    148     BOOLEAN             b_rseg;                             /* Segment 0 read from tag                              */
    149     BOOLEAN             b_hard_lock;                        /* Hard lock the tag as part of config tag to Read only */
    150 #if (defined (RW_NDEF_INCLUDED) && (RW_NDEF_INCLUDED == TRUE))
    151     UINT8               segment;                            /* Current Tag segment                                  */
    152     UINT8               substate;                           /* Current substate of RW module                        */
    153     UINT16              work_offset;                        /* Working byte offset                                  */
    154     UINT8               ndef_first_block[T1T_BLOCK_SIZE];   /* Buffer for ndef first block                          */
    155     UINT8               ndef_final_block[T1T_BLOCK_SIZE];   /* Buffer for ndef last block                           */
    156     UINT8               *p_ndef_buffer;                     /* Buffer to store ndef message                         */
    157     UINT16              new_ndef_msg_len;                   /* Lenght of new updating NDEF Message                  */
    158     UINT8               block_read;                         /* Last read Block                                      */
    159     UINT8               write_byte;                         /* Index of last written byte                           */
    160     UINT8               tlv_detect;                         /* TLV type under detection                             */
    161     UINT16              ndef_msg_offset;                    /* The offset on Tag where first NDEF message is present*/
    162     UINT16              ndef_msg_len;                       /* Lenght of NDEF Message                               */
    163     UINT16              max_ndef_msg_len;                   /* Maximum size of NDEF that can be written on the tag  */
    164     UINT16              ndef_header_offset;                 /* The offset on Tag where first NDEF tlv is present    */
    165     UINT8               ndef_block_written;                 /* Last block where NDEF bytes are written              */
    166     UINT8               num_ndef_finalblock;                /* Block number where NDEF's last byte will be present  */
    167     UINT8               num_lock_tlvs;                      /* Number of lcok tlvs detected in the tag              */
    168     tRW_T1T_LOCK_INFO   lock_tlv[RW_T1T_MAX_LOCK_TLVS];     /* Information retrieved from lock control tlv          */
    169     UINT8               num_lockbytes;                      /* Number of dynamic lock bytes present in the tag      */
    170     tRW_T1T_LOCK        lockbyte[RW_T1T_MAX_LOCK_BYTES];    /* Dynamic Lock byte information                        */
    171     UINT8               num_mem_tlvs;                       /* Number of memory tlvs detected in the tag            */
    172     tRW_T1T_RES_INFO    mem_tlv[RW_T1T_MAX_MEM_TLVS];       /* Information retrieved from mem tlv                   */
    173     UINT8               attr_seg;                           /* Tag segment for which attributes are prepared        */
    174     UINT8               lock_attr_seg;                      /* Tag segment for which lock attributes are prepared   */
    175     UINT8               attr[T1T_BLOCKS_PER_SEGMENT];       /* byte information - Reserved/lock/otp or data         */
    176     UINT8               lock_attr[T1T_BLOCKS_PER_SEGMENT];  /* byte information - read only or read write           */
    177 #endif
    178 } tRW_T1T_CB;
    179 
    180 /* Mifare Ultalight/ Ultralight Family blank tag version block settings */
    181 #define T2T_MIFARE_VERSION_BLOCK                        0x04    /* Block where version number of the tag is stored */
    182 #define T2T_MIFARE_ULTRALIGHT_VER_NO                    0xFFFF  /* Blank Ultralight tag - Block 4 (byte 0, byte 1) */
    183 #define T2T_MIFARE_ULTRALIGHT_FAMILY_VER_NO             0x0200  /* Blank Ultralight family tag - Block 4 (byte 0, byte 1) */
    184 
    185 /* Infineon my-d move / my-d blank tag uid block settings */
    186 #define T2T_INFINEON_VERSION_BLOCK                      0x00
    187 #define T2T_INFINEON_MYD_MOVE_LEAN                      0x0570
    188 #define T2T_INFINEON_MYD_MOVE                           0x0530
    189 
    190 #define T2T_BRCM_VERSION_BLOCK                          0x00
    191 #define T2T_BRCM_STATIC_MEM                             0x2E01
    192 #define T2T_BRCM_DYNAMIC_MEM                            0x2E02
    193 
    194 /* CC2 value on MiFare ULC tag */
    195 #define T2T_MIFARE_ULC_TMS                              0x12
    196 /* Possible corrupt cc2 value range on MiFare ULC tags */
    197 #define T2T_INVALID_CC_TMS_VAL0                         0x10
    198 #define T2T_INVALID_CC_TMS_VAL1                         0x1F
    199 
    200 #define T2T_NDEF_NOT_DETECTED                           0x00
    201 #define T2T_NDEF_DETECTED                               0x01
    202 #define T2T_NDEF_READ                                   0x02
    203 
    204 #define T2T_MAX_NDEF_OFFSET                             128     /* Max offset of an NDEF message in a T2 tag */
    205 #define T2T_MAX_RESERVED_BYTES_IN_TAG                   0x64
    206 #define T2T_MAX_LOCK_BYTES_IN_TAG                       0x64
    207 
    208 #define RW_T2T_MAX_MEM_TLVS                             0x05    /* Maximum supported Memory control TLVS in the tag         */
    209 #define RW_T2T_MAX_LOCK_TLVS                            0x05    /* Maximum supported Lock control TLVS in the tag           */
    210 #define RW_T2T_MAX_LOCK_BYTES                           0x1E    /* Maximum supported dynamic lock bytes                     */
    211 #define RW_T2T_SEGMENT_BYTES                            128
    212 #define RW_T2T_SEGMENT_SIZE                             16
    213 
    214 #define RW_T2T_LOCK_NOT_UPDATED                         0x00    /* Lock not yet set as part of SET TAG RO op                */
    215 #define RW_T2T_LOCK_UPDATE_INITIATED                    0x01    /* Sent command to set the Lock bytes                       */
    216 #define RW_T2T_LOCK_UPDATED                             0x02    /* Lock bytes are set                                       */
    217 typedef UINT8 tRW_T2T_LOCK_STATUS;
    218 
    219 
    220 /* States */
    221 #define RW_T2T_STATE_NOT_ACTIVATED                      0x00    /* Tag not activated                                        */
    222 #define RW_T2T_STATE_IDLE                               0x01    /* T1 Tag activated and ready to perform rw operation on Tag*/
    223 #define RW_T2T_STATE_READ                               0x02    /* waiting response for read command sent to tag            */
    224 #define RW_T2T_STATE_WRITE                              0x03    /* waiting response for write command sent to tag           */
    225 #define RW_T2T_STATE_SELECT_SECTOR                      0x04    /* Waiting response for sector select command               */
    226 #define RW_T2T_STATE_DETECT_TLV                         0x05    /* Detecting Lock/Memory/NDEF/Proprietary TLV in the Tag    */
    227 #define RW_T2T_STATE_READ_NDEF                          0x06    /* Performing NDEF Read procedure                           */
    228 #define RW_T2T_STATE_WRITE_NDEF                         0x07    /* Performing NDEF Write procedure                          */
    229 #define RW_T2T_STATE_SET_TAG_RO                         0x08    /* Setting Tag as Read only tag                             */
    230 #define RW_T2T_STATE_CHECK_PRESENCE                     0x09    /* Check if Tag is still present                            */
    231 #define RW_T2T_STATE_FORMAT_TAG                         0x0A    /* Format the tag                                           */
    232 
    233 /* rw_t2t_read/rw_t2t_write takes care of sector change if the block to read/write is in a different sector
    234  * Next Substate should be assigned to control variable 'substate' before calling these function for State Machine to
    235  * move back to the particular substate after Sector change is completed and read/write command is sent on new sector       */
    236 
    237 /* Sub states */
    238 #define RW_T2T_SUBSTATE_NONE                            0x00
    239 
    240 /* Sub states in RW_T2T_STATE_SELECT_SECTOR state */
    241 #define RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR_SUPPORT      0x01    /* waiting for response of sector select CMD 1              */
    242 #define RW_T2T_SUBSTATE_WAIT_SELECT_SECTOR              0x02    /* waiting for response of sector select CMD 2              */
    243 
    244 /* Sub states in RW_T1T_STATE_DETECT_XXX state */
    245 #define RW_T2T_SUBSTATE_WAIT_READ_CC                    0x03    /* waiting for the detection of a tlv in a tag              */
    246 #define RW_T2T_SUBSTATE_WAIT_TLV_DETECT                 0x04    /* waiting for the detection of a tlv in a tag              */
    247 #define RW_T2T_SUBSTATE_WAIT_FIND_LEN_FIELD_LEN         0x05    /* waiting for finding the len field is 1 or 3 bytes long   */
    248 #define RW_T2T_SUBSTATE_WAIT_READ_TLV_LEN0              0x06    /* waiting for extracting len field value                   */
    249 #define RW_T2T_SUBSTATE_WAIT_READ_TLV_LEN1              0x07    /* waiting for extracting len field value                   */
    250 #define RW_T2T_SUBSTATE_WAIT_READ_TLV_VALUE             0x08    /* waiting for extracting value field in the TLV            */
    251 #define RW_T2T_SUBSTATE_WAIT_READ_LOCKS                 0x09    /* waiting for reading dynamic locks in the TLV             */
    252 
    253 /* Sub states in RW_T2T_STATE_WRITE_NDEF state */
    254 #define RW_T2T_SUBSTATE_WAIT_READ_NDEF_FIRST_BLOCK      0x0A    /* waiting for rsp to reading the block where NDEF starts   */
    255 #define RW_T2T_SUBSTATE_WAIT_READ_NDEF_LAST_BLOCK       0x0B    /* waiting for rsp to reading block where new NDEF Msg ends */
    256 #define RW_T2T_SUBSTATE_WAIT_READ_TERM_TLV_BLOCK        0x0C    /* waiting for rsp to reading block where Trm tlv gets added*/
    257 #define RW_T2T_SUBSTATE_WAIT_READ_NDEF_NEXT_BLOCK       0x0D    /* waiting for rsp to reading block where nxt NDEF write    */
    258 #define RW_T2T_SUBSTATE_WAIT_WRITE_NDEF_NEXT_BLOCK      0x0E    /* waiting for rsp to writting NDEF block                   */
    259 #define RW_T2T_SUBSTATE_WAIT_WRITE_NDEF_LAST_BLOCK      0x0F    /* waiting for rsp to last NDEF block write cmd             */
    260 #define RW_T2T_SUBSTATE_WAIT_READ_NDEF_LEN_BLOCK        0x10    /* waiting for rsp to reading NDEF len field block          */
    261 #define RW_T2T_SUBSTATE_WAIT_WRITE_NDEF_LEN_BLOCK       0x11    /* waiting for rsp of updating first NDEF len field block   */
    262 #define RW_T2T_SUBSTATE_WAIT_WRITE_NDEF_LEN_NEXT_BLOCK  0x12    /* waiting for rsp of updating next NDEF len field block    */
    263 #define RW_T2T_SUBSTATE_WAIT_WRITE_TERM_TLV_CMPLT       0x13    /* waiting for rsp to writing to Terminator tlv             */
    264 
    265 /* Sub states in RW_T2T_STATE_FORMAT_TAG state */
    266 #define RW_T2T_SUBSTATE_WAIT_READ_VERSION_INFO          0x14
    267 #define RW_T2T_SUBSTATE_WAIT_SET_CC                     0x15    /* waiting for response to format/set capability container  */
    268 #define RW_T2T_SUBSTATE_WAIT_SET_LOCK_TLV               0x16
    269 #define RW_T2T_SUBSTATE_WAIT_SET_NULL_NDEF              0x17    /* waiting for response to format/set NULL NDEF             */
    270 
    271 /* Sub states in RW_T2T_STATE_SET_TAG_RO state */
    272 #define RW_T2T_SUBSTATE_WAIT_SET_CC_RO                  0x19    /* waiting for response to set CC3 to RO                    */
    273 #define RW_T2T_SUBSTATE_WAIT_READ_DYN_LOCK_BYTE_BLOCK   0x1A    /* waiting for response to read dynamic lock bytes block    */
    274 #define RW_T2T_SUBSTATE_WAIT_SET_DYN_LOCK_BITS          0x1B    /* waiting for response to set dynamic lock bits            */
    275 #define RW_T2T_SUBSTATE_WAIT_SET_ST_LOCK_BITS           0x1C    /* waiting for response to set static lock bits             */
    276 
    277 typedef struct
    278 {
    279     UINT16              offset;                             /* Offset of the lock byte in the Tag                       */
    280     UINT8               num_bits;                           /* Number of lock bits in the lock byte                     */
    281     UINT8               bytes_locked_per_bit;               /* No. of tag bytes gets locked by a bit in this byte       */
    282 } tRW_T2T_LOCK_INFO;
    283 
    284 typedef struct
    285 {
    286     UINT16  offset;                                         /* Reserved bytes offset taken from Memory control TLV      */
    287     UINT8   num_bytes;                                      /* Number of reserved bytes as per the TLV                  */
    288 }tRW_T2T_RES_INFO;
    289 
    290 typedef struct
    291 {
    292     UINT8               tlv_index;                          /* Index of Lock control tlv that points to this address    */
    293     UINT8               byte_index;                         /* Index of Lock byte pointed by the TLV                    */
    294     UINT8               lock_byte;                          /* Value in the lock byte                                   */
    295     tRW_T2T_LOCK_STATUS lock_status;                        /* Indicates if it is modifed to set tag as Read only       */
    296     BOOLEAN             b_lock_read;                        /* Is the lock byte is already read from tag                */
    297 } tRW_T2T_LOCK;
    298 
    299 /* RW Type 2 Tag control block */
    300 typedef struct
    301 {
    302     UINT8               state;                              /* Reader/writer state                                          */
    303     UINT8               substate;                           /* Reader/write substate in NDEF write state                    */
    304     UINT8               prev_substate;                      /* Substate of the tag before moving to different sector        */
    305     UINT8               sector;                             /* Sector number that is selected                               */
    306     UINT8               select_sector;                      /* Sector number that is expected to get selected               */
    307     UINT8               tag_hdr[T2T_READ_DATA_LEN];         /* T2T Header blocks                                            */
    308     UINT8               tag_data[T2T_READ_DATA_LEN];        /* T2T Block 4 - 7 data                                         */
    309     UINT8               ndef_status;                        /* The current status of NDEF Write operation                   */
    310     UINT16              block_read;                         /* Read block                                                   */
    311     UINT16              block_written;                      /* Written block                                                */
    312     tT2T_CMD_RSP_INFO   *p_cmd_rsp_info;                    /* Pointer to Command rsp info of last sent command             */
    313     BT_HDR              *p_cur_cmd_buf;                     /* Copy of current command, for retx/send after sector change   */
    314     BT_HDR              *p_sec_cmd_buf;                     /* Copy of command, to send after sector change                 */
    315     TIMER_LIST_ENT      t2_timer;                           /* timeout for each API call                                    */
    316     BOOLEAN             b_read_hdr;                         /* Tag header read from tag                                     */
    317     BOOLEAN             b_read_data;                        /* Tag data block read from tag                                 */
    318     BOOLEAN             b_hard_lock;                        /* Hard lock the tag as part of config tag to Read only         */
    319 #if (defined (RW_NDEF_INCLUDED) && (RW_NDEF_INCLUDED == TRUE))
    320     UINT8               found_tlv;                          /* The Tlv found while searching a particular TLV               */
    321     UINT8               tlv_detect;                         /* TLV type under detection                                     */
    322     UINT8               num_lock_tlvs;                      /* Number of lcok tlvs detected in the tag                      */
    323     UINT8               attr_seg;                           /* Tag segment for which attributes are prepared                */
    324     UINT8               lock_attr_seg;                      /* Tag segment for which lock attributes are prepared           */
    325     UINT8               segment;                            /* Current operating segment                                    */
    326     UINT8               ndef_final_block[T2T_BLOCK_SIZE];   /* Buffer for ndef last block                                   */
    327     UINT8               num_mem_tlvs;                       /* Number of memory tlvs detected in the tag                    */
    328     UINT8               num_lockbytes;                      /* Number of dynamic lock bytes present in the tag              */
    329     UINT8               attr[RW_T2T_SEGMENT_SIZE];          /* byte information - Reserved/lock/otp or data                 */
    330     UINT8               lock_attr[RW_T2T_SEGMENT_SIZE];     /* byte information - read only or read write                   */
    331     UINT8               tlv_value[3];                       /* Read value field of TLV                                      */
    332     UINT8               ndef_first_block[T2T_BLOCK_LEN];    /* NDEF TLV Header block                                        */
    333     UINT8               ndef_read_block[T2T_BLOCK_LEN];     /* Buffer to hold read before write block                       */
    334     UINT8               ndef_last_block[T2T_BLOCK_LEN];     /* Terminator TLV block after NDEF Write operation              */
    335     UINT8               terminator_tlv_block[T2T_BLOCK_LEN];/* Terminator TLV Block                                         */
    336     UINT16              ndef_last_block_num;                /* Block where last byte of updating ndef message will exist    */
    337     UINT16              ndef_read_block_num;                /* Block read during NDEF Write to avoid overwritting res bytes */
    338     UINT16              bytes_count;                        /* No. of bytes remaining to collect during tlv detect          */
    339     UINT16              terminator_byte_index;              /* The offset of the tag where terminator tlv may be added      */
    340     UINT16              work_offset;                        /* Working byte offset                                          */
    341     UINT16              ndef_header_offset;
    342     UINT16              ndef_msg_offset;                    /* Offset on Tag where first NDEF message is present            */
    343     UINT16              ndef_msg_len;                       /* Lenght of NDEF Message                                       */
    344     UINT16              max_ndef_msg_len;                   /* Maximum size of NDEF that can be written on the tag          */
    345     UINT16              new_ndef_msg_len;                   /* Lenght of new updating NDEF Message                          */
    346     UINT16              ndef_write_block;
    347     UINT16              prop_msg_len;                       /* Proprietary tlv length                                       */
    348     UINT8               *p_new_ndef_buffer;                 /* Pointer to updating NDEF Message                             */
    349     UINT8               *p_ndef_buffer;                     /* Pointer to NDEF Message                                      */
    350     tRW_T2T_LOCK_INFO   lock_tlv[RW_T2T_MAX_LOCK_TLVS];     /* Information retrieved from lock control tlv                  */
    351     tRW_T2T_LOCK        lockbyte[RW_T2T_MAX_LOCK_BYTES];    /* Dynamic Lock byte information                                */
    352     tRW_T2T_RES_INFO    mem_tlv[RW_T2T_MAX_MEM_TLVS];       /* Information retrieved from mem tlv                           */
    353 #endif
    354 } tRW_T2T_CB;
    355 
    356 /* Type 3 Tag control block */
    357 typedef UINT8 tRW_T3T_RW_STATE;
    358 
    359 typedef struct
    360 {
    361     tNFC_STATUS         status;
    362     UINT8               version;        /* Ver: peer version */
    363     UINT8               nbr;            /* NBr: number of blocks that can be read using one Check command */
    364     UINT8               nbw;            /* Nbw: number of blocks that can be written using one Update command */
    365     UINT16              nmaxb;          /* Nmaxb: maximum number of blocks available for NDEF data */
    366     UINT8               writef;         /* WriteFlag: 00h if writing data finished; 0Fh if writing data in progress */
    367     UINT8               rwflag;         /* RWFlag: 00h NDEF is read-only; 01h if read/write available */
    368     UINT32              ln;             /* Ln: actual size of stored NDEF data (in bytes) */
    369 } tRW_T3T_DETECT;
    370 
    371 /* RW_T3T control block flags */
    372 #define RW_T3T_FL_IS_FINAL_NDEF_SEGMENT         0x01    /* The final command for completing the NDEF read/write */
    373 #define RW_T3T_FL_W4_PRESENCE_CHECK_POLL_RSP    0x02    /* Waiting for POLL response for presence check */
    374 #define RW_T3T_FL_W4_GET_SC_POLL_RSP            0x04    /* Waiting for POLL response for RW_T3tGetSystemCodes */
    375 #define RW_T3T_FL_W4_NDEF_DETECT_POLL_RSP       0x08    /* Waiting for POLL response for RW_T3tDetectNDef */
    376 #define RW_T3T_FL_W4_FMT_FELICA_LITE_POLL_RSP   0x10    /* Waiting for POLL response for RW_T3tFormat */
    377 #define RW_T3T_FL_W4_SRO_FELICA_LITE_POLL_RSP   0x20    /* Waiting for POLL response for RW_T3tSetReadOnly */
    378 
    379 typedef struct
    380 {
    381     UINT32              cur_tout;               /* Current command timeout */
    382     tRW_T3T_RW_STATE    rw_state;               /* Reader/writer state */
    383     UINT8               rw_substate;
    384     UINT8               cur_cmd;                /* Current command being executed */
    385     BT_HDR              *p_cur_cmd_buf;         /* Copy of current command, for retransmission */
    386     TIMER_LIST_ENT      timer;                  /* timeout for waiting for response */
    387     TIMER_LIST_ENT      poll_timer;             /* timeout for waiting for response */
    388 
    389     tRW_T3T_DETECT      ndef_attrib;            /* T3T NDEF attribute information */
    390 
    391     UINT32              ndef_msg_len;           /* Length of ndef message to send */
    392     UINT32              ndef_msg_bytes_sent;    /* Length of ndef message sent so far */
    393     UINT8               *ndef_msg;              /* Buffer for outgoing NDEF message */
    394     UINT32              ndef_rx_readlen;        /* Number of bytes read in current CHECK command */
    395     UINT32              ndef_rx_offset;         /* Length of ndef message read so far */
    396 
    397     UINT8               num_system_codes;       /* System codes detected */
    398     UINT16              system_codes[T3T_MAX_SYSTEM_CODES];
    399 
    400     UINT8               peer_nfcid2[NCI_NFCID2_LEN];
    401     UINT8               cur_poll_rc;            /* RC used in current POLL command */
    402 
    403     UINT8               flags;                  /* Flags see RW_T3T_FL_* */
    404 } tRW_T3T_CB;
    405 
    406 
    407 /*
    408 **  Type 4 Tag
    409 */
    410 
    411 /* Max data size using a single ReadBinary. 2 bytes are for status bytes */
    412 #define RW_T4T_MAX_DATA_PER_READ           (NFC_RW_POOL_BUF_SIZE - BT_HDR_SIZE - NCI_DATA_HDR_SIZE - T4T_RSP_STATUS_WORDS_SIZE)
    413 
    414 /* Max data size using a single UpdateBinary. 6 bytes are for CLA, INS, P1, P2, Lc */
    415 #define RW_T4T_MAX_DATA_PER_WRITE          (NFC_RW_POOL_BUF_SIZE - BT_HDR_SIZE - NCI_MSG_OFFSET_SIZE - NCI_DATA_HDR_SIZE - T4T_CMD_MAX_HDR_SIZE)
    416 
    417 
    418 
    419 /* Mandatory NDEF file control */
    420 typedef struct
    421 {
    422     UINT16              file_id;        /* File Identifier          */
    423     UINT16              max_file_size;  /* Max NDEF file size       */
    424     UINT8               read_access;    /* read access condition    */
    425     UINT8               write_access;   /* write access condition   */
    426 } tRW_T4T_NDEF_FC;
    427 
    428 /* Capability Container */
    429 typedef struct
    430 {
    431     UINT16              cclen;      /* the size of this capability container        */
    432     UINT8               version;    /* the mapping specification version            */
    433     UINT16              max_le;     /* the max data size by a single ReadBinary     */
    434     UINT16              max_lc;     /* the max data size by a single UpdateBinary   */
    435     tRW_T4T_NDEF_FC     ndef_fc;    /* Mandatory NDEF file control                  */
    436 } tRW_T4T_CC;
    437 
    438 typedef UINT8 tRW_T4T_RW_STATE;
    439 typedef UINT8 tRW_T4T_RW_SUBSTATE;
    440 
    441 /* Type 4 Tag Control Block */
    442 typedef struct
    443 {
    444     tRW_T4T_RW_STATE    state;              /* main state                       */
    445     tRW_T4T_RW_SUBSTATE sub_state;          /* sub state                        */
    446     UINT8               version;            /* currently effective version      */
    447     TIMER_LIST_ENT      timer;              /* timeout for each API call        */
    448 
    449     UINT16              ndef_length;        /* length of NDEF data              */
    450     UINT8              *p_update_data;      /* pointer of data to update        */
    451     UINT16              rw_length;          /* remaining bytes to read/write    */
    452     UINT16              rw_offset;          /* remaining offset to read/write   */
    453     BT_HDR             *p_data_to_free;     /* GKI buffet to delete after done  */
    454 
    455     tRW_T4T_CC          cc_file;            /* Capability Container File        */
    456 
    457 #define RW_T4T_NDEF_STATUS_NDEF_DETECTED    0x01    /* NDEF has been detected   */
    458 #define RW_T4T_NDEF_STATUS_NDEF_READ_ONLY   0x02    /* NDEF file is read-only   */
    459 
    460     UINT8               ndef_status;        /* bitmap for NDEF status           */
    461 
    462     UINT16              max_read_size;      /* max reading size per a command   */
    463     UINT16              max_update_size;    /* max updating size per a command  */
    464 } tRW_T4T_CB;
    465 
    466 /* RW retransmission statistics */
    467 #if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
    468 typedef struct
    469 {
    470     UINT32              start_tick;         /* System tick count at activation */
    471     UINT32              bytes_sent;         /* Total bytes sent since activation */
    472     UINT32              bytes_received;     /* Total bytes received since activation */
    473     UINT32              num_ops;            /* Number of operations since activation */
    474     UINT32              num_retries;        /* Number of retranmissions since activation */
    475     UINT32              num_crc;            /* Number of crc failures */
    476     UINT32              num_trans_err;      /* Number of transmission error notifications */
    477     UINT32              num_fail;           /* Number of aborts (failures after retries) */
    478 } tRW_STATS;
    479 #endif  /* RW_STATS_INCLUDED */
    480 
    481 /* ISO 15693 RW Control Block */
    482 typedef UINT8 tRW_I93_RW_STATE;
    483 typedef UINT8 tRW_I93_RW_SUBSTATE;
    484 
    485 #define RW_I93_FLAG_READ_ONLY           0x01    /* tag is read-only                        */
    486 #define RW_I93_FLAG_READ_MULTI_BLOCK    0x02    /* tag supports read multi block           */
    487 #define RW_I93_FLAG_RESET_DSFID         0x04    /* need to reset DSFID for formatting      */
    488 #define RW_I93_FLAG_RESET_AFI           0x08    /* need to reset AFI for formatting        */
    489 #define RW_I93_FLAG_16BIT_NUM_BLOCK     0x10    /* use 2 bytes for number of blocks        */
    490 
    491 #define RW_I93_TLV_DETECT_STATE_TYPE      0x01  /* searching for type                      */
    492 #define RW_I93_TLV_DETECT_STATE_LENGTH_1  0x02  /* searching for the first byte of length  */
    493 #define RW_I93_TLV_DETECT_STATE_LENGTH_2  0x03  /* searching for the second byte of length */
    494 #define RW_I93_TLV_DETECT_STATE_LENGTH_3  0x04  /* searching for the third byte of length  */
    495 #define RW_I93_TLV_DETECT_STATE_VALUE     0x05  /* reading value field                     */
    496 
    497 enum
    498 {
    499     RW_I93_ICODE_SLI,                   /* ICODE SLI, SLIX                  */
    500     RW_I93_ICODE_SLI_S,                 /* ICODE SLI-S, SLIX-S              */
    501     RW_I93_ICODE_SLI_L,                 /* ICODE SLI-L, SLIX-L              */
    502     RW_I93_TAG_IT_HF_I_PLUS_INLAY,      /* Tag-it HF-I Plus Inlay           */
    503     RW_I93_TAG_IT_HF_I_PLUS_CHIP,       /* Tag-it HF-I Plus Chip            */
    504     RW_I93_TAG_IT_HF_I_STD_CHIP_INLAY,  /* Tag-it HF-I Standard Chip/Inlyas */
    505     RW_I93_TAG_IT_HF_I_PRO_CHIP_INLAY,  /* Tag-it HF-I Pro Chip/Inlyas      */
    506     RW_I93_STM_LRI1K,                   /* STM LRI1K                        */
    507     RW_I93_STM_LRI2K,                   /* STM LRI2K                        */
    508     RW_I93_STM_LRIS2K,                  /* STM LRIS2K                       */
    509     RW_I93_STM_LRIS64K,                 /* STM LRIS64K                      */
    510     RW_I93_STM_M24LR64_R,               /* STM M24LR64-R                    */
    511     RW_I93_STM_M24LR04E_R,              /* STM M24LR04E-R                   */
    512     RW_I93_STM_M24LR16E_R,              /* STM M24LR16E-R                   */
    513     RW_I93_STM_M24LR64E_R,              /* STM M24LR64E-R                   */
    514     RW_I93_UNKNOWN_PRODUCT              /* Unknwon product version          */
    515 };
    516 
    517 typedef struct
    518 {
    519     tRW_I93_RW_STATE    state;                  /* main state                       */
    520     tRW_I93_RW_SUBSTATE sub_state;              /* sub state                        */
    521     TIMER_LIST_ENT      timer;                  /* timeout for each sent command    */
    522     UINT8               sent_cmd;               /* last sent command                */
    523     UINT8               retry_count;            /* number of retry                  */
    524     BT_HDR             *p_retry_cmd;            /* buffer to store cmd sent last    */
    525 
    526     UINT8               info_flags;             /* information flags                */
    527     UINT8               uid[I93_UID_BYTE_LEN];  /* UID of currently activated       */
    528     UINT8               dsfid;                  /* DSFID if I93_INFO_FLAG_DSFID     */
    529     UINT8               afi;                    /* AFI if I93_INFO_FLAG_AFI         */
    530     UINT8               block_size;             /* block size of tag, in bytes      */
    531     UINT16              num_block;              /* number of blocks in tag          */
    532     UINT8               ic_reference;           /* IC Reference of tag              */
    533     UINT8               product_version;        /* tag product version              */
    534 
    535     UINT8               intl_flags;             /* flags for internal information   */
    536 
    537     UINT8               tlv_detect_state;       /* TLV detecting state              */
    538     UINT8               tlv_type;               /* currently detected type          */
    539     UINT16              tlv_length;             /* currently detected length        */
    540 
    541     UINT16              ndef_tlv_start_offset;  /* offset of first byte of NDEF TLV */
    542     UINT16              ndef_tlv_last_offset;   /* offset of last byte of NDEF TLV  */
    543     UINT16              max_ndef_length;        /* max NDEF length the tag contains */
    544     UINT16              ndef_length;            /* length of NDEF data              */
    545 
    546     UINT8              *p_update_data;          /* pointer of data to update        */
    547     UINT16              rw_length;              /* bytes to read/write              */
    548     UINT16              rw_offset;              /* offset to read/write             */
    549 } tRW_I93_CB;
    550 
    551 /* RW memory control blocks */
    552 typedef union
    553 {
    554     tRW_T1T_CB          t1t;
    555     tRW_T2T_CB          t2t;
    556     tRW_T3T_CB          t3t;
    557     tRW_T4T_CB          t4t;
    558     tRW_I93_CB          i93;
    559 } tRW_TCB;
    560 
    561 /* RW control blocks */
    562 typedef struct
    563 {
    564     tRW_TCB             tcb;
    565     tRW_CBACK           *p_cback;
    566     UINT32              cur_retry;          /* Retry count for the current operation */
    567 #if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
    568     tRW_STATS           stats;
    569 #endif  /* RW_STATS_INCLUDED */
    570     UINT8               trace_level;
    571 } tRW_CB;
    572 
    573 
    574 /*****************************************************************************
    575 **  EXTERNAL FUNCTION DECLARATIONS
    576 *****************************************************************************/
    577 
    578 #ifdef __cplusplus
    579 extern "C" {
    580 #endif
    581 
    582 /* Global NFC data */
    583 #if NFC_DYNAMIC_MEMORY == FALSE
    584 NFC_API extern tRW_CB  rw_cb;
    585 #else
    586 NFC_API extern tRW_CB *rw_cb_ptr;
    587 #define rw_cb (*rw_cb_ptr)
    588 #endif
    589 
    590 /* from .c */
    591 
    592 #if (defined (RW_NDEF_INCLUDED) && (RW_NDEF_INCLUDED == TRUE))
    593 extern tRW_EVENT rw_t1t_handle_rsp (const tT1T_CMD_RSP_INFO * p_info, BOOLEAN *p_notify, UINT8 *p_data, tNFC_STATUS *p_status);
    594 extern tRW_EVENT rw_t1t_info_to_event (const tT1T_CMD_RSP_INFO * p_info);
    595 #else
    596 #define rw_t1t_handle_rsp(p, a, b, c)       t1t_info_to_evt (p)
    597 #define rw_t1t_info_to_event(p)             t1t_info_to_evt (p)
    598 #endif
    599 
    600 extern void rw_init (void);
    601 extern tNFC_STATUS rw_t1t_select (UINT8 hr[T1T_HR_LEN], UINT8 uid[T1T_CMD_UID_LEN]);
    602 extern tNFC_STATUS rw_t1t_send_dyn_cmd (UINT8 opcode, UINT8 add, UINT8 *p_dat);
    603 extern tNFC_STATUS rw_t1t_send_static_cmd (UINT8 opcode, UINT8 add, UINT8 dat);
    604 extern void rw_t1t_process_timeout (TIMER_LIST_ENT *p_tle);
    605 extern void rw_t1t_handle_op_complete (void);
    606 
    607 #if (defined (RW_NDEF_INCLUDED) && (RW_NDEF_INCLUDED == TRUE))
    608 extern tRW_EVENT rw_t2t_info_to_event (const tT2T_CMD_RSP_INFO *p_info);
    609 extern void rw_t2t_handle_rsp (UINT8 *p_data);
    610 #else
    611 #define rw_t2t_info_to_event(p)             t2t_info_to_evt (p)
    612 #define rw_t2t_handle_rsp(p)
    613 #endif
    614 
    615 extern tNFC_STATUS rw_t2t_sector_change (UINT8 sector);
    616 extern tNFC_STATUS rw_t2t_read (UINT16 block);
    617 extern tNFC_STATUS rw_t2t_write (UINT16 block, UINT8 *p_write_data);
    618 extern void rw_t2t_process_timeout (TIMER_LIST_ENT *p_tle);
    619 extern tNFC_STATUS rw_t2t_select (void);
    620 void rw_t2t_handle_op_complete (void);
    621 
    622 extern void rw_t3t_process_timeout (TIMER_LIST_ENT *p_tle);
    623 extern tNFC_STATUS rw_t3t_select (UINT8 peer_nfcid2[NCI_RF_F_UID_LEN], UINT8 mrti_check, UINT8 mrti_update);
    624 void rw_t3t_handle_nci_poll_ntf (UINT8 nci_status, UINT8 num_responses, UINT8 sensf_res_buf_size, UINT8 *p_sensf_res_buf);
    625 
    626 extern tNFC_STATUS rw_t4t_select (void);
    627 extern void rw_t4t_process_timeout (TIMER_LIST_ENT *p_tle);
    628 
    629 extern tNFC_STATUS rw_i93_select (UINT8 *p_uid);
    630 extern void rw_i93_process_timeout (TIMER_LIST_ENT *p_tle);
    631 
    632 #if (defined (RW_STATS_INCLUDED) && (RW_STATS_INCLUDED == TRUE))
    633 /* Internal fcns for statistics (from rw_main.c) */
    634 void rw_main_reset_stats (void);
    635 void rw_main_update_tx_stats (UINT32 bytes_tx, BOOLEAN is_retry);
    636 void rw_main_update_rx_stats (UINT32 bytes_rx);
    637 void rw_main_update_crc_error_stats (void);
    638 void rw_main_update_trans_error_stats (void);
    639 void rw_main_update_fail_stats (void);
    640 void rw_main_log_stats (void);
    641 #endif  /* RW_STATS_INCLUDED */
    642 
    643 #ifdef __cplusplus
    644 }
    645 #endif
    646 
    647 #endif /* RW_INT_H_ */
    648