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