blob: 2e3dee42196d064d0fdcf5ad7c6e4fe50945d57b [file] [log] [blame]
Ilan Elias6a2968a2011-09-18 11:19:35 +03001/*
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
Samuel Ortiz52858b52011-12-14 16:43:05 +010028#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perchesed1e0ad2011-11-29 11:37:32 -080029
Ilan Elias6a2968a2011-09-18 11:19:35 +030030#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#include <linux/nfc.h>
39
40/* Handle NCI Notification packets */
41
42static void nci_core_conn_credits_ntf_packet(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010043 struct sk_buff *skb)
Ilan Elias6a2968a2011-09-18 11:19:35 +030044{
45 struct nci_core_conn_credit_ntf *ntf = (void *) skb->data;
46 int i;
47
Joe Perches24bf3302011-11-29 11:37:35 -080048 pr_debug("num_entries %d\n", ntf->num_entries);
Ilan Elias6a2968a2011-09-18 11:19:35 +030049
50 if (ntf->num_entries > NCI_MAX_NUM_CONN)
51 ntf->num_entries = NCI_MAX_NUM_CONN;
52
53 /* update the credits */
54 for (i = 0; i < ntf->num_entries; i++) {
Ilan Elias637d85a2011-12-20 16:57:40 +020055 ntf->conn_entries[i].conn_id =
56 nci_conn_id(&ntf->conn_entries[i].conn_id);
57
Joe Perches20c239c2011-11-29 11:37:33 -080058 pr_debug("entry[%d]: conn_id %d, credits %d\n",
59 i, ntf->conn_entries[i].conn_id,
60 ntf->conn_entries[i].credits);
Ilan Elias6a2968a2011-09-18 11:19:35 +030061
Ilan Eliase8c0dac2011-11-09 12:09:14 +020062 if (ntf->conn_entries[i].conn_id == NCI_STATIC_RF_CONN_ID) {
Ilan Elias6a2968a2011-09-18 11:19:35 +030063 /* found static rf connection */
64 atomic_add(ntf->conn_entries[i].credits,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010065 &ndev->credits_cnt);
Ilan Elias6a2968a2011-09-18 11:19:35 +030066 }
67 }
68
69 /* trigger the next tx */
70 if (!skb_queue_empty(&ndev->tx_q))
71 queue_work(ndev->tx_wq, &ndev->tx_work);
72}
73
Ilan Elias019c4fb2012-01-18 13:16:14 +020074static void nci_core_generic_error_ntf_packet(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010075 struct sk_buff *skb)
Ilan Elias019c4fb2012-01-18 13:16:14 +020076{
77 __u8 status = skb->data[0];
78
79 pr_debug("status 0x%x\n", status);
80
81 if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
82 /* Activation failed, so complete the request
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +010083 (the state remains the same) */
Ilan Elias019c4fb2012-01-18 13:16:14 +020084 nci_req_complete(ndev, status);
85 }
86}
87
Ilan Elias004161c2011-12-20 16:57:41 +020088static void nci_core_conn_intf_error_ntf_packet(struct nci_dev *ndev,
89 struct sk_buff *skb)
90{
91 struct nci_core_intf_error_ntf *ntf = (void *) skb->data;
92
93 ntf->conn_id = nci_conn_id(&ntf->conn_id);
94
95 pr_debug("status 0x%x, conn_id %d\n", ntf->status, ntf->conn_id);
96
97 /* complete the data exchange transaction, if exists */
98 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
99 nci_data_exchange_complete(ndev, NULL, -EIO);
100}
101
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200102static __u8 *nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev,
Ilan Elias019c4fb2012-01-18 13:16:14 +0200103 struct rf_tech_specific_params_nfca_poll *nfca_poll,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100104 __u8 *data)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300105{
Ilan Elias6a2968a2011-09-18 11:19:35 +0300106 nfca_poll->sens_res = __le16_to_cpu(*((__u16 *)data));
107 data += 2;
108
109 nfca_poll->nfcid1_len = *data++;
110
Joe Perches20c239c2011-11-29 11:37:33 -0800111 pr_debug("sens_res 0x%x, nfcid1_len %d\n",
112 nfca_poll->sens_res, nfca_poll->nfcid1_len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300113
114 memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len);
115 data += nfca_poll->nfcid1_len;
116
117 nfca_poll->sel_res_len = *data++;
118
119 if (nfca_poll->sel_res_len != 0)
120 nfca_poll->sel_res = *data++;
121
Joe Perches20c239c2011-11-29 11:37:33 -0800122 pr_debug("sel_res_len %d, sel_res 0x%x\n",
123 nfca_poll->sel_res_len,
124 nfca_poll->sel_res);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300125
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200126 return data;
127}
128
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200129static __u8 *nci_extract_rf_params_nfcb_passive_poll(struct nci_dev *ndev,
Ilan Elias019c4fb2012-01-18 13:16:14 +0200130 struct rf_tech_specific_params_nfcb_poll *nfcb_poll,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100131 __u8 *data)
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200132{
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200133 nfcb_poll->sensb_res_len = *data++;
134
135 pr_debug("sensb_res_len %d\n", nfcb_poll->sensb_res_len);
136
137 memcpy(nfcb_poll->sensb_res, data, nfcb_poll->sensb_res_len);
138 data += nfcb_poll->sensb_res_len;
139
140 return data;
141}
142
143static __u8 *nci_extract_rf_params_nfcf_passive_poll(struct nci_dev *ndev,
Ilan Elias019c4fb2012-01-18 13:16:14 +0200144 struct rf_tech_specific_params_nfcf_poll *nfcf_poll,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100145 __u8 *data)
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200146{
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200147 nfcf_poll->bit_rate = *data++;
148 nfcf_poll->sensf_res_len = *data++;
149
150 pr_debug("bit_rate %d, sensf_res_len %d\n",
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100151 nfcf_poll->bit_rate, nfcf_poll->sensf_res_len);
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200152
153 memcpy(nfcf_poll->sensf_res, data, nfcf_poll->sensf_res_len);
154 data += nfcf_poll->sensf_res_len;
155
156 return data;
157}
158
Ilan Elias019c4fb2012-01-18 13:16:14 +0200159static int nci_add_new_protocol(struct nci_dev *ndev,
160 struct nfc_target *target,
161 __u8 rf_protocol,
162 __u8 rf_tech_and_mode,
163 void *params)
164{
165 struct rf_tech_specific_params_nfca_poll *nfca_poll;
166 struct rf_tech_specific_params_nfcb_poll *nfcb_poll;
167 struct rf_tech_specific_params_nfcf_poll *nfcf_poll;
168 __u32 protocol;
169
170 if (rf_protocol == NCI_RF_PROTOCOL_T2T)
171 protocol = NFC_PROTO_MIFARE_MASK;
172 else if (rf_protocol == NCI_RF_PROTOCOL_ISO_DEP)
173 protocol = NFC_PROTO_ISO14443_MASK;
174 else if (rf_protocol == NCI_RF_PROTOCOL_T3T)
175 protocol = NFC_PROTO_FELICA_MASK;
176 else
177 protocol = 0;
178
179 if (!(protocol & ndev->poll_prots)) {
180 pr_err("the target found does not have the desired protocol\n");
181 return -EPROTO;
182 }
183
184 if (rf_tech_and_mode == NCI_NFC_A_PASSIVE_POLL_MODE) {
185 nfca_poll = (struct rf_tech_specific_params_nfca_poll *)params;
186
187 target->sens_res = nfca_poll->sens_res;
188 target->sel_res = nfca_poll->sel_res;
189 target->nfcid1_len = nfca_poll->nfcid1_len;
190 if (target->nfcid1_len > 0) {
191 memcpy(target->nfcid1, nfca_poll->nfcid1,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100192 target->nfcid1_len);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200193 }
194 } else if (rf_tech_and_mode == NCI_NFC_B_PASSIVE_POLL_MODE) {
195 nfcb_poll = (struct rf_tech_specific_params_nfcb_poll *)params;
196
197 target->sensb_res_len = nfcb_poll->sensb_res_len;
198 if (target->sensb_res_len > 0) {
199 memcpy(target->sensb_res, nfcb_poll->sensb_res,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100200 target->sensb_res_len);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200201 }
202 } else if (rf_tech_and_mode == NCI_NFC_F_PASSIVE_POLL_MODE) {
203 nfcf_poll = (struct rf_tech_specific_params_nfcf_poll *)params;
204
205 target->sensf_res_len = nfcf_poll->sensf_res_len;
206 if (target->sensf_res_len > 0) {
207 memcpy(target->sensf_res, nfcf_poll->sensf_res,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100208 target->sensf_res_len);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200209 }
210 } else {
211 pr_err("unsupported rf_tech_and_mode 0x%x\n", rf_tech_and_mode);
212 return -EPROTO;
213 }
214
215 target->supported_protocols |= protocol;
216
217 pr_debug("protocol 0x%x\n", protocol);
218
219 return 0;
220}
221
222static void nci_add_new_target(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100223 struct nci_rf_discover_ntf *ntf)
Ilan Elias019c4fb2012-01-18 13:16:14 +0200224{
225 struct nfc_target *target;
226 int i, rc;
227
228 for (i = 0; i < ndev->n_targets; i++) {
229 target = &ndev->targets[i];
230 if (target->idx == ntf->rf_discovery_id) {
231 /* This target already exists, add the new protocol */
232 nci_add_new_protocol(ndev, target, ntf->rf_protocol,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100233 ntf->rf_tech_and_mode,
234 &ntf->rf_tech_specific_params);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200235 return;
236 }
237 }
238
239 /* This is a new target, check if we've enough room */
240 if (ndev->n_targets == NCI_MAX_DISCOVERED_TARGETS) {
241 pr_debug("not enough room, ignoring new target...\n");
242 return;
243 }
244
245 target = &ndev->targets[ndev->n_targets];
246
247 rc = nci_add_new_protocol(ndev, target, ntf->rf_protocol,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100248 ntf->rf_tech_and_mode,
249 &ntf->rf_tech_specific_params);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200250 if (!rc) {
251 target->idx = ntf->rf_discovery_id;
252 ndev->n_targets++;
253
254 pr_debug("target_idx %d, n_targets %d\n", target->idx,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100255 ndev->n_targets);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200256 }
257}
258
259void nci_clear_target_list(struct nci_dev *ndev)
260{
261 memset(ndev->targets, 0,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100262 (sizeof(struct nfc_target)*NCI_MAX_DISCOVERED_TARGETS));
Ilan Elias019c4fb2012-01-18 13:16:14 +0200263
264 ndev->n_targets = 0;
265}
266
267static void nci_rf_discover_ntf_packet(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100268 struct sk_buff *skb)
Ilan Elias019c4fb2012-01-18 13:16:14 +0200269{
270 struct nci_rf_discover_ntf ntf;
271 __u8 *data = skb->data;
272 bool add_target = true;
273
274 ntf.rf_discovery_id = *data++;
275 ntf.rf_protocol = *data++;
276 ntf.rf_tech_and_mode = *data++;
277 ntf.rf_tech_specific_params_len = *data++;
278
279 pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id);
280 pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol);
281 pr_debug("rf_tech_and_mode 0x%x\n", ntf.rf_tech_and_mode);
282 pr_debug("rf_tech_specific_params_len %d\n",
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100283 ntf.rf_tech_specific_params_len);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200284
285 if (ntf.rf_tech_specific_params_len > 0) {
286 switch (ntf.rf_tech_and_mode) {
287 case NCI_NFC_A_PASSIVE_POLL_MODE:
288 data = nci_extract_rf_params_nfca_passive_poll(ndev,
289 &(ntf.rf_tech_specific_params.nfca_poll), data);
290 break;
291
292 case NCI_NFC_B_PASSIVE_POLL_MODE:
293 data = nci_extract_rf_params_nfcb_passive_poll(ndev,
294 &(ntf.rf_tech_specific_params.nfcb_poll), data);
295 break;
296
297 case NCI_NFC_F_PASSIVE_POLL_MODE:
298 data = nci_extract_rf_params_nfcf_passive_poll(ndev,
299 &(ntf.rf_tech_specific_params.nfcf_poll), data);
300 break;
301
302 default:
303 pr_err("unsupported rf_tech_and_mode 0x%x\n",
304 ntf.rf_tech_and_mode);
305 data += ntf.rf_tech_specific_params_len;
306 add_target = false;
307 }
308 }
309
310 ntf.ntf_type = *data++;
311 pr_debug("ntf_type %d\n", ntf.ntf_type);
312
313 if (add_target == true)
314 nci_add_new_target(ndev, &ntf);
315
316 if (ntf.ntf_type == NCI_DISCOVER_NTF_TYPE_MORE) {
317 atomic_set(&ndev->state, NCI_W4_ALL_DISCOVERIES);
318 } else {
319 atomic_set(&ndev->state, NCI_W4_HOST_SELECT);
320 nfc_targets_found(ndev->nfc_dev, ndev->targets,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100321 ndev->n_targets);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200322 }
323}
324
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200325static int nci_extract_activation_params_iso_dep(struct nci_dev *ndev,
326 struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
327{
328 struct activation_params_nfca_poll_iso_dep *nfca_poll;
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200329 struct activation_params_nfcb_poll_iso_dep *nfcb_poll;
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200330
331 switch (ntf->activation_rf_tech_and_mode) {
332 case NCI_NFC_A_PASSIVE_POLL_MODE:
333 nfca_poll = &ntf->activation_params.nfca_poll_iso_dep;
334 nfca_poll->rats_res_len = *data++;
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200335 pr_debug("rats_res_len %d\n", nfca_poll->rats_res_len);
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200336 if (nfca_poll->rats_res_len > 0) {
337 memcpy(nfca_poll->rats_res,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100338 data, nfca_poll->rats_res_len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300339 }
340 break;
341
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200342 case NCI_NFC_B_PASSIVE_POLL_MODE:
343 nfcb_poll = &ntf->activation_params.nfcb_poll_iso_dep;
344 nfcb_poll->attrib_res_len = *data++;
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100345 pr_debug("attrib_res_len %d\n", nfcb_poll->attrib_res_len);
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200346 if (nfcb_poll->attrib_res_len > 0) {
347 memcpy(nfcb_poll->attrib_res,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100348 data, nfcb_poll->attrib_res_len);
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200349 }
350 break;
351
Ilan Elias6a2968a2011-09-18 11:19:35 +0300352 default:
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800353 pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
354 ntf->activation_rf_tech_and_mode);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200355 return NCI_STATUS_RF_PROTOCOL_ERROR;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300356 }
357
Ilan Elias019c4fb2012-01-18 13:16:14 +0200358 return NCI_STATUS_OK;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300359}
360
Ilan Elias019c4fb2012-01-18 13:16:14 +0200361static void nci_target_auto_activated(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100362 struct nci_rf_intf_activated_ntf *ntf)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300363{
Ilan Elias019c4fb2012-01-18 13:16:14 +0200364 struct nfc_target *target;
365 int rc;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300366
Ilan Elias019c4fb2012-01-18 13:16:14 +0200367 target = &ndev->targets[ndev->n_targets];
Ilan Elias6a2968a2011-09-18 11:19:35 +0300368
Ilan Elias019c4fb2012-01-18 13:16:14 +0200369 rc = nci_add_new_protocol(ndev, target, ntf->rf_protocol,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100370 ntf->activation_rf_tech_and_mode,
371 &ntf->rf_tech_specific_params);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200372 if (rc)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300373 return;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300374
Ilan Elias019c4fb2012-01-18 13:16:14 +0200375 target->idx = ntf->rf_discovery_id;
376 ndev->n_targets++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300377
Ilan Elias019c4fb2012-01-18 13:16:14 +0200378 pr_debug("target_idx %d, n_targets %d\n", target->idx, ndev->n_targets);
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200379
Ilan Elias019c4fb2012-01-18 13:16:14 +0200380 nfc_targets_found(ndev->nfc_dev, ndev->targets, ndev->n_targets);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300381}
382
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200383static void nci_rf_intf_activated_ntf_packet(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100384 struct sk_buff *skb)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300385{
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200386 struct nci_rf_intf_activated_ntf ntf;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300387 __u8 *data = skb->data;
Ilan Elias019c4fb2012-01-18 13:16:14 +0200388 int err = NCI_STATUS_OK;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300389
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200390 ntf.rf_discovery_id = *data++;
Ilan Elias637d85a2011-12-20 16:57:40 +0200391 ntf.rf_interface = *data++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300392 ntf.rf_protocol = *data++;
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200393 ntf.activation_rf_tech_and_mode = *data++;
Ilan Elias637d85a2011-12-20 16:57:40 +0200394 ntf.max_data_pkt_payload_size = *data++;
395 ntf.initial_num_credits = *data++;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300396 ntf.rf_tech_specific_params_len = *data++;
397
Joe Perches20c239c2011-11-29 11:37:33 -0800398 pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id);
Ilan Elias637d85a2011-12-20 16:57:40 +0200399 pr_debug("rf_interface 0x%x\n", ntf.rf_interface);
Joe Perches20c239c2011-11-29 11:37:33 -0800400 pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol);
401 pr_debug("activation_rf_tech_and_mode 0x%x\n",
402 ntf.activation_rf_tech_and_mode);
Ilan Elias637d85a2011-12-20 16:57:40 +0200403 pr_debug("max_data_pkt_payload_size 0x%x\n",
404 ntf.max_data_pkt_payload_size);
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100405 pr_debug("initial_num_credits 0x%x\n",
406 ntf.initial_num_credits);
Joe Perches20c239c2011-11-29 11:37:33 -0800407 pr_debug("rf_tech_specific_params_len %d\n",
408 ntf.rf_tech_specific_params_len);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300409
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200410 if (ntf.rf_tech_specific_params_len > 0) {
411 switch (ntf.activation_rf_tech_and_mode) {
412 case NCI_NFC_A_PASSIVE_POLL_MODE:
413 data = nci_extract_rf_params_nfca_passive_poll(ndev,
Ilan Elias019c4fb2012-01-18 13:16:14 +0200414 &(ntf.rf_tech_specific_params.nfca_poll), data);
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200415 break;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300416
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200417 case NCI_NFC_B_PASSIVE_POLL_MODE:
418 data = nci_extract_rf_params_nfcb_passive_poll(ndev,
Ilan Elias019c4fb2012-01-18 13:16:14 +0200419 &(ntf.rf_tech_specific_params.nfcb_poll), data);
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200420 break;
421
422 case NCI_NFC_F_PASSIVE_POLL_MODE:
423 data = nci_extract_rf_params_nfcf_passive_poll(ndev,
Ilan Elias019c4fb2012-01-18 13:16:14 +0200424 &(ntf.rf_tech_specific_params.nfcf_poll), data);
Ilan Eliasd5a2ca62012-01-17 11:06:43 +0200425 break;
426
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200427 default:
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800428 pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
429 ntf.activation_rf_tech_and_mode);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200430 err = NCI_STATUS_RF_PROTOCOL_ERROR;
431 goto exit;
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200432 }
Ilan Elias6a2968a2011-09-18 11:19:35 +0300433 }
434
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200435 ntf.data_exch_rf_tech_and_mode = *data++;
436 ntf.data_exch_tx_bit_rate = *data++;
437 ntf.data_exch_rx_bit_rate = *data++;
438 ntf.activation_params_len = *data++;
439
Joe Perches20c239c2011-11-29 11:37:33 -0800440 pr_debug("data_exch_rf_tech_and_mode 0x%x\n",
441 ntf.data_exch_rf_tech_and_mode);
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100442 pr_debug("data_exch_tx_bit_rate 0x%x\n", ntf.data_exch_tx_bit_rate);
443 pr_debug("data_exch_rx_bit_rate 0x%x\n", ntf.data_exch_rx_bit_rate);
444 pr_debug("activation_params_len %d\n", ntf.activation_params_len);
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200445
446 if (ntf.activation_params_len > 0) {
Ilan Elias637d85a2011-12-20 16:57:40 +0200447 switch (ntf.rf_interface) {
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200448 case NCI_RF_INTERFACE_ISO_DEP:
449 err = nci_extract_activation_params_iso_dep(ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100450 &ntf, data);
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200451 break;
452
453 case NCI_RF_INTERFACE_FRAME:
454 /* no activation params */
455 break;
456
457 default:
Ilan Elias637d85a2011-12-20 16:57:40 +0200458 pr_err("unsupported rf_interface 0x%x\n",
459 ntf.rf_interface);
Ilan Elias019c4fb2012-01-18 13:16:14 +0200460 err = NCI_STATUS_RF_PROTOCOL_ERROR;
461 break;
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200462 }
463 }
464
Ilan Elias019c4fb2012-01-18 13:16:14 +0200465exit:
466 if (err == NCI_STATUS_OK) {
467 ndev->max_data_pkt_payload_size = ntf.max_data_pkt_payload_size;
468 ndev->initial_num_credits = ntf.initial_num_credits;
469
470 /* set the available credits to initial value */
471 atomic_set(&ndev->credits_cnt, ndev->initial_num_credits);
472 }
473
474 if (atomic_read(&ndev->state) == NCI_DISCOVERY) {
475 /* A single target was found and activated automatically */
476 atomic_set(&ndev->state, NCI_POLL_ACTIVE);
477 if (err == NCI_STATUS_OK)
478 nci_target_auto_activated(ndev, &ntf);
479 } else { /* ndev->state == NCI_W4_HOST_SELECT */
480 /* A selected target was activated, so complete the request */
481 atomic_set(&ndev->state, NCI_POLL_ACTIVE);
482 nci_req_complete(ndev, err);
483 }
Ilan Elias6a2968a2011-09-18 11:19:35 +0300484}
485
486static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev,
Samuel Ortizeb9bc6e2012-03-05 01:03:54 +0100487 struct sk_buff *skb)
Ilan Elias6a2968a2011-09-18 11:19:35 +0300488{
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200489 struct nci_rf_deactivate_ntf *ntf = (void *) skb->data;
Ilan Elias6a2968a2011-09-18 11:19:35 +0300490
Joe Perches20c239c2011-11-29 11:37:33 -0800491 pr_debug("entry, type 0x%x, reason 0x%x\n", ntf->type, ntf->reason);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300492
Ilan Elias6a2968a2011-09-18 11:19:35 +0300493 /* drop tx data queue */
494 skb_queue_purge(&ndev->tx_q);
495
496 /* drop partial rx data packet */
497 if (ndev->rx_data_reassembly) {
498 kfree_skb(ndev->rx_data_reassembly);
499 ndev->rx_data_reassembly = 0;
500 }
501
502 /* complete the data exchange transaction, if exists */
Ilan Elias38f04c62011-09-22 11:36:19 +0300503 if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
Ilan Elias6a2968a2011-09-18 11:19:35 +0300504 nci_data_exchange_complete(ndev, NULL, -EIO);
Ilan Eliasbd7e01b2012-01-08 11:21:53 +0200505
Ilan Elias019c4fb2012-01-18 13:16:14 +0200506 nci_clear_target_list(ndev);
507 atomic_set(&ndev->state, NCI_IDLE);
Ilan Eliasbd7e01b2012-01-08 11:21:53 +0200508 nci_req_complete(ndev, NCI_STATUS_OK);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300509}
510
511void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb)
512{
513 __u16 ntf_opcode = nci_opcode(skb->data);
514
Joe Perches20c239c2011-11-29 11:37:33 -0800515 pr_debug("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
516 nci_pbf(skb->data),
517 nci_opcode_gid(ntf_opcode),
518 nci_opcode_oid(ntf_opcode),
519 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300520
521 /* strip the nci control header */
522 skb_pull(skb, NCI_CTRL_HDR_SIZE);
523
524 switch (ntf_opcode) {
525 case NCI_OP_CORE_CONN_CREDITS_NTF:
526 nci_core_conn_credits_ntf_packet(ndev, skb);
527 break;
528
Ilan Elias019c4fb2012-01-18 13:16:14 +0200529 case NCI_OP_CORE_GENERIC_ERROR_NTF:
530 nci_core_generic_error_ntf_packet(ndev, skb);
531 break;
532
Ilan Elias004161c2011-12-20 16:57:41 +0200533 case NCI_OP_CORE_INTF_ERROR_NTF:
534 nci_core_conn_intf_error_ntf_packet(ndev, skb);
535 break;
536
Ilan Elias019c4fb2012-01-18 13:16:14 +0200537 case NCI_OP_RF_DISCOVER_NTF:
538 nci_rf_discover_ntf_packet(ndev, skb);
539 break;
540
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200541 case NCI_OP_RF_INTF_ACTIVATED_NTF:
542 nci_rf_intf_activated_ntf_packet(ndev, skb);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300543 break;
544
545 case NCI_OP_RF_DEACTIVATE_NTF:
546 nci_rf_deactivate_ntf_packet(ndev, skb);
547 break;
548
549 default:
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800550 pr_err("unknown ntf opcode 0x%x\n", ntf_opcode);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300551 break;
552 }
553
554 kfree_skb(skb);
555}