blob: 5e0de0fef1d4ecfaac811bcc2a66b082bf361a1f [file] [log] [blame]
Thomas Gleixner46fe7772019-05-31 01:09:57 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Christophe Ricard68957302014-03-25 06:51:47 +01002/*
3 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
Christophe Ricard68957302014-03-25 06:51:47 +01004 */
5
6#ifndef __LOCAL_ST21NFCA_H_
7#define __LOCAL_ST21NFCA_H_
8
9#include <net/nfc/hci.h>
Christophe Ricard1c547952015-10-25 22:54:18 +010010#include <linux/skbuff.h>
11#include <linux/workqueue.h>
Christophe Ricard1892bf82014-05-20 22:21:59 +020012
Christophe Ricard68957302014-03-25 06:51:47 +010013#define HCI_MODE 0
14
15/* framing in HCI mode */
16#define ST21NFCA_SOF_EOF_LEN 2
17
18/* Almost every time value is 0 */
19#define ST21NFCA_HCI_LLC_LEN 1
20
21/* Size in worst case :
22 * In normal case CRC len = 2 but byte stuffing
23 * may appear in case one CRC byte = ST21NFCA_SOF_EOF
24 */
25#define ST21NFCA_HCI_LLC_CRC 4
26
27#define ST21NFCA_HCI_LLC_LEN_CRC (ST21NFCA_SOF_EOF_LEN + \
28 ST21NFCA_HCI_LLC_LEN + \
29 ST21NFCA_HCI_LLC_CRC)
30#define ST21NFCA_HCI_LLC_MIN_SIZE (1 + ST21NFCA_HCI_LLC_LEN_CRC)
31
32/* Worst case when adding byte stuffing between each byte */
33#define ST21NFCA_HCI_LLC_MAX_PAYLOAD 29
34#define ST21NFCA_HCI_LLC_MAX_SIZE (ST21NFCA_HCI_LLC_LEN_CRC + 1 + \
35 ST21NFCA_HCI_LLC_MAX_PAYLOAD)
36
Christophe Ricard1c547952015-10-25 22:54:18 +010037/* Reader RF commands */
38#define ST21NFCA_WR_XCHG_DATA 0x10
39
40#define ST21NFCA_DEVICE_MGNT_GATE 0x01
41#define ST21NFCA_RF_READER_F_GATE 0x14
42#define ST21NFCA_RF_CARD_F_GATE 0x24
43#define ST21NFCA_APDU_READER_GATE 0xf0
44#define ST21NFCA_CONNECTIVITY_GATE 0x41
45
46/*
47 * ref ISO7816-3 chap 8.1. the initial character TS is followed by a
48 * sequence of at most 32 characters.
49 */
50#define ST21NFCA_ESE_MAX_LENGTH 33
51#define ST21NFCA_ESE_HOST_ID 0xc0
52
Christophe Ricard68957302014-03-25 06:51:47 +010053#define DRIVER_DESC "HCI NFC driver for ST21NFCA"
54
Christophe Ricard1c547952015-10-25 22:54:18 +010055#define ST21NFCA_HCI_MODE 0
56#define ST21NFCA_NUM_DEVICES 256
Christophe Ricard68957302014-03-25 06:51:47 +010057
Christophe Ricard15d17172015-10-26 07:50:11 +010058#define ST21NFCA_VENDOR_OUI 0x0080E1 /* STMicroelectronics */
59#define ST21NFCA_FACTORY_MODE 2
60
Christophe Ricard2130fb92015-01-27 01:18:19 +010061struct st21nfca_se_status {
62 bool is_ese_present;
63 bool is_uicc_present;
64};
65
Christophe Ricard68957302014-03-25 06:51:47 +010066enum st21nfca_state {
67 ST21NFCA_ST_COLD,
68 ST21NFCA_ST_READY,
69};
70
Christophe Ricard15d17172015-10-26 07:50:11 +010071/**
72 * enum nfc_vendor_cmds - supported nfc vendor commands
73 *
74 * @FACTORY_MODE: Allow to set the driver into a mode where no secure element
75 * are activated. It does not consider any NFC_ATTR_VENDOR_DATA.
76 * @HCI_CLEAR_ALL_PIPES: Allow to execute a HCI clear all pipes command.
77 * It does not consider any NFC_ATTR_VENDOR_DATA.
78 * @HCI_DM_PUT_DATA: Allow to configure specific CLF registry as for example
79 * RF trimmings or low level drivers configurations (I2C, SPI, SWP).
80 * @HCI_DM_UPDATE_AID: Allow to configure an AID routing into the CLF routing
81 * table following RF technology, CLF mode or protocol.
82 * @HCI_DM_GET_INFO: Allow to retrieve CLF information.
83 * @HCI_DM_GET_DATA: Allow to retrieve CLF configurable data such as low
84 * level drivers configurations or RF trimmings.
85 * @HCI_DM_LOAD: Allow to load a firmware into the CLF. A complete
86 * packet can be more than 8KB.
87 * @HCI_DM_RESET: Allow to run a CLF reset in order to "commit" CLF
88 * configuration changes without CLF power off.
89 * @HCI_GET_PARAM: Allow to retrieve an HCI CLF parameter (for example the
90 * white list).
91 * @HCI_DM_FIELD_GENERATOR: Allow to generate different kind of RF
92 * technology. When using this command to anti-collision is done.
93 * @HCI_LOOPBACK: Allow to echo a command and test the Dh to CLF
94 * connectivity.
95 */
96enum nfc_vendor_cmds {
97 FACTORY_MODE,
98 HCI_CLEAR_ALL_PIPES,
99 HCI_DM_PUT_DATA,
100 HCI_DM_UPDATE_AID,
101 HCI_DM_GET_INFO,
102 HCI_DM_GET_DATA,
103 HCI_DM_LOAD,
104 HCI_DM_RESET,
105 HCI_GET_PARAM,
106 HCI_DM_FIELD_GENERATOR,
107 HCI_LOOPBACK,
108};
109
110struct st21nfca_vendor_info {
111 struct completion req_completion;
112 struct sk_buff *rx_skb;
113};
114
Christophe Ricard1c547952015-10-25 22:54:18 +0100115struct st21nfca_dep_info {
116 struct sk_buff *tx_pending;
117 struct work_struct tx_work;
118 u8 curr_nfc_dep_pni;
119 u32 idx;
120 u8 to;
121 u8 did;
122 u8 bsi;
123 u8 bri;
124 u8 lri;
125} __packed;
126
127struct st21nfca_se_info {
128 u8 atr[ST21NFCA_ESE_MAX_LENGTH];
129 struct completion req_completion;
130
131 struct timer_list bwi_timer;
132 int wt_timeout; /* in msecs */
133 bool bwi_active;
134
135 struct timer_list se_active_timer;
136 bool se_active;
137 int expected_pipes;
138 int count_pipes;
139
140 bool xch_error;
141
142 se_io_cb_t cb;
143 void *cb_context;
144};
145
Christophe Ricard68957302014-03-25 06:51:47 +0100146struct st21nfca_hci_info {
147 struct nfc_phy_ops *phy_ops;
148 void *phy_id;
149
150 struct nfc_hci_dev *hdev;
Christophe Ricard2130fb92015-01-27 01:18:19 +0100151 struct st21nfca_se_status *se_status;
Christophe Ricard68957302014-03-25 06:51:47 +0100152
153 enum st21nfca_state state;
154
155 struct mutex info_lock;
156
157 int async_cb_type;
158 data_exchange_cb_t async_cb;
159 void *async_cb_context;
160
Christophe Ricard1892bf82014-05-20 22:21:59 +0200161 struct st21nfca_dep_info dep_info;
Christophe Ricard2130fb92015-01-27 01:18:19 +0100162 struct st21nfca_se_info se_info;
Christophe Ricard15d17172015-10-26 07:50:11 +0100163 struct st21nfca_vendor_info vendor_info;
Christophe Ricard1892bf82014-05-20 22:21:59 +0200164};
Christophe Ricard68957302014-03-25 06:51:47 +0100165
Christophe Ricard1c547952015-10-25 22:54:18 +0100166int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
167 char *llc_name, int phy_headroom, int phy_tailroom,
168 int phy_payload, struct nfc_hci_dev **hdev,
Christophe Ricard15d17172015-10-26 07:50:11 +0100169 struct st21nfca_se_status *se_status);
Christophe Ricard1c547952015-10-25 22:54:18 +0100170void st21nfca_hci_remove(struct nfc_hci_dev *hdev);
Christophe Ricard68957302014-03-25 06:51:47 +0100171
Christophe Ricard1c547952015-10-25 22:54:18 +0100172int st21nfca_dep_event_received(struct nfc_hci_dev *hdev,
173 u8 event, struct sk_buff *skb);
174int st21nfca_tm_send_dep_res(struct nfc_hci_dev *hdev, struct sk_buff *skb);
175
176int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len);
177int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb);
178void st21nfca_dep_init(struct nfc_hci_dev *hdev);
179void st21nfca_dep_deinit(struct nfc_hci_dev *hdev);
180
181int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host,
182 u8 event, struct sk_buff *skb);
183int st21nfca_apdu_reader_event_received(struct nfc_hci_dev *hdev,
184 u8 event, struct sk_buff *skb);
185
186int st21nfca_hci_discover_se(struct nfc_hci_dev *hdev);
187int st21nfca_hci_enable_se(struct nfc_hci_dev *hdev, u32 se_idx);
188int st21nfca_hci_disable_se(struct nfc_hci_dev *hdev, u32 se_idx);
189int st21nfca_hci_se_io(struct nfc_hci_dev *hdev, u32 se_idx,
190 u8 *apdu, size_t apdu_length,
191 se_io_cb_t cb, void *cb_context);
192
193void st21nfca_se_init(struct nfc_hci_dev *hdev);
194void st21nfca_se_deinit(struct nfc_hci_dev *hdev);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200195
Christophe Ricard15d17172015-10-26 07:50:11 +0100196int st21nfca_hci_loopback_event_received(struct nfc_hci_dev *ndev, u8 event,
197 struct sk_buff *skb);
198int st21nfca_vendor_cmds_init(struct nfc_hci_dev *ndev);
199
Christophe Ricard68957302014-03-25 06:51:47 +0100200#endif /* __LOCAL_ST21NFCA_H_ */