Linux Audio

Check our new training course

Yocto distribution development and maintenance

Need a Yocto distribution for your embedded project?
Loading...
v6.9.4
   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) 2011 Texas Instruments, Inc.
   7 *  Copyright (C) 2014 Marvell International Ltd.
   8 *
   9 *  Written by Ilan Elias <ilane@ti.com>
  10 *
  11 *  Acknowledgements:
  12 *  This file is based on hci_core.c, which was written
  13 *  by Maxim Krasnyansky.
 
 
 
 
 
 
 
 
 
 
 
 
 
  14 */
  15
  16#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  17
  18#include <linux/module.h>
  19#include <linux/kernel.h>
  20#include <linux/types.h>
  21#include <linux/workqueue.h>
  22#include <linux/completion.h>
  23#include <linux/export.h>
  24#include <linux/sched.h>
  25#include <linux/bitops.h>
  26#include <linux/skbuff.h>
  27#include <linux/kcov.h>
  28
  29#include "../nfc.h"
  30#include <net/nfc/nci.h>
  31#include <net/nfc/nci_core.h>
  32#include <linux/nfc.h>
  33
  34struct core_conn_create_data {
  35	int length;
  36	struct nci_core_conn_create_cmd *cmd;
  37};
  38
  39static void nci_cmd_work(struct work_struct *work);
  40static void nci_rx_work(struct work_struct *work);
  41static void nci_tx_work(struct work_struct *work);
  42
  43struct nci_conn_info *nci_get_conn_info_by_conn_id(struct nci_dev *ndev,
  44						   int conn_id)
  45{
  46	struct nci_conn_info *conn_info;
  47
  48	list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
  49		if (conn_info->conn_id == conn_id)
  50			return conn_info;
  51	}
  52
  53	return NULL;
  54}
  55
  56int nci_get_conn_info_by_dest_type_params(struct nci_dev *ndev, u8 dest_type,
  57					  const struct dest_spec_params *params)
  58{
  59	const struct nci_conn_info *conn_info;
  60
  61	list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
  62		if (conn_info->dest_type == dest_type) {
  63			if (!params)
  64				return conn_info->conn_id;
  65
  66			if (params->id == conn_info->dest_params->id &&
  67			    params->protocol == conn_info->dest_params->protocol)
  68				return conn_info->conn_id;
  69		}
  70	}
  71
  72	return -EINVAL;
  73}
  74EXPORT_SYMBOL(nci_get_conn_info_by_dest_type_params);
  75
  76/* ---- NCI requests ---- */
  77
  78void nci_req_complete(struct nci_dev *ndev, int result)
  79{
  80	if (ndev->req_status == NCI_REQ_PEND) {
  81		ndev->req_result = result;
  82		ndev->req_status = NCI_REQ_DONE;
  83		complete(&ndev->req_completion);
  84	}
  85}
  86EXPORT_SYMBOL(nci_req_complete);
  87
  88static void nci_req_cancel(struct nci_dev *ndev, int err)
  89{
  90	if (ndev->req_status == NCI_REQ_PEND) {
  91		ndev->req_result = err;
  92		ndev->req_status = NCI_REQ_CANCELED;
  93		complete(&ndev->req_completion);
  94	}
  95}
  96
  97/* Execute request and wait for completion. */
  98static int __nci_request(struct nci_dev *ndev,
  99			 void (*req)(struct nci_dev *ndev, const void *opt),
 100			 const void *opt, __u32 timeout)
 101{
 102	int rc = 0;
 103	long completion_rc;
 104
 105	ndev->req_status = NCI_REQ_PEND;
 106
 107	reinit_completion(&ndev->req_completion);
 108	req(ndev, opt);
 109	completion_rc =
 110		wait_for_completion_interruptible_timeout(&ndev->req_completion,
 111							  timeout);
 112
 113	pr_debug("wait_for_completion return %ld\n", completion_rc);
 114
 115	if (completion_rc > 0) {
 116		switch (ndev->req_status) {
 117		case NCI_REQ_DONE:
 118			rc = nci_to_errno(ndev->req_result);
 119			break;
 120
 121		case NCI_REQ_CANCELED:
 122			rc = -ndev->req_result;
 123			break;
 124
 125		default:
 126			rc = -ETIMEDOUT;
 127			break;
 128		}
 129	} else {
 130		pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
 131		       completion_rc);
 132
 133		rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
 134	}
 135
 136	ndev->req_status = ndev->req_result = 0;
 137
 138	return rc;
 139}
 140
 141inline int nci_request(struct nci_dev *ndev,
 142		       void (*req)(struct nci_dev *ndev,
 143				   const void *opt),
 144		       const void *opt, __u32 timeout)
 145{
 146	int rc;
 147
 
 
 
 148	/* Serialize all requests */
 149	mutex_lock(&ndev->req_lock);
 150	/* check the state after obtaing the lock against any races
 151	 * from nci_close_device when the device gets removed.
 152	 */
 153	if (test_bit(NCI_UP, &ndev->flags))
 154		rc = __nci_request(ndev, req, opt, timeout);
 155	else
 156		rc = -ENETDOWN;
 157	mutex_unlock(&ndev->req_lock);
 158
 159	return rc;
 160}
 161
 162static void nci_reset_req(struct nci_dev *ndev, const void *opt)
 163{
 164	struct nci_core_reset_cmd cmd;
 165
 166	cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
 167	nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
 168}
 169
 170static void nci_init_req(struct nci_dev *ndev, const void *opt)
 171{
 172	u8 plen = 0;
 173
 174	if (opt)
 175		plen = sizeof(struct nci_core_init_v2_cmd);
 176
 177	nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, plen, opt);
 178}
 179
 180static void nci_init_complete_req(struct nci_dev *ndev, const void *opt)
 181{
 182	struct nci_rf_disc_map_cmd cmd;
 183	struct disc_map_config *cfg = cmd.mapping_configs;
 184	__u8 *num = &cmd.num_mapping_configs;
 185	int i;
 186
 187	/* set rf mapping configurations */
 188	*num = 0;
 189
 190	/* by default mapping is set to NCI_RF_INTERFACE_FRAME */
 191	for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
 192		if (ndev->supported_rf_interfaces[i] ==
 193		    NCI_RF_INTERFACE_ISO_DEP) {
 194			cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
 195			cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
 196				NCI_DISC_MAP_MODE_LISTEN;
 197			cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
 198			(*num)++;
 199		} else if (ndev->supported_rf_interfaces[i] ==
 200			   NCI_RF_INTERFACE_NFC_DEP) {
 201			cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
 202			cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
 203				NCI_DISC_MAP_MODE_LISTEN;
 204			cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
 205			(*num)++;
 206		}
 207
 208		if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
 209			break;
 210	}
 211
 212	nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
 213		     (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
 214}
 215
 216struct nci_set_config_param {
 217	__u8		id;
 218	size_t		len;
 219	const __u8	*val;
 220};
 221
 222static void nci_set_config_req(struct nci_dev *ndev, const void *opt)
 223{
 224	const struct nci_set_config_param *param = opt;
 225	struct nci_core_set_config_cmd cmd;
 226
 227	BUG_ON(param->len > NCI_MAX_PARAM_LEN);
 228
 229	cmd.num_params = 1;
 230	cmd.param.id = param->id;
 231	cmd.param.len = param->len;
 232	memcpy(cmd.param.val, param->val, param->len);
 233
 234	nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
 235}
 236
 237struct nci_rf_discover_param {
 238	__u32	im_protocols;
 239	__u32	tm_protocols;
 240};
 241
 242static void nci_rf_discover_req(struct nci_dev *ndev, const void *opt)
 243{
 244	const struct nci_rf_discover_param *param = opt;
 
 245	struct nci_rf_disc_cmd cmd;
 246
 247	cmd.num_disc_configs = 0;
 248
 249	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
 250	    (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
 251	     param->im_protocols & NFC_PROTO_MIFARE_MASK ||
 252	     param->im_protocols & NFC_PROTO_ISO14443_MASK ||
 253	     param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
 254		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
 255			NCI_NFC_A_PASSIVE_POLL_MODE;
 256		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
 257		cmd.num_disc_configs++;
 258	}
 259
 260	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
 261	    (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
 262		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
 263			NCI_NFC_B_PASSIVE_POLL_MODE;
 264		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
 265		cmd.num_disc_configs++;
 266	}
 267
 268	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
 269	    (param->im_protocols & NFC_PROTO_FELICA_MASK ||
 270	     param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
 271		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
 272			NCI_NFC_F_PASSIVE_POLL_MODE;
 273		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
 274		cmd.num_disc_configs++;
 275	}
 276
 277	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
 278	    (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
 279		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
 280			NCI_NFC_V_PASSIVE_POLL_MODE;
 281		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
 282		cmd.num_disc_configs++;
 283	}
 284
 285	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
 286	    (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
 287		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
 288			NCI_NFC_A_PASSIVE_LISTEN_MODE;
 289		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
 290		cmd.num_disc_configs++;
 291		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
 292			NCI_NFC_F_PASSIVE_LISTEN_MODE;
 293		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
 294		cmd.num_disc_configs++;
 295	}
 296
 297	nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
 298		     (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
 299		     &cmd);
 300}
 301
 302struct nci_rf_discover_select_param {
 303	__u8	rf_discovery_id;
 304	__u8	rf_protocol;
 305};
 306
 307static void nci_rf_discover_select_req(struct nci_dev *ndev, const void *opt)
 308{
 309	const struct nci_rf_discover_select_param *param = opt;
 
 310	struct nci_rf_discover_select_cmd cmd;
 311
 312	cmd.rf_discovery_id = param->rf_discovery_id;
 313	cmd.rf_protocol = param->rf_protocol;
 314
 315	switch (cmd.rf_protocol) {
 316	case NCI_RF_PROTOCOL_ISO_DEP:
 317		cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
 318		break;
 319
 320	case NCI_RF_PROTOCOL_NFC_DEP:
 321		cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
 322		break;
 323
 324	default:
 325		cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
 326		break;
 327	}
 328
 329	nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
 330		     sizeof(struct nci_rf_discover_select_cmd), &cmd);
 331}
 332
 333static void nci_rf_deactivate_req(struct nci_dev *ndev, const void *opt)
 334{
 335	struct nci_rf_deactivate_cmd cmd;
 336
 337	cmd.type = (unsigned long)opt;
 338
 339	nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
 340		     sizeof(struct nci_rf_deactivate_cmd), &cmd);
 341}
 342
 343struct nci_cmd_param {
 344	__u16 opcode;
 345	size_t len;
 346	const __u8 *payload;
 347};
 348
 349static void nci_generic_req(struct nci_dev *ndev, const void *opt)
 350{
 351	const struct nci_cmd_param *param = opt;
 
 352
 353	nci_send_cmd(ndev, param->opcode, param->len, param->payload);
 354}
 355
 356int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, const __u8 *payload)
 357{
 358	struct nci_cmd_param param;
 359
 360	param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid);
 361	param.len = len;
 362	param.payload = payload;
 363
 364	return __nci_request(ndev, nci_generic_req, &param,
 365			     msecs_to_jiffies(NCI_CMD_TIMEOUT));
 366}
 367EXPORT_SYMBOL(nci_prop_cmd);
 368
 369int nci_core_cmd(struct nci_dev *ndev, __u16 opcode, size_t len,
 370		 const __u8 *payload)
 371{
 372	struct nci_cmd_param param;
 373
 374	param.opcode = opcode;
 375	param.len = len;
 376	param.payload = payload;
 377
 378	return __nci_request(ndev, nci_generic_req, &param,
 379			     msecs_to_jiffies(NCI_CMD_TIMEOUT));
 380}
 381EXPORT_SYMBOL(nci_core_cmd);
 382
 383int nci_core_reset(struct nci_dev *ndev)
 384{
 385	return __nci_request(ndev, nci_reset_req, (void *)0,
 386			     msecs_to_jiffies(NCI_RESET_TIMEOUT));
 387}
 388EXPORT_SYMBOL(nci_core_reset);
 389
 390int nci_core_init(struct nci_dev *ndev)
 391{
 392	return __nci_request(ndev, nci_init_req, (void *)0,
 393			     msecs_to_jiffies(NCI_INIT_TIMEOUT));
 394}
 395EXPORT_SYMBOL(nci_core_init);
 396
 397struct nci_loopback_data {
 398	u8 conn_id;
 399	struct sk_buff *data;
 400};
 401
 402static void nci_send_data_req(struct nci_dev *ndev, const void *opt)
 403{
 404	const struct nci_loopback_data *data = opt;
 405
 406	nci_send_data(ndev, data->conn_id, data->data);
 407}
 408
 409static void nci_nfcc_loopback_cb(void *context, struct sk_buff *skb, int err)
 410{
 411	struct nci_dev *ndev = (struct nci_dev *)context;
 412	struct nci_conn_info *conn_info;
 413
 414	conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
 415	if (!conn_info) {
 416		nci_req_complete(ndev, NCI_STATUS_REJECTED);
 417		return;
 418	}
 419
 420	conn_info->rx_skb = skb;
 421
 422	nci_req_complete(ndev, NCI_STATUS_OK);
 423}
 424
 425int nci_nfcc_loopback(struct nci_dev *ndev, const void *data, size_t data_len,
 426		      struct sk_buff **resp)
 427{
 428	int r;
 429	struct nci_loopback_data loopback_data;
 430	struct nci_conn_info *conn_info;
 431	struct sk_buff *skb;
 432	int conn_id = nci_get_conn_info_by_dest_type_params(ndev,
 433					NCI_DESTINATION_NFCC_LOOPBACK, NULL);
 434
 435	if (conn_id < 0) {
 436		r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCC_LOOPBACK,
 437					 0, 0, NULL);
 438		if (r != NCI_STATUS_OK)
 439			return r;
 440
 441		conn_id = nci_get_conn_info_by_dest_type_params(ndev,
 442					NCI_DESTINATION_NFCC_LOOPBACK,
 443					NULL);
 444	}
 445
 446	conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
 447	if (!conn_info)
 448		return -EPROTO;
 449
 450	/* store cb and context to be used on receiving data */
 451	conn_info->data_exchange_cb = nci_nfcc_loopback_cb;
 452	conn_info->data_exchange_cb_context = ndev;
 453
 454	skb = nci_skb_alloc(ndev, NCI_DATA_HDR_SIZE + data_len, GFP_KERNEL);
 455	if (!skb)
 456		return -ENOMEM;
 457
 458	skb_reserve(skb, NCI_DATA_HDR_SIZE);
 459	skb_put_data(skb, data, data_len);
 460
 461	loopback_data.conn_id = conn_id;
 462	loopback_data.data = skb;
 463
 464	ndev->cur_conn_id = conn_id;
 465	r = nci_request(ndev, nci_send_data_req, &loopback_data,
 466			msecs_to_jiffies(NCI_DATA_TIMEOUT));
 467	if (r == NCI_STATUS_OK && resp)
 468		*resp = conn_info->rx_skb;
 469
 470	return r;
 471}
 472EXPORT_SYMBOL(nci_nfcc_loopback);
 473
 474static int nci_open_device(struct nci_dev *ndev)
 475{
 476	int rc = 0;
 477
 478	mutex_lock(&ndev->req_lock);
 479
 480	if (test_bit(NCI_UNREG, &ndev->flags)) {
 481		rc = -ENODEV;
 482		goto done;
 483	}
 484
 485	if (test_bit(NCI_UP, &ndev->flags)) {
 486		rc = -EALREADY;
 487		goto done;
 488	}
 489
 490	if (ndev->ops->open(ndev)) {
 491		rc = -EIO;
 492		goto done;
 493	}
 494
 495	atomic_set(&ndev->cmd_cnt, 1);
 496
 497	set_bit(NCI_INIT, &ndev->flags);
 498
 499	if (ndev->ops->init)
 500		rc = ndev->ops->init(ndev);
 501
 502	if (!rc) {
 503		rc = __nci_request(ndev, nci_reset_req, (void *)0,
 504				   msecs_to_jiffies(NCI_RESET_TIMEOUT));
 505	}
 506
 507	if (!rc && ndev->ops->setup) {
 508		rc = ndev->ops->setup(ndev);
 509	}
 510
 511	if (!rc) {
 512		struct nci_core_init_v2_cmd nci_init_v2_cmd = {
 513			.feature1 = NCI_FEATURE_DISABLE,
 514			.feature2 = NCI_FEATURE_DISABLE
 515		};
 516		const void *opt = NULL;
 517
 518		if (ndev->nci_ver & NCI_VER_2_MASK)
 519			opt = &nci_init_v2_cmd;
 520
 521		rc = __nci_request(ndev, nci_init_req, opt,
 522				   msecs_to_jiffies(NCI_INIT_TIMEOUT));
 523	}
 524
 525	if (!rc && ndev->ops->post_setup)
 526		rc = ndev->ops->post_setup(ndev);
 527
 528	if (!rc) {
 529		rc = __nci_request(ndev, nci_init_complete_req, (void *)0,
 530				   msecs_to_jiffies(NCI_INIT_TIMEOUT));
 531	}
 532
 533	clear_bit(NCI_INIT, &ndev->flags);
 534
 535	if (!rc) {
 536		set_bit(NCI_UP, &ndev->flags);
 537		nci_clear_target_list(ndev);
 538		atomic_set(&ndev->state, NCI_IDLE);
 539	} else {
 540		/* Init failed, cleanup */
 541		skb_queue_purge(&ndev->cmd_q);
 542		skb_queue_purge(&ndev->rx_q);
 543		skb_queue_purge(&ndev->tx_q);
 544
 545		ndev->ops->close(ndev);
 546		ndev->flags &= BIT(NCI_UNREG);
 547	}
 548
 549done:
 550	mutex_unlock(&ndev->req_lock);
 551	return rc;
 552}
 553
 554static int nci_close_device(struct nci_dev *ndev)
 555{
 556	nci_req_cancel(ndev, ENODEV);
 557
 558	/* This mutex needs to be held as a barrier for
 559	 * caller nci_unregister_device
 560	 */
 561	mutex_lock(&ndev->req_lock);
 562
 563	if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
 564		/* Need to flush the cmd wq in case
 565		 * there is a queued/running cmd_work
 566		 */
 567		flush_workqueue(ndev->cmd_wq);
 568		del_timer_sync(&ndev->cmd_timer);
 569		del_timer_sync(&ndev->data_timer);
 570		mutex_unlock(&ndev->req_lock);
 571		return 0;
 572	}
 573
 574	/* Drop RX and TX queues */
 575	skb_queue_purge(&ndev->rx_q);
 576	skb_queue_purge(&ndev->tx_q);
 577
 578	/* Flush RX and TX wq */
 579	flush_workqueue(ndev->rx_wq);
 580	flush_workqueue(ndev->tx_wq);
 581
 582	/* Reset device */
 583	skb_queue_purge(&ndev->cmd_q);
 584	atomic_set(&ndev->cmd_cnt, 1);
 585
 586	set_bit(NCI_INIT, &ndev->flags);
 587	__nci_request(ndev, nci_reset_req, (void *)0,
 588		      msecs_to_jiffies(NCI_RESET_TIMEOUT));
 589
 590	/* After this point our queues are empty
 591	 * and no works are scheduled.
 592	 */
 593	ndev->ops->close(ndev);
 594
 595	clear_bit(NCI_INIT, &ndev->flags);
 596
 
 
 597	/* Flush cmd wq */
 598	flush_workqueue(ndev->cmd_wq);
 599
 600	del_timer_sync(&ndev->cmd_timer);
 601
 602	/* Clear flags except NCI_UNREG */
 603	ndev->flags &= BIT(NCI_UNREG);
 604
 605	mutex_unlock(&ndev->req_lock);
 606
 607	return 0;
 608}
 609
 610/* NCI command timer function */
 611static void nci_cmd_timer(struct timer_list *t)
 612{
 613	struct nci_dev *ndev = from_timer(ndev, t, cmd_timer);
 614
 615	atomic_set(&ndev->cmd_cnt, 1);
 616	queue_work(ndev->cmd_wq, &ndev->cmd_work);
 617}
 618
 619/* NCI data exchange timer function */
 620static void nci_data_timer(struct timer_list *t)
 621{
 622	struct nci_dev *ndev = from_timer(ndev, t, data_timer);
 623
 624	set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
 625	queue_work(ndev->rx_wq, &ndev->rx_work);
 626}
 627
 628static int nci_dev_up(struct nfc_dev *nfc_dev)
 629{
 630	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 631
 632	return nci_open_device(ndev);
 633}
 634
 635static int nci_dev_down(struct nfc_dev *nfc_dev)
 636{
 637	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 638
 639	return nci_close_device(ndev);
 640}
 641
 642int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, const __u8 *val)
 643{
 644	struct nci_set_config_param param;
 645
 646	if (!val || !len)
 647		return 0;
 648
 649	param.id = id;
 650	param.len = len;
 651	param.val = val;
 652
 653	return __nci_request(ndev, nci_set_config_req, &param,
 654			     msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
 655}
 656EXPORT_SYMBOL(nci_set_config);
 657
 658static void nci_nfcee_discover_req(struct nci_dev *ndev, const void *opt)
 659{
 660	struct nci_nfcee_discover_cmd cmd;
 661	__u8 action = (unsigned long)opt;
 662
 663	cmd.discovery_action = action;
 664
 665	nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
 666}
 667
 668int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
 669{
 670	unsigned long opt = action;
 671
 672	return __nci_request(ndev, nci_nfcee_discover_req, (void *)opt,
 673				msecs_to_jiffies(NCI_CMD_TIMEOUT));
 674}
 675EXPORT_SYMBOL(nci_nfcee_discover);
 676
 677static void nci_nfcee_mode_set_req(struct nci_dev *ndev, const void *opt)
 678{
 679	const struct nci_nfcee_mode_set_cmd *cmd = opt;
 
 680
 681	nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
 682		     sizeof(struct nci_nfcee_mode_set_cmd), cmd);
 683}
 684
 685int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
 686{
 687	struct nci_nfcee_mode_set_cmd cmd;
 688
 689	cmd.nfcee_id = nfcee_id;
 690	cmd.nfcee_mode = nfcee_mode;
 691
 692	return __nci_request(ndev, nci_nfcee_mode_set_req, &cmd,
 
 693			     msecs_to_jiffies(NCI_CMD_TIMEOUT));
 694}
 695EXPORT_SYMBOL(nci_nfcee_mode_set);
 696
 697static void nci_core_conn_create_req(struct nci_dev *ndev, const void *opt)
 698{
 699	const struct core_conn_create_data *data = opt;
 
 700
 701	nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
 702}
 703
 704int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
 705			 u8 number_destination_params,
 706			 size_t params_len,
 707			 const struct core_conn_create_dest_spec_params *params)
 708{
 709	int r;
 710	struct nci_core_conn_create_cmd *cmd;
 711	struct core_conn_create_data data;
 712
 
 
 
 713	data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
 714	cmd = kzalloc(data.length, GFP_KERNEL);
 715	if (!cmd)
 716		return -ENOMEM;
 717
 718	cmd->destination_type = destination_type;
 719	cmd->number_destination_params = number_destination_params;
 
 720
 721	data.cmd = cmd;
 722
 723	if (params) {
 724		memcpy(cmd->params, params, params_len);
 725		if (params->length > 0)
 726			memcpy(&ndev->cur_params,
 727			       &params->value[DEST_SPEC_PARAMS_ID_INDEX],
 728			       sizeof(struct dest_spec_params));
 729		else
 730			ndev->cur_params.id = 0;
 731	} else {
 732		ndev->cur_params.id = 0;
 733	}
 734	ndev->cur_dest_type = destination_type;
 735
 736	r = __nci_request(ndev, nci_core_conn_create_req, &data,
 
 737			  msecs_to_jiffies(NCI_CMD_TIMEOUT));
 738	kfree(cmd);
 739	return r;
 740}
 741EXPORT_SYMBOL(nci_core_conn_create);
 742
 743static void nci_core_conn_close_req(struct nci_dev *ndev, const void *opt)
 744{
 745	__u8 conn_id = (unsigned long)opt;
 746
 747	nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
 748}
 749
 750int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
 751{
 752	unsigned long opt = conn_id;
 753
 754	ndev->cur_conn_id = conn_id;
 755	return __nci_request(ndev, nci_core_conn_close_req, (void *)opt,
 756			     msecs_to_jiffies(NCI_CMD_TIMEOUT));
 757}
 758EXPORT_SYMBOL(nci_core_conn_close);
 759
 760static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
 761{
 762	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 763	struct nci_set_config_param param;
 764	int rc;
 765
 766	param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
 767	if ((param.val == NULL) || (param.len == 0))
 768		return 0;
 769
 770	if (param.len > NFC_MAX_GT_LEN)
 771		return -EINVAL;
 772
 773	param.id = NCI_PN_ATR_REQ_GEN_BYTES;
 774
 775	rc = nci_request(ndev, nci_set_config_req, &param,
 776			 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
 777	if (rc)
 778		return rc;
 779
 780	param.id = NCI_LN_ATR_RES_GEN_BYTES;
 781
 782	return nci_request(ndev, nci_set_config_req, &param,
 783			   msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
 784}
 785
 786static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
 787{
 788	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 789	int rc;
 790	__u8 val;
 791
 792	val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
 793
 794	rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
 795	if (rc)
 796		return rc;
 797
 798	val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
 799
 800	rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
 801	if (rc)
 802		return rc;
 803
 804	val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
 805
 806	return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
 807}
 808
 809static int nci_start_poll(struct nfc_dev *nfc_dev,
 810			  __u32 im_protocols, __u32 tm_protocols)
 811{
 812	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 813	struct nci_rf_discover_param param;
 814	int rc;
 815
 816	if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
 817	    (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
 818		pr_err("unable to start poll, since poll is already active\n");
 819		return -EBUSY;
 820	}
 821
 822	if (ndev->target_active_prot) {
 823		pr_err("there is an active target\n");
 824		return -EBUSY;
 825	}
 826
 827	if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
 828	    (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
 829		pr_debug("target active or w4 select, implicitly deactivate\n");
 830
 831		rc = nci_request(ndev, nci_rf_deactivate_req,
 832				 (void *)NCI_DEACTIVATE_TYPE_IDLE_MODE,
 833				 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
 834		if (rc)
 835			return -EBUSY;
 836	}
 837
 838	if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
 839		rc = nci_set_local_general_bytes(nfc_dev);
 840		if (rc) {
 841			pr_err("failed to set local general bytes\n");
 842			return rc;
 843		}
 844	}
 845
 846	if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
 847		rc = nci_set_listen_parameters(nfc_dev);
 848		if (rc)
 849			pr_err("failed to set listen parameters\n");
 850	}
 851
 852	param.im_protocols = im_protocols;
 853	param.tm_protocols = tm_protocols;
 854	rc = nci_request(ndev, nci_rf_discover_req, &param,
 855			 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
 856
 857	if (!rc)
 858		ndev->poll_prots = im_protocols;
 859
 860	return rc;
 861}
 862
 863static void nci_stop_poll(struct nfc_dev *nfc_dev)
 864{
 865	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 866
 867	if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
 868	    (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
 869		pr_err("unable to stop poll, since poll is not active\n");
 870		return;
 871	}
 872
 873	nci_request(ndev, nci_rf_deactivate_req,
 874		    (void *)NCI_DEACTIVATE_TYPE_IDLE_MODE,
 875		    msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
 876}
 877
 878static int nci_activate_target(struct nfc_dev *nfc_dev,
 879			       struct nfc_target *target, __u32 protocol)
 880{
 881	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 882	struct nci_rf_discover_select_param param;
 883	const struct nfc_target *nci_target = NULL;
 884	int i;
 885	int rc = 0;
 886
 887	pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
 888
 889	if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
 890	    (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
 891		pr_err("there is no available target to activate\n");
 892		return -EINVAL;
 893	}
 894
 895	if (ndev->target_active_prot) {
 896		pr_err("there is already an active target\n");
 897		return -EBUSY;
 898	}
 899
 900	for (i = 0; i < ndev->n_targets; i++) {
 901		if (ndev->targets[i].idx == target->idx) {
 902			nci_target = &ndev->targets[i];
 903			break;
 904		}
 905	}
 906
 907	if (!nci_target) {
 908		pr_err("unable to find the selected target\n");
 909		return -EINVAL;
 910	}
 911
 912	if (protocol >= NFC_PROTO_MAX) {
 913		pr_err("the requested nfc protocol is invalid\n");
 914		return -EINVAL;
 915	}
 916
 917	if (!(nci_target->supported_protocols & (1 << protocol))) {
 918		pr_err("target does not support the requested protocol 0x%x\n",
 919		       protocol);
 920		return -EINVAL;
 921	}
 922
 923	if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
 924		param.rf_discovery_id = nci_target->logical_idx;
 925
 926		if (protocol == NFC_PROTO_JEWEL)
 927			param.rf_protocol = NCI_RF_PROTOCOL_T1T;
 928		else if (protocol == NFC_PROTO_MIFARE)
 929			param.rf_protocol = NCI_RF_PROTOCOL_T2T;
 930		else if (protocol == NFC_PROTO_FELICA)
 931			param.rf_protocol = NCI_RF_PROTOCOL_T3T;
 932		else if (protocol == NFC_PROTO_ISO14443 ||
 933			 protocol == NFC_PROTO_ISO14443_B)
 934			param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
 935		else
 936			param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
 937
 938		rc = nci_request(ndev, nci_rf_discover_select_req, &param,
 
 939				 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
 940	}
 941
 942	if (!rc)
 943		ndev->target_active_prot = protocol;
 944
 945	return rc;
 946}
 947
 948static void nci_deactivate_target(struct nfc_dev *nfc_dev,
 949				  struct nfc_target *target,
 950				  __u8 mode)
 951{
 952	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 953	unsigned long nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
 
 
 954
 955	if (!ndev->target_active_prot) {
 956		pr_err("unable to deactivate target, no active target\n");
 957		return;
 958	}
 959
 960	ndev->target_active_prot = 0;
 961
 962	switch (mode) {
 963	case NFC_TARGET_MODE_SLEEP:
 964		nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
 965		break;
 966	}
 967
 968	if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
 969		nci_request(ndev, nci_rf_deactivate_req, (void *)nci_mode,
 970			    msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
 971	}
 972}
 973
 974static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
 975			   __u8 comm_mode, __u8 *gb, size_t gb_len)
 976{
 977	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 978	int rc;
 979
 980	pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
 981
 982	rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
 983	if (rc)
 984		return rc;
 985
 986	rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
 987					  ndev->remote_gb_len);
 988	if (!rc)
 989		rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
 990					NFC_RF_INITIATOR);
 991
 992	return rc;
 993}
 994
 995static int nci_dep_link_down(struct nfc_dev *nfc_dev)
 996{
 997	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 998	int rc;
 999
 
 
1000	if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
1001		nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
1002	} else {
1003		if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
1004		    atomic_read(&ndev->state) == NCI_DISCOVERY) {
1005			nci_request(ndev, nci_rf_deactivate_req, (void *)0,
1006				    msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
1007		}
1008
1009		rc = nfc_tm_deactivated(nfc_dev);
1010		if (rc)
1011			pr_err("error when signaling tm deactivation\n");
1012	}
1013
1014	return 0;
1015}
1016
1017
1018static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
1019			  struct sk_buff *skb,
1020			  data_exchange_cb_t cb, void *cb_context)
1021{
1022	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1023	int rc;
1024	struct nci_conn_info *conn_info;
1025
1026	conn_info = ndev->rf_conn_info;
1027	if (!conn_info)
1028		return -EPROTO;
1029
1030	pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
1031
1032	if (!ndev->target_active_prot) {
1033		pr_err("unable to exchange data, no active target\n");
1034		return -EINVAL;
1035	}
1036
1037	if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1038		return -EBUSY;
1039
1040	/* store cb and context to be used on receiving data */
1041	conn_info->data_exchange_cb = cb;
1042	conn_info->data_exchange_cb_context = cb_context;
1043
1044	rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1045	if (rc)
1046		clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
1047
1048	return rc;
1049}
1050
1051static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
1052{
1053	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1054	int rc;
1055
1056	rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1057	if (rc)
1058		pr_err("unable to send data\n");
1059
1060	return rc;
1061}
1062
1063static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1064{
1065	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1066
1067	if (ndev->ops->enable_se)
1068		return ndev->ops->enable_se(ndev, se_idx);
1069
1070	return 0;
1071}
1072
1073static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1074{
1075	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1076
1077	if (ndev->ops->disable_se)
1078		return ndev->ops->disable_se(ndev, se_idx);
1079
1080	return 0;
1081}
1082
1083static int nci_discover_se(struct nfc_dev *nfc_dev)
1084{
1085	int r;
1086	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1087
1088	if (ndev->ops->discover_se) {
1089		r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
1090		if (r != NCI_STATUS_OK)
1091			return -EPROTO;
1092
1093		return ndev->ops->discover_se(ndev);
1094	}
1095
1096	return 0;
1097}
1098
1099static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
1100		     u8 *apdu, size_t apdu_length,
1101		     se_io_cb_t cb, void *cb_context)
1102{
1103	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1104
1105	if (ndev->ops->se_io)
1106		return ndev->ops->se_io(ndev, se_idx, apdu,
1107				apdu_length, cb, cb_context);
1108
1109	return 0;
1110}
1111
1112static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
1113{
1114	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1115
1116	if (!ndev->ops->fw_download)
1117		return -ENOTSUPP;
1118
1119	return ndev->ops->fw_download(ndev, firmware_name);
1120}
1121
1122static const struct nfc_ops nci_nfc_ops = {
1123	.dev_up = nci_dev_up,
1124	.dev_down = nci_dev_down,
1125	.start_poll = nci_start_poll,
1126	.stop_poll = nci_stop_poll,
1127	.dep_link_up = nci_dep_link_up,
1128	.dep_link_down = nci_dep_link_down,
1129	.activate_target = nci_activate_target,
1130	.deactivate_target = nci_deactivate_target,
1131	.im_transceive = nci_transceive,
1132	.tm_send = nci_tm_send,
1133	.enable_se = nci_enable_se,
1134	.disable_se = nci_disable_se,
1135	.discover_se = nci_discover_se,
1136	.se_io = nci_se_io,
1137	.fw_download = nci_fw_download,
1138};
1139
1140/* ---- Interface to NCI drivers ---- */
1141/**
1142 * nci_allocate_device - allocate a new nci device
1143 *
1144 * @ops: device operations
1145 * @supported_protocols: NFC protocols supported by the device
1146 * @tx_headroom: Reserved space at beginning of skb
1147 * @tx_tailroom: Reserved space at end of skb
1148 */
1149struct nci_dev *nci_allocate_device(const struct nci_ops *ops,
1150				    __u32 supported_protocols,
1151				    int tx_headroom, int tx_tailroom)
1152{
1153	struct nci_dev *ndev;
1154
1155	pr_debug("supported_protocols 0x%x\n", supported_protocols);
1156
1157	if (!ops->open || !ops->close || !ops->send)
1158		return NULL;
1159
1160	if (!supported_protocols)
1161		return NULL;
1162
1163	ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1164	if (!ndev)
1165		return NULL;
1166
1167	ndev->ops = ops;
1168
1169	if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1170		pr_err("Too many proprietary commands: %zd\n",
1171		       ops->n_prop_ops);
1172		goto free_nci;
 
1173	}
1174
1175	ndev->tx_headroom = tx_headroom;
1176	ndev->tx_tailroom = tx_tailroom;
1177	init_completion(&ndev->req_completion);
1178
1179	ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
1180					    supported_protocols,
1181					    tx_headroom + NCI_DATA_HDR_SIZE,
1182					    tx_tailroom);
1183	if (!ndev->nfc_dev)
1184		goto free_nci;
1185
1186	ndev->hci_dev = nci_hci_allocate(ndev);
1187	if (!ndev->hci_dev)
1188		goto free_nfc;
1189
1190	nfc_set_drvdata(ndev->nfc_dev, ndev);
1191
1192	return ndev;
1193
1194free_nfc:
1195	nfc_free_device(ndev->nfc_dev);
 
1196free_nci:
1197	kfree(ndev);
1198	return NULL;
1199}
1200EXPORT_SYMBOL(nci_allocate_device);
1201
1202/**
1203 * nci_free_device - deallocate nci device
1204 *
1205 * @ndev: The nci device to deallocate
1206 */
1207void nci_free_device(struct nci_dev *ndev)
1208{
1209	nfc_free_device(ndev->nfc_dev);
1210	nci_hci_deallocate(ndev);
1211
1212	/* drop partial rx data packet if present */
1213	if (ndev->rx_data_reassembly)
1214		kfree_skb(ndev->rx_data_reassembly);
1215	kfree(ndev);
1216}
1217EXPORT_SYMBOL(nci_free_device);
1218
1219/**
1220 * nci_register_device - register a nci device in the nfc subsystem
1221 *
1222 * @ndev: The nci device to register
1223 */
1224int nci_register_device(struct nci_dev *ndev)
1225{
1226	int rc;
1227	struct device *dev = &ndev->nfc_dev->dev;
1228	char name[32];
1229
1230	ndev->flags = 0;
1231
1232	INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1233	snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1234	ndev->cmd_wq = create_singlethread_workqueue(name);
1235	if (!ndev->cmd_wq) {
1236		rc = -ENOMEM;
1237		goto exit;
1238	}
1239
1240	INIT_WORK(&ndev->rx_work, nci_rx_work);
1241	snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1242	ndev->rx_wq = create_singlethread_workqueue(name);
1243	if (!ndev->rx_wq) {
1244		rc = -ENOMEM;
1245		goto destroy_cmd_wq_exit;
1246	}
1247
1248	INIT_WORK(&ndev->tx_work, nci_tx_work);
1249	snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1250	ndev->tx_wq = create_singlethread_workqueue(name);
1251	if (!ndev->tx_wq) {
1252		rc = -ENOMEM;
1253		goto destroy_rx_wq_exit;
1254	}
1255
1256	skb_queue_head_init(&ndev->cmd_q);
1257	skb_queue_head_init(&ndev->rx_q);
1258	skb_queue_head_init(&ndev->tx_q);
1259
1260	timer_setup(&ndev->cmd_timer, nci_cmd_timer, 0);
1261	timer_setup(&ndev->data_timer, nci_data_timer, 0);
 
 
1262
1263	mutex_init(&ndev->req_lock);
1264	INIT_LIST_HEAD(&ndev->conn_info_list);
1265
1266	rc = nfc_register_device(ndev->nfc_dev);
1267	if (rc)
1268		goto destroy_tx_wq_exit;
1269
1270	goto exit;
1271
1272destroy_tx_wq_exit:
1273	destroy_workqueue(ndev->tx_wq);
1274
1275destroy_rx_wq_exit:
1276	destroy_workqueue(ndev->rx_wq);
1277
1278destroy_cmd_wq_exit:
1279	destroy_workqueue(ndev->cmd_wq);
1280
1281exit:
1282	return rc;
1283}
1284EXPORT_SYMBOL(nci_register_device);
1285
1286/**
1287 * nci_unregister_device - unregister a nci device in the nfc subsystem
1288 *
1289 * @ndev: The nci device to unregister
1290 */
1291void nci_unregister_device(struct nci_dev *ndev)
1292{
1293	struct nci_conn_info *conn_info, *n;
1294
1295	/* This set_bit is not protected with specialized barrier,
1296	 * However, it is fine because the mutex_lock(&ndev->req_lock);
1297	 * in nci_close_device() will help to emit one.
1298	 */
1299	set_bit(NCI_UNREG, &ndev->flags);
1300
1301	nci_close_device(ndev);
1302
1303	destroy_workqueue(ndev->cmd_wq);
1304	destroy_workqueue(ndev->rx_wq);
1305	destroy_workqueue(ndev->tx_wq);
1306
1307	list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1308		list_del(&conn_info->list);
1309		/* conn_info is allocated with devm_kzalloc */
1310	}
1311
1312	nfc_unregister_device(ndev->nfc_dev);
1313}
1314EXPORT_SYMBOL(nci_unregister_device);
1315
1316/**
1317 * nci_recv_frame - receive frame from NCI drivers
1318 *
1319 * @ndev: The nci device
1320 * @skb: The sk_buff to receive
1321 */
1322int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1323{
1324	pr_debug("len %d\n", skb->len);
1325
1326	if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1327	    !test_bit(NCI_INIT, &ndev->flags))) {
1328		kfree_skb(skb);
1329		return -ENXIO;
1330	}
1331
1332	/* Queue frame for rx worker thread */
1333	skb_queue_tail(&ndev->rx_q, skb);
1334	queue_work(ndev->rx_wq, &ndev->rx_work);
1335
1336	return 0;
1337}
1338EXPORT_SYMBOL(nci_recv_frame);
1339
1340int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1341{
1342	pr_debug("len %d\n", skb->len);
1343
1344	if (!ndev) {
1345		kfree_skb(skb);
1346		return -ENODEV;
1347	}
1348
1349	/* Get rid of skb owner, prior to sending to the driver. */
1350	skb_orphan(skb);
1351
1352	/* Send copy to sniffer */
1353	nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1354			     RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1355
1356	return ndev->ops->send(ndev, skb);
1357}
1358EXPORT_SYMBOL(nci_send_frame);
1359
1360/* Send NCI command */
1361int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, const void *payload)
1362{
1363	struct nci_ctrl_hdr *hdr;
1364	struct sk_buff *skb;
1365
1366	pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1367
1368	skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1369	if (!skb) {
1370		pr_err("no memory for command\n");
1371		return -ENOMEM;
1372	}
1373
1374	hdr = skb_put(skb, NCI_CTRL_HDR_SIZE);
1375	hdr->gid = nci_opcode_gid(opcode);
1376	hdr->oid = nci_opcode_oid(opcode);
1377	hdr->plen = plen;
1378
1379	nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1380	nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1381
1382	if (plen)
1383		skb_put_data(skb, payload, plen);
1384
1385	skb_queue_tail(&ndev->cmd_q, skb);
1386	queue_work(ndev->cmd_wq, &ndev->cmd_work);
1387
1388	return 0;
1389}
1390EXPORT_SYMBOL(nci_send_cmd);
1391
1392/* Proprietary commands API */
1393static const struct nci_driver_ops *ops_cmd_lookup(const struct nci_driver_ops *ops,
1394						   size_t n_ops,
1395						   __u16 opcode)
1396{
1397	size_t i;
1398	const struct nci_driver_ops *op;
1399
1400	if (!ops || !n_ops)
1401		return NULL;
1402
1403	for (i = 0; i < n_ops; i++) {
1404		op = &ops[i];
1405		if (op->opcode == opcode)
1406			return op;
1407	}
1408
1409	return NULL;
1410}
1411
1412static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1413			     struct sk_buff *skb, const struct nci_driver_ops *ops,
1414			     size_t n_ops)
1415{
1416	const struct nci_driver_ops *op;
1417
1418	op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
1419	if (!op || !op->rsp)
1420		return -ENOTSUPP;
1421
1422	return op->rsp(ndev, skb);
1423}
1424
1425static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1426			     struct sk_buff *skb, const struct nci_driver_ops *ops,
1427			     size_t n_ops)
1428{
1429	const struct nci_driver_ops *op;
1430
1431	op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
1432	if (!op || !op->ntf)
1433		return -ENOTSUPP;
1434
1435	return op->ntf(ndev, skb);
1436}
1437
1438int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1439			struct sk_buff *skb)
1440{
1441	return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1442				 ndev->ops->n_prop_ops);
1443}
1444
1445int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1446			struct sk_buff *skb)
1447{
1448	return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1449				 ndev->ops->n_prop_ops);
1450}
1451
1452int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1453			struct sk_buff *skb)
1454{
1455	return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
1456				  ndev->ops->n_core_ops);
1457}
1458
1459int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1460			struct sk_buff *skb)
1461{
1462	return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
1463				 ndev->ops->n_core_ops);
1464}
1465
1466static bool nci_valid_size(struct sk_buff *skb)
1467{
1468	BUILD_BUG_ON(NCI_CTRL_HDR_SIZE != NCI_DATA_HDR_SIZE);
1469	unsigned int hdr_size = NCI_CTRL_HDR_SIZE;
1470
1471	if (skb->len < hdr_size ||
1472	    !nci_plen(skb->data) ||
1473	    skb->len < hdr_size + nci_plen(skb->data)) {
1474		return false;
1475	}
1476	return true;
1477}
1478
1479/* ---- NCI TX Data worker thread ---- */
1480
1481static void nci_tx_work(struct work_struct *work)
1482{
1483	struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1484	struct nci_conn_info *conn_info;
1485	struct sk_buff *skb;
1486
1487	conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1488	if (!conn_info)
1489		return;
1490
1491	pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1492
1493	/* Send queued tx data */
1494	while (atomic_read(&conn_info->credits_cnt)) {
1495		skb = skb_dequeue(&ndev->tx_q);
1496		if (!skb)
1497			return;
1498		kcov_remote_start_common(skb_get_kcov_handle(skb));
1499
1500		/* Check if data flow control is used */
1501		if (atomic_read(&conn_info->credits_cnt) !=
1502		    NCI_DATA_FLOW_CONTROL_NOT_USED)
1503			atomic_dec(&conn_info->credits_cnt);
1504
1505		pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1506			 nci_pbf(skb->data),
1507			 nci_conn_id(skb->data),
1508			 nci_plen(skb->data));
1509
1510		nci_send_frame(ndev, skb);
1511
1512		mod_timer(&ndev->data_timer,
1513			  jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1514		kcov_remote_stop();
1515	}
1516}
1517
1518/* ----- NCI RX worker thread (data & control) ----- */
1519
1520static void nci_rx_work(struct work_struct *work)
1521{
1522	struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1523	struct sk_buff *skb;
1524
1525	for (; (skb = skb_dequeue(&ndev->rx_q)); kcov_remote_stop()) {
1526		kcov_remote_start_common(skb_get_kcov_handle(skb));
1527
1528		/* Send copy to sniffer */
1529		nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1530				     RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1531
1532		if (!nci_valid_size(skb)) {
1533			kfree_skb(skb);
1534			continue;
1535		}
1536
1537		/* Process frame */
1538		switch (nci_mt(skb->data)) {
1539		case NCI_MT_RSP_PKT:
1540			nci_rsp_packet(ndev, skb);
1541			break;
1542
1543		case NCI_MT_NTF_PKT:
1544			nci_ntf_packet(ndev, skb);
1545			break;
1546
1547		case NCI_MT_DATA_PKT:
1548			nci_rx_data_packet(ndev, skb);
1549			break;
1550
1551		default:
1552			pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1553			kfree_skb(skb);
1554			break;
1555		}
1556	}
1557
1558	/* check if a data exchange timeout has occurred */
1559	if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1560		/* complete the data exchange transaction, if exists */
1561		if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1562			nci_data_exchange_complete(ndev, NULL,
1563						   ndev->cur_conn_id,
1564						   -ETIMEDOUT);
1565
1566		clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1567	}
1568}
1569
1570/* ----- NCI TX CMD worker thread ----- */
1571
1572static void nci_cmd_work(struct work_struct *work)
1573{
1574	struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1575	struct sk_buff *skb;
1576
1577	pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1578
1579	/* Send queued command */
1580	if (atomic_read(&ndev->cmd_cnt)) {
1581		skb = skb_dequeue(&ndev->cmd_q);
1582		if (!skb)
1583			return;
1584
1585		kcov_remote_start_common(skb_get_kcov_handle(skb));
1586		atomic_dec(&ndev->cmd_cnt);
1587
1588		pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1589			 nci_pbf(skb->data),
1590			 nci_opcode_gid(nci_opcode(skb->data)),
1591			 nci_opcode_oid(nci_opcode(skb->data)),
1592			 nci_plen(skb->data));
1593
1594		nci_send_frame(ndev, skb);
1595
1596		mod_timer(&ndev->cmd_timer,
1597			  jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1598		kcov_remote_stop();
1599	}
1600}
1601
1602MODULE_DESCRIPTION("NFC Controller Interface");
1603MODULE_LICENSE("GPL");
v4.6
 
   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) 2011 Texas Instruments, Inc.
   6 *  Copyright (C) 2014 Marvell International Ltd.
   7 *
   8 *  Written by Ilan Elias <ilane@ti.com>
   9 *
  10 *  Acknowledgements:
  11 *  This file is based on hci_core.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/module.h>
  31#include <linux/kernel.h>
  32#include <linux/types.h>
  33#include <linux/workqueue.h>
  34#include <linux/completion.h>
  35#include <linux/export.h>
  36#include <linux/sched.h>
  37#include <linux/bitops.h>
  38#include <linux/skbuff.h>
 
  39
  40#include "../nfc.h"
  41#include <net/nfc/nci.h>
  42#include <net/nfc/nci_core.h>
  43#include <linux/nfc.h>
  44
  45struct core_conn_create_data {
  46	int length;
  47	struct nci_core_conn_create_cmd *cmd;
  48};
  49
  50static void nci_cmd_work(struct work_struct *work);
  51static void nci_rx_work(struct work_struct *work);
  52static void nci_tx_work(struct work_struct *work);
  53
  54struct nci_conn_info *nci_get_conn_info_by_conn_id(struct nci_dev *ndev,
  55						   int conn_id)
  56{
  57	struct nci_conn_info *conn_info;
  58
  59	list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
  60		if (conn_info->conn_id == conn_id)
  61			return conn_info;
  62	}
  63
  64	return NULL;
  65}
  66
  67int nci_get_conn_info_by_id(struct nci_dev *ndev, u8 id)
 
  68{
  69	struct nci_conn_info *conn_info;
  70
  71	list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
  72		if (conn_info->id == id)
  73			return conn_info->conn_id;
 
 
 
 
 
 
  74	}
  75
  76	return -EINVAL;
  77}
  78EXPORT_SYMBOL(nci_get_conn_info_by_id);
  79
  80/* ---- NCI requests ---- */
  81
  82void nci_req_complete(struct nci_dev *ndev, int result)
  83{
  84	if (ndev->req_status == NCI_REQ_PEND) {
  85		ndev->req_result = result;
  86		ndev->req_status = NCI_REQ_DONE;
  87		complete(&ndev->req_completion);
  88	}
  89}
  90EXPORT_SYMBOL(nci_req_complete);
  91
  92static void nci_req_cancel(struct nci_dev *ndev, int err)
  93{
  94	if (ndev->req_status == NCI_REQ_PEND) {
  95		ndev->req_result = err;
  96		ndev->req_status = NCI_REQ_CANCELED;
  97		complete(&ndev->req_completion);
  98	}
  99}
 100
 101/* Execute request and wait for completion. */
 102static int __nci_request(struct nci_dev *ndev,
 103			 void (*req)(struct nci_dev *ndev, unsigned long opt),
 104			 unsigned long opt, __u32 timeout)
 105{
 106	int rc = 0;
 107	long completion_rc;
 108
 109	ndev->req_status = NCI_REQ_PEND;
 110
 111	reinit_completion(&ndev->req_completion);
 112	req(ndev, opt);
 113	completion_rc =
 114		wait_for_completion_interruptible_timeout(&ndev->req_completion,
 115							  timeout);
 116
 117	pr_debug("wait_for_completion return %ld\n", completion_rc);
 118
 119	if (completion_rc > 0) {
 120		switch (ndev->req_status) {
 121		case NCI_REQ_DONE:
 122			rc = nci_to_errno(ndev->req_result);
 123			break;
 124
 125		case NCI_REQ_CANCELED:
 126			rc = -ndev->req_result;
 127			break;
 128
 129		default:
 130			rc = -ETIMEDOUT;
 131			break;
 132		}
 133	} else {
 134		pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
 135		       completion_rc);
 136
 137		rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
 138	}
 139
 140	ndev->req_status = ndev->req_result = 0;
 141
 142	return rc;
 143}
 144
 145inline int nci_request(struct nci_dev *ndev,
 146		       void (*req)(struct nci_dev *ndev,
 147				   unsigned long opt),
 148		       unsigned long opt, __u32 timeout)
 149{
 150	int rc;
 151
 152	if (!test_bit(NCI_UP, &ndev->flags))
 153		return -ENETDOWN;
 154
 155	/* Serialize all requests */
 156	mutex_lock(&ndev->req_lock);
 157	rc = __nci_request(ndev, req, opt, timeout);
 
 
 
 
 
 
 158	mutex_unlock(&ndev->req_lock);
 159
 160	return rc;
 161}
 162
 163static void nci_reset_req(struct nci_dev *ndev, unsigned long opt)
 164{
 165	struct nci_core_reset_cmd cmd;
 166
 167	cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
 168	nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
 169}
 170
 171static void nci_init_req(struct nci_dev *ndev, unsigned long opt)
 172{
 173	nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, 0, NULL);
 
 
 
 
 
 174}
 175
 176static void nci_init_complete_req(struct nci_dev *ndev, unsigned long opt)
 177{
 178	struct nci_rf_disc_map_cmd cmd;
 179	struct disc_map_config *cfg = cmd.mapping_configs;
 180	__u8 *num = &cmd.num_mapping_configs;
 181	int i;
 182
 183	/* set rf mapping configurations */
 184	*num = 0;
 185
 186	/* by default mapping is set to NCI_RF_INTERFACE_FRAME */
 187	for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
 188		if (ndev->supported_rf_interfaces[i] ==
 189		    NCI_RF_INTERFACE_ISO_DEP) {
 190			cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
 191			cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
 192				NCI_DISC_MAP_MODE_LISTEN;
 193			cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
 194			(*num)++;
 195		} else if (ndev->supported_rf_interfaces[i] ==
 196			   NCI_RF_INTERFACE_NFC_DEP) {
 197			cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
 198			cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
 199				NCI_DISC_MAP_MODE_LISTEN;
 200			cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
 201			(*num)++;
 202		}
 203
 204		if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
 205			break;
 206	}
 207
 208	nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
 209		     (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
 210}
 211
 212struct nci_set_config_param {
 213	__u8	id;
 214	size_t	len;
 215	__u8	*val;
 216};
 217
 218static void nci_set_config_req(struct nci_dev *ndev, unsigned long opt)
 219{
 220	struct nci_set_config_param *param = (struct nci_set_config_param *)opt;
 221	struct nci_core_set_config_cmd cmd;
 222
 223	BUG_ON(param->len > NCI_MAX_PARAM_LEN);
 224
 225	cmd.num_params = 1;
 226	cmd.param.id = param->id;
 227	cmd.param.len = param->len;
 228	memcpy(cmd.param.val, param->val, param->len);
 229
 230	nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
 231}
 232
 233struct nci_rf_discover_param {
 234	__u32	im_protocols;
 235	__u32	tm_protocols;
 236};
 237
 238static void nci_rf_discover_req(struct nci_dev *ndev, unsigned long opt)
 239{
 240	struct nci_rf_discover_param *param =
 241		(struct nci_rf_discover_param *)opt;
 242	struct nci_rf_disc_cmd cmd;
 243
 244	cmd.num_disc_configs = 0;
 245
 246	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
 247	    (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
 248	     param->im_protocols & NFC_PROTO_MIFARE_MASK ||
 249	     param->im_protocols & NFC_PROTO_ISO14443_MASK ||
 250	     param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
 251		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
 252			NCI_NFC_A_PASSIVE_POLL_MODE;
 253		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
 254		cmd.num_disc_configs++;
 255	}
 256
 257	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
 258	    (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
 259		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
 260			NCI_NFC_B_PASSIVE_POLL_MODE;
 261		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
 262		cmd.num_disc_configs++;
 263	}
 264
 265	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
 266	    (param->im_protocols & NFC_PROTO_FELICA_MASK ||
 267	     param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
 268		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
 269			NCI_NFC_F_PASSIVE_POLL_MODE;
 270		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
 271		cmd.num_disc_configs++;
 272	}
 273
 274	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
 275	    (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
 276		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
 277			NCI_NFC_V_PASSIVE_POLL_MODE;
 278		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
 279		cmd.num_disc_configs++;
 280	}
 281
 282	if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
 283	    (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
 284		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
 285			NCI_NFC_A_PASSIVE_LISTEN_MODE;
 286		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
 287		cmd.num_disc_configs++;
 288		cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
 289			NCI_NFC_F_PASSIVE_LISTEN_MODE;
 290		cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
 291		cmd.num_disc_configs++;
 292	}
 293
 294	nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
 295		     (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
 296		     &cmd);
 297}
 298
 299struct nci_rf_discover_select_param {
 300	__u8	rf_discovery_id;
 301	__u8	rf_protocol;
 302};
 303
 304static void nci_rf_discover_select_req(struct nci_dev *ndev, unsigned long opt)
 305{
 306	struct nci_rf_discover_select_param *param =
 307		(struct nci_rf_discover_select_param *)opt;
 308	struct nci_rf_discover_select_cmd cmd;
 309
 310	cmd.rf_discovery_id = param->rf_discovery_id;
 311	cmd.rf_protocol = param->rf_protocol;
 312
 313	switch (cmd.rf_protocol) {
 314	case NCI_RF_PROTOCOL_ISO_DEP:
 315		cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
 316		break;
 317
 318	case NCI_RF_PROTOCOL_NFC_DEP:
 319		cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
 320		break;
 321
 322	default:
 323		cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
 324		break;
 325	}
 326
 327	nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
 328		     sizeof(struct nci_rf_discover_select_cmd), &cmd);
 329}
 330
 331static void nci_rf_deactivate_req(struct nci_dev *ndev, unsigned long opt)
 332{
 333	struct nci_rf_deactivate_cmd cmd;
 334
 335	cmd.type = opt;
 336
 337	nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
 338		     sizeof(struct nci_rf_deactivate_cmd), &cmd);
 339}
 340
 341struct nci_cmd_param {
 342	__u16 opcode;
 343	size_t len;
 344	__u8 *payload;
 345};
 346
 347static void nci_generic_req(struct nci_dev *ndev, unsigned long opt)
 348{
 349	struct nci_cmd_param *param =
 350		(struct nci_cmd_param *)opt;
 351
 352	nci_send_cmd(ndev, param->opcode, param->len, param->payload);
 353}
 354
 355int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, __u8 *payload)
 356{
 357	struct nci_cmd_param param;
 358
 359	param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid);
 360	param.len = len;
 361	param.payload = payload;
 362
 363	return __nci_request(ndev, nci_generic_req, (unsigned long)&param,
 364			     msecs_to_jiffies(NCI_CMD_TIMEOUT));
 365}
 366EXPORT_SYMBOL(nci_prop_cmd);
 367
 368int nci_core_cmd(struct nci_dev *ndev, __u16 opcode, size_t len, __u8 *payload)
 
 369{
 370	struct nci_cmd_param param;
 371
 372	param.opcode = opcode;
 373	param.len = len;
 374	param.payload = payload;
 375
 376	return __nci_request(ndev, nci_generic_req, (unsigned long)&param,
 377			     msecs_to_jiffies(NCI_CMD_TIMEOUT));
 378}
 379EXPORT_SYMBOL(nci_core_cmd);
 380
 381int nci_core_reset(struct nci_dev *ndev)
 382{
 383	return __nci_request(ndev, nci_reset_req, 0,
 384			     msecs_to_jiffies(NCI_RESET_TIMEOUT));
 385}
 386EXPORT_SYMBOL(nci_core_reset);
 387
 388int nci_core_init(struct nci_dev *ndev)
 389{
 390	return __nci_request(ndev, nci_init_req, 0,
 391			     msecs_to_jiffies(NCI_INIT_TIMEOUT));
 392}
 393EXPORT_SYMBOL(nci_core_init);
 394
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 395static int nci_open_device(struct nci_dev *ndev)
 396{
 397	int rc = 0;
 398
 399	mutex_lock(&ndev->req_lock);
 400
 
 
 
 
 
 401	if (test_bit(NCI_UP, &ndev->flags)) {
 402		rc = -EALREADY;
 403		goto done;
 404	}
 405
 406	if (ndev->ops->open(ndev)) {
 407		rc = -EIO;
 408		goto done;
 409	}
 410
 411	atomic_set(&ndev->cmd_cnt, 1);
 412
 413	set_bit(NCI_INIT, &ndev->flags);
 414
 415	if (ndev->ops->init)
 416		rc = ndev->ops->init(ndev);
 417
 418	if (!rc) {
 419		rc = __nci_request(ndev, nci_reset_req, 0,
 420				   msecs_to_jiffies(NCI_RESET_TIMEOUT));
 421	}
 422
 423	if (!rc && ndev->ops->setup) {
 424		rc = ndev->ops->setup(ndev);
 425	}
 426
 427	if (!rc) {
 428		rc = __nci_request(ndev, nci_init_req, 0,
 
 
 
 
 
 
 
 
 
 429				   msecs_to_jiffies(NCI_INIT_TIMEOUT));
 430	}
 431
 432	if (!rc && ndev->ops->post_setup)
 433		rc = ndev->ops->post_setup(ndev);
 434
 435	if (!rc) {
 436		rc = __nci_request(ndev, nci_init_complete_req, 0,
 437				   msecs_to_jiffies(NCI_INIT_TIMEOUT));
 438	}
 439
 440	clear_bit(NCI_INIT, &ndev->flags);
 441
 442	if (!rc) {
 443		set_bit(NCI_UP, &ndev->flags);
 444		nci_clear_target_list(ndev);
 445		atomic_set(&ndev->state, NCI_IDLE);
 446	} else {
 447		/* Init failed, cleanup */
 448		skb_queue_purge(&ndev->cmd_q);
 449		skb_queue_purge(&ndev->rx_q);
 450		skb_queue_purge(&ndev->tx_q);
 451
 452		ndev->ops->close(ndev);
 453		ndev->flags = 0;
 454	}
 455
 456done:
 457	mutex_unlock(&ndev->req_lock);
 458	return rc;
 459}
 460
 461static int nci_close_device(struct nci_dev *ndev)
 462{
 463	nci_req_cancel(ndev, ENODEV);
 
 
 
 
 464	mutex_lock(&ndev->req_lock);
 465
 466	if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
 
 
 
 
 467		del_timer_sync(&ndev->cmd_timer);
 468		del_timer_sync(&ndev->data_timer);
 469		mutex_unlock(&ndev->req_lock);
 470		return 0;
 471	}
 472
 473	/* Drop RX and TX queues */
 474	skb_queue_purge(&ndev->rx_q);
 475	skb_queue_purge(&ndev->tx_q);
 476
 477	/* Flush RX and TX wq */
 478	flush_workqueue(ndev->rx_wq);
 479	flush_workqueue(ndev->tx_wq);
 480
 481	/* Reset device */
 482	skb_queue_purge(&ndev->cmd_q);
 483	atomic_set(&ndev->cmd_cnt, 1);
 484
 485	set_bit(NCI_INIT, &ndev->flags);
 486	__nci_request(ndev, nci_reset_req, 0,
 487		      msecs_to_jiffies(NCI_RESET_TIMEOUT));
 488
 489	/* After this point our queues are empty
 490	 * and no works are scheduled.
 491	 */
 492	ndev->ops->close(ndev);
 493
 494	clear_bit(NCI_INIT, &ndev->flags);
 495
 496	del_timer_sync(&ndev->cmd_timer);
 497
 498	/* Flush cmd wq */
 499	flush_workqueue(ndev->cmd_wq);
 500
 501	/* Clear flags */
 502	ndev->flags = 0;
 
 
 503
 504	mutex_unlock(&ndev->req_lock);
 505
 506	return 0;
 507}
 508
 509/* NCI command timer function */
 510static void nci_cmd_timer(unsigned long arg)
 511{
 512	struct nci_dev *ndev = (void *) arg;
 513
 514	atomic_set(&ndev->cmd_cnt, 1);
 515	queue_work(ndev->cmd_wq, &ndev->cmd_work);
 516}
 517
 518/* NCI data exchange timer function */
 519static void nci_data_timer(unsigned long arg)
 520{
 521	struct nci_dev *ndev = (void *) arg;
 522
 523	set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
 524	queue_work(ndev->rx_wq, &ndev->rx_work);
 525}
 526
 527static int nci_dev_up(struct nfc_dev *nfc_dev)
 528{
 529	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 530
 531	return nci_open_device(ndev);
 532}
 533
 534static int nci_dev_down(struct nfc_dev *nfc_dev)
 535{
 536	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 537
 538	return nci_close_device(ndev);
 539}
 540
 541int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, __u8 *val)
 542{
 543	struct nci_set_config_param param;
 544
 545	if (!val || !len)
 546		return 0;
 547
 548	param.id = id;
 549	param.len = len;
 550	param.val = val;
 551
 552	return __nci_request(ndev, nci_set_config_req, (unsigned long)&param,
 553			     msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
 554}
 555EXPORT_SYMBOL(nci_set_config);
 556
 557static void nci_nfcee_discover_req(struct nci_dev *ndev, unsigned long opt)
 558{
 559	struct nci_nfcee_discover_cmd cmd;
 560	__u8 action = opt;
 561
 562	cmd.discovery_action = action;
 563
 564	nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
 565}
 566
 567int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
 568{
 569	return __nci_request(ndev, nci_nfcee_discover_req, action,
 
 
 570				msecs_to_jiffies(NCI_CMD_TIMEOUT));
 571}
 572EXPORT_SYMBOL(nci_nfcee_discover);
 573
 574static void nci_nfcee_mode_set_req(struct nci_dev *ndev, unsigned long opt)
 575{
 576	struct nci_nfcee_mode_set_cmd *cmd =
 577					(struct nci_nfcee_mode_set_cmd *)opt;
 578
 579	nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
 580		     sizeof(struct nci_nfcee_mode_set_cmd), cmd);
 581}
 582
 583int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
 584{
 585	struct nci_nfcee_mode_set_cmd cmd;
 586
 587	cmd.nfcee_id = nfcee_id;
 588	cmd.nfcee_mode = nfcee_mode;
 589
 590	return __nci_request(ndev, nci_nfcee_mode_set_req,
 591			     (unsigned long)&cmd,
 592			     msecs_to_jiffies(NCI_CMD_TIMEOUT));
 593}
 594EXPORT_SYMBOL(nci_nfcee_mode_set);
 595
 596static void nci_core_conn_create_req(struct nci_dev *ndev, unsigned long opt)
 597{
 598	struct core_conn_create_data *data =
 599					(struct core_conn_create_data *)opt;
 600
 601	nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
 602}
 603
 604int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
 605			 u8 number_destination_params,
 606			 size_t params_len,
 607			 struct core_conn_create_dest_spec_params *params)
 608{
 609	int r;
 610	struct nci_core_conn_create_cmd *cmd;
 611	struct core_conn_create_data data;
 612
 613	if (!number_destination_params)
 614		return -EINVAL;
 615
 616	data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
 617	cmd = kzalloc(data.length, GFP_KERNEL);
 618	if (!cmd)
 619		return -ENOMEM;
 620
 621	cmd->destination_type = destination_type;
 622	cmd->number_destination_params = number_destination_params;
 623	memcpy(cmd->params, params, params_len);
 624
 625	data.cmd = cmd;
 626
 627	if (params->length > 0)
 628		ndev->cur_id = params->value[DEST_SPEC_PARAMS_ID_INDEX];
 629	else
 630		ndev->cur_id = 0;
 
 
 
 
 
 
 
 
 631
 632	r = __nci_request(ndev, nci_core_conn_create_req,
 633			  (unsigned long)&data,
 634			  msecs_to_jiffies(NCI_CMD_TIMEOUT));
 635	kfree(cmd);
 636	return r;
 637}
 638EXPORT_SYMBOL(nci_core_conn_create);
 639
 640static void nci_core_conn_close_req(struct nci_dev *ndev, unsigned long opt)
 641{
 642	__u8 conn_id = opt;
 643
 644	nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
 645}
 646
 647int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
 648{
 649	return __nci_request(ndev, nci_core_conn_close_req, conn_id,
 
 
 
 650			     msecs_to_jiffies(NCI_CMD_TIMEOUT));
 651}
 652EXPORT_SYMBOL(nci_core_conn_close);
 653
 654static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
 655{
 656	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 657	struct nci_set_config_param param;
 658	int rc;
 659
 660	param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
 661	if ((param.val == NULL) || (param.len == 0))
 662		return 0;
 663
 664	if (param.len > NFC_MAX_GT_LEN)
 665		return -EINVAL;
 666
 667	param.id = NCI_PN_ATR_REQ_GEN_BYTES;
 668
 669	rc = nci_request(ndev, nci_set_config_req, (unsigned long)&param,
 670			 msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
 671	if (rc)
 672		return rc;
 673
 674	param.id = NCI_LN_ATR_RES_GEN_BYTES;
 675
 676	return nci_request(ndev, nci_set_config_req, (unsigned long)&param,
 677			   msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
 678}
 679
 680static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
 681{
 682	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 683	int rc;
 684	__u8 val;
 685
 686	val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
 687
 688	rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
 689	if (rc)
 690		return rc;
 691
 692	val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
 693
 694	rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
 695	if (rc)
 696		return rc;
 697
 698	val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
 699
 700	return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
 701}
 702
 703static int nci_start_poll(struct nfc_dev *nfc_dev,
 704			  __u32 im_protocols, __u32 tm_protocols)
 705{
 706	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 707	struct nci_rf_discover_param param;
 708	int rc;
 709
 710	if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
 711	    (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
 712		pr_err("unable to start poll, since poll is already active\n");
 713		return -EBUSY;
 714	}
 715
 716	if (ndev->target_active_prot) {
 717		pr_err("there is an active target\n");
 718		return -EBUSY;
 719	}
 720
 721	if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
 722	    (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
 723		pr_debug("target active or w4 select, implicitly deactivate\n");
 724
 725		rc = nci_request(ndev, nci_rf_deactivate_req,
 726				 NCI_DEACTIVATE_TYPE_IDLE_MODE,
 727				 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
 728		if (rc)
 729			return -EBUSY;
 730	}
 731
 732	if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
 733		rc = nci_set_local_general_bytes(nfc_dev);
 734		if (rc) {
 735			pr_err("failed to set local general bytes\n");
 736			return rc;
 737		}
 738	}
 739
 740	if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
 741		rc = nci_set_listen_parameters(nfc_dev);
 742		if (rc)
 743			pr_err("failed to set listen parameters\n");
 744	}
 745
 746	param.im_protocols = im_protocols;
 747	param.tm_protocols = tm_protocols;
 748	rc = nci_request(ndev, nci_rf_discover_req, (unsigned long)&param,
 749			 msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
 750
 751	if (!rc)
 752		ndev->poll_prots = im_protocols;
 753
 754	return rc;
 755}
 756
 757static void nci_stop_poll(struct nfc_dev *nfc_dev)
 758{
 759	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 760
 761	if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
 762	    (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
 763		pr_err("unable to stop poll, since poll is not active\n");
 764		return;
 765	}
 766
 767	nci_request(ndev, nci_rf_deactivate_req, NCI_DEACTIVATE_TYPE_IDLE_MODE,
 
 768		    msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
 769}
 770
 771static int nci_activate_target(struct nfc_dev *nfc_dev,
 772			       struct nfc_target *target, __u32 protocol)
 773{
 774	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 775	struct nci_rf_discover_select_param param;
 776	struct nfc_target *nci_target = NULL;
 777	int i;
 778	int rc = 0;
 779
 780	pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
 781
 782	if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
 783	    (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
 784		pr_err("there is no available target to activate\n");
 785		return -EINVAL;
 786	}
 787
 788	if (ndev->target_active_prot) {
 789		pr_err("there is already an active target\n");
 790		return -EBUSY;
 791	}
 792
 793	for (i = 0; i < ndev->n_targets; i++) {
 794		if (ndev->targets[i].idx == target->idx) {
 795			nci_target = &ndev->targets[i];
 796			break;
 797		}
 798	}
 799
 800	if (!nci_target) {
 801		pr_err("unable to find the selected target\n");
 802		return -EINVAL;
 803	}
 804
 
 
 
 
 
 805	if (!(nci_target->supported_protocols & (1 << protocol))) {
 806		pr_err("target does not support the requested protocol 0x%x\n",
 807		       protocol);
 808		return -EINVAL;
 809	}
 810
 811	if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
 812		param.rf_discovery_id = nci_target->logical_idx;
 813
 814		if (protocol == NFC_PROTO_JEWEL)
 815			param.rf_protocol = NCI_RF_PROTOCOL_T1T;
 816		else if (protocol == NFC_PROTO_MIFARE)
 817			param.rf_protocol = NCI_RF_PROTOCOL_T2T;
 818		else if (protocol == NFC_PROTO_FELICA)
 819			param.rf_protocol = NCI_RF_PROTOCOL_T3T;
 820		else if (protocol == NFC_PROTO_ISO14443 ||
 821			 protocol == NFC_PROTO_ISO14443_B)
 822			param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
 823		else
 824			param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
 825
 826		rc = nci_request(ndev, nci_rf_discover_select_req,
 827				 (unsigned long)&param,
 828				 msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
 829	}
 830
 831	if (!rc)
 832		ndev->target_active_prot = protocol;
 833
 834	return rc;
 835}
 836
 837static void nci_deactivate_target(struct nfc_dev *nfc_dev,
 838				  struct nfc_target *target,
 839				  __u8 mode)
 840{
 841	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 842	u8 nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
 843
 844	pr_debug("entry\n");
 845
 846	if (!ndev->target_active_prot) {
 847		pr_err("unable to deactivate target, no active target\n");
 848		return;
 849	}
 850
 851	ndev->target_active_prot = 0;
 852
 853	switch (mode) {
 854	case NFC_TARGET_MODE_SLEEP:
 855		nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
 856		break;
 857	}
 858
 859	if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
 860		nci_request(ndev, nci_rf_deactivate_req, nci_mode,
 861			    msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
 862	}
 863}
 864
 865static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
 866			   __u8 comm_mode, __u8 *gb, size_t gb_len)
 867{
 868	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 869	int rc;
 870
 871	pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
 872
 873	rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
 874	if (rc)
 875		return rc;
 876
 877	rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
 878					  ndev->remote_gb_len);
 879	if (!rc)
 880		rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
 881					NFC_RF_INITIATOR);
 882
 883	return rc;
 884}
 885
 886static int nci_dep_link_down(struct nfc_dev *nfc_dev)
 887{
 888	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 889	int rc;
 890
 891	pr_debug("entry\n");
 892
 893	if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
 894		nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
 895	} else {
 896		if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
 897		    atomic_read(&ndev->state) == NCI_DISCOVERY) {
 898			nci_request(ndev, nci_rf_deactivate_req, 0,
 899				msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
 900		}
 901
 902		rc = nfc_tm_deactivated(nfc_dev);
 903		if (rc)
 904			pr_err("error when signaling tm deactivation\n");
 905	}
 906
 907	return 0;
 908}
 909
 910
 911static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
 912			  struct sk_buff *skb,
 913			  data_exchange_cb_t cb, void *cb_context)
 914{
 915	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 916	int rc;
 917	struct nci_conn_info    *conn_info;
 918
 919	conn_info = ndev->rf_conn_info;
 920	if (!conn_info)
 921		return -EPROTO;
 922
 923	pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
 924
 925	if (!ndev->target_active_prot) {
 926		pr_err("unable to exchange data, no active target\n");
 927		return -EINVAL;
 928	}
 929
 930	if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
 931		return -EBUSY;
 932
 933	/* store cb and context to be used on receiving data */
 934	conn_info->data_exchange_cb = cb;
 935	conn_info->data_exchange_cb_context = cb_context;
 936
 937	rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
 938	if (rc)
 939		clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
 940
 941	return rc;
 942}
 943
 944static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
 945{
 946	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 947	int rc;
 948
 949	rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
 950	if (rc)
 951		pr_err("unable to send data\n");
 952
 953	return rc;
 954}
 955
 956static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
 957{
 958	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 959
 960	if (ndev->ops->enable_se)
 961		return ndev->ops->enable_se(ndev, se_idx);
 962
 963	return 0;
 964}
 965
 966static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
 967{
 968	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 969
 970	if (ndev->ops->disable_se)
 971		return ndev->ops->disable_se(ndev, se_idx);
 972
 973	return 0;
 974}
 975
 976static int nci_discover_se(struct nfc_dev *nfc_dev)
 977{
 978	int r;
 979	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 980
 981	if (ndev->ops->discover_se) {
 982		r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
 983		if (r != NCI_STATUS_OK)
 984			return -EPROTO;
 985
 986		return ndev->ops->discover_se(ndev);
 987	}
 988
 989	return 0;
 990}
 991
 992static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
 993		     u8 *apdu, size_t apdu_length,
 994		     se_io_cb_t cb, void *cb_context)
 995{
 996	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
 997
 998	if (ndev->ops->se_io)
 999		return ndev->ops->se_io(ndev, se_idx, apdu,
1000				apdu_length, cb, cb_context);
1001
1002	return 0;
1003}
1004
1005static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
1006{
1007	struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1008
1009	if (!ndev->ops->fw_download)
1010		return -ENOTSUPP;
1011
1012	return ndev->ops->fw_download(ndev, firmware_name);
1013}
1014
1015static struct nfc_ops nci_nfc_ops = {
1016	.dev_up = nci_dev_up,
1017	.dev_down = nci_dev_down,
1018	.start_poll = nci_start_poll,
1019	.stop_poll = nci_stop_poll,
1020	.dep_link_up = nci_dep_link_up,
1021	.dep_link_down = nci_dep_link_down,
1022	.activate_target = nci_activate_target,
1023	.deactivate_target = nci_deactivate_target,
1024	.im_transceive = nci_transceive,
1025	.tm_send = nci_tm_send,
1026	.enable_se = nci_enable_se,
1027	.disable_se = nci_disable_se,
1028	.discover_se = nci_discover_se,
1029	.se_io = nci_se_io,
1030	.fw_download = nci_fw_download,
1031};
1032
1033/* ---- Interface to NCI drivers ---- */
1034/**
1035 * nci_allocate_device - allocate a new nci device
1036 *
1037 * @ops: device operations
1038 * @supported_protocols: NFC protocols supported by the device
 
 
1039 */
1040struct nci_dev *nci_allocate_device(struct nci_ops *ops,
1041				    __u32 supported_protocols,
1042				    int tx_headroom, int tx_tailroom)
1043{
1044	struct nci_dev *ndev;
1045
1046	pr_debug("supported_protocols 0x%x\n", supported_protocols);
1047
1048	if (!ops->open || !ops->close || !ops->send)
1049		return NULL;
1050
1051	if (!supported_protocols)
1052		return NULL;
1053
1054	ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1055	if (!ndev)
1056		return NULL;
1057
1058	ndev->ops = ops;
1059
1060	if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1061		pr_err("Too many proprietary commands: %zd\n",
1062		       ops->n_prop_ops);
1063		ops->prop_ops = NULL;
1064		ops->n_prop_ops = 0;
1065	}
1066
1067	ndev->tx_headroom = tx_headroom;
1068	ndev->tx_tailroom = tx_tailroom;
1069	init_completion(&ndev->req_completion);
1070
1071	ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
1072					    supported_protocols,
1073					    tx_headroom + NCI_DATA_HDR_SIZE,
1074					    tx_tailroom);
1075	if (!ndev->nfc_dev)
1076		goto free_nci;
1077
1078	ndev->hci_dev = nci_hci_allocate(ndev);
1079	if (!ndev->hci_dev)
1080		goto free_nfc;
1081
1082	nfc_set_drvdata(ndev->nfc_dev, ndev);
1083
1084	return ndev;
1085
1086free_nfc:
1087	kfree(ndev->nfc_dev);
1088
1089free_nci:
1090	kfree(ndev);
1091	return NULL;
1092}
1093EXPORT_SYMBOL(nci_allocate_device);
1094
1095/**
1096 * nci_free_device - deallocate nci device
1097 *
1098 * @ndev: The nci device to deallocate
1099 */
1100void nci_free_device(struct nci_dev *ndev)
1101{
1102	nfc_free_device(ndev->nfc_dev);
 
 
 
 
 
1103	kfree(ndev);
1104}
1105EXPORT_SYMBOL(nci_free_device);
1106
1107/**
1108 * nci_register_device - register a nci device in the nfc subsystem
1109 *
1110 * @dev: The nci device to register
1111 */
1112int nci_register_device(struct nci_dev *ndev)
1113{
1114	int rc;
1115	struct device *dev = &ndev->nfc_dev->dev;
1116	char name[32];
1117
1118	ndev->flags = 0;
1119
1120	INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1121	snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1122	ndev->cmd_wq = create_singlethread_workqueue(name);
1123	if (!ndev->cmd_wq) {
1124		rc = -ENOMEM;
1125		goto exit;
1126	}
1127
1128	INIT_WORK(&ndev->rx_work, nci_rx_work);
1129	snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1130	ndev->rx_wq = create_singlethread_workqueue(name);
1131	if (!ndev->rx_wq) {
1132		rc = -ENOMEM;
1133		goto destroy_cmd_wq_exit;
1134	}
1135
1136	INIT_WORK(&ndev->tx_work, nci_tx_work);
1137	snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1138	ndev->tx_wq = create_singlethread_workqueue(name);
1139	if (!ndev->tx_wq) {
1140		rc = -ENOMEM;
1141		goto destroy_rx_wq_exit;
1142	}
1143
1144	skb_queue_head_init(&ndev->cmd_q);
1145	skb_queue_head_init(&ndev->rx_q);
1146	skb_queue_head_init(&ndev->tx_q);
1147
1148	setup_timer(&ndev->cmd_timer, nci_cmd_timer,
1149		    (unsigned long) ndev);
1150	setup_timer(&ndev->data_timer, nci_data_timer,
1151		    (unsigned long) ndev);
1152
1153	mutex_init(&ndev->req_lock);
1154	INIT_LIST_HEAD(&ndev->conn_info_list);
1155
1156	rc = nfc_register_device(ndev->nfc_dev);
1157	if (rc)
1158		goto destroy_rx_wq_exit;
1159
1160	goto exit;
1161
 
 
 
1162destroy_rx_wq_exit:
1163	destroy_workqueue(ndev->rx_wq);
1164
1165destroy_cmd_wq_exit:
1166	destroy_workqueue(ndev->cmd_wq);
1167
1168exit:
1169	return rc;
1170}
1171EXPORT_SYMBOL(nci_register_device);
1172
1173/**
1174 * nci_unregister_device - unregister a nci device in the nfc subsystem
1175 *
1176 * @dev: The nci device to unregister
1177 */
1178void nci_unregister_device(struct nci_dev *ndev)
1179{
1180	struct nci_conn_info    *conn_info, *n;
 
 
 
 
 
 
1181
1182	nci_close_device(ndev);
1183
1184	destroy_workqueue(ndev->cmd_wq);
1185	destroy_workqueue(ndev->rx_wq);
1186	destroy_workqueue(ndev->tx_wq);
1187
1188	list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1189		list_del(&conn_info->list);
1190		/* conn_info is allocated with devm_kzalloc */
1191	}
1192
1193	nfc_unregister_device(ndev->nfc_dev);
1194}
1195EXPORT_SYMBOL(nci_unregister_device);
1196
1197/**
1198 * nci_recv_frame - receive frame from NCI drivers
1199 *
1200 * @ndev: The nci device
1201 * @skb: The sk_buff to receive
1202 */
1203int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1204{
1205	pr_debug("len %d\n", skb->len);
1206
1207	if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1208	    !test_bit(NCI_INIT, &ndev->flags))) {
1209		kfree_skb(skb);
1210		return -ENXIO;
1211	}
1212
1213	/* Queue frame for rx worker thread */
1214	skb_queue_tail(&ndev->rx_q, skb);
1215	queue_work(ndev->rx_wq, &ndev->rx_work);
1216
1217	return 0;
1218}
1219EXPORT_SYMBOL(nci_recv_frame);
1220
1221int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1222{
1223	pr_debug("len %d\n", skb->len);
1224
1225	if (!ndev) {
1226		kfree_skb(skb);
1227		return -ENODEV;
1228	}
1229
1230	/* Get rid of skb owner, prior to sending to the driver. */
1231	skb_orphan(skb);
1232
1233	/* Send copy to sniffer */
1234	nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1235			     RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1236
1237	return ndev->ops->send(ndev, skb);
1238}
1239EXPORT_SYMBOL(nci_send_frame);
1240
1241/* Send NCI command */
1242int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, void *payload)
1243{
1244	struct nci_ctrl_hdr *hdr;
1245	struct sk_buff *skb;
1246
1247	pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1248
1249	skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1250	if (!skb) {
1251		pr_err("no memory for command\n");
1252		return -ENOMEM;
1253	}
1254
1255	hdr = (struct nci_ctrl_hdr *) skb_put(skb, NCI_CTRL_HDR_SIZE);
1256	hdr->gid = nci_opcode_gid(opcode);
1257	hdr->oid = nci_opcode_oid(opcode);
1258	hdr->plen = plen;
1259
1260	nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1261	nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1262
1263	if (plen)
1264		memcpy(skb_put(skb, plen), payload, plen);
1265
1266	skb_queue_tail(&ndev->cmd_q, skb);
1267	queue_work(ndev->cmd_wq, &ndev->cmd_work);
1268
1269	return 0;
1270}
1271EXPORT_SYMBOL(nci_send_cmd);
1272
1273/* Proprietary commands API */
1274static struct nci_driver_ops *ops_cmd_lookup(struct nci_driver_ops *ops,
1275					     size_t n_ops,
1276					     __u16 opcode)
1277{
1278	size_t i;
1279	struct nci_driver_ops *op;
1280
1281	if (!ops || !n_ops)
1282		return NULL;
1283
1284	for (i = 0; i < n_ops; i++) {
1285		op = &ops[i];
1286		if (op->opcode == opcode)
1287			return op;
1288	}
1289
1290	return NULL;
1291}
1292
1293static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1294			     struct sk_buff *skb, struct nci_driver_ops *ops,
1295			     size_t n_ops)
1296{
1297	struct nci_driver_ops *op;
1298
1299	op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
1300	if (!op || !op->rsp)
1301		return -ENOTSUPP;
1302
1303	return op->rsp(ndev, skb);
1304}
1305
1306static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1307			     struct sk_buff *skb, struct nci_driver_ops *ops,
1308			     size_t n_ops)
1309{
1310	struct nci_driver_ops *op;
1311
1312	op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
1313	if (!op || !op->ntf)
1314		return -ENOTSUPP;
1315
1316	return op->ntf(ndev, skb);
1317}
1318
1319int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1320			struct sk_buff *skb)
1321{
1322	return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1323				 ndev->ops->n_prop_ops);
1324}
1325
1326int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1327			struct sk_buff *skb)
1328{
1329	return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1330				 ndev->ops->n_prop_ops);
1331}
1332
1333int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1334			struct sk_buff *skb)
1335{
1336	return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
1337				  ndev->ops->n_core_ops);
1338}
1339
1340int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1341			struct sk_buff *skb)
1342{
1343	return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
1344				 ndev->ops->n_core_ops);
1345}
1346
 
 
 
 
 
 
 
 
 
 
 
 
 
1347/* ---- NCI TX Data worker thread ---- */
1348
1349static void nci_tx_work(struct work_struct *work)
1350{
1351	struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1352	struct nci_conn_info    *conn_info;
1353	struct sk_buff *skb;
1354
1355	conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1356	if (!conn_info)
1357		return;
1358
1359	pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1360
1361	/* Send queued tx data */
1362	while (atomic_read(&conn_info->credits_cnt)) {
1363		skb = skb_dequeue(&ndev->tx_q);
1364		if (!skb)
1365			return;
 
1366
1367		/* Check if data flow control is used */
1368		if (atomic_read(&conn_info->credits_cnt) !=
1369		    NCI_DATA_FLOW_CONTROL_NOT_USED)
1370			atomic_dec(&conn_info->credits_cnt);
1371
1372		pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1373			 nci_pbf(skb->data),
1374			 nci_conn_id(skb->data),
1375			 nci_plen(skb->data));
1376
1377		nci_send_frame(ndev, skb);
1378
1379		mod_timer(&ndev->data_timer,
1380			  jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
 
1381	}
1382}
1383
1384/* ----- NCI RX worker thread (data & control) ----- */
1385
1386static void nci_rx_work(struct work_struct *work)
1387{
1388	struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1389	struct sk_buff *skb;
1390
1391	while ((skb = skb_dequeue(&ndev->rx_q))) {
 
1392
1393		/* Send copy to sniffer */
1394		nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1395				     RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1396
 
 
 
 
 
1397		/* Process frame */
1398		switch (nci_mt(skb->data)) {
1399		case NCI_MT_RSP_PKT:
1400			nci_rsp_packet(ndev, skb);
1401			break;
1402
1403		case NCI_MT_NTF_PKT:
1404			nci_ntf_packet(ndev, skb);
1405			break;
1406
1407		case NCI_MT_DATA_PKT:
1408			nci_rx_data_packet(ndev, skb);
1409			break;
1410
1411		default:
1412			pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1413			kfree_skb(skb);
1414			break;
1415		}
1416	}
1417
1418	/* check if a data exchange timout has occurred */
1419	if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1420		/* complete the data exchange transaction, if exists */
1421		if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1422			nci_data_exchange_complete(ndev, NULL,
1423						   ndev->cur_conn_id,
1424						   -ETIMEDOUT);
1425
1426		clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1427	}
1428}
1429
1430/* ----- NCI TX CMD worker thread ----- */
1431
1432static void nci_cmd_work(struct work_struct *work)
1433{
1434	struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1435	struct sk_buff *skb;
1436
1437	pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1438
1439	/* Send queued command */
1440	if (atomic_read(&ndev->cmd_cnt)) {
1441		skb = skb_dequeue(&ndev->cmd_q);
1442		if (!skb)
1443			return;
1444
 
1445		atomic_dec(&ndev->cmd_cnt);
1446
1447		pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1448			 nci_pbf(skb->data),
1449			 nci_opcode_gid(nci_opcode(skb->data)),
1450			 nci_opcode_oid(nci_opcode(skb->data)),
1451			 nci_plen(skb->data));
1452
1453		nci_send_frame(ndev, skb);
1454
1455		mod_timer(&ndev->cmd_timer,
1456			  jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
 
1457	}
1458}
1459
 
1460MODULE_LICENSE("GPL");