xref: /linux/include/net/nfc/nci.h (revision 4949009eb8d40a441dcddcd96e101e77d31cf1b2)
1 /*
2  *  The NFC Controller Interface is the communication protocol between an
3  *  NFC Controller (NFCC) and a Device Host (DH).
4  *
5  *  Copyright (C) 2014 Marvell International Ltd.
6  *  Copyright (C) 2011 Texas Instruments, Inc.
7  *
8  *  Written by Ilan Elias <ilane@ti.com>
9  *
10  *  Acknowledgements:
11  *  This file is based on hci.h, which was written
12  *  by Maxim Krasnyansky.
13  *
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License version 2
16  *  as published by the Free Software Foundation
17  *
18  *  This program is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU General Public License for more details.
22  *
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
25  *
26  */
27 
28 #ifndef __NCI_H
29 #define __NCI_H
30 
31 #include <net/nfc/nfc.h>
32 
33 /* NCI constants */
34 #define NCI_MAX_NUM_MAPPING_CONFIGS				10
35 #define NCI_MAX_NUM_RF_CONFIGS					10
36 #define NCI_MAX_NUM_CONN					10
37 #define NCI_MAX_PARAM_LEN					251
38 
39 /* NCI Status Codes */
40 #define NCI_STATUS_OK						0x00
41 #define NCI_STATUS_REJECTED					0x01
42 #define NCI_STATUS_RF_FRAME_CORRUPTED				0x02
43 #define NCI_STATUS_FAILED					0x03
44 #define NCI_STATUS_NOT_INITIALIZED				0x04
45 #define NCI_STATUS_SYNTAX_ERROR					0x05
46 #define NCI_STATUS_SEMANTIC_ERROR				0x06
47 #define NCI_STATUS_UNKNOWN_GID					0x07
48 #define NCI_STATUS_UNKNOWN_OID					0x08
49 #define NCI_STATUS_INVALID_PARAM				0x09
50 #define NCI_STATUS_MESSAGE_SIZE_EXCEEDED			0x0a
51 /* Discovery Specific Status Codes */
52 #define NCI_STATUS_DISCOVERY_ALREADY_STARTED			0xa0
53 #define NCI_STATUS_DISCOVERY_TARGET_ACTIVATION_FAILED		0xa1
54 #define NCI_STATUS_DISCOVERY_TEAR_DOWN				0xa2
55 /* RF Interface Specific Status Codes */
56 #define NCI_STATUS_RF_TRANSMISSION_ERROR			0xb0
57 #define NCI_STATUS_RF_PROTOCOL_ERROR				0xb1
58 #define NCI_STATUS_RF_TIMEOUT_ERROR				0xb2
59 /* NFCEE Interface Specific Status Codes */
60 #define NCI_STATUS_NFCEE_INTERFACE_ACTIVATION_FAILED		0xc0
61 #define NCI_STATUS_NFCEE_TRANSMISSION_ERROR			0xc1
62 #define NCI_STATUS_NFCEE_PROTOCOL_ERROR				0xc2
63 #define NCI_STATUS_NFCEE_TIMEOUT_ERROR				0xc3
64 
65 /* NCI RF Technology and Mode */
66 #define NCI_NFC_A_PASSIVE_POLL_MODE				0x00
67 #define NCI_NFC_B_PASSIVE_POLL_MODE				0x01
68 #define NCI_NFC_F_PASSIVE_POLL_MODE				0x02
69 #define NCI_NFC_A_ACTIVE_POLL_MODE				0x03
70 #define NCI_NFC_F_ACTIVE_POLL_MODE				0x05
71 #define NCI_NFC_V_PASSIVE_POLL_MODE				0x06
72 #define NCI_NFC_A_PASSIVE_LISTEN_MODE				0x80
73 #define NCI_NFC_B_PASSIVE_LISTEN_MODE				0x81
74 #define NCI_NFC_F_PASSIVE_LISTEN_MODE				0x82
75 #define NCI_NFC_A_ACTIVE_LISTEN_MODE				0x83
76 #define NCI_NFC_F_ACTIVE_LISTEN_MODE				0x85
77 
78 #define NCI_RF_TECH_MODE_LISTEN_MASK				0x80
79 
80 /* NCI RF Technologies */
81 #define NCI_NFC_RF_TECHNOLOGY_A					0x00
82 #define NCI_NFC_RF_TECHNOLOGY_B					0x01
83 #define NCI_NFC_RF_TECHNOLOGY_F					0x02
84 #define NCI_NFC_RF_TECHNOLOGY_V					0x03
85 
86 /* NCI Bit Rates */
87 #define NCI_NFC_BIT_RATE_106					0x00
88 #define NCI_NFC_BIT_RATE_212					0x01
89 #define NCI_NFC_BIT_RATE_424					0x02
90 #define NCI_NFC_BIT_RATE_848					0x03
91 #define NCI_NFC_BIT_RATE_1695					0x04
92 #define NCI_NFC_BIT_RATE_3390					0x05
93 #define NCI_NFC_BIT_RATE_6780					0x06
94 #define NCI_NFC_BIT_RATE_26					0x20
95 
96 /* NCI RF Protocols */
97 #define NCI_RF_PROTOCOL_UNKNOWN					0x00
98 #define NCI_RF_PROTOCOL_T1T					0x01
99 #define NCI_RF_PROTOCOL_T2T					0x02
100 #define NCI_RF_PROTOCOL_T3T					0x03
101 #define NCI_RF_PROTOCOL_ISO_DEP					0x04
102 #define NCI_RF_PROTOCOL_NFC_DEP					0x05
103 #define NCI_RF_PROTOCOL_T5T					0x06
104 
105 /* NCI RF Interfaces */
106 #define NCI_RF_INTERFACE_NFCEE_DIRECT				0x00
107 #define NCI_RF_INTERFACE_FRAME					0x01
108 #define NCI_RF_INTERFACE_ISO_DEP				0x02
109 #define NCI_RF_INTERFACE_NFC_DEP				0x03
110 
111 /* NCI Configuration Parameter Tags */
112 #define NCI_PN_ATR_REQ_GEN_BYTES				0x29
113 #define NCI_LN_ATR_RES_GEN_BYTES				0x61
114 #define NCI_LA_SEL_INFO						0x32
115 #define NCI_LF_PROTOCOL_TYPE					0x50
116 #define NCI_LF_CON_BITR_F					0x54
117 
118 /* NCI Configuration Parameters masks */
119 #define NCI_LA_SEL_INFO_ISO_DEP_MASK				0x20
120 #define NCI_LA_SEL_INFO_NFC_DEP_MASK				0x40
121 #define NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK			0x02
122 #define NCI_LF_CON_BITR_F_212					0x02
123 #define NCI_LF_CON_BITR_F_424					0x04
124 
125 /* NCI Reset types */
126 #define NCI_RESET_TYPE_KEEP_CONFIG				0x00
127 #define NCI_RESET_TYPE_RESET_CONFIG				0x01
128 
129 /* NCI Static RF connection ID */
130 #define NCI_STATIC_RF_CONN_ID					0x00
131 
132 /* NCI Data Flow Control */
133 #define NCI_DATA_FLOW_CONTROL_NOT_USED				0xff
134 
135 /* NCI RF_DISCOVER_MAP_CMD modes */
136 #define NCI_DISC_MAP_MODE_POLL					0x01
137 #define NCI_DISC_MAP_MODE_LISTEN				0x02
138 
139 /* NCI Discover Notification Type */
140 #define NCI_DISCOVER_NTF_TYPE_LAST				0x00
141 #define NCI_DISCOVER_NTF_TYPE_LAST_NFCC				0x01
142 #define NCI_DISCOVER_NTF_TYPE_MORE				0x02
143 
144 /* NCI Deactivation Type */
145 #define NCI_DEACTIVATE_TYPE_IDLE_MODE				0x00
146 #define NCI_DEACTIVATE_TYPE_SLEEP_MODE				0x01
147 #define NCI_DEACTIVATE_TYPE_SLEEP_AF_MODE			0x02
148 #define NCI_DEACTIVATE_TYPE_DISCOVERY				0x03
149 
150 /* Message Type (MT) */
151 #define NCI_MT_DATA_PKT						0x00
152 #define NCI_MT_CMD_PKT						0x01
153 #define NCI_MT_RSP_PKT						0x02
154 #define NCI_MT_NTF_PKT						0x03
155 
156 #define nci_mt(hdr)			(((hdr)[0]>>5)&0x07)
157 #define nci_mt_set(hdr, mt)		((hdr)[0] |= (__u8)(((mt)&0x07)<<5))
158 
159 /* Packet Boundary Flag (PBF) */
160 #define NCI_PBF_LAST						0x00
161 #define NCI_PBF_CONT						0x01
162 
163 #define nci_pbf(hdr)			(__u8)(((hdr)[0]>>4)&0x01)
164 #define nci_pbf_set(hdr, pbf)		((hdr)[0] |= (__u8)(((pbf)&0x01)<<4))
165 
166 /* Control Opcode manipulation */
167 #define nci_opcode_pack(gid, oid)	(__u16)((((__u16)((gid)&0x0f))<<8)|\
168 					((__u16)((oid)&0x3f)))
169 #define nci_opcode(hdr)			nci_opcode_pack(hdr[0], hdr[1])
170 #define nci_opcode_gid(op)		(__u8)(((op)&0x0f00)>>8)
171 #define nci_opcode_oid(op)		(__u8)((op)&0x003f)
172 
173 /* Payload Length */
174 #define nci_plen(hdr)			(__u8)((hdr)[2])
175 
176 /* Connection ID */
177 #define nci_conn_id(hdr)		(__u8)(((hdr)[0])&0x0f)
178 
179 /* GID values */
180 #define NCI_GID_CORE						0x0
181 #define NCI_GID_RF_MGMT						0x1
182 #define NCI_GID_NFCEE_MGMT					0x2
183 #define NCI_GID_PROPRIETARY					0xf
184 
185 /* ----- NCI over SPI head/crc(tail) room needed for outgoing frames ----- */
186 #define NCI_SPI_HDR_LEN						4
187 #define NCI_SPI_CRC_LEN						2
188 
189 /* ---- NCI Packet structures ---- */
190 #define NCI_CTRL_HDR_SIZE					3
191 #define NCI_DATA_HDR_SIZE					3
192 
193 struct nci_ctrl_hdr {
194 	__u8	gid;		/* MT & PBF & GID */
195 	__u8	oid;
196 	__u8	plen;
197 } __packed;
198 
199 struct nci_data_hdr {
200 	__u8	conn_id;	/* MT & PBF & ConnID */
201 	__u8	rfu;
202 	__u8	plen;
203 } __packed;
204 
205 /* ------------------------ */
206 /* -----  NCI Commands ---- */
207 /* ------------------------ */
208 #define NCI_OP_CORE_RESET_CMD		nci_opcode_pack(NCI_GID_CORE, 0x00)
209 struct nci_core_reset_cmd {
210 	__u8	reset_type;
211 } __packed;
212 
213 #define NCI_OP_CORE_INIT_CMD		nci_opcode_pack(NCI_GID_CORE, 0x01)
214 
215 #define NCI_OP_CORE_SET_CONFIG_CMD	nci_opcode_pack(NCI_GID_CORE, 0x02)
216 struct set_config_param {
217 	__u8	id;
218 	__u8	len;
219 	__u8	val[NCI_MAX_PARAM_LEN];
220 } __packed;
221 
222 struct nci_core_set_config_cmd {
223 	__u8	num_params;
224 	struct	set_config_param param; /* support 1 param per cmd is enough */
225 } __packed;
226 
227 #define NCI_OP_RF_DISCOVER_MAP_CMD	nci_opcode_pack(NCI_GID_RF_MGMT, 0x00)
228 struct disc_map_config {
229 	__u8	rf_protocol;
230 	__u8	mode;
231 	__u8	rf_interface;
232 } __packed;
233 
234 struct nci_rf_disc_map_cmd {
235 	__u8				num_mapping_configs;
236 	struct disc_map_config		mapping_configs
237 					[NCI_MAX_NUM_MAPPING_CONFIGS];
238 } __packed;
239 
240 #define NCI_OP_RF_DISCOVER_CMD		nci_opcode_pack(NCI_GID_RF_MGMT, 0x03)
241 struct disc_config {
242 	__u8	rf_tech_and_mode;
243 	__u8	frequency;
244 } __packed;
245 
246 struct nci_rf_disc_cmd {
247 	__u8				num_disc_configs;
248 	struct disc_config		disc_configs[NCI_MAX_NUM_RF_CONFIGS];
249 } __packed;
250 
251 #define NCI_OP_RF_DISCOVER_SELECT_CMD	nci_opcode_pack(NCI_GID_RF_MGMT, 0x04)
252 struct nci_rf_discover_select_cmd {
253 	__u8	rf_discovery_id;
254 	__u8	rf_protocol;
255 	__u8	rf_interface;
256 } __packed;
257 
258 #define NCI_OP_RF_DEACTIVATE_CMD	nci_opcode_pack(NCI_GID_RF_MGMT, 0x06)
259 struct nci_rf_deactivate_cmd {
260 	__u8	type;
261 } __packed;
262 
263 /* ----------------------- */
264 /* ---- NCI Responses ---- */
265 /* ----------------------- */
266 #define NCI_OP_CORE_RESET_RSP		nci_opcode_pack(NCI_GID_CORE, 0x00)
267 struct nci_core_reset_rsp {
268 	__u8	status;
269 	__u8	nci_ver;
270 	__u8	config_status;
271 } __packed;
272 
273 #define NCI_OP_CORE_INIT_RSP		nci_opcode_pack(NCI_GID_CORE, 0x01)
274 struct nci_core_init_rsp_1 {
275 	__u8	status;
276 	__le32	nfcc_features;
277 	__u8	num_supported_rf_interfaces;
278 	__u8	supported_rf_interfaces[0];	/* variable size array */
279 	/* continuted in nci_core_init_rsp_2 */
280 } __packed;
281 
282 struct nci_core_init_rsp_2 {
283 	__u8	max_logical_connections;
284 	__le16	max_routing_table_size;
285 	__u8	max_ctrl_pkt_payload_len;
286 	__le16	max_size_for_large_params;
287 	__u8	manufact_id;
288 	__le32	manufact_specific_info;
289 } __packed;
290 
291 #define NCI_OP_CORE_SET_CONFIG_RSP	nci_opcode_pack(NCI_GID_CORE, 0x02)
292 struct nci_core_set_config_rsp {
293 	__u8	status;
294 	__u8	num_params;
295 	__u8	params_id[0];	/* variable size array */
296 } __packed;
297 
298 #define NCI_OP_RF_DISCOVER_MAP_RSP	nci_opcode_pack(NCI_GID_RF_MGMT, 0x00)
299 
300 #define NCI_OP_RF_DISCOVER_RSP		nci_opcode_pack(NCI_GID_RF_MGMT, 0x03)
301 
302 #define NCI_OP_RF_DISCOVER_SELECT_RSP	nci_opcode_pack(NCI_GID_RF_MGMT, 0x04)
303 
304 #define NCI_OP_RF_DEACTIVATE_RSP	nci_opcode_pack(NCI_GID_RF_MGMT, 0x06)
305 
306 /* --------------------------- */
307 /* ---- NCI Notifications ---- */
308 /* --------------------------- */
309 #define NCI_OP_CORE_CONN_CREDITS_NTF	nci_opcode_pack(NCI_GID_CORE, 0x06)
310 struct conn_credit_entry {
311 	__u8	conn_id;
312 	__u8	credits;
313 } __packed;
314 
315 struct nci_core_conn_credit_ntf {
316 	__u8				num_entries;
317 	struct conn_credit_entry	conn_entries[NCI_MAX_NUM_CONN];
318 } __packed;
319 
320 #define NCI_OP_CORE_GENERIC_ERROR_NTF	nci_opcode_pack(NCI_GID_CORE, 0x07)
321 
322 #define NCI_OP_CORE_INTF_ERROR_NTF	nci_opcode_pack(NCI_GID_CORE, 0x08)
323 struct nci_core_intf_error_ntf {
324 	__u8	status;
325 	__u8	conn_id;
326 } __packed;
327 
328 #define NCI_OP_RF_DISCOVER_NTF		nci_opcode_pack(NCI_GID_RF_MGMT, 0x03)
329 struct rf_tech_specific_params_nfca_poll {
330 	__u16	sens_res;
331 	__u8	nfcid1_len;	/* 0, 4, 7, or 10 Bytes */
332 	__u8	nfcid1[NFC_NFCID1_MAXSIZE];
333 	__u8	sel_res_len;	/* 0 or 1 Bytes */
334 	__u8	sel_res;
335 } __packed;
336 
337 struct rf_tech_specific_params_nfcb_poll {
338 	__u8	sensb_res_len;
339 	__u8	sensb_res[NFC_SENSB_RES_MAXSIZE];	/* 11 or 12 Bytes */
340 } __packed;
341 
342 struct rf_tech_specific_params_nfcf_poll {
343 	__u8	bit_rate;
344 	__u8	sensf_res_len;
345 	__u8	sensf_res[NFC_SENSF_RES_MAXSIZE];	/* 16 or 18 Bytes */
346 } __packed;
347 
348 struct rf_tech_specific_params_nfcv_poll {
349 	__u8	res_flags;
350 	__u8	dsfid;
351 	__u8	uid[NFC_ISO15693_UID_MAXSIZE];	/* 8 Bytes */
352 } __packed;
353 
354 struct rf_tech_specific_params_nfcf_listen {
355 	__u8	local_nfcid2_len;
356 	__u8	local_nfcid2[NFC_NFCID2_MAXSIZE];	/* 0 or 8 Bytes */
357 } __packed;
358 
359 struct nci_rf_discover_ntf {
360 	__u8	rf_discovery_id;
361 	__u8	rf_protocol;
362 	__u8	rf_tech_and_mode;
363 	__u8	rf_tech_specific_params_len;
364 
365 	union {
366 		struct rf_tech_specific_params_nfca_poll nfca_poll;
367 		struct rf_tech_specific_params_nfcb_poll nfcb_poll;
368 		struct rf_tech_specific_params_nfcf_poll nfcf_poll;
369 		struct rf_tech_specific_params_nfcv_poll nfcv_poll;
370 	} rf_tech_specific_params;
371 
372 	__u8	ntf_type;
373 } __packed;
374 
375 #define NCI_OP_RF_INTF_ACTIVATED_NTF	nci_opcode_pack(NCI_GID_RF_MGMT, 0x05)
376 struct activation_params_nfca_poll_iso_dep {
377 	__u8	rats_res_len;
378 	__u8	rats_res[20];
379 };
380 
381 struct activation_params_nfcb_poll_iso_dep {
382 	__u8	attrib_res_len;
383 	__u8	attrib_res[50];
384 };
385 
386 struct activation_params_poll_nfc_dep {
387 	__u8	atr_res_len;
388 	__u8	atr_res[NFC_ATR_RES_MAXSIZE - 2]; /* ATR_RES from byte 3 */
389 };
390 
391 struct activation_params_listen_nfc_dep {
392 	__u8	atr_req_len;
393 	__u8	atr_req[NFC_ATR_REQ_MAXSIZE - 2]; /* ATR_REQ from byte 3 */
394 };
395 
396 struct nci_rf_intf_activated_ntf {
397 	__u8	rf_discovery_id;
398 	__u8	rf_interface;
399 	__u8	rf_protocol;
400 	__u8	activation_rf_tech_and_mode;
401 	__u8	max_data_pkt_payload_size;
402 	__u8	initial_num_credits;
403 	__u8	rf_tech_specific_params_len;
404 
405 	union {
406 		struct rf_tech_specific_params_nfca_poll nfca_poll;
407 		struct rf_tech_specific_params_nfcb_poll nfcb_poll;
408 		struct rf_tech_specific_params_nfcf_poll nfcf_poll;
409 		struct rf_tech_specific_params_nfcv_poll nfcv_poll;
410 		struct rf_tech_specific_params_nfcf_listen nfcf_listen;
411 	} rf_tech_specific_params;
412 
413 	__u8	data_exch_rf_tech_and_mode;
414 	__u8	data_exch_tx_bit_rate;
415 	__u8	data_exch_rx_bit_rate;
416 	__u8	activation_params_len;
417 
418 	union {
419 		struct activation_params_nfca_poll_iso_dep nfca_poll_iso_dep;
420 		struct activation_params_nfcb_poll_iso_dep nfcb_poll_iso_dep;
421 		struct activation_params_poll_nfc_dep poll_nfc_dep;
422 		struct activation_params_listen_nfc_dep listen_nfc_dep;
423 	} activation_params;
424 
425 } __packed;
426 
427 #define NCI_OP_RF_DEACTIVATE_NTF	nci_opcode_pack(NCI_GID_RF_MGMT, 0x06)
428 struct nci_rf_deactivate_ntf {
429 	__u8	type;
430 	__u8	reason;
431 } __packed;
432 
433 #endif /* __NCI_H */
434