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