ntf.c 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 *  The NFC Controller Interface is the communication protocol between an
 *  NFC Controller (NFCC) and a Device Host (DH).
 *
 *  Copyright (C) 2011 Texas Instruments, Inc.
 *
 *  Written by Ilan Elias <ilane@ti.com>
 *
 *  Acknowledgements:
 *  This file is based on hci_event.c, which was written
 *  by Maxim Krasnyansky.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

28
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
J
Joe Perches 已提交
29

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/bitops.h>
#include <linux/skbuff.h>

#include "../nfc.h"
#include <net/nfc/nci.h>
#include <net/nfc/nci_core.h>
#include <linux/nfc.h>

/* Handle NCI Notification packets */

static void nci_core_conn_credits_ntf_packet(struct nci_dev *ndev,
						struct sk_buff *skb)
{
	struct nci_core_conn_credit_ntf *ntf = (void *) skb->data;
	int i;

48
	pr_debug("num_entries %d\n", ntf->num_entries);
49 50 51 52 53 54

	if (ntf->num_entries > NCI_MAX_NUM_CONN)
		ntf->num_entries = NCI_MAX_NUM_CONN;

	/* update the credits */
	for (i = 0; i < ntf->num_entries; i++) {
55 56 57
		ntf->conn_entries[i].conn_id =
			nci_conn_id(&ntf->conn_entries[i].conn_id);

J
Joe Perches 已提交
58 59 60
		pr_debug("entry[%d]: conn_id %d, credits %d\n",
			 i, ntf->conn_entries[i].conn_id,
			 ntf->conn_entries[i].credits);
61

62
		if (ntf->conn_entries[i].conn_id == NCI_STATIC_RF_CONN_ID) {
63 64 65 66 67 68 69 70 71 72 73
			/* found static rf connection */
			atomic_add(ntf->conn_entries[i].credits,
				&ndev->credits_cnt);
		}
	}

	/* trigger the next tx */
	if (!skb_queue_empty(&ndev->tx_q))
		queue_work(ndev->tx_wq, &ndev->tx_work);
}

74 75 76 77 78 79 80 81 82 83 84 85 86 87
static void nci_core_conn_intf_error_ntf_packet(struct nci_dev *ndev,
						struct sk_buff *skb)
{
	struct nci_core_intf_error_ntf *ntf = (void *) skb->data;

	ntf->conn_id = nci_conn_id(&ntf->conn_id);

	pr_debug("status 0x%x, conn_id %d\n", ntf->status, ntf->conn_id);

	/* complete the data exchange transaction, if exists */
	if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
		nci_data_exchange_complete(ndev, NULL, -EIO);
}

88 89
static __u8 *nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev,
			struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
90 91 92 93 94 95 96 97 98 99
{
	struct rf_tech_specific_params_nfca_poll *nfca_poll;

	nfca_poll = &ntf->rf_tech_specific_params.nfca_poll;

	nfca_poll->sens_res = __le16_to_cpu(*((__u16 *)data));
	data += 2;

	nfca_poll->nfcid1_len = *data++;

J
Joe Perches 已提交
100 101
	pr_debug("sens_res 0x%x, nfcid1_len %d\n",
		 nfca_poll->sens_res, nfca_poll->nfcid1_len);
102 103 104 105 106 107 108 109 110

	memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len);
	data += nfca_poll->nfcid1_len;

	nfca_poll->sel_res_len = *data++;

	if (nfca_poll->sel_res_len != 0)
		nfca_poll->sel_res = *data++;

J
Joe Perches 已提交
111 112 113
	pr_debug("sel_res_len %d, sel_res 0x%x\n",
		 nfca_poll->sel_res_len,
		 nfca_poll->sel_res);
114 115 116 117

	return data;
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
static __u8 *nci_extract_rf_params_nfcb_passive_poll(struct nci_dev *ndev,
			struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
{
	struct rf_tech_specific_params_nfcb_poll *nfcb_poll;

	nfcb_poll = &ntf->rf_tech_specific_params.nfcb_poll;

	nfcb_poll->sensb_res_len = *data++;

	pr_debug("sensb_res_len %d\n", nfcb_poll->sensb_res_len);

	memcpy(nfcb_poll->sensb_res, data, nfcb_poll->sensb_res_len);
	data += nfcb_poll->sensb_res_len;

	return data;
}

static __u8 *nci_extract_rf_params_nfcf_passive_poll(struct nci_dev *ndev,
			struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
{
	struct rf_tech_specific_params_nfcf_poll *nfcf_poll;

	nfcf_poll = &ntf->rf_tech_specific_params.nfcf_poll;

	nfcf_poll->bit_rate = *data++;
	nfcf_poll->sensf_res_len = *data++;

	pr_debug("bit_rate %d, sensf_res_len %d\n",
		nfcf_poll->bit_rate, nfcf_poll->sensf_res_len);

	memcpy(nfcf_poll->sensf_res, data, nfcf_poll->sensf_res_len);
	data += nfcf_poll->sensf_res_len;

	return data;
}

154 155 156 157
static int nci_extract_activation_params_iso_dep(struct nci_dev *ndev,
			struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
{
	struct activation_params_nfca_poll_iso_dep *nfca_poll;
158
	struct activation_params_nfcb_poll_iso_dep *nfcb_poll;
159 160 161 162 163

	switch (ntf->activation_rf_tech_and_mode) {
	case NCI_NFC_A_PASSIVE_POLL_MODE:
		nfca_poll = &ntf->activation_params.nfca_poll_iso_dep;
		nfca_poll->rats_res_len = *data++;
164
		pr_debug("rats_res_len %d\n", nfca_poll->rats_res_len);
165 166
		if (nfca_poll->rats_res_len > 0) {
			memcpy(nfca_poll->rats_res,
167
				data,
168
				nfca_poll->rats_res_len);
169 170 171
		}
		break;

172 173 174 175 176 177 178 179 180 181 182 183
	case NCI_NFC_B_PASSIVE_POLL_MODE:
		nfcb_poll = &ntf->activation_params.nfcb_poll_iso_dep;
		nfcb_poll->attrib_res_len = *data++;
		pr_debug("attrib_res_len %d\n",
			nfcb_poll->attrib_res_len);
		if (nfcb_poll->attrib_res_len > 0) {
			memcpy(nfcb_poll->attrib_res,
				data,
				nfcb_poll->attrib_res_len);
		}
		break;

184
	default:
J
Joe Perches 已提交
185 186
		pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
		       ntf->activation_rf_tech_and_mode);
187 188 189 190 191 192 193
		return -EPROTO;
	}

	return 0;
}

static void nci_target_found(struct nci_dev *ndev,
194
				struct nci_rf_intf_activated_ntf *ntf)
195 196 197
{
	struct nfc_target nfc_tgt;

198 199 200
	memset(&nfc_tgt, 0, sizeof(nfc_tgt));

	if (ntf->rf_protocol == NCI_RF_PROTOCOL_T2T)
201
		nfc_tgt.supported_protocols = NFC_PROTO_MIFARE_MASK;
202
	else if (ntf->rf_protocol == NCI_RF_PROTOCOL_ISO_DEP)
203
		nfc_tgt.supported_protocols = NFC_PROTO_ISO14443_MASK;
204 205
	else if (ntf->rf_protocol == NCI_RF_PROTOCOL_T3T)
		nfc_tgt.supported_protocols = NFC_PROTO_FELICA_MASK;
206 207

	if (!(nfc_tgt.supported_protocols & ndev->poll_prots)) {
J
Joe Perches 已提交
208
		pr_debug("the target found does not have the desired protocol\n");
209 210 211
		return;
	}

J
Joe Perches 已提交
212 213
	pr_debug("new target found,  supported_protocols 0x%x\n",
		 nfc_tgt.supported_protocols);
214

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	if (ntf->activation_rf_tech_and_mode == NCI_NFC_A_PASSIVE_POLL_MODE) {
		nfc_tgt.sens_res =
			ntf->rf_tech_specific_params.nfca_poll.sens_res;
		nfc_tgt.sel_res =
			ntf->rf_tech_specific_params.nfca_poll.sel_res;
		nfc_tgt.nfcid1_len =
			ntf->rf_tech_specific_params.nfca_poll.nfcid1_len;
		if (nfc_tgt.nfcid1_len > 0) {
			memcpy(nfc_tgt.nfcid1,
				ntf->rf_tech_specific_params.nfca_poll.nfcid1,
				nfc_tgt.nfcid1_len);
		}
	} else if (ntf->activation_rf_tech_and_mode ==
						NCI_NFC_B_PASSIVE_POLL_MODE) {
		nfc_tgt.sensb_res_len =
			ntf->rf_tech_specific_params.nfcb_poll.sensb_res_len;
		if (nfc_tgt.sensb_res_len > 0) {
			memcpy(nfc_tgt.sensb_res,
			       ntf->rf_tech_specific_params.nfcb_poll.sensb_res,
			       nfc_tgt.sensb_res_len);
		}
	} else if (ntf->activation_rf_tech_and_mode ==
						NCI_NFC_F_PASSIVE_POLL_MODE) {
		nfc_tgt.sensf_res_len =
			ntf->rf_tech_specific_params.nfcf_poll.sensf_res_len;
		if (nfc_tgt.sensf_res_len > 0) {
			memcpy(nfc_tgt.sensf_res,
			       ntf->rf_tech_specific_params.nfcf_poll.sensf_res,
			       nfc_tgt.sensf_res_len);
		}
	}

247
	ndev->target_available_prots = nfc_tgt.supported_protocols;
248 249 250 251 252
	ndev->max_data_pkt_payload_size = ntf->max_data_pkt_payload_size;
	ndev->initial_num_credits = ntf->initial_num_credits;

	/* set the available credits to initial value */
	atomic_set(&ndev->credits_cnt, ndev->initial_num_credits);
253 254 255 256

	nfc_targets_found(ndev->nfc_dev, &nfc_tgt, 1);
}

257 258
static void nci_rf_intf_activated_ntf_packet(struct nci_dev *ndev,
						struct sk_buff *skb)
259
{
260
	struct nci_rf_intf_activated_ntf ntf;
261
	__u8 *data = skb->data;
262
	int err = 0;
263

264
	atomic_set(&ndev->state, NCI_POLL_ACTIVE);
265

266
	ntf.rf_discovery_id = *data++;
267
	ntf.rf_interface = *data++;
268
	ntf.rf_protocol = *data++;
269
	ntf.activation_rf_tech_and_mode = *data++;
270 271
	ntf.max_data_pkt_payload_size = *data++;
	ntf.initial_num_credits = *data++;
272 273
	ntf.rf_tech_specific_params_len = *data++;

J
Joe Perches 已提交
274
	pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id);
275
	pr_debug("rf_interface 0x%x\n", ntf.rf_interface);
J
Joe Perches 已提交
276 277 278
	pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol);
	pr_debug("activation_rf_tech_and_mode 0x%x\n",
		 ntf.activation_rf_tech_and_mode);
279 280 281
	pr_debug("max_data_pkt_payload_size 0x%x\n",
		 ntf.max_data_pkt_payload_size);
	pr_debug("initial_num_credits 0x%x\n", ntf.initial_num_credits);
J
Joe Perches 已提交
282 283
	pr_debug("rf_tech_specific_params_len %d\n",
		 ntf.rf_tech_specific_params_len);
284

285 286 287 288 289 290 291
	if (ntf.rf_tech_specific_params_len > 0) {
		switch (ntf.activation_rf_tech_and_mode) {
		case NCI_NFC_A_PASSIVE_POLL_MODE:
			data = nci_extract_rf_params_nfca_passive_poll(ndev,
				&ntf, data);
			break;

292 293 294 295 296 297 298 299 300 301
		case NCI_NFC_B_PASSIVE_POLL_MODE:
			data = nci_extract_rf_params_nfcb_passive_poll(ndev,
				&ntf, data);
			break;

		case NCI_NFC_F_PASSIVE_POLL_MODE:
			data = nci_extract_rf_params_nfcf_passive_poll(ndev,
				&ntf, data);
			break;

302
		default:
J
Joe Perches 已提交
303 304
			pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
			       ntf.activation_rf_tech_and_mode);
305 306 307
			return;
		}
	}
308

309 310 311 312 313
	ntf.data_exch_rf_tech_and_mode = *data++;
	ntf.data_exch_tx_bit_rate = *data++;
	ntf.data_exch_rx_bit_rate = *data++;
	ntf.activation_params_len = *data++;

J
Joe Perches 已提交
314 315 316 317 318 319 320 321
	pr_debug("data_exch_rf_tech_and_mode 0x%x\n",
		 ntf.data_exch_rf_tech_and_mode);
	pr_debug("data_exch_tx_bit_rate 0x%x\n",
		 ntf.data_exch_tx_bit_rate);
	pr_debug("data_exch_rx_bit_rate 0x%x\n",
		 ntf.data_exch_rx_bit_rate);
	pr_debug("activation_params_len %d\n",
		 ntf.activation_params_len);
322 323

	if (ntf.activation_params_len > 0) {
324
		switch (ntf.rf_interface) {
325 326 327 328 329 330 331 332 333 334
		case NCI_RF_INTERFACE_ISO_DEP:
			err = nci_extract_activation_params_iso_dep(ndev,
				&ntf, data);
			break;

		case NCI_RF_INTERFACE_FRAME:
			/* no activation params */
			break;

		default:
335 336
			pr_err("unsupported rf_interface 0x%x\n",
			       ntf.rf_interface);
337 338
			return;
		}
339 340
	}

341
	if (!err)
342 343 344 345 346 347
		nci_target_found(ndev, &ntf);
}

static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev,
					struct sk_buff *skb)
{
348
	struct nci_rf_deactivate_ntf *ntf = (void *) skb->data;
349

J
Joe Perches 已提交
350
	pr_debug("entry, type 0x%x, reason 0x%x\n", ntf->type, ntf->reason);
351

352
	atomic_set(&ndev->state, NCI_IDLE);
353 354 355 356 357 358 359 360 361 362 363 364
	ndev->target_active_prot = 0;

	/* drop tx data queue */
	skb_queue_purge(&ndev->tx_q);

	/* drop partial rx data packet */
	if (ndev->rx_data_reassembly) {
		kfree_skb(ndev->rx_data_reassembly);
		ndev->rx_data_reassembly = 0;
	}

	/* complete the data exchange transaction, if exists */
365
	if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
366
		nci_data_exchange_complete(ndev, NULL, -EIO);
367 368

	nci_req_complete(ndev, NCI_STATUS_OK);
369 370 371 372 373 374
}

void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb)
{
	__u16 ntf_opcode = nci_opcode(skb->data);

J
Joe Perches 已提交
375 376 377 378 379
	pr_debug("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
		 nci_pbf(skb->data),
		 nci_opcode_gid(ntf_opcode),
		 nci_opcode_oid(ntf_opcode),
		 nci_plen(skb->data));
380 381 382 383 384 385 386 387 388

	/* strip the nci control header */
	skb_pull(skb, NCI_CTRL_HDR_SIZE);

	switch (ntf_opcode) {
	case NCI_OP_CORE_CONN_CREDITS_NTF:
		nci_core_conn_credits_ntf_packet(ndev, skb);
		break;

389 390 391 392
	case NCI_OP_CORE_INTF_ERROR_NTF:
		nci_core_conn_intf_error_ntf_packet(ndev, skb);
		break;

393 394
	case NCI_OP_RF_INTF_ACTIVATED_NTF:
		nci_rf_intf_activated_ntf_packet(ndev, skb);
395 396 397 398 399 400 401
		break;

	case NCI_OP_RF_DEACTIVATE_NTF:
		nci_rf_deactivate_ntf_packet(ndev, skb);
		break;

	default:
J
Joe Perches 已提交
402
		pr_err("unknown ntf opcode 0x%x\n", ntf_opcode);
403 404 405 406 407
		break;
	}

	kfree_skb(skb);
}