xref: /openbmc/linux/net/nfc/nci/ntf.c (revision bcd684aa)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  The NFC Controller Interface is the communication protocol between an
4  *  NFC Controller (NFCC) and a Device Host (DH).
5  *
6  *  Copyright (C) 2014 Marvell International Ltd.
7  *  Copyright (C) 2011 Texas Instruments, Inc.
8  *
9  *  Written by Ilan Elias <ilane@ti.com>
10  *
11  *  Acknowledgements:
12  *  This file is based on hci_event.c, which was written
13  *  by Maxim Krasnyansky.
14  */
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
17 
18 #include <linux/types.h>
19 #include <linux/interrupt.h>
20 #include <linux/bitops.h>
21 #include <linux/skbuff.h>
22 
23 #include "../nfc.h"
24 #include <net/nfc/nci.h>
25 #include <net/nfc/nci_core.h>
26 #include <linux/nfc.h>
27 
28 /* Handle NCI Notification packets */
29 
30 static void nci_core_reset_ntf_packet(struct nci_dev *ndev,
31 				      struct sk_buff *skb)
32 {
33 	/* Handle NCI 2.x core reset notification */
34 	struct nci_core_reset_ntf *ntf = (void *)skb->data;
35 
36 	ndev->nci_ver = ntf->nci_ver;
37 	pr_debug("nci_ver 0x%x, config_status 0x%x\n",
38 		 ntf->nci_ver, ntf->config_status);
39 
40 	ndev->manufact_id = ntf->manufact_id;
41 	ndev->manufact_specific_info =
42 		__le32_to_cpu(ntf->manufact_specific_info);
43 
44 	nci_req_complete(ndev, NCI_STATUS_OK);
45 }
46 
47 static void nci_core_conn_credits_ntf_packet(struct nci_dev *ndev,
48 					     struct sk_buff *skb)
49 {
50 	struct nci_core_conn_credit_ntf *ntf = (void *) skb->data;
51 	struct nci_conn_info	*conn_info;
52 	int i;
53 
54 	pr_debug("num_entries %d\n", ntf->num_entries);
55 
56 	if (ntf->num_entries > NCI_MAX_NUM_CONN)
57 		ntf->num_entries = NCI_MAX_NUM_CONN;
58 
59 	/* update the credits */
60 	for (i = 0; i < ntf->num_entries; i++) {
61 		ntf->conn_entries[i].conn_id =
62 			nci_conn_id(&ntf->conn_entries[i].conn_id);
63 
64 		pr_debug("entry[%d]: conn_id %d, credits %d\n",
65 			 i, ntf->conn_entries[i].conn_id,
66 			 ntf->conn_entries[i].credits);
67 
68 		conn_info = nci_get_conn_info_by_conn_id(ndev,
69 							 ntf->conn_entries[i].conn_id);
70 		if (!conn_info)
71 			return;
72 
73 		atomic_add(ntf->conn_entries[i].credits,
74 			   &conn_info->credits_cnt);
75 	}
76 
77 	/* trigger the next tx */
78 	if (!skb_queue_empty(&ndev->tx_q))
79 		queue_work(ndev->tx_wq, &ndev->tx_work);
80 }
81 
82 static void nci_core_generic_error_ntf_packet(struct nci_dev *ndev,
83 					      struct sk_buff *skb)
84 {
85 	__u8 status = skb->data[0];
86 
87 	pr_debug("status 0x%x\n", status);
88 
89 	if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
90 		/* Activation failed, so complete the request
91 		   (the state remains the same) */
92 		nci_req_complete(ndev, status);
93 	}
94 }
95 
96 static void nci_core_conn_intf_error_ntf_packet(struct nci_dev *ndev,
97 						struct sk_buff *skb)
98 {
99 	struct nci_core_intf_error_ntf *ntf = (void *) skb->data;
100 
101 	ntf->conn_id = nci_conn_id(&ntf->conn_id);
102 
103 	pr_debug("status 0x%x, conn_id %d\n", ntf->status, ntf->conn_id);
104 
105 	/* complete the data exchange transaction, if exists */
106 	if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
107 		nci_data_exchange_complete(ndev, NULL, ntf->conn_id, -EIO);
108 }
109 
110 static __u8 *nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev,
111 			struct rf_tech_specific_params_nfca_poll *nfca_poll,
112 						     __u8 *data)
113 {
114 	nfca_poll->sens_res = __le16_to_cpu(*((__le16 *)data));
115 	data += 2;
116 
117 	nfca_poll->nfcid1_len = min_t(__u8, *data++, NFC_NFCID1_MAXSIZE);
118 
119 	pr_debug("sens_res 0x%x, nfcid1_len %d\n",
120 		 nfca_poll->sens_res, nfca_poll->nfcid1_len);
121 
122 	memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len);
123 	data += nfca_poll->nfcid1_len;
124 
125 	nfca_poll->sel_res_len = *data++;
126 
127 	if (nfca_poll->sel_res_len != 0)
128 		nfca_poll->sel_res = *data++;
129 
130 	pr_debug("sel_res_len %d, sel_res 0x%x\n",
131 		 nfca_poll->sel_res_len,
132 		 nfca_poll->sel_res);
133 
134 	return data;
135 }
136 
137 static __u8 *nci_extract_rf_params_nfcb_passive_poll(struct nci_dev *ndev,
138 			struct rf_tech_specific_params_nfcb_poll *nfcb_poll,
139 						     __u8 *data)
140 {
141 	nfcb_poll->sensb_res_len = min_t(__u8, *data++, NFC_SENSB_RES_MAXSIZE);
142 
143 	pr_debug("sensb_res_len %d\n", nfcb_poll->sensb_res_len);
144 
145 	memcpy(nfcb_poll->sensb_res, data, nfcb_poll->sensb_res_len);
146 	data += nfcb_poll->sensb_res_len;
147 
148 	return data;
149 }
150 
151 static __u8 *nci_extract_rf_params_nfcf_passive_poll(struct nci_dev *ndev,
152 			struct rf_tech_specific_params_nfcf_poll *nfcf_poll,
153 						     __u8 *data)
154 {
155 	nfcf_poll->bit_rate = *data++;
156 	nfcf_poll->sensf_res_len = min_t(__u8, *data++, NFC_SENSF_RES_MAXSIZE);
157 
158 	pr_debug("bit_rate %d, sensf_res_len %d\n",
159 		 nfcf_poll->bit_rate, nfcf_poll->sensf_res_len);
160 
161 	memcpy(nfcf_poll->sensf_res, data, nfcf_poll->sensf_res_len);
162 	data += nfcf_poll->sensf_res_len;
163 
164 	return data;
165 }
166 
167 static __u8 *nci_extract_rf_params_nfcv_passive_poll(struct nci_dev *ndev,
168 			struct rf_tech_specific_params_nfcv_poll *nfcv_poll,
169 						     __u8 *data)
170 {
171 	++data;
172 	nfcv_poll->dsfid = *data++;
173 	memcpy(nfcv_poll->uid, data, NFC_ISO15693_UID_MAXSIZE);
174 	data += NFC_ISO15693_UID_MAXSIZE;
175 	return data;
176 }
177 
178 static __u8 *nci_extract_rf_params_nfcf_passive_listen(struct nci_dev *ndev,
179 			struct rf_tech_specific_params_nfcf_listen *nfcf_listen,
180 						     __u8 *data)
181 {
182 	nfcf_listen->local_nfcid2_len = min_t(__u8, *data++,
183 					      NFC_NFCID2_MAXSIZE);
184 	memcpy(nfcf_listen->local_nfcid2, data, nfcf_listen->local_nfcid2_len);
185 	data += nfcf_listen->local_nfcid2_len;
186 
187 	return data;
188 }
189 
190 static __u32 nci_get_prop_rf_protocol(struct nci_dev *ndev, __u8 rf_protocol)
191 {
192 	if (ndev->ops->get_rfprotocol)
193 		return ndev->ops->get_rfprotocol(ndev, rf_protocol);
194 	return 0;
195 }
196 
197 static int nci_add_new_protocol(struct nci_dev *ndev,
198 				struct nfc_target *target,
199 				__u8 rf_protocol,
200 				__u8 rf_tech_and_mode,
201 				void *params)
202 {
203 	struct rf_tech_specific_params_nfca_poll *nfca_poll;
204 	struct rf_tech_specific_params_nfcb_poll *nfcb_poll;
205 	struct rf_tech_specific_params_nfcf_poll *nfcf_poll;
206 	struct rf_tech_specific_params_nfcv_poll *nfcv_poll;
207 	__u32 protocol;
208 
209 	if (rf_protocol == NCI_RF_PROTOCOL_T1T)
210 		protocol = NFC_PROTO_JEWEL_MASK;
211 	else if (rf_protocol == NCI_RF_PROTOCOL_T2T)
212 		protocol = NFC_PROTO_MIFARE_MASK;
213 	else if (rf_protocol == NCI_RF_PROTOCOL_ISO_DEP)
214 		if (rf_tech_and_mode == NCI_NFC_A_PASSIVE_POLL_MODE)
215 			protocol = NFC_PROTO_ISO14443_MASK;
216 		else
217 			protocol = NFC_PROTO_ISO14443_B_MASK;
218 	else if (rf_protocol == NCI_RF_PROTOCOL_T3T)
219 		protocol = NFC_PROTO_FELICA_MASK;
220 	else if (rf_protocol == NCI_RF_PROTOCOL_NFC_DEP)
221 		protocol = NFC_PROTO_NFC_DEP_MASK;
222 	else if (rf_protocol == NCI_RF_PROTOCOL_T5T)
223 		protocol = NFC_PROTO_ISO15693_MASK;
224 	else
225 		protocol = nci_get_prop_rf_protocol(ndev, rf_protocol);
226 
227 	if (!(protocol & ndev->poll_prots)) {
228 		pr_err("the target found does not have the desired protocol\n");
229 		return -EPROTO;
230 	}
231 
232 	if (rf_tech_and_mode == NCI_NFC_A_PASSIVE_POLL_MODE) {
233 		nfca_poll = (struct rf_tech_specific_params_nfca_poll *)params;
234 
235 		target->sens_res = nfca_poll->sens_res;
236 		target->sel_res = nfca_poll->sel_res;
237 		target->nfcid1_len = nfca_poll->nfcid1_len;
238 		if (target->nfcid1_len > 0) {
239 			memcpy(target->nfcid1, nfca_poll->nfcid1,
240 			       target->nfcid1_len);
241 		}
242 	} else if (rf_tech_and_mode == NCI_NFC_B_PASSIVE_POLL_MODE) {
243 		nfcb_poll = (struct rf_tech_specific_params_nfcb_poll *)params;
244 
245 		target->sensb_res_len = nfcb_poll->sensb_res_len;
246 		if (target->sensb_res_len > 0) {
247 			memcpy(target->sensb_res, nfcb_poll->sensb_res,
248 			       target->sensb_res_len);
249 		}
250 	} else if (rf_tech_and_mode == NCI_NFC_F_PASSIVE_POLL_MODE) {
251 		nfcf_poll = (struct rf_tech_specific_params_nfcf_poll *)params;
252 
253 		target->sensf_res_len = nfcf_poll->sensf_res_len;
254 		if (target->sensf_res_len > 0) {
255 			memcpy(target->sensf_res, nfcf_poll->sensf_res,
256 			       target->sensf_res_len);
257 		}
258 	} else if (rf_tech_and_mode == NCI_NFC_V_PASSIVE_POLL_MODE) {
259 		nfcv_poll = (struct rf_tech_specific_params_nfcv_poll *)params;
260 
261 		target->is_iso15693 = 1;
262 		target->iso15693_dsfid = nfcv_poll->dsfid;
263 		memcpy(target->iso15693_uid, nfcv_poll->uid, NFC_ISO15693_UID_MAXSIZE);
264 	} else {
265 		pr_err("unsupported rf_tech_and_mode 0x%x\n", rf_tech_and_mode);
266 		return -EPROTO;
267 	}
268 
269 	target->supported_protocols |= protocol;
270 
271 	pr_debug("protocol 0x%x\n", protocol);
272 
273 	return 0;
274 }
275 
276 static void nci_add_new_target(struct nci_dev *ndev,
277 			       struct nci_rf_discover_ntf *ntf)
278 {
279 	struct nfc_target *target;
280 	int i, rc;
281 
282 	for (i = 0; i < ndev->n_targets; i++) {
283 		target = &ndev->targets[i];
284 		if (target->logical_idx == ntf->rf_discovery_id) {
285 			/* This target already exists, add the new protocol */
286 			nci_add_new_protocol(ndev, target, ntf->rf_protocol,
287 					     ntf->rf_tech_and_mode,
288 					     &ntf->rf_tech_specific_params);
289 			return;
290 		}
291 	}
292 
293 	/* This is a new target, check if we've enough room */
294 	if (ndev->n_targets == NCI_MAX_DISCOVERED_TARGETS) {
295 		pr_debug("not enough room, ignoring new target...\n");
296 		return;
297 	}
298 
299 	target = &ndev->targets[ndev->n_targets];
300 
301 	rc = nci_add_new_protocol(ndev, target, ntf->rf_protocol,
302 				  ntf->rf_tech_and_mode,
303 				  &ntf->rf_tech_specific_params);
304 	if (!rc) {
305 		target->logical_idx = ntf->rf_discovery_id;
306 		ndev->n_targets++;
307 
308 		pr_debug("logical idx %d, n_targets %d\n", target->logical_idx,
309 			 ndev->n_targets);
310 	}
311 }
312 
313 void nci_clear_target_list(struct nci_dev *ndev)
314 {
315 	memset(ndev->targets, 0,
316 	       (sizeof(struct nfc_target)*NCI_MAX_DISCOVERED_TARGETS));
317 
318 	ndev->n_targets = 0;
319 }
320 
321 static void nci_rf_discover_ntf_packet(struct nci_dev *ndev,
322 				       struct sk_buff *skb)
323 {
324 	struct nci_rf_discover_ntf ntf;
325 	__u8 *data = skb->data;
326 	bool add_target = true;
327 
328 	ntf.rf_discovery_id = *data++;
329 	ntf.rf_protocol = *data++;
330 	ntf.rf_tech_and_mode = *data++;
331 	ntf.rf_tech_specific_params_len = *data++;
332 
333 	pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id);
334 	pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol);
335 	pr_debug("rf_tech_and_mode 0x%x\n", ntf.rf_tech_and_mode);
336 	pr_debug("rf_tech_specific_params_len %d\n",
337 		 ntf.rf_tech_specific_params_len);
338 
339 	if (ntf.rf_tech_specific_params_len > 0) {
340 		switch (ntf.rf_tech_and_mode) {
341 		case NCI_NFC_A_PASSIVE_POLL_MODE:
342 			data = nci_extract_rf_params_nfca_passive_poll(ndev,
343 				&(ntf.rf_tech_specific_params.nfca_poll), data);
344 			break;
345 
346 		case NCI_NFC_B_PASSIVE_POLL_MODE:
347 			data = nci_extract_rf_params_nfcb_passive_poll(ndev,
348 				&(ntf.rf_tech_specific_params.nfcb_poll), data);
349 			break;
350 
351 		case NCI_NFC_F_PASSIVE_POLL_MODE:
352 			data = nci_extract_rf_params_nfcf_passive_poll(ndev,
353 				&(ntf.rf_tech_specific_params.nfcf_poll), data);
354 			break;
355 
356 		case NCI_NFC_V_PASSIVE_POLL_MODE:
357 			data = nci_extract_rf_params_nfcv_passive_poll(ndev,
358 				&(ntf.rf_tech_specific_params.nfcv_poll), data);
359 			break;
360 
361 		default:
362 			pr_err("unsupported rf_tech_and_mode 0x%x\n",
363 			       ntf.rf_tech_and_mode);
364 			data += ntf.rf_tech_specific_params_len;
365 			add_target = false;
366 		}
367 	}
368 
369 	ntf.ntf_type = *data++;
370 	pr_debug("ntf_type %d\n", ntf.ntf_type);
371 
372 	if (add_target == true)
373 		nci_add_new_target(ndev, &ntf);
374 
375 	if (ntf.ntf_type == NCI_DISCOVER_NTF_TYPE_MORE) {
376 		atomic_set(&ndev->state, NCI_W4_ALL_DISCOVERIES);
377 	} else {
378 		atomic_set(&ndev->state, NCI_W4_HOST_SELECT);
379 		nfc_targets_found(ndev->nfc_dev, ndev->targets,
380 				  ndev->n_targets);
381 	}
382 }
383 
384 static int nci_extract_activation_params_iso_dep(struct nci_dev *ndev,
385 			struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
386 {
387 	struct activation_params_nfca_poll_iso_dep *nfca_poll;
388 	struct activation_params_nfcb_poll_iso_dep *nfcb_poll;
389 
390 	switch (ntf->activation_rf_tech_and_mode) {
391 	case NCI_NFC_A_PASSIVE_POLL_MODE:
392 		nfca_poll = &ntf->activation_params.nfca_poll_iso_dep;
393 		nfca_poll->rats_res_len = min_t(__u8, *data++, 20);
394 		pr_debug("rats_res_len %d\n", nfca_poll->rats_res_len);
395 		if (nfca_poll->rats_res_len > 0) {
396 			memcpy(nfca_poll->rats_res,
397 			       data, nfca_poll->rats_res_len);
398 		}
399 		break;
400 
401 	case NCI_NFC_B_PASSIVE_POLL_MODE:
402 		nfcb_poll = &ntf->activation_params.nfcb_poll_iso_dep;
403 		nfcb_poll->attrib_res_len = min_t(__u8, *data++, 50);
404 		pr_debug("attrib_res_len %d\n", nfcb_poll->attrib_res_len);
405 		if (nfcb_poll->attrib_res_len > 0) {
406 			memcpy(nfcb_poll->attrib_res,
407 			       data, nfcb_poll->attrib_res_len);
408 		}
409 		break;
410 
411 	default:
412 		pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
413 		       ntf->activation_rf_tech_and_mode);
414 		return NCI_STATUS_RF_PROTOCOL_ERROR;
415 	}
416 
417 	return NCI_STATUS_OK;
418 }
419 
420 static int nci_extract_activation_params_nfc_dep(struct nci_dev *ndev,
421 			struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
422 {
423 	struct activation_params_poll_nfc_dep *poll;
424 	struct activation_params_listen_nfc_dep *listen;
425 
426 	switch (ntf->activation_rf_tech_and_mode) {
427 	case NCI_NFC_A_PASSIVE_POLL_MODE:
428 	case NCI_NFC_F_PASSIVE_POLL_MODE:
429 		poll = &ntf->activation_params.poll_nfc_dep;
430 		poll->atr_res_len = min_t(__u8, *data++,
431 					  NFC_ATR_RES_MAXSIZE - 2);
432 		pr_debug("atr_res_len %d\n", poll->atr_res_len);
433 		if (poll->atr_res_len > 0)
434 			memcpy(poll->atr_res, data, poll->atr_res_len);
435 		break;
436 
437 	case NCI_NFC_A_PASSIVE_LISTEN_MODE:
438 	case NCI_NFC_F_PASSIVE_LISTEN_MODE:
439 		listen = &ntf->activation_params.listen_nfc_dep;
440 		listen->atr_req_len = min_t(__u8, *data++,
441 					    NFC_ATR_REQ_MAXSIZE - 2);
442 		pr_debug("atr_req_len %d\n", listen->atr_req_len);
443 		if (listen->atr_req_len > 0)
444 			memcpy(listen->atr_req, data, listen->atr_req_len);
445 		break;
446 
447 	default:
448 		pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
449 		       ntf->activation_rf_tech_and_mode);
450 		return NCI_STATUS_RF_PROTOCOL_ERROR;
451 	}
452 
453 	return NCI_STATUS_OK;
454 }
455 
456 static void nci_target_auto_activated(struct nci_dev *ndev,
457 				      struct nci_rf_intf_activated_ntf *ntf)
458 {
459 	struct nfc_target *target;
460 	int rc;
461 
462 	target = &ndev->targets[ndev->n_targets];
463 
464 	rc = nci_add_new_protocol(ndev, target, ntf->rf_protocol,
465 				  ntf->activation_rf_tech_and_mode,
466 				  &ntf->rf_tech_specific_params);
467 	if (rc)
468 		return;
469 
470 	target->logical_idx = ntf->rf_discovery_id;
471 	ndev->n_targets++;
472 
473 	pr_debug("logical idx %d, n_targets %d\n",
474 		 target->logical_idx, ndev->n_targets);
475 
476 	nfc_targets_found(ndev->nfc_dev, ndev->targets, ndev->n_targets);
477 }
478 
479 static int nci_store_general_bytes_nfc_dep(struct nci_dev *ndev,
480 		struct nci_rf_intf_activated_ntf *ntf)
481 {
482 	ndev->remote_gb_len = 0;
483 
484 	if (ntf->activation_params_len <= 0)
485 		return NCI_STATUS_OK;
486 
487 	switch (ntf->activation_rf_tech_and_mode) {
488 	case NCI_NFC_A_PASSIVE_POLL_MODE:
489 	case NCI_NFC_F_PASSIVE_POLL_MODE:
490 		ndev->remote_gb_len = min_t(__u8,
491 			(ntf->activation_params.poll_nfc_dep.atr_res_len
492 						- NFC_ATR_RES_GT_OFFSET),
493 			NFC_ATR_RES_GB_MAXSIZE);
494 		memcpy(ndev->remote_gb,
495 		       (ntf->activation_params.poll_nfc_dep.atr_res
496 						+ NFC_ATR_RES_GT_OFFSET),
497 		       ndev->remote_gb_len);
498 		break;
499 
500 	case NCI_NFC_A_PASSIVE_LISTEN_MODE:
501 	case NCI_NFC_F_PASSIVE_LISTEN_MODE:
502 		ndev->remote_gb_len = min_t(__u8,
503 			(ntf->activation_params.listen_nfc_dep.atr_req_len
504 						- NFC_ATR_REQ_GT_OFFSET),
505 			NFC_ATR_REQ_GB_MAXSIZE);
506 		memcpy(ndev->remote_gb,
507 		       (ntf->activation_params.listen_nfc_dep.atr_req
508 						+ NFC_ATR_REQ_GT_OFFSET),
509 		       ndev->remote_gb_len);
510 		break;
511 
512 	default:
513 		pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
514 		       ntf->activation_rf_tech_and_mode);
515 		return NCI_STATUS_RF_PROTOCOL_ERROR;
516 	}
517 
518 	return NCI_STATUS_OK;
519 }
520 
521 static void nci_rf_intf_activated_ntf_packet(struct nci_dev *ndev,
522 					     struct sk_buff *skb)
523 {
524 	struct nci_conn_info    *conn_info;
525 	struct nci_rf_intf_activated_ntf ntf;
526 	__u8 *data = skb->data;
527 	int err = NCI_STATUS_OK;
528 
529 	ntf.rf_discovery_id = *data++;
530 	ntf.rf_interface = *data++;
531 	ntf.rf_protocol = *data++;
532 	ntf.activation_rf_tech_and_mode = *data++;
533 	ntf.max_data_pkt_payload_size = *data++;
534 	ntf.initial_num_credits = *data++;
535 	ntf.rf_tech_specific_params_len = *data++;
536 
537 	pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id);
538 	pr_debug("rf_interface 0x%x\n", ntf.rf_interface);
539 	pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol);
540 	pr_debug("activation_rf_tech_and_mode 0x%x\n",
541 		 ntf.activation_rf_tech_and_mode);
542 	pr_debug("max_data_pkt_payload_size 0x%x\n",
543 		 ntf.max_data_pkt_payload_size);
544 	pr_debug("initial_num_credits 0x%x\n",
545 		 ntf.initial_num_credits);
546 	pr_debug("rf_tech_specific_params_len %d\n",
547 		 ntf.rf_tech_specific_params_len);
548 
549 	/* If this contains a value of 0x00 (NFCEE Direct RF
550 	 * Interface) then all following parameters SHALL contain a
551 	 * value of 0 and SHALL be ignored.
552 	 */
553 	if (ntf.rf_interface == NCI_RF_INTERFACE_NFCEE_DIRECT)
554 		goto listen;
555 
556 	if (ntf.rf_tech_specific_params_len > 0) {
557 		switch (ntf.activation_rf_tech_and_mode) {
558 		case NCI_NFC_A_PASSIVE_POLL_MODE:
559 			data = nci_extract_rf_params_nfca_passive_poll(ndev,
560 				&(ntf.rf_tech_specific_params.nfca_poll), data);
561 			break;
562 
563 		case NCI_NFC_B_PASSIVE_POLL_MODE:
564 			data = nci_extract_rf_params_nfcb_passive_poll(ndev,
565 				&(ntf.rf_tech_specific_params.nfcb_poll), data);
566 			break;
567 
568 		case NCI_NFC_F_PASSIVE_POLL_MODE:
569 			data = nci_extract_rf_params_nfcf_passive_poll(ndev,
570 				&(ntf.rf_tech_specific_params.nfcf_poll), data);
571 			break;
572 
573 		case NCI_NFC_V_PASSIVE_POLL_MODE:
574 			data = nci_extract_rf_params_nfcv_passive_poll(ndev,
575 				&(ntf.rf_tech_specific_params.nfcv_poll), data);
576 			break;
577 
578 		case NCI_NFC_A_PASSIVE_LISTEN_MODE:
579 			/* no RF technology specific parameters */
580 			break;
581 
582 		case NCI_NFC_F_PASSIVE_LISTEN_MODE:
583 			data = nci_extract_rf_params_nfcf_passive_listen(ndev,
584 				&(ntf.rf_tech_specific_params.nfcf_listen),
585 				data);
586 			break;
587 
588 		default:
589 			pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
590 			       ntf.activation_rf_tech_and_mode);
591 			err = NCI_STATUS_RF_PROTOCOL_ERROR;
592 			goto exit;
593 		}
594 	}
595 
596 	ntf.data_exch_rf_tech_and_mode = *data++;
597 	ntf.data_exch_tx_bit_rate = *data++;
598 	ntf.data_exch_rx_bit_rate = *data++;
599 	ntf.activation_params_len = *data++;
600 
601 	pr_debug("data_exch_rf_tech_and_mode 0x%x\n",
602 		 ntf.data_exch_rf_tech_and_mode);
603 	pr_debug("data_exch_tx_bit_rate 0x%x\n", ntf.data_exch_tx_bit_rate);
604 	pr_debug("data_exch_rx_bit_rate 0x%x\n", ntf.data_exch_rx_bit_rate);
605 	pr_debug("activation_params_len %d\n", ntf.activation_params_len);
606 
607 	if (ntf.activation_params_len > 0) {
608 		switch (ntf.rf_interface) {
609 		case NCI_RF_INTERFACE_ISO_DEP:
610 			err = nci_extract_activation_params_iso_dep(ndev,
611 								    &ntf, data);
612 			break;
613 
614 		case NCI_RF_INTERFACE_NFC_DEP:
615 			err = nci_extract_activation_params_nfc_dep(ndev,
616 								    &ntf, data);
617 			break;
618 
619 		case NCI_RF_INTERFACE_FRAME:
620 			/* no activation params */
621 			break;
622 
623 		default:
624 			pr_err("unsupported rf_interface 0x%x\n",
625 			       ntf.rf_interface);
626 			err = NCI_STATUS_RF_PROTOCOL_ERROR;
627 			break;
628 		}
629 	}
630 
631 exit:
632 	if (err == NCI_STATUS_OK) {
633 		conn_info = ndev->rf_conn_info;
634 		if (!conn_info)
635 			return;
636 
637 		conn_info->max_pkt_payload_len = ntf.max_data_pkt_payload_size;
638 		conn_info->initial_num_credits = ntf.initial_num_credits;
639 
640 		/* set the available credits to initial value */
641 		atomic_set(&conn_info->credits_cnt,
642 			   conn_info->initial_num_credits);
643 
644 		/* store general bytes to be reported later in dep_link_up */
645 		if (ntf.rf_interface == NCI_RF_INTERFACE_NFC_DEP) {
646 			err = nci_store_general_bytes_nfc_dep(ndev, &ntf);
647 			if (err != NCI_STATUS_OK)
648 				pr_err("unable to store general bytes\n");
649 		}
650 	}
651 
652 	if (!(ntf.activation_rf_tech_and_mode & NCI_RF_TECH_MODE_LISTEN_MASK)) {
653 		/* Poll mode */
654 		if (atomic_read(&ndev->state) == NCI_DISCOVERY) {
655 			/* A single target was found and activated
656 			 * automatically */
657 			atomic_set(&ndev->state, NCI_POLL_ACTIVE);
658 			if (err == NCI_STATUS_OK)
659 				nci_target_auto_activated(ndev, &ntf);
660 		} else {	/* ndev->state == NCI_W4_HOST_SELECT */
661 			/* A selected target was activated, so complete the
662 			 * request */
663 			atomic_set(&ndev->state, NCI_POLL_ACTIVE);
664 			nci_req_complete(ndev, err);
665 		}
666 	} else {
667 listen:
668 		/* Listen mode */
669 		atomic_set(&ndev->state, NCI_LISTEN_ACTIVE);
670 		if (err == NCI_STATUS_OK &&
671 		    ntf.rf_protocol == NCI_RF_PROTOCOL_NFC_DEP) {
672 			err = nfc_tm_activated(ndev->nfc_dev,
673 					       NFC_PROTO_NFC_DEP_MASK,
674 					       NFC_COMM_PASSIVE,
675 					       ndev->remote_gb,
676 					       ndev->remote_gb_len);
677 			if (err != NCI_STATUS_OK)
678 				pr_err("error when signaling tm activation\n");
679 		}
680 	}
681 }
682 
683 static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev,
684 					 struct sk_buff *skb)
685 {
686 	struct nci_conn_info    *conn_info;
687 	struct nci_rf_deactivate_ntf *ntf = (void *) skb->data;
688 
689 	pr_debug("entry, type 0x%x, reason 0x%x\n", ntf->type, ntf->reason);
690 
691 	conn_info = ndev->rf_conn_info;
692 	if (!conn_info)
693 		return;
694 
695 	/* drop tx data queue */
696 	skb_queue_purge(&ndev->tx_q);
697 
698 	/* drop partial rx data packet */
699 	if (ndev->rx_data_reassembly) {
700 		kfree_skb(ndev->rx_data_reassembly);
701 		ndev->rx_data_reassembly = NULL;
702 	}
703 
704 	/* complete the data exchange transaction, if exists */
705 	if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
706 		nci_data_exchange_complete(ndev, NULL, NCI_STATIC_RF_CONN_ID,
707 					   -EIO);
708 
709 	switch (ntf->type) {
710 	case NCI_DEACTIVATE_TYPE_IDLE_MODE:
711 		nci_clear_target_list(ndev);
712 		atomic_set(&ndev->state, NCI_IDLE);
713 		break;
714 	case NCI_DEACTIVATE_TYPE_SLEEP_MODE:
715 	case NCI_DEACTIVATE_TYPE_SLEEP_AF_MODE:
716 		atomic_set(&ndev->state, NCI_W4_HOST_SELECT);
717 		break;
718 	case NCI_DEACTIVATE_TYPE_DISCOVERY:
719 		nci_clear_target_list(ndev);
720 		atomic_set(&ndev->state, NCI_DISCOVERY);
721 		break;
722 	}
723 
724 	nci_req_complete(ndev, NCI_STATUS_OK);
725 }
726 
727 static void nci_nfcee_discover_ntf_packet(struct nci_dev *ndev,
728 					  struct sk_buff *skb)
729 {
730 	u8 status = NCI_STATUS_OK;
731 	struct nci_nfcee_discover_ntf   *nfcee_ntf =
732 				(struct nci_nfcee_discover_ntf *)skb->data;
733 
734 	pr_debug("\n");
735 
736 	/* NFCForum NCI 9.2.1 HCI Network Specific Handling
737 	 * If the NFCC supports the HCI Network, it SHALL return one,
738 	 * and only one, NFCEE_DISCOVER_NTF with a Protocol type of
739 	 * “HCI Access”, even if the HCI Network contains multiple NFCEEs.
740 	 */
741 	ndev->hci_dev->nfcee_id = nfcee_ntf->nfcee_id;
742 	ndev->cur_params.id = nfcee_ntf->nfcee_id;
743 
744 	nci_req_complete(ndev, status);
745 }
746 
747 static void nci_nfcee_action_ntf_packet(struct nci_dev *ndev,
748 					struct sk_buff *skb)
749 {
750 	pr_debug("\n");
751 }
752 
753 void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb)
754 {
755 	__u16 ntf_opcode = nci_opcode(skb->data);
756 
757 	pr_debug("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
758 		 nci_pbf(skb->data),
759 		 nci_opcode_gid(ntf_opcode),
760 		 nci_opcode_oid(ntf_opcode),
761 		 nci_plen(skb->data));
762 
763 	/* strip the nci control header */
764 	skb_pull(skb, NCI_CTRL_HDR_SIZE);
765 
766 	if (nci_opcode_gid(ntf_opcode) == NCI_GID_PROPRIETARY) {
767 		if (nci_prop_ntf_packet(ndev, ntf_opcode, skb) == -ENOTSUPP) {
768 			pr_err("unsupported ntf opcode 0x%x\n",
769 			       ntf_opcode);
770 		}
771 
772 		goto end;
773 	}
774 
775 	switch (ntf_opcode) {
776 	case NCI_OP_CORE_RESET_NTF:
777 		nci_core_reset_ntf_packet(ndev, skb);
778 		break;
779 
780 	case NCI_OP_CORE_CONN_CREDITS_NTF:
781 		nci_core_conn_credits_ntf_packet(ndev, skb);
782 		break;
783 
784 	case NCI_OP_CORE_GENERIC_ERROR_NTF:
785 		nci_core_generic_error_ntf_packet(ndev, skb);
786 		break;
787 
788 	case NCI_OP_CORE_INTF_ERROR_NTF:
789 		nci_core_conn_intf_error_ntf_packet(ndev, skb);
790 		break;
791 
792 	case NCI_OP_RF_DISCOVER_NTF:
793 		nci_rf_discover_ntf_packet(ndev, skb);
794 		break;
795 
796 	case NCI_OP_RF_INTF_ACTIVATED_NTF:
797 		nci_rf_intf_activated_ntf_packet(ndev, skb);
798 		break;
799 
800 	case NCI_OP_RF_DEACTIVATE_NTF:
801 		nci_rf_deactivate_ntf_packet(ndev, skb);
802 		break;
803 
804 	case NCI_OP_NFCEE_DISCOVER_NTF:
805 		nci_nfcee_discover_ntf_packet(ndev, skb);
806 		break;
807 
808 	case NCI_OP_RF_NFCEE_ACTION_NTF:
809 		nci_nfcee_action_ntf_packet(ndev, skb);
810 		break;
811 
812 	default:
813 		pr_err("unknown ntf opcode 0x%x\n", ntf_opcode);
814 		break;
815 	}
816 
817 	nci_core_ntf_packet(ndev, ntf_opcode, skb);
818 end:
819 	kfree_skb(skb);
820 }
821