NFC: Clearly separate NCI states from flags
[deliverable/linux.git] / net / nfc / nci / rsp.c
CommitLineData
6a2968aa
IE
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 *
7 * Written by Ilan Elias <ilane@ti.com>
8 *
9 * Acknowledgements:
10 * This file is based on hci_event.c, which was written
11 * by Maxim Krasnyansky.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
52858b51 28#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
ed1e0ad8 29
6a2968aa
IE
30#include <linux/types.h>
31#include <linux/interrupt.h>
32#include <linux/bitops.h>
33#include <linux/skbuff.h>
34
35#include "../nfc.h"
36#include <net/nfc/nci.h>
37#include <net/nfc/nci_core.h>
38
39/* Handle NCI Response packets */
40
41static void nci_core_reset_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
42{
43 struct nci_core_reset_rsp *rsp = (void *) skb->data;
44
24bf3304 45 pr_debug("status 0x%x\n", rsp->status);
6a2968aa 46
e8c0dacd 47 if (rsp->status == NCI_STATUS_OK) {
6a2968aa 48 ndev->nci_ver = rsp->nci_ver;
20c239c1
JP
49 pr_debug("nci_ver 0x%x, config_status 0x%x\n",
50 rsp->nci_ver, rsp->config_status);
e8c0dacd 51 }
6a2968aa
IE
52
53 nci_req_complete(ndev, rsp->status);
54}
55
56static void nci_core_init_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
57{
58 struct nci_core_init_rsp_1 *rsp_1 = (void *) skb->data;
59 struct nci_core_init_rsp_2 *rsp_2;
60
24bf3304 61 pr_debug("status 0x%x\n", rsp_1->status);
6a2968aa
IE
62
63 if (rsp_1->status != NCI_STATUS_OK)
e8c0dacd 64 goto exit;
6a2968aa
IE
65
66 ndev->nfcc_features = __le32_to_cpu(rsp_1->nfcc_features);
67 ndev->num_supported_rf_interfaces = rsp_1->num_supported_rf_interfaces;
68
69 if (ndev->num_supported_rf_interfaces >
e8c0dacd 70 NCI_MAX_SUPPORTED_RF_INTERFACES) {
6a2968aa
IE
71 ndev->num_supported_rf_interfaces =
72 NCI_MAX_SUPPORTED_RF_INTERFACES;
73 }
74
75 memcpy(ndev->supported_rf_interfaces,
76 rsp_1->supported_rf_interfaces,
77 ndev->num_supported_rf_interfaces);
78
e8c0dacd 79 rsp_2 = (void *) (skb->data + 6 + rsp_1->num_supported_rf_interfaces);
6a2968aa
IE
80
81 ndev->max_logical_connections =
82 rsp_2->max_logical_connections;
83 ndev->max_routing_table_size =
84 __le16_to_cpu(rsp_2->max_routing_table_size);
e8c0dacd
IE
85 ndev->max_ctrl_pkt_payload_len =
86 rsp_2->max_ctrl_pkt_payload_len;
87 ndev->max_size_for_large_params =
88 __le16_to_cpu(rsp_2->max_size_for_large_params);
e8c0dacd
IE
89 ndev->manufact_id =
90 rsp_2->manufact_id;
91 ndev->manufact_specific_info =
92 __le32_to_cpu(rsp_2->manufact_specific_info);
93
20c239c1
JP
94 pr_debug("nfcc_features 0x%x\n",
95 ndev->nfcc_features);
96 pr_debug("num_supported_rf_interfaces %d\n",
97 ndev->num_supported_rf_interfaces);
98 pr_debug("supported_rf_interfaces[0] 0x%x\n",
99 ndev->supported_rf_interfaces[0]);
100 pr_debug("supported_rf_interfaces[1] 0x%x\n",
101 ndev->supported_rf_interfaces[1]);
102 pr_debug("supported_rf_interfaces[2] 0x%x\n",
103 ndev->supported_rf_interfaces[2]);
104 pr_debug("supported_rf_interfaces[3] 0x%x\n",
105 ndev->supported_rf_interfaces[3]);
106 pr_debug("max_logical_connections %d\n",
107 ndev->max_logical_connections);
108 pr_debug("max_routing_table_size %d\n",
109 ndev->max_routing_table_size);
110 pr_debug("max_ctrl_pkt_payload_len %d\n",
111 ndev->max_ctrl_pkt_payload_len);
112 pr_debug("max_size_for_large_params %d\n",
113 ndev->max_size_for_large_params);
20c239c1
JP
114 pr_debug("manufact_id 0x%x\n",
115 ndev->manufact_id);
116 pr_debug("manufact_specific_info 0x%x\n",
117 ndev->manufact_specific_info);
e8c0dacd
IE
118
119exit:
6a2968aa
IE
120 nci_req_complete(ndev, rsp_1->status);
121}
122
6a2968aa
IE
123static void nci_rf_disc_map_rsp_packet(struct nci_dev *ndev,
124 struct sk_buff *skb)
125{
126 __u8 status = skb->data[0];
127
24bf3304 128 pr_debug("status 0x%x\n", status);
6a2968aa
IE
129
130 nci_req_complete(ndev, status);
131}
132
133static void nci_rf_disc_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
134{
135 __u8 status = skb->data[0];
136
24bf3304 137 pr_debug("status 0x%x\n", status);
6a2968aa
IE
138
139 if (status == NCI_STATUS_OK)
8939e47f 140 atomic_set(&ndev->state, NCI_DISCOVERY);
6a2968aa
IE
141
142 nci_req_complete(ndev, status);
143}
144
145static void nci_rf_deactivate_rsp_packet(struct nci_dev *ndev,
146 struct sk_buff *skb)
147{
148 __u8 status = skb->data[0];
149
24bf3304 150 pr_debug("status 0x%x\n", status);
6a2968aa 151
bd7e01bc
IE
152 /* If target was active, complete the request only in deactivate_ntf */
153 if ((status != NCI_STATUS_OK) ||
8939e47f
IE
154 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
155 atomic_set(&ndev->state, NCI_IDLE);
bd7e01bc 156 nci_req_complete(ndev, status);
8939e47f 157 }
6a2968aa
IE
158}
159
160void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
161{
162 __u16 rsp_opcode = nci_opcode(skb->data);
163
164 /* we got a rsp, stop the cmd timer */
165 del_timer(&ndev->cmd_timer);
166
20c239c1
JP
167 pr_debug("NCI RX: MT=rsp, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
168 nci_pbf(skb->data),
169 nci_opcode_gid(rsp_opcode),
170 nci_opcode_oid(rsp_opcode),
171 nci_plen(skb->data));
6a2968aa
IE
172
173 /* strip the nci control header */
174 skb_pull(skb, NCI_CTRL_HDR_SIZE);
175
176 switch (rsp_opcode) {
177 case NCI_OP_CORE_RESET_RSP:
178 nci_core_reset_rsp_packet(ndev, skb);
179 break;
180
181 case NCI_OP_CORE_INIT_RSP:
182 nci_core_init_rsp_packet(ndev, skb);
183 break;
184
6a2968aa
IE
185 case NCI_OP_RF_DISCOVER_MAP_RSP:
186 nci_rf_disc_map_rsp_packet(ndev, skb);
187 break;
188
189 case NCI_OP_RF_DISCOVER_RSP:
190 nci_rf_disc_rsp_packet(ndev, skb);
191 break;
192
193 case NCI_OP_RF_DEACTIVATE_RSP:
194 nci_rf_deactivate_rsp_packet(ndev, skb);
195 break;
196
197 default:
ed1e0ad8 198 pr_err("unknown rsp opcode 0x%x\n", rsp_opcode);
6a2968aa
IE
199 break;
200 }
201
202 kfree_skb(skb);
203
204 /* trigger the next cmd */
205 atomic_set(&ndev->cmd_cnt, 1);
206 if (!skb_queue_empty(&ndev->cmd_q))
207 queue_work(ndev->cmd_wq, &ndev->cmd_work);
208}
This page took 0.050544 seconds and 5 git commands to generate.