percpu: Replace smp_read_barrier_depends() with lockless_dereference()
[sfrench/cifs-2.6.git] / net / nfc / nci / ntf.c
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) 2014 Marvell International Ltd.
6  *  Copyright (C) 2011 Texas Instruments, Inc.
7  *
8  *  Written by Ilan Elias <ilane@ti.com>
9  *
10  *  Acknowledgements:
11  *  This file is based on hci_event.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/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
42 static void nci_core_conn_credits_ntf_packet(struct nci_dev *ndev,
43                                              struct sk_buff *skb)
44 {
45         struct nci_core_conn_credit_ntf *ntf = (void *) skb->data;
46         int i;
47
48         pr_debug("num_entries %d\n", ntf->num_entries);
49
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++) {
55                 ntf->conn_entries[i].conn_id =
56                         nci_conn_id(&ntf->conn_entries[i].conn_id);
57
58                 pr_debug("entry[%d]: conn_id %d, credits %d\n",
59                          i, ntf->conn_entries[i].conn_id,
60                          ntf->conn_entries[i].credits);
61
62                 if (ntf->conn_entries[i].conn_id == NCI_STATIC_RF_CONN_ID) {
63                         /* found static rf connection */
64                         atomic_add(ntf->conn_entries[i].credits,
65                                    &ndev->credits_cnt);
66                 }
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
74 static void nci_core_generic_error_ntf_packet(struct nci_dev *ndev,
75                                               struct sk_buff *skb)
76 {
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
83                    (the state remains the same) */
84                 nci_req_complete(ndev, status);
85         }
86 }
87
88 static 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
102 static __u8 *nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev,
103                         struct rf_tech_specific_params_nfca_poll *nfca_poll,
104                                                      __u8 *data)
105 {
106         nfca_poll->sens_res = __le16_to_cpu(*((__u16 *)data));
107         data += 2;
108
109         nfca_poll->nfcid1_len = min_t(__u8, *data++, NFC_NFCID1_MAXSIZE);
110
111         pr_debug("sens_res 0x%x, nfcid1_len %d\n",
112                  nfca_poll->sens_res, nfca_poll->nfcid1_len);
113
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
122         pr_debug("sel_res_len %d, sel_res 0x%x\n",
123                  nfca_poll->sel_res_len,
124                  nfca_poll->sel_res);
125
126         return data;
127 }
128
129 static __u8 *nci_extract_rf_params_nfcb_passive_poll(struct nci_dev *ndev,
130                         struct rf_tech_specific_params_nfcb_poll *nfcb_poll,
131                                                      __u8 *data)
132 {
133         nfcb_poll->sensb_res_len = min_t(__u8, *data++, NFC_SENSB_RES_MAXSIZE);
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
143 static __u8 *nci_extract_rf_params_nfcf_passive_poll(struct nci_dev *ndev,
144                         struct rf_tech_specific_params_nfcf_poll *nfcf_poll,
145                                                      __u8 *data)
146 {
147         nfcf_poll->bit_rate = *data++;
148         nfcf_poll->sensf_res_len = min_t(__u8, *data++, NFC_SENSF_RES_MAXSIZE);
149
150         pr_debug("bit_rate %d, sensf_res_len %d\n",
151                  nfcf_poll->bit_rate, nfcf_poll->sensf_res_len);
152
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
159 static __u8 *nci_extract_rf_params_nfcv_passive_poll(struct nci_dev *ndev,
160                         struct rf_tech_specific_params_nfcv_poll *nfcv_poll,
161                                                      __u8 *data)
162 {
163         ++data;
164         nfcv_poll->dsfid = *data++;
165         memcpy(nfcv_poll->uid, data, NFC_ISO15693_UID_MAXSIZE);
166         data += NFC_ISO15693_UID_MAXSIZE;
167         return data;
168 }
169
170 __u32 nci_get_prop_rf_protocol(struct nci_dev *ndev, __u8 rf_protocol)
171 {
172         if (ndev->ops->get_rfprotocol)
173                 return ndev->ops->get_rfprotocol(ndev, rf_protocol);
174         return 0;
175 }
176
177 static int nci_add_new_protocol(struct nci_dev *ndev,
178                                 struct nfc_target *target,
179                                 __u8 rf_protocol,
180                                 __u8 rf_tech_and_mode,
181                                 void *params)
182 {
183         struct rf_tech_specific_params_nfca_poll *nfca_poll;
184         struct rf_tech_specific_params_nfcb_poll *nfcb_poll;
185         struct rf_tech_specific_params_nfcf_poll *nfcf_poll;
186         struct rf_tech_specific_params_nfcv_poll *nfcv_poll;
187         __u32 protocol;
188
189         if (rf_protocol == NCI_RF_PROTOCOL_T1T)
190                 protocol = NFC_PROTO_JEWEL_MASK;
191         else if (rf_protocol == NCI_RF_PROTOCOL_T2T)
192                 protocol = NFC_PROTO_MIFARE_MASK;
193         else if (rf_protocol == NCI_RF_PROTOCOL_ISO_DEP)
194                 if (rf_tech_and_mode == NCI_NFC_A_PASSIVE_POLL_MODE)
195                         protocol = NFC_PROTO_ISO14443_MASK;
196                 else
197                         protocol = NFC_PROTO_ISO14443_B_MASK;
198         else if (rf_protocol == NCI_RF_PROTOCOL_T3T)
199                 protocol = NFC_PROTO_FELICA_MASK;
200         else if (rf_protocol == NCI_RF_PROTOCOL_NFC_DEP)
201                 protocol = NFC_PROTO_NFC_DEP_MASK;
202         else if (rf_protocol == NCI_RF_PROTOCOL_T5T)
203                 protocol = NFC_PROTO_ISO15693_MASK;
204         else
205                 protocol = nci_get_prop_rf_protocol(ndev, rf_protocol);
206
207         if (!(protocol & ndev->poll_prots)) {
208                 pr_err("the target found does not have the desired protocol\n");
209                 return -EPROTO;
210         }
211
212         if (rf_tech_and_mode == NCI_NFC_A_PASSIVE_POLL_MODE) {
213                 nfca_poll = (struct rf_tech_specific_params_nfca_poll *)params;
214
215                 target->sens_res = nfca_poll->sens_res;
216                 target->sel_res = nfca_poll->sel_res;
217                 target->nfcid1_len = nfca_poll->nfcid1_len;
218                 if (target->nfcid1_len > 0) {
219                         memcpy(target->nfcid1, nfca_poll->nfcid1,
220                                target->nfcid1_len);
221                 }
222         } else if (rf_tech_and_mode == NCI_NFC_B_PASSIVE_POLL_MODE) {
223                 nfcb_poll = (struct rf_tech_specific_params_nfcb_poll *)params;
224
225                 target->sensb_res_len = nfcb_poll->sensb_res_len;
226                 if (target->sensb_res_len > 0) {
227                         memcpy(target->sensb_res, nfcb_poll->sensb_res,
228                                target->sensb_res_len);
229                 }
230         } else if (rf_tech_and_mode == NCI_NFC_F_PASSIVE_POLL_MODE) {
231                 nfcf_poll = (struct rf_tech_specific_params_nfcf_poll *)params;
232
233                 target->sensf_res_len = nfcf_poll->sensf_res_len;
234                 if (target->sensf_res_len > 0) {
235                         memcpy(target->sensf_res, nfcf_poll->sensf_res,
236                                target->sensf_res_len);
237                 }
238         } else if (rf_tech_and_mode == NCI_NFC_V_PASSIVE_POLL_MODE) {
239                 nfcv_poll = (struct rf_tech_specific_params_nfcv_poll *)params;
240
241                 target->is_iso15693 = 1;
242                 target->iso15693_dsfid = nfcv_poll->dsfid;
243                 memcpy(target->iso15693_uid, nfcv_poll->uid, NFC_ISO15693_UID_MAXSIZE);
244         } else {
245                 pr_err("unsupported rf_tech_and_mode 0x%x\n", rf_tech_and_mode);
246                 return -EPROTO;
247         }
248
249         target->supported_protocols |= protocol;
250
251         pr_debug("protocol 0x%x\n", protocol);
252
253         return 0;
254 }
255
256 static void nci_add_new_target(struct nci_dev *ndev,
257                                struct nci_rf_discover_ntf *ntf)
258 {
259         struct nfc_target *target;
260         int i, rc;
261
262         for (i = 0; i < ndev->n_targets; i++) {
263                 target = &ndev->targets[i];
264                 if (target->logical_idx == ntf->rf_discovery_id) {
265                         /* This target already exists, add the new protocol */
266                         nci_add_new_protocol(ndev, target, ntf->rf_protocol,
267                                              ntf->rf_tech_and_mode,
268                                              &ntf->rf_tech_specific_params);
269                         return;
270                 }
271         }
272
273         /* This is a new target, check if we've enough room */
274         if (ndev->n_targets == NCI_MAX_DISCOVERED_TARGETS) {
275                 pr_debug("not enough room, ignoring new target...\n");
276                 return;
277         }
278
279         target = &ndev->targets[ndev->n_targets];
280
281         rc = nci_add_new_protocol(ndev, target, ntf->rf_protocol,
282                                   ntf->rf_tech_and_mode,
283                                   &ntf->rf_tech_specific_params);
284         if (!rc) {
285                 target->logical_idx = ntf->rf_discovery_id;
286                 ndev->n_targets++;
287
288                 pr_debug("logical idx %d, n_targets %d\n", target->logical_idx,
289                          ndev->n_targets);
290         }
291 }
292
293 void nci_clear_target_list(struct nci_dev *ndev)
294 {
295         memset(ndev->targets, 0,
296                (sizeof(struct nfc_target)*NCI_MAX_DISCOVERED_TARGETS));
297
298         ndev->n_targets = 0;
299 }
300
301 static void nci_rf_discover_ntf_packet(struct nci_dev *ndev,
302                                        struct sk_buff *skb)
303 {
304         struct nci_rf_discover_ntf ntf;
305         __u8 *data = skb->data;
306         bool add_target = true;
307
308         ntf.rf_discovery_id = *data++;
309         ntf.rf_protocol = *data++;
310         ntf.rf_tech_and_mode = *data++;
311         ntf.rf_tech_specific_params_len = *data++;
312
313         pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id);
314         pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol);
315         pr_debug("rf_tech_and_mode 0x%x\n", ntf.rf_tech_and_mode);
316         pr_debug("rf_tech_specific_params_len %d\n",
317                  ntf.rf_tech_specific_params_len);
318
319         if (ntf.rf_tech_specific_params_len > 0) {
320                 switch (ntf.rf_tech_and_mode) {
321                 case NCI_NFC_A_PASSIVE_POLL_MODE:
322                         data = nci_extract_rf_params_nfca_passive_poll(ndev,
323                                 &(ntf.rf_tech_specific_params.nfca_poll), data);
324                         break;
325
326                 case NCI_NFC_B_PASSIVE_POLL_MODE:
327                         data = nci_extract_rf_params_nfcb_passive_poll(ndev,
328                                 &(ntf.rf_tech_specific_params.nfcb_poll), data);
329                         break;
330
331                 case NCI_NFC_F_PASSIVE_POLL_MODE:
332                         data = nci_extract_rf_params_nfcf_passive_poll(ndev,
333                                 &(ntf.rf_tech_specific_params.nfcf_poll), data);
334                         break;
335
336                 case NCI_NFC_V_PASSIVE_POLL_MODE:
337                         data = nci_extract_rf_params_nfcv_passive_poll(ndev,
338                                 &(ntf.rf_tech_specific_params.nfcv_poll), data);
339                         break;
340
341                 default:
342                         pr_err("unsupported rf_tech_and_mode 0x%x\n",
343                                ntf.rf_tech_and_mode);
344                         data += ntf.rf_tech_specific_params_len;
345                         add_target = false;
346                 }
347         }
348
349         ntf.ntf_type = *data++;
350         pr_debug("ntf_type %d\n", ntf.ntf_type);
351
352         if (add_target == true)
353                 nci_add_new_target(ndev, &ntf);
354
355         if (ntf.ntf_type == NCI_DISCOVER_NTF_TYPE_MORE) {
356                 atomic_set(&ndev->state, NCI_W4_ALL_DISCOVERIES);
357         } else {
358                 atomic_set(&ndev->state, NCI_W4_HOST_SELECT);
359                 nfc_targets_found(ndev->nfc_dev, ndev->targets,
360                                   ndev->n_targets);
361         }
362 }
363
364 static int nci_extract_activation_params_iso_dep(struct nci_dev *ndev,
365                         struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
366 {
367         struct activation_params_nfca_poll_iso_dep *nfca_poll;
368         struct activation_params_nfcb_poll_iso_dep *nfcb_poll;
369
370         switch (ntf->activation_rf_tech_and_mode) {
371         case NCI_NFC_A_PASSIVE_POLL_MODE:
372                 nfca_poll = &ntf->activation_params.nfca_poll_iso_dep;
373                 nfca_poll->rats_res_len = min_t(__u8, *data++, 20);
374                 pr_debug("rats_res_len %d\n", nfca_poll->rats_res_len);
375                 if (nfca_poll->rats_res_len > 0) {
376                         memcpy(nfca_poll->rats_res,
377                                data, nfca_poll->rats_res_len);
378                 }
379                 break;
380
381         case NCI_NFC_B_PASSIVE_POLL_MODE:
382                 nfcb_poll = &ntf->activation_params.nfcb_poll_iso_dep;
383                 nfcb_poll->attrib_res_len = min_t(__u8, *data++, 50);
384                 pr_debug("attrib_res_len %d\n", nfcb_poll->attrib_res_len);
385                 if (nfcb_poll->attrib_res_len > 0) {
386                         memcpy(nfcb_poll->attrib_res,
387                                data, nfcb_poll->attrib_res_len);
388                 }
389                 break;
390
391         default:
392                 pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
393                        ntf->activation_rf_tech_and_mode);
394                 return NCI_STATUS_RF_PROTOCOL_ERROR;
395         }
396
397         return NCI_STATUS_OK;
398 }
399
400 static int nci_extract_activation_params_nfc_dep(struct nci_dev *ndev,
401                         struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
402 {
403         struct activation_params_poll_nfc_dep *poll;
404
405         switch (ntf->activation_rf_tech_and_mode) {
406         case NCI_NFC_A_PASSIVE_POLL_MODE:
407         case NCI_NFC_F_PASSIVE_POLL_MODE:
408                 poll = &ntf->activation_params.poll_nfc_dep;
409                 poll->atr_res_len = min_t(__u8, *data++, 63);
410                 pr_debug("atr_res_len %d\n", poll->atr_res_len);
411                 if (poll->atr_res_len > 0)
412                         memcpy(poll->atr_res, data, poll->atr_res_len);
413                 break;
414
415         default:
416                 pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
417                        ntf->activation_rf_tech_and_mode);
418                 return NCI_STATUS_RF_PROTOCOL_ERROR;
419         }
420
421         return NCI_STATUS_OK;
422 }
423
424 static void nci_target_auto_activated(struct nci_dev *ndev,
425                                       struct nci_rf_intf_activated_ntf *ntf)
426 {
427         struct nfc_target *target;
428         int rc;
429
430         target = &ndev->targets[ndev->n_targets];
431
432         rc = nci_add_new_protocol(ndev, target, ntf->rf_protocol,
433                                   ntf->activation_rf_tech_and_mode,
434                                   &ntf->rf_tech_specific_params);
435         if (rc)
436                 return;
437
438         target->logical_idx = ntf->rf_discovery_id;
439         ndev->n_targets++;
440
441         pr_debug("logical idx %d, n_targets %d\n",
442                  target->logical_idx, ndev->n_targets);
443
444         nfc_targets_found(ndev->nfc_dev, ndev->targets, ndev->n_targets);
445 }
446
447 static void nci_rf_intf_activated_ntf_packet(struct nci_dev *ndev,
448                                              struct sk_buff *skb)
449 {
450         struct nci_rf_intf_activated_ntf ntf;
451         __u8 *data = skb->data;
452         int err = NCI_STATUS_OK;
453
454         ntf.rf_discovery_id = *data++;
455         ntf.rf_interface = *data++;
456         ntf.rf_protocol = *data++;
457         ntf.activation_rf_tech_and_mode = *data++;
458         ntf.max_data_pkt_payload_size = *data++;
459         ntf.initial_num_credits = *data++;
460         ntf.rf_tech_specific_params_len = *data++;
461
462         pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id);
463         pr_debug("rf_interface 0x%x\n", ntf.rf_interface);
464         pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol);
465         pr_debug("activation_rf_tech_and_mode 0x%x\n",
466                  ntf.activation_rf_tech_and_mode);
467         pr_debug("max_data_pkt_payload_size 0x%x\n",
468                  ntf.max_data_pkt_payload_size);
469         pr_debug("initial_num_credits 0x%x\n",
470                  ntf.initial_num_credits);
471         pr_debug("rf_tech_specific_params_len %d\n",
472                  ntf.rf_tech_specific_params_len);
473
474         if (ntf.rf_tech_specific_params_len > 0) {
475                 switch (ntf.activation_rf_tech_and_mode) {
476                 case NCI_NFC_A_PASSIVE_POLL_MODE:
477                         data = nci_extract_rf_params_nfca_passive_poll(ndev,
478                                 &(ntf.rf_tech_specific_params.nfca_poll), data);
479                         break;
480
481                 case NCI_NFC_B_PASSIVE_POLL_MODE:
482                         data = nci_extract_rf_params_nfcb_passive_poll(ndev,
483                                 &(ntf.rf_tech_specific_params.nfcb_poll), data);
484                         break;
485
486                 case NCI_NFC_F_PASSIVE_POLL_MODE:
487                         data = nci_extract_rf_params_nfcf_passive_poll(ndev,
488                                 &(ntf.rf_tech_specific_params.nfcf_poll), data);
489                         break;
490
491                 case NCI_NFC_V_PASSIVE_POLL_MODE:
492                         data = nci_extract_rf_params_nfcv_passive_poll(ndev,
493                                 &(ntf.rf_tech_specific_params.nfcv_poll), data);
494                         break;
495
496                 default:
497                         pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
498                                ntf.activation_rf_tech_and_mode);
499                         err = NCI_STATUS_RF_PROTOCOL_ERROR;
500                         goto exit;
501                 }
502         }
503
504         ntf.data_exch_rf_tech_and_mode = *data++;
505         ntf.data_exch_tx_bit_rate = *data++;
506         ntf.data_exch_rx_bit_rate = *data++;
507         ntf.activation_params_len = *data++;
508
509         pr_debug("data_exch_rf_tech_and_mode 0x%x\n",
510                  ntf.data_exch_rf_tech_and_mode);
511         pr_debug("data_exch_tx_bit_rate 0x%x\n", ntf.data_exch_tx_bit_rate);
512         pr_debug("data_exch_rx_bit_rate 0x%x\n", ntf.data_exch_rx_bit_rate);
513         pr_debug("activation_params_len %d\n", ntf.activation_params_len);
514
515         if (ntf.activation_params_len > 0) {
516                 switch (ntf.rf_interface) {
517                 case NCI_RF_INTERFACE_ISO_DEP:
518                         err = nci_extract_activation_params_iso_dep(ndev,
519                                                                     &ntf, data);
520                         break;
521
522                 case NCI_RF_INTERFACE_NFC_DEP:
523                         err = nci_extract_activation_params_nfc_dep(ndev,
524                                                                     &ntf, data);
525                         break;
526
527                 case NCI_RF_INTERFACE_FRAME:
528                         /* no activation params */
529                         break;
530
531                 default:
532                         pr_err("unsupported rf_interface 0x%x\n",
533                                ntf.rf_interface);
534                         err = NCI_STATUS_RF_PROTOCOL_ERROR;
535                         break;
536                 }
537         }
538
539 exit:
540         if (err == NCI_STATUS_OK) {
541                 ndev->max_data_pkt_payload_size = ntf.max_data_pkt_payload_size;
542                 ndev->initial_num_credits = ntf.initial_num_credits;
543
544                 /* set the available credits to initial value */
545                 atomic_set(&ndev->credits_cnt, ndev->initial_num_credits);
546
547                 /* store general bytes to be reported later in dep_link_up */
548                 if (ntf.rf_interface == NCI_RF_INTERFACE_NFC_DEP) {
549                         ndev->remote_gb_len = 0;
550
551                         if (ntf.activation_params_len > 0) {
552                                 /* ATR_RES general bytes at offset 15 */
553                                 ndev->remote_gb_len = min_t(__u8,
554                                         (ntf.activation_params
555                                         .poll_nfc_dep.atr_res_len
556                                         - NFC_ATR_RES_GT_OFFSET),
557                                         NFC_MAX_GT_LEN);
558                                 memcpy(ndev->remote_gb,
559                                        (ntf.activation_params.poll_nfc_dep
560                                        .atr_res + NFC_ATR_RES_GT_OFFSET),
561                                        ndev->remote_gb_len);
562                         }
563                 }
564         }
565
566         if (atomic_read(&ndev->state) == NCI_DISCOVERY) {
567                 /* A single target was found and activated automatically */
568                 atomic_set(&ndev->state, NCI_POLL_ACTIVE);
569                 if (err == NCI_STATUS_OK)
570                         nci_target_auto_activated(ndev, &ntf);
571         } else {        /* ndev->state == NCI_W4_HOST_SELECT */
572                 /* A selected target was activated, so complete the request */
573                 atomic_set(&ndev->state, NCI_POLL_ACTIVE);
574                 nci_req_complete(ndev, err);
575         }
576 }
577
578 static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev,
579                                          struct sk_buff *skb)
580 {
581         struct nci_rf_deactivate_ntf *ntf = (void *) skb->data;
582
583         pr_debug("entry, type 0x%x, reason 0x%x\n", ntf->type, ntf->reason);
584
585         /* drop tx data queue */
586         skb_queue_purge(&ndev->tx_q);
587
588         /* drop partial rx data packet */
589         if (ndev->rx_data_reassembly) {
590                 kfree_skb(ndev->rx_data_reassembly);
591                 ndev->rx_data_reassembly = NULL;
592         }
593
594         /* complete the data exchange transaction, if exists */
595         if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
596                 nci_data_exchange_complete(ndev, NULL, -EIO);
597
598         nci_clear_target_list(ndev);
599         atomic_set(&ndev->state, NCI_IDLE);
600         nci_req_complete(ndev, NCI_STATUS_OK);
601 }
602
603 void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb)
604 {
605         __u16 ntf_opcode = nci_opcode(skb->data);
606
607         pr_debug("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
608                  nci_pbf(skb->data),
609                  nci_opcode_gid(ntf_opcode),
610                  nci_opcode_oid(ntf_opcode),
611                  nci_plen(skb->data));
612
613         /* strip the nci control header */
614         skb_pull(skb, NCI_CTRL_HDR_SIZE);
615
616         switch (ntf_opcode) {
617         case NCI_OP_CORE_CONN_CREDITS_NTF:
618                 nci_core_conn_credits_ntf_packet(ndev, skb);
619                 break;
620
621         case NCI_OP_CORE_GENERIC_ERROR_NTF:
622                 nci_core_generic_error_ntf_packet(ndev, skb);
623                 break;
624
625         case NCI_OP_CORE_INTF_ERROR_NTF:
626                 nci_core_conn_intf_error_ntf_packet(ndev, skb);
627                 break;
628
629         case NCI_OP_RF_DISCOVER_NTF:
630                 nci_rf_discover_ntf_packet(ndev, skb);
631                 break;
632
633         case NCI_OP_RF_INTF_ACTIVATED_NTF:
634                 nci_rf_intf_activated_ntf_packet(ndev, skb);
635                 break;
636
637         case NCI_OP_RF_DEACTIVATE_NTF:
638                 nci_rf_deactivate_ntf_packet(ndev, skb);
639                 break;
640
641         default:
642                 pr_err("unknown ntf opcode 0x%x\n", ntf_opcode);
643                 break;
644         }
645
646         kfree_skb(skb);
647 }