xref: /linux/net/nfc/nci/rsp.c (revision f7f793f31378d5e83276871339c2a8374b0e8657)
16a2968aaSIlan Elias /*
26a2968aaSIlan Elias  *  The NFC Controller Interface is the communication protocol between an
36a2968aaSIlan Elias  *  NFC Controller (NFCC) and a Device Host (DH).
46a2968aaSIlan Elias  *
56a2968aaSIlan Elias  *  Copyright (C) 2011 Texas Instruments, Inc.
66a2968aaSIlan Elias  *
76a2968aaSIlan Elias  *  Written by Ilan Elias <ilane@ti.com>
86a2968aaSIlan Elias  *
96a2968aaSIlan Elias  *  Acknowledgements:
106a2968aaSIlan Elias  *  This file is based on hci_event.c, which was written
116a2968aaSIlan Elias  *  by Maxim Krasnyansky.
126a2968aaSIlan Elias  *
136a2968aaSIlan Elias  *  This program is free software; you can redistribute it and/or modify
146a2968aaSIlan Elias  *  it under the terms of the GNU General Public License version 2
156a2968aaSIlan Elias  *  as published by the Free Software Foundation
166a2968aaSIlan Elias  *
176a2968aaSIlan Elias  *  This program is distributed in the hope that it will be useful,
186a2968aaSIlan Elias  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
196a2968aaSIlan Elias  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
206a2968aaSIlan Elias  *  GNU General Public License for more details.
216a2968aaSIlan Elias  *
226a2968aaSIlan Elias  *  You should have received a copy of the GNU General Public License
2398b32decSJeff Kirsher  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
246a2968aaSIlan Elias  *
256a2968aaSIlan Elias  */
266a2968aaSIlan Elias 
2752858b51SSamuel Ortiz #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
28ed1e0ad8SJoe Perches 
296a2968aaSIlan Elias #include <linux/types.h>
306a2968aaSIlan Elias #include <linux/interrupt.h>
316a2968aaSIlan Elias #include <linux/bitops.h>
326a2968aaSIlan Elias #include <linux/skbuff.h>
336a2968aaSIlan Elias 
346a2968aaSIlan Elias #include "../nfc.h"
356a2968aaSIlan Elias #include <net/nfc/nci.h>
366a2968aaSIlan Elias #include <net/nfc/nci_core.h>
376a2968aaSIlan Elias 
386a2968aaSIlan Elias /* Handle NCI Response packets */
396a2968aaSIlan Elias 
406a2968aaSIlan Elias static void nci_core_reset_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
416a2968aaSIlan Elias {
426a2968aaSIlan Elias 	struct nci_core_reset_rsp *rsp = (void *) skb->data;
436a2968aaSIlan Elias 
4424bf3304SJoe Perches 	pr_debug("status 0x%x\n", rsp->status);
456a2968aaSIlan Elias 
46e8c0dacdSIlan Elias 	if (rsp->status == NCI_STATUS_OK) {
476a2968aaSIlan Elias 		ndev->nci_ver = rsp->nci_ver;
4820c239c1SJoe Perches 		pr_debug("nci_ver 0x%x, config_status 0x%x\n",
49e8c0dacdSIlan Elias 			 rsp->nci_ver, rsp->config_status);
50e8c0dacdSIlan Elias 	}
516a2968aaSIlan Elias 
526a2968aaSIlan Elias 	nci_req_complete(ndev, rsp->status);
536a2968aaSIlan Elias }
546a2968aaSIlan Elias 
556a2968aaSIlan Elias static void nci_core_init_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
566a2968aaSIlan Elias {
576a2968aaSIlan Elias 	struct nci_core_init_rsp_1 *rsp_1 = (void *) skb->data;
586a2968aaSIlan Elias 	struct nci_core_init_rsp_2 *rsp_2;
596a2968aaSIlan Elias 
6024bf3304SJoe Perches 	pr_debug("status 0x%x\n", rsp_1->status);
616a2968aaSIlan Elias 
626a2968aaSIlan Elias 	if (rsp_1->status != NCI_STATUS_OK)
63e8c0dacdSIlan Elias 		goto exit;
646a2968aaSIlan Elias 
656a2968aaSIlan Elias 	ndev->nfcc_features = __le32_to_cpu(rsp_1->nfcc_features);
666a2968aaSIlan Elias 	ndev->num_supported_rf_interfaces = rsp_1->num_supported_rf_interfaces;
676a2968aaSIlan Elias 
686a2968aaSIlan Elias 	if (ndev->num_supported_rf_interfaces >
696a2968aaSIlan Elias 	    NCI_MAX_SUPPORTED_RF_INTERFACES) {
706a2968aaSIlan Elias 		ndev->num_supported_rf_interfaces =
716a2968aaSIlan Elias 			NCI_MAX_SUPPORTED_RF_INTERFACES;
726a2968aaSIlan Elias 	}
736a2968aaSIlan Elias 
746a2968aaSIlan Elias 	memcpy(ndev->supported_rf_interfaces,
756a2968aaSIlan Elias 	       rsp_1->supported_rf_interfaces,
766a2968aaSIlan Elias 	       ndev->num_supported_rf_interfaces);
776a2968aaSIlan Elias 
78e8c0dacdSIlan Elias 	rsp_2 = (void *) (skb->data + 6 + rsp_1->num_supported_rf_interfaces);
796a2968aaSIlan Elias 
80eb9bc6e9SSamuel Ortiz 	ndev->max_logical_connections = rsp_2->max_logical_connections;
816a2968aaSIlan Elias 	ndev->max_routing_table_size =
826a2968aaSIlan Elias 		__le16_to_cpu(rsp_2->max_routing_table_size);
83e8c0dacdSIlan Elias 	ndev->max_ctrl_pkt_payload_len =
84e8c0dacdSIlan Elias 		rsp_2->max_ctrl_pkt_payload_len;
85e8c0dacdSIlan Elias 	ndev->max_size_for_large_params =
86e8c0dacdSIlan Elias 		__le16_to_cpu(rsp_2->max_size_for_large_params);
87e8c0dacdSIlan Elias 	ndev->manufact_id =
88e8c0dacdSIlan Elias 		rsp_2->manufact_id;
89e8c0dacdSIlan Elias 	ndev->manufact_specific_info =
90e8c0dacdSIlan Elias 		__le32_to_cpu(rsp_2->manufact_specific_info);
91e8c0dacdSIlan Elias 
9220c239c1SJoe Perches 	pr_debug("nfcc_features 0x%x\n",
936a2968aaSIlan Elias 		 ndev->nfcc_features);
9420c239c1SJoe Perches 	pr_debug("num_supported_rf_interfaces %d\n",
956a2968aaSIlan Elias 		 ndev->num_supported_rf_interfaces);
9620c239c1SJoe Perches 	pr_debug("supported_rf_interfaces[0] 0x%x\n",
976a2968aaSIlan Elias 		 ndev->supported_rf_interfaces[0]);
9820c239c1SJoe Perches 	pr_debug("supported_rf_interfaces[1] 0x%x\n",
996a2968aaSIlan Elias 		 ndev->supported_rf_interfaces[1]);
10020c239c1SJoe Perches 	pr_debug("supported_rf_interfaces[2] 0x%x\n",
1016a2968aaSIlan Elias 		 ndev->supported_rf_interfaces[2]);
10220c239c1SJoe Perches 	pr_debug("supported_rf_interfaces[3] 0x%x\n",
1036a2968aaSIlan Elias 		 ndev->supported_rf_interfaces[3]);
10420c239c1SJoe Perches 	pr_debug("max_logical_connections %d\n",
1056a2968aaSIlan Elias 		 ndev->max_logical_connections);
10620c239c1SJoe Perches 	pr_debug("max_routing_table_size %d\n",
1076a2968aaSIlan Elias 		 ndev->max_routing_table_size);
10820c239c1SJoe Perches 	pr_debug("max_ctrl_pkt_payload_len %d\n",
109e8c0dacdSIlan Elias 		 ndev->max_ctrl_pkt_payload_len);
11020c239c1SJoe Perches 	pr_debug("max_size_for_large_params %d\n",
111e8c0dacdSIlan Elias 		 ndev->max_size_for_large_params);
11220c239c1SJoe Perches 	pr_debug("manufact_id 0x%x\n",
113e8c0dacdSIlan Elias 		 ndev->manufact_id);
11420c239c1SJoe Perches 	pr_debug("manufact_specific_info 0x%x\n",
115e8c0dacdSIlan Elias 		 ndev->manufact_specific_info);
1166a2968aaSIlan Elias 
117e8c0dacdSIlan Elias exit:
1186a2968aaSIlan Elias 	nci_req_complete(ndev, rsp_1->status);
1196a2968aaSIlan Elias }
1206a2968aaSIlan Elias 
1217e035230SIlan Elias static void nci_core_set_config_rsp_packet(struct nci_dev *ndev,
1227e035230SIlan Elias 					   struct sk_buff *skb)
1237e035230SIlan Elias {
1247e035230SIlan Elias 	struct nci_core_set_config_rsp *rsp = (void *) skb->data;
1257e035230SIlan Elias 
1267e035230SIlan Elias 	pr_debug("status 0x%x\n", rsp->status);
1277e035230SIlan Elias 
1287e035230SIlan Elias 	nci_req_complete(ndev, rsp->status);
1297e035230SIlan Elias }
1307e035230SIlan Elias 
1316a2968aaSIlan Elias static void nci_rf_disc_map_rsp_packet(struct nci_dev *ndev,
1326a2968aaSIlan Elias 				       struct sk_buff *skb)
1336a2968aaSIlan Elias {
1346a2968aaSIlan Elias 	__u8 status = skb->data[0];
1356a2968aaSIlan Elias 
13624bf3304SJoe Perches 	pr_debug("status 0x%x\n", status);
1376a2968aaSIlan Elias 
1386a2968aaSIlan Elias 	nci_req_complete(ndev, status);
1396a2968aaSIlan Elias }
1406a2968aaSIlan Elias 
1416a2968aaSIlan Elias static void nci_rf_disc_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
1426a2968aaSIlan Elias {
1434aeee687SChristophe Ricard 	struct nci_conn_info    *conn_info;
1446a2968aaSIlan Elias 	__u8 status = skb->data[0];
1456a2968aaSIlan Elias 
14624bf3304SJoe Perches 	pr_debug("status 0x%x\n", status);
1476a2968aaSIlan Elias 
1484aeee687SChristophe Ricard 	if (status == NCI_STATUS_OK) {
1498939e47fSIlan Elias 		atomic_set(&ndev->state, NCI_DISCOVERY);
1506a2968aaSIlan Elias 
1514aeee687SChristophe Ricard 		conn_info = nci_get_conn_info_by_conn_id(ndev,
1524aeee687SChristophe Ricard 							 NCI_STATIC_RF_CONN_ID);
1534aeee687SChristophe Ricard 		if (!conn_info) {
1544aeee687SChristophe Ricard 			conn_info = devm_kzalloc(&ndev->nfc_dev->dev,
1554aeee687SChristophe Ricard 						 sizeof(struct nci_conn_info),
1564aeee687SChristophe Ricard 						 GFP_KERNEL);
1574aeee687SChristophe Ricard 			if (!conn_info) {
1584aeee687SChristophe Ricard 				status = NCI_STATUS_REJECTED;
1594aeee687SChristophe Ricard 				goto exit;
1604aeee687SChristophe Ricard 			}
1614aeee687SChristophe Ricard 			conn_info->conn_id = NCI_STATIC_RF_CONN_ID;
1624aeee687SChristophe Ricard 			INIT_LIST_HEAD(&conn_info->list);
1634aeee687SChristophe Ricard 			list_add(&conn_info->list, &ndev->conn_info_list);
1644aeee687SChristophe Ricard 		}
1654aeee687SChristophe Ricard 	}
1664aeee687SChristophe Ricard 
1674aeee687SChristophe Ricard exit:
1686a2968aaSIlan Elias 	nci_req_complete(ndev, status);
1696a2968aaSIlan Elias }
1706a2968aaSIlan Elias 
171019c4fbaSIlan Elias static void nci_rf_disc_select_rsp_packet(struct nci_dev *ndev,
172019c4fbaSIlan Elias 					  struct sk_buff *skb)
173019c4fbaSIlan Elias {
174019c4fbaSIlan Elias 	__u8 status = skb->data[0];
175019c4fbaSIlan Elias 
176019c4fbaSIlan Elias 	pr_debug("status 0x%x\n", status);
177019c4fbaSIlan Elias 
178019c4fbaSIlan Elias 	/* Complete the request on intf_activated_ntf or generic_error_ntf */
179019c4fbaSIlan Elias 	if (status != NCI_STATUS_OK)
180019c4fbaSIlan Elias 		nci_req_complete(ndev, status);
181019c4fbaSIlan Elias }
182019c4fbaSIlan Elias 
1836a2968aaSIlan Elias static void nci_rf_deactivate_rsp_packet(struct nci_dev *ndev,
1846a2968aaSIlan Elias 					 struct sk_buff *skb)
1856a2968aaSIlan Elias {
1866a2968aaSIlan Elias 	__u8 status = skb->data[0];
1876a2968aaSIlan Elias 
18824bf3304SJoe Perches 	pr_debug("status 0x%x\n", status);
1896a2968aaSIlan Elias 
190bd7e01bcSIlan Elias 	/* If target was active, complete the request only in deactivate_ntf */
191bd7e01bcSIlan Elias 	if ((status != NCI_STATUS_OK) ||
1928939e47fSIlan Elias 	    (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
193019c4fbaSIlan Elias 		nci_clear_target_list(ndev);
1948939e47fSIlan Elias 		atomic_set(&ndev->state, NCI_IDLE);
1956a2968aaSIlan Elias 		nci_req_complete(ndev, status);
1966a2968aaSIlan Elias 	}
1978939e47fSIlan Elias }
1986a2968aaSIlan Elias 
199af9c8aa6SChristophe Ricard static void nci_nfcee_discover_rsp_packet(struct nci_dev *ndev,
200af9c8aa6SChristophe Ricard 					  struct sk_buff *skb)
201af9c8aa6SChristophe Ricard {
202af9c8aa6SChristophe Ricard 	struct nci_nfcee_discover_rsp *discover_rsp;
203af9c8aa6SChristophe Ricard 
204af9c8aa6SChristophe Ricard 	if (skb->len != 2) {
205af9c8aa6SChristophe Ricard 		nci_req_complete(ndev, NCI_STATUS_NFCEE_PROTOCOL_ERROR);
206af9c8aa6SChristophe Ricard 		return;
207af9c8aa6SChristophe Ricard 	}
208af9c8aa6SChristophe Ricard 
209af9c8aa6SChristophe Ricard 	discover_rsp = (struct nci_nfcee_discover_rsp *)skb->data;
210af9c8aa6SChristophe Ricard 
211af9c8aa6SChristophe Ricard 	if (discover_rsp->status != NCI_STATUS_OK ||
212af9c8aa6SChristophe Ricard 	    discover_rsp->num_nfcee == 0)
213af9c8aa6SChristophe Ricard 		nci_req_complete(ndev, discover_rsp->status);
214af9c8aa6SChristophe Ricard }
215af9c8aa6SChristophe Ricard 
216*f7f793f3SChristophe Ricard static void nci_nfcee_mode_set_rsp_packet(struct nci_dev *ndev,
217*f7f793f3SChristophe Ricard 					  struct sk_buff *skb)
218*f7f793f3SChristophe Ricard {
219*f7f793f3SChristophe Ricard 	__u8 status = skb->data[0];
220*f7f793f3SChristophe Ricard 
221*f7f793f3SChristophe Ricard 	pr_debug("status 0x%x\n", status);
222*f7f793f3SChristophe Ricard 	nci_req_complete(ndev, status);
223*f7f793f3SChristophe Ricard }
224*f7f793f3SChristophe Ricard 
2256a2968aaSIlan Elias void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
2266a2968aaSIlan Elias {
2276a2968aaSIlan Elias 	__u16 rsp_opcode = nci_opcode(skb->data);
2286a2968aaSIlan Elias 
2296a2968aaSIlan Elias 	/* we got a rsp, stop the cmd timer */
2306a2968aaSIlan Elias 	del_timer(&ndev->cmd_timer);
2316a2968aaSIlan Elias 
23220c239c1SJoe Perches 	pr_debug("NCI RX: MT=rsp, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
2336a2968aaSIlan Elias 		 nci_pbf(skb->data),
2346a2968aaSIlan Elias 		 nci_opcode_gid(rsp_opcode),
2356a2968aaSIlan Elias 		 nci_opcode_oid(rsp_opcode),
2366a2968aaSIlan Elias 		 nci_plen(skb->data));
2376a2968aaSIlan Elias 
2386a2968aaSIlan Elias 	/* strip the nci control header */
2396a2968aaSIlan Elias 	skb_pull(skb, NCI_CTRL_HDR_SIZE);
2406a2968aaSIlan Elias 
2416a2968aaSIlan Elias 	switch (rsp_opcode) {
2426a2968aaSIlan Elias 	case NCI_OP_CORE_RESET_RSP:
2436a2968aaSIlan Elias 		nci_core_reset_rsp_packet(ndev, skb);
2446a2968aaSIlan Elias 		break;
2456a2968aaSIlan Elias 
2466a2968aaSIlan Elias 	case NCI_OP_CORE_INIT_RSP:
2476a2968aaSIlan Elias 		nci_core_init_rsp_packet(ndev, skb);
2486a2968aaSIlan Elias 		break;
2496a2968aaSIlan Elias 
2507e035230SIlan Elias 	case NCI_OP_CORE_SET_CONFIG_RSP:
2517e035230SIlan Elias 		nci_core_set_config_rsp_packet(ndev, skb);
2527e035230SIlan Elias 		break;
2537e035230SIlan Elias 
2546a2968aaSIlan Elias 	case NCI_OP_RF_DISCOVER_MAP_RSP:
2556a2968aaSIlan Elias 		nci_rf_disc_map_rsp_packet(ndev, skb);
2566a2968aaSIlan Elias 		break;
2576a2968aaSIlan Elias 
2586a2968aaSIlan Elias 	case NCI_OP_RF_DISCOVER_RSP:
2596a2968aaSIlan Elias 		nci_rf_disc_rsp_packet(ndev, skb);
2606a2968aaSIlan Elias 		break;
2616a2968aaSIlan Elias 
262019c4fbaSIlan Elias 	case NCI_OP_RF_DISCOVER_SELECT_RSP:
263019c4fbaSIlan Elias 		nci_rf_disc_select_rsp_packet(ndev, skb);
264019c4fbaSIlan Elias 		break;
265019c4fbaSIlan Elias 
2666a2968aaSIlan Elias 	case NCI_OP_RF_DEACTIVATE_RSP:
2676a2968aaSIlan Elias 		nci_rf_deactivate_rsp_packet(ndev, skb);
2686a2968aaSIlan Elias 		break;
2696a2968aaSIlan Elias 
270af9c8aa6SChristophe Ricard 	case NCI_OP_NFCEE_DISCOVER_RSP:
271af9c8aa6SChristophe Ricard 		nci_nfcee_discover_rsp_packet(ndev, skb);
272af9c8aa6SChristophe Ricard 		break;
273af9c8aa6SChristophe Ricard 
274*f7f793f3SChristophe Ricard 	case NCI_OP_NFCEE_MODE_SET_RSP:
275*f7f793f3SChristophe Ricard 		nci_nfcee_mode_set_rsp_packet(ndev, skb);
276*f7f793f3SChristophe Ricard 		break;
277*f7f793f3SChristophe Ricard 
2786a2968aaSIlan Elias 	default:
279ed1e0ad8SJoe Perches 		pr_err("unknown rsp opcode 0x%x\n", rsp_opcode);
2806a2968aaSIlan Elias 		break;
2816a2968aaSIlan Elias 	}
2826a2968aaSIlan Elias 
2836a2968aaSIlan Elias 	kfree_skb(skb);
2846a2968aaSIlan Elias 
2856a2968aaSIlan Elias 	/* trigger the next cmd */
2866a2968aaSIlan Elias 	atomic_set(&ndev->cmd_cnt, 1);
2876a2968aaSIlan Elias 	if (!skb_queue_empty(&ndev->cmd_q))
2886a2968aaSIlan Elias 		queue_work(ndev->cmd_wq, &ndev->cmd_work);
2896a2968aaSIlan Elias }
290