Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (C) 2014  STMicroelectronics SAS. All rights reserved.
0004  */
0005 
0006 #ifndef __LOCAL_ST21NFCA_H_
0007 #define __LOCAL_ST21NFCA_H_
0008 
0009 #include <net/nfc/hci.h>
0010 #include <linux/skbuff.h>
0011 #include <linux/workqueue.h>
0012 
0013 #define HCI_MODE 0
0014 
0015 /* framing in HCI mode */
0016 #define ST21NFCA_SOF_EOF_LEN    2
0017 
0018 /* Almost every time value is 0 */
0019 #define ST21NFCA_HCI_LLC_LEN    1
0020 
0021 /* Size in worst case :
0022  * In normal case CRC len = 2 but byte stuffing
0023  * may appear in case one CRC byte = ST21NFCA_SOF_EOF
0024  */
0025 #define ST21NFCA_HCI_LLC_CRC    4
0026 
0027 #define ST21NFCA_HCI_LLC_LEN_CRC        (ST21NFCA_SOF_EOF_LEN + \
0028                         ST21NFCA_HCI_LLC_LEN + \
0029                         ST21NFCA_HCI_LLC_CRC)
0030 #define ST21NFCA_HCI_LLC_MIN_SIZE       (1 + ST21NFCA_HCI_LLC_LEN_CRC)
0031 
0032 /* Worst case when adding byte stuffing between each byte */
0033 #define ST21NFCA_HCI_LLC_MAX_PAYLOAD    29
0034 #define ST21NFCA_HCI_LLC_MAX_SIZE       (ST21NFCA_HCI_LLC_LEN_CRC + 1 + \
0035                     ST21NFCA_HCI_LLC_MAX_PAYLOAD)
0036 
0037 /* Reader RF commands */
0038 #define ST21NFCA_WR_XCHG_DATA           0x10
0039 
0040 #define ST21NFCA_DEVICE_MGNT_GATE       0x01
0041 #define ST21NFCA_RF_READER_F_GATE       0x14
0042 #define ST21NFCA_RF_CARD_F_GATE     0x24
0043 #define ST21NFCA_APDU_READER_GATE   0xf0
0044 #define ST21NFCA_CONNECTIVITY_GATE  0x41
0045 
0046 /*
0047  * ref ISO7816-3 chap 8.1. the initial character TS is followed by a
0048  * sequence of at most 32 characters.
0049  */
0050 #define ST21NFCA_ESE_MAX_LENGTH     33
0051 #define ST21NFCA_ESE_HOST_ID        0xc0
0052 
0053 #define DRIVER_DESC "HCI NFC driver for ST21NFCA"
0054 
0055 #define ST21NFCA_HCI_MODE       0
0056 #define ST21NFCA_NUM_DEVICES        256
0057 
0058 #define ST21NFCA_VENDOR_OUI     0x0080E1 /* STMicroelectronics */
0059 #define ST21NFCA_FACTORY_MODE       2
0060 
0061 struct st21nfca_se_status {
0062     bool is_ese_present;
0063     bool is_uicc_present;
0064 };
0065 
0066 enum st21nfca_state {
0067     ST21NFCA_ST_COLD,
0068     ST21NFCA_ST_READY,
0069 };
0070 
0071 /**
0072  * enum nfc_vendor_cmds - supported nfc vendor commands
0073  *
0074  * @FACTORY_MODE: Allow to set the driver into a mode where no secure element
0075  *  are activated. It does not consider any NFC_ATTR_VENDOR_DATA.
0076  * @HCI_CLEAR_ALL_PIPES: Allow to execute a HCI clear all pipes command.
0077  *  It does not consider any NFC_ATTR_VENDOR_DATA.
0078  * @HCI_DM_PUT_DATA: Allow to configure specific CLF registry as for example
0079  *  RF trimmings or low level drivers configurations (I2C, SPI, SWP).
0080  * @HCI_DM_UPDATE_AID: Allow to configure an AID routing into the CLF routing
0081  *  table following RF technology, CLF mode or protocol.
0082  * @HCI_DM_GET_INFO: Allow to retrieve CLF information.
0083  * @HCI_DM_GET_DATA: Allow to retrieve CLF configurable data such as low
0084  *  level drivers configurations or RF trimmings.
0085  * @HCI_DM_LOAD: Allow to load a firmware into the CLF. A complete
0086  *  packet can be more than 8KB.
0087  * @HCI_DM_RESET: Allow to run a CLF reset in order to "commit" CLF
0088  *  configuration changes without CLF power off.
0089  * @HCI_GET_PARAM: Allow to retrieve an HCI CLF parameter (for example the
0090  *  white list).
0091  * @HCI_DM_FIELD_GENERATOR: Allow to generate different kind of RF
0092  *  technology. When using this command to anti-collision is done.
0093  * @HCI_LOOPBACK: Allow to echo a command and test the Dh to CLF
0094  *  connectivity.
0095  */
0096 enum nfc_vendor_cmds {
0097     FACTORY_MODE,
0098     HCI_CLEAR_ALL_PIPES,
0099     HCI_DM_PUT_DATA,
0100     HCI_DM_UPDATE_AID,
0101     HCI_DM_GET_INFO,
0102     HCI_DM_GET_DATA,
0103     HCI_DM_LOAD,
0104     HCI_DM_RESET,
0105     HCI_GET_PARAM,
0106     HCI_DM_FIELD_GENERATOR,
0107     HCI_LOOPBACK,
0108 };
0109 
0110 struct st21nfca_vendor_info {
0111     struct completion req_completion;
0112     struct sk_buff *rx_skb;
0113 };
0114 
0115 struct st21nfca_dep_info {
0116     struct sk_buff *tx_pending;
0117     struct work_struct tx_work;
0118     u8 curr_nfc_dep_pni;
0119     u32 idx;
0120     u8 to;
0121     u8 did;
0122     u8 bsi;
0123     u8 bri;
0124     u8 lri;
0125 } __packed;
0126 
0127 struct st21nfca_se_info {
0128     u8 atr[ST21NFCA_ESE_MAX_LENGTH];
0129     struct completion req_completion;
0130 
0131     struct timer_list bwi_timer;
0132     int wt_timeout; /* in msecs */
0133     bool bwi_active;
0134 
0135     struct timer_list se_active_timer;
0136     bool se_active;
0137     int expected_pipes;
0138     int count_pipes;
0139 
0140     bool xch_error;
0141 
0142     se_io_cb_t cb;
0143     void *cb_context;
0144     struct work_struct timeout_work;
0145 };
0146 
0147 struct st21nfca_hci_info {
0148     const struct nfc_phy_ops *phy_ops;
0149     void *phy_id;
0150 
0151     struct nfc_hci_dev *hdev;
0152     struct st21nfca_se_status *se_status;
0153 
0154     enum st21nfca_state state;
0155 
0156     struct mutex info_lock;
0157 
0158     int async_cb_type;
0159     data_exchange_cb_t async_cb;
0160     void *async_cb_context;
0161 
0162     struct st21nfca_dep_info dep_info;
0163     struct st21nfca_se_info se_info;
0164     struct st21nfca_vendor_info vendor_info;
0165 };
0166 
0167 int st21nfca_hci_probe(void *phy_id, const struct nfc_phy_ops *phy_ops,
0168                char *llc_name, int phy_headroom, int phy_tailroom,
0169                int phy_payload, struct nfc_hci_dev **hdev,
0170                struct st21nfca_se_status *se_status);
0171 void st21nfca_hci_remove(struct nfc_hci_dev *hdev);
0172 
0173 int st21nfca_dep_event_received(struct nfc_hci_dev *hdev,
0174                 u8 event, struct sk_buff *skb);
0175 int st21nfca_tm_send_dep_res(struct nfc_hci_dev *hdev, struct sk_buff *skb);
0176 
0177 int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len);
0178 int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb);
0179 void st21nfca_dep_init(struct nfc_hci_dev *hdev);
0180 void st21nfca_dep_deinit(struct nfc_hci_dev *hdev);
0181 
0182 int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host,
0183                     u8 event, struct sk_buff *skb);
0184 int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev,
0185                     u8 event, struct sk_buff *skb);
0186 
0187 int st21nfca_hci_discover_se(struct nfc_hci_dev *hdev);
0188 int st21nfca_hci_enable_se(struct nfc_hci_dev *hdev, u32 se_idx);
0189 int st21nfca_hci_disable_se(struct nfc_hci_dev *hdev, u32 se_idx);
0190 int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx,
0191         u8 *apdu, size_t apdu_length,
0192         se_io_cb_t cb, void *cb_context);
0193 
0194 void st21nfca_se_init(struct nfc_hci_dev *hdev);
0195 void st21nfca_se_deinit(struct nfc_hci_dev *hdev);
0196 
0197 int st21nfca_hci_loopback_event_received(struct nfc_hci_dev *ndev, u8 event,
0198                      struct sk_buff *skb);
0199 int st21nfca_vendor_cmds_init(struct nfc_hci_dev *ndev);
0200 
0201 #endif /* __LOCAL_ST21NFCA_H_ */