Mention branches and keyring.
[releases.git] / nfc / nci / ntf.c
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) 2014 Marvell International Ltd.
7  *  Copyright (C) 2011 Texas Instruments, Inc.
8  *
9  *  Written by Ilan Elias <ilane@ti.com>
10  *
11  *  Acknowledgements:
12  *  This file is based on hci_event.c, which was written
13  *  by Maxim Krasnyansky.
14  */
15
16 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
17
18 #include <linux/types.h>
19 #include <linux/interrupt.h>
20 #include <linux/bitops.h>
21 #include <linux/skbuff.h>
22
23 #include "../nfc.h"
24 #include <net/nfc/nci.h>
25 #include <net/nfc/nci_core.h>
26 #include <linux/nfc.h>
27
28 /* Handle NCI Notification packets */
29
30 static void nci_core_reset_ntf_packet(struct nci_dev *ndev,
31                                       const struct sk_buff *skb)
32 {
33         /* Handle NCI 2.x core reset notification */
34         const struct nci_core_reset_ntf *ntf = (void *)skb->data;
35
36         ndev->nci_ver = ntf->nci_ver;
37         pr_debug("nci_ver 0x%x, config_status 0x%x\n",
38                  ntf->nci_ver, ntf->config_status);
39
40         ndev->manufact_id = ntf->manufact_id;
41         ndev->manufact_specific_info =
42                 __le32_to_cpu(ntf->manufact_specific_info);
43
44         nci_req_complete(ndev, NCI_STATUS_OK);
45 }
46
47 static void nci_core_conn_credits_ntf_packet(struct nci_dev *ndev,
48                                              struct sk_buff *skb)
49 {
50         struct nci_core_conn_credit_ntf *ntf = (void *) skb->data;
51         struct nci_conn_info *conn_info;
52         int i;
53
54         pr_debug("num_entries %d\n", ntf->num_entries);
55
56         if (ntf->num_entries > NCI_MAX_NUM_CONN)
57                 ntf->num_entries = NCI_MAX_NUM_CONN;
58
59         /* update the credits */
60         for (i = 0; i < ntf->num_entries; i++) {
61                 ntf->conn_entries[i].conn_id =
62                         nci_conn_id(&ntf->conn_entries[i].conn_id);
63
64                 pr_debug("entry[%d]: conn_id %d, credits %d\n",
65                          i, ntf->conn_entries[i].conn_id,
66                          ntf->conn_entries[i].credits);
67
68                 conn_info = nci_get_conn_info_by_conn_id(ndev,
69                                                          ntf->conn_entries[i].conn_id);
70                 if (!conn_info)
71                         return;
72
73                 atomic_add(ntf->conn_entries[i].credits,
74                            &conn_info->credits_cnt);
75         }
76
77         /* trigger the next tx */
78         if (!skb_queue_empty(&ndev->tx_q))
79                 queue_work(ndev->tx_wq, &ndev->tx_work);
80 }
81
82 static void nci_core_generic_error_ntf_packet(struct nci_dev *ndev,
83                                               const struct sk_buff *skb)
84 {
85         __u8 status = skb->data[0];
86
87         pr_debug("status 0x%x\n", status);
88
89         if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
90                 /* Activation failed, so complete the request
91                    (the state remains the same) */
92                 nci_req_complete(ndev, status);
93         }
94 }
95
96 static void nci_core_conn_intf_error_ntf_packet(struct nci_dev *ndev,
97                                                 struct sk_buff *skb)
98 {
99         struct nci_core_intf_error_ntf *ntf = (void *) skb->data;
100
101         ntf->conn_id = nci_conn_id(&ntf->conn_id);
102
103         pr_debug("status 0x%x, conn_id %d\n", ntf->status, ntf->conn_id);
104
105         /* complete the data exchange transaction, if exists */
106         if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
107                 nci_data_exchange_complete(ndev, NULL, ntf->conn_id, -EIO);
108 }
109
110 static const __u8 *
111 nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev,
112                                         struct rf_tech_specific_params_nfca_poll *nfca_poll,
113                                         const __u8 *data)
114 {
115         nfca_poll->sens_res = __le16_to_cpu(*((__le16 *)data));
116         data += 2;
117
118         nfca_poll->nfcid1_len = min_t(__u8, *data++, NFC_NFCID1_MAXSIZE);
119
120         pr_debug("sens_res 0x%x, nfcid1_len %d\n",
121                  nfca_poll->sens_res, nfca_poll->nfcid1_len);
122
123         memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len);
124         data += nfca_poll->nfcid1_len;
125
126         nfca_poll->sel_res_len = *data++;
127
128         if (nfca_poll->sel_res_len != 0)
129                 nfca_poll->sel_res = *data++;
130
131         pr_debug("sel_res_len %d, sel_res 0x%x\n",
132                  nfca_poll->sel_res_len,
133                  nfca_poll->sel_res);
134
135         return data;
136 }
137
138 static const __u8 *
139 nci_extract_rf_params_nfcb_passive_poll(struct nci_dev *ndev,
140                                         struct rf_tech_specific_params_nfcb_poll *nfcb_poll,
141                                         const __u8 *data)
142 {
143         nfcb_poll->sensb_res_len = min_t(__u8, *data++, NFC_SENSB_RES_MAXSIZE);
144
145         pr_debug("sensb_res_len %d\n", nfcb_poll->sensb_res_len);
146
147         memcpy(nfcb_poll->sensb_res, data, nfcb_poll->sensb_res_len);
148         data += nfcb_poll->sensb_res_len;
149
150         return data;
151 }
152
153 static const __u8 *
154 nci_extract_rf_params_nfcf_passive_poll(struct nci_dev *ndev,
155                                         struct rf_tech_specific_params_nfcf_poll *nfcf_poll,
156                                         const __u8 *data)
157 {
158         nfcf_poll->bit_rate = *data++;
159         nfcf_poll->sensf_res_len = min_t(__u8, *data++, NFC_SENSF_RES_MAXSIZE);
160
161         pr_debug("bit_rate %d, sensf_res_len %d\n",
162                  nfcf_poll->bit_rate, nfcf_poll->sensf_res_len);
163
164         memcpy(nfcf_poll->sensf_res, data, nfcf_poll->sensf_res_len);
165         data += nfcf_poll->sensf_res_len;
166
167         return data;
168 }
169
170 static const __u8 *
171 nci_extract_rf_params_nfcv_passive_poll(struct nci_dev *ndev,
172                                         struct rf_tech_specific_params_nfcv_poll *nfcv_poll,
173                                         const __u8 *data)
174 {
175         ++data;
176         nfcv_poll->dsfid = *data++;
177         memcpy(nfcv_poll->uid, data, NFC_ISO15693_UID_MAXSIZE);
178         data += NFC_ISO15693_UID_MAXSIZE;
179         return data;
180 }
181
182 static const __u8 *
183 nci_extract_rf_params_nfcf_passive_listen(struct nci_dev *ndev,
184                                           struct rf_tech_specific_params_nfcf_listen *nfcf_listen,
185                                           const __u8 *data)
186 {
187         nfcf_listen->local_nfcid2_len = min_t(__u8, *data++,
188                                               NFC_NFCID2_MAXSIZE);
189         memcpy(nfcf_listen->local_nfcid2, data, nfcf_listen->local_nfcid2_len);
190         data += nfcf_listen->local_nfcid2_len;
191
192         return data;
193 }
194
195 static __u32 nci_get_prop_rf_protocol(struct nci_dev *ndev, __u8 rf_protocol)
196 {
197         if (ndev->ops->get_rfprotocol)
198                 return ndev->ops->get_rfprotocol(ndev, rf_protocol);
199         return 0;
200 }
201
202 static int nci_add_new_protocol(struct nci_dev *ndev,
203                                 struct nfc_target *target,
204                                 __u8 rf_protocol,
205                                 __u8 rf_tech_and_mode,
206                                 const void *params)
207 {
208         const struct rf_tech_specific_params_nfca_poll *nfca_poll;
209         const struct rf_tech_specific_params_nfcb_poll *nfcb_poll;
210         const struct rf_tech_specific_params_nfcf_poll *nfcf_poll;
211         const struct rf_tech_specific_params_nfcv_poll *nfcv_poll;
212         __u32 protocol;
213
214         if (rf_protocol == NCI_RF_PROTOCOL_T1T)
215                 protocol = NFC_PROTO_JEWEL_MASK;
216         else if (rf_protocol == NCI_RF_PROTOCOL_T2T)
217                 protocol = NFC_PROTO_MIFARE_MASK;
218         else if (rf_protocol == NCI_RF_PROTOCOL_ISO_DEP)
219                 if (rf_tech_and_mode == NCI_NFC_A_PASSIVE_POLL_MODE)
220                         protocol = NFC_PROTO_ISO14443_MASK;
221                 else
222                         protocol = NFC_PROTO_ISO14443_B_MASK;
223         else if (rf_protocol == NCI_RF_PROTOCOL_T3T)
224                 protocol = NFC_PROTO_FELICA_MASK;
225         else if (rf_protocol == NCI_RF_PROTOCOL_NFC_DEP)
226                 protocol = NFC_PROTO_NFC_DEP_MASK;
227         else if (rf_protocol == NCI_RF_PROTOCOL_T5T)
228                 protocol = NFC_PROTO_ISO15693_MASK;
229         else
230                 protocol = nci_get_prop_rf_protocol(ndev, rf_protocol);
231
232         if (!(protocol & ndev->poll_prots)) {
233                 pr_err("the target found does not have the desired protocol\n");
234                 return -EPROTO;
235         }
236
237         if (rf_tech_and_mode == NCI_NFC_A_PASSIVE_POLL_MODE) {
238                 nfca_poll = (struct rf_tech_specific_params_nfca_poll *)params;
239
240                 target->sens_res = nfca_poll->sens_res;
241                 target->sel_res = nfca_poll->sel_res;
242                 target->nfcid1_len = nfca_poll->nfcid1_len;
243                 if (target->nfcid1_len > 0) {
244                         memcpy(target->nfcid1, nfca_poll->nfcid1,
245                                target->nfcid1_len);
246                 }
247         } else if (rf_tech_and_mode == NCI_NFC_B_PASSIVE_POLL_MODE) {
248                 nfcb_poll = (struct rf_tech_specific_params_nfcb_poll *)params;
249
250                 target->sensb_res_len = nfcb_poll->sensb_res_len;
251                 if (target->sensb_res_len > 0) {
252                         memcpy(target->sensb_res, nfcb_poll->sensb_res,
253                                target->sensb_res_len);
254                 }
255         } else if (rf_tech_and_mode == NCI_NFC_F_PASSIVE_POLL_MODE) {
256                 nfcf_poll = (struct rf_tech_specific_params_nfcf_poll *)params;
257
258                 target->sensf_res_len = nfcf_poll->sensf_res_len;
259                 if (target->sensf_res_len > 0) {
260                         memcpy(target->sensf_res, nfcf_poll->sensf_res,
261                                target->sensf_res_len);
262                 }
263         } else if (rf_tech_and_mode == NCI_NFC_V_PASSIVE_POLL_MODE) {
264                 nfcv_poll = (struct rf_tech_specific_params_nfcv_poll *)params;
265
266                 target->is_iso15693 = 1;
267                 target->iso15693_dsfid = nfcv_poll->dsfid;
268                 memcpy(target->iso15693_uid, nfcv_poll->uid, NFC_ISO15693_UID_MAXSIZE);
269         } else {
270                 pr_err("unsupported rf_tech_and_mode 0x%x\n", rf_tech_and_mode);
271                 return -EPROTO;
272         }
273
274         target->supported_protocols |= protocol;
275
276         pr_debug("protocol 0x%x\n", protocol);
277
278         return 0;
279 }
280
281 static void nci_add_new_target(struct nci_dev *ndev,
282                                const struct nci_rf_discover_ntf *ntf)
283 {
284         struct nfc_target *target;
285         int i, rc;
286
287         for (i = 0; i < ndev->n_targets; i++) {
288                 target = &ndev->targets[i];
289                 if (target->logical_idx == ntf->rf_discovery_id) {
290                         /* This target already exists, add the new protocol */
291                         nci_add_new_protocol(ndev, target, ntf->rf_protocol,
292                                              ntf->rf_tech_and_mode,
293                                              &ntf->rf_tech_specific_params);
294                         return;
295                 }
296         }
297
298         /* This is a new target, check if we've enough room */
299         if (ndev->n_targets == NCI_MAX_DISCOVERED_TARGETS) {
300                 pr_debug("not enough room, ignoring new target...\n");
301                 return;
302         }
303
304         target = &ndev->targets[ndev->n_targets];
305
306         rc = nci_add_new_protocol(ndev, target, ntf->rf_protocol,
307                                   ntf->rf_tech_and_mode,
308                                   &ntf->rf_tech_specific_params);
309         if (!rc) {
310                 target->logical_idx = ntf->rf_discovery_id;
311                 ndev->n_targets++;
312
313                 pr_debug("logical idx %d, n_targets %d\n", target->logical_idx,
314                          ndev->n_targets);
315         }
316 }
317
318 void nci_clear_target_list(struct nci_dev *ndev)
319 {
320         memset(ndev->targets, 0,
321                (sizeof(struct nfc_target)*NCI_MAX_DISCOVERED_TARGETS));
322
323         ndev->n_targets = 0;
324 }
325
326 static void nci_rf_discover_ntf_packet(struct nci_dev *ndev,
327                                        const struct sk_buff *skb)
328 {
329         struct nci_rf_discover_ntf ntf;
330         const __u8 *data = skb->data;
331         bool add_target = true;
332
333         ntf.rf_discovery_id = *data++;
334         ntf.rf_protocol = *data++;
335         ntf.rf_tech_and_mode = *data++;
336         ntf.rf_tech_specific_params_len = *data++;
337
338         pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id);
339         pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol);
340         pr_debug("rf_tech_and_mode 0x%x\n", ntf.rf_tech_and_mode);
341         pr_debug("rf_tech_specific_params_len %d\n",
342                  ntf.rf_tech_specific_params_len);
343
344         if (ntf.rf_tech_specific_params_len > 0) {
345                 switch (ntf.rf_tech_and_mode) {
346                 case NCI_NFC_A_PASSIVE_POLL_MODE:
347                         data = nci_extract_rf_params_nfca_passive_poll(ndev,
348                                 &(ntf.rf_tech_specific_params.nfca_poll), data);
349                         break;
350
351                 case NCI_NFC_B_PASSIVE_POLL_MODE:
352                         data = nci_extract_rf_params_nfcb_passive_poll(ndev,
353                                 &(ntf.rf_tech_specific_params.nfcb_poll), data);
354                         break;
355
356                 case NCI_NFC_F_PASSIVE_POLL_MODE:
357                         data = nci_extract_rf_params_nfcf_passive_poll(ndev,
358                                 &(ntf.rf_tech_specific_params.nfcf_poll), data);
359                         break;
360
361                 case NCI_NFC_V_PASSIVE_POLL_MODE:
362                         data = nci_extract_rf_params_nfcv_passive_poll(ndev,
363                                 &(ntf.rf_tech_specific_params.nfcv_poll), data);
364                         break;
365
366                 default:
367                         pr_err("unsupported rf_tech_and_mode 0x%x\n",
368                                ntf.rf_tech_and_mode);
369                         data += ntf.rf_tech_specific_params_len;
370                         add_target = false;
371                 }
372         }
373
374         ntf.ntf_type = *data++;
375         pr_debug("ntf_type %d\n", ntf.ntf_type);
376
377         if (add_target == true)
378                 nci_add_new_target(ndev, &ntf);
379
380         if (ntf.ntf_type == NCI_DISCOVER_NTF_TYPE_MORE) {
381                 atomic_set(&ndev->state, NCI_W4_ALL_DISCOVERIES);
382         } else {
383                 atomic_set(&ndev->state, NCI_W4_HOST_SELECT);
384                 nfc_targets_found(ndev->nfc_dev, ndev->targets,
385                                   ndev->n_targets);
386         }
387 }
388
389 static int nci_extract_activation_params_iso_dep(struct nci_dev *ndev,
390                                                  struct nci_rf_intf_activated_ntf *ntf,
391                                                  const __u8 *data)
392 {
393         struct activation_params_nfca_poll_iso_dep *nfca_poll;
394         struct activation_params_nfcb_poll_iso_dep *nfcb_poll;
395
396         switch (ntf->activation_rf_tech_and_mode) {
397         case NCI_NFC_A_PASSIVE_POLL_MODE:
398                 nfca_poll = &ntf->activation_params.nfca_poll_iso_dep;
399                 nfca_poll->rats_res_len = min_t(__u8, *data++, 20);
400                 pr_debug("rats_res_len %d\n", nfca_poll->rats_res_len);
401                 if (nfca_poll->rats_res_len > 0) {
402                         memcpy(nfca_poll->rats_res,
403                                data, nfca_poll->rats_res_len);
404                 }
405                 break;
406
407         case NCI_NFC_B_PASSIVE_POLL_MODE:
408                 nfcb_poll = &ntf->activation_params.nfcb_poll_iso_dep;
409                 nfcb_poll->attrib_res_len = min_t(__u8, *data++, 50);
410                 pr_debug("attrib_res_len %d\n", nfcb_poll->attrib_res_len);
411                 if (nfcb_poll->attrib_res_len > 0) {
412                         memcpy(nfcb_poll->attrib_res,
413                                data, nfcb_poll->attrib_res_len);
414                 }
415                 break;
416
417         default:
418                 pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
419                        ntf->activation_rf_tech_and_mode);
420                 return NCI_STATUS_RF_PROTOCOL_ERROR;
421         }
422
423         return NCI_STATUS_OK;
424 }
425
426 static int nci_extract_activation_params_nfc_dep(struct nci_dev *ndev,
427                                                  struct nci_rf_intf_activated_ntf *ntf,
428                                                  const __u8 *data)
429 {
430         struct activation_params_poll_nfc_dep *poll;
431         struct activation_params_listen_nfc_dep *listen;
432
433         switch (ntf->activation_rf_tech_and_mode) {
434         case NCI_NFC_A_PASSIVE_POLL_MODE:
435         case NCI_NFC_F_PASSIVE_POLL_MODE:
436                 poll = &ntf->activation_params.poll_nfc_dep;
437                 poll->atr_res_len = min_t(__u8, *data++,
438                                           NFC_ATR_RES_MAXSIZE - 2);
439                 pr_debug("atr_res_len %d\n", poll->atr_res_len);
440                 if (poll->atr_res_len > 0)
441                         memcpy(poll->atr_res, data, poll->atr_res_len);
442                 break;
443
444         case NCI_NFC_A_PASSIVE_LISTEN_MODE:
445         case NCI_NFC_F_PASSIVE_LISTEN_MODE:
446                 listen = &ntf->activation_params.listen_nfc_dep;
447                 listen->atr_req_len = min_t(__u8, *data++,
448                                             NFC_ATR_REQ_MAXSIZE - 2);
449                 pr_debug("atr_req_len %d\n", listen->atr_req_len);
450                 if (listen->atr_req_len > 0)
451                         memcpy(listen->atr_req, data, listen->atr_req_len);
452                 break;
453
454         default:
455                 pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
456                        ntf->activation_rf_tech_and_mode);
457                 return NCI_STATUS_RF_PROTOCOL_ERROR;
458         }
459
460         return NCI_STATUS_OK;
461 }
462
463 static void nci_target_auto_activated(struct nci_dev *ndev,
464                                       const struct nci_rf_intf_activated_ntf *ntf)
465 {
466         struct nfc_target *target;
467         int rc;
468
469         target = &ndev->targets[ndev->n_targets];
470
471         rc = nci_add_new_protocol(ndev, target, ntf->rf_protocol,
472                                   ntf->activation_rf_tech_and_mode,
473                                   &ntf->rf_tech_specific_params);
474         if (rc)
475                 return;
476
477         target->logical_idx = ntf->rf_discovery_id;
478         ndev->n_targets++;
479
480         pr_debug("logical idx %d, n_targets %d\n",
481                  target->logical_idx, ndev->n_targets);
482
483         nfc_targets_found(ndev->nfc_dev, ndev->targets, ndev->n_targets);
484 }
485
486 static int nci_store_general_bytes_nfc_dep(struct nci_dev *ndev,
487                                            const struct nci_rf_intf_activated_ntf *ntf)
488 {
489         ndev->remote_gb_len = 0;
490
491         if (ntf->activation_params_len <= 0)
492                 return NCI_STATUS_OK;
493
494         switch (ntf->activation_rf_tech_and_mode) {
495         case NCI_NFC_A_PASSIVE_POLL_MODE:
496         case NCI_NFC_F_PASSIVE_POLL_MODE:
497                 ndev->remote_gb_len = min_t(__u8,
498                         (ntf->activation_params.poll_nfc_dep.atr_res_len
499                                                 - NFC_ATR_RES_GT_OFFSET),
500                         NFC_ATR_RES_GB_MAXSIZE);
501                 memcpy(ndev->remote_gb,
502                        (ntf->activation_params.poll_nfc_dep.atr_res
503                                                 + NFC_ATR_RES_GT_OFFSET),
504                        ndev->remote_gb_len);
505                 break;
506
507         case NCI_NFC_A_PASSIVE_LISTEN_MODE:
508         case NCI_NFC_F_PASSIVE_LISTEN_MODE:
509                 ndev->remote_gb_len = min_t(__u8,
510                         (ntf->activation_params.listen_nfc_dep.atr_req_len
511                                                 - NFC_ATR_REQ_GT_OFFSET),
512                         NFC_ATR_REQ_GB_MAXSIZE);
513                 memcpy(ndev->remote_gb,
514                        (ntf->activation_params.listen_nfc_dep.atr_req
515                                                 + NFC_ATR_REQ_GT_OFFSET),
516                        ndev->remote_gb_len);
517                 break;
518
519         default:
520                 pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
521                        ntf->activation_rf_tech_and_mode);
522                 return NCI_STATUS_RF_PROTOCOL_ERROR;
523         }
524
525         return NCI_STATUS_OK;
526 }
527
528 static void nci_rf_intf_activated_ntf_packet(struct nci_dev *ndev,
529                                              const struct sk_buff *skb)
530 {
531         struct nci_conn_info *conn_info;
532         struct nci_rf_intf_activated_ntf ntf;
533         const __u8 *data = skb->data;
534         int err = NCI_STATUS_OK;
535
536         ntf.rf_discovery_id = *data++;
537         ntf.rf_interface = *data++;
538         ntf.rf_protocol = *data++;
539         ntf.activation_rf_tech_and_mode = *data++;
540         ntf.max_data_pkt_payload_size = *data++;
541         ntf.initial_num_credits = *data++;
542         ntf.rf_tech_specific_params_len = *data++;
543
544         pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id);
545         pr_debug("rf_interface 0x%x\n", ntf.rf_interface);
546         pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol);
547         pr_debug("activation_rf_tech_and_mode 0x%x\n",
548                  ntf.activation_rf_tech_and_mode);
549         pr_debug("max_data_pkt_payload_size 0x%x\n",
550                  ntf.max_data_pkt_payload_size);
551         pr_debug("initial_num_credits 0x%x\n",
552                  ntf.initial_num_credits);
553         pr_debug("rf_tech_specific_params_len %d\n",
554                  ntf.rf_tech_specific_params_len);
555
556         /* If this contains a value of 0x00 (NFCEE Direct RF
557          * Interface) then all following parameters SHALL contain a
558          * value of 0 and SHALL be ignored.
559          */
560         if (ntf.rf_interface == NCI_RF_INTERFACE_NFCEE_DIRECT)
561                 goto listen;
562
563         if (ntf.rf_tech_specific_params_len > 0) {
564                 switch (ntf.activation_rf_tech_and_mode) {
565                 case NCI_NFC_A_PASSIVE_POLL_MODE:
566                         data = nci_extract_rf_params_nfca_passive_poll(ndev,
567                                 &(ntf.rf_tech_specific_params.nfca_poll), data);
568                         break;
569
570                 case NCI_NFC_B_PASSIVE_POLL_MODE:
571                         data = nci_extract_rf_params_nfcb_passive_poll(ndev,
572                                 &(ntf.rf_tech_specific_params.nfcb_poll), data);
573                         break;
574
575                 case NCI_NFC_F_PASSIVE_POLL_MODE:
576                         data = nci_extract_rf_params_nfcf_passive_poll(ndev,
577                                 &(ntf.rf_tech_specific_params.nfcf_poll), data);
578                         break;
579
580                 case NCI_NFC_V_PASSIVE_POLL_MODE:
581                         data = nci_extract_rf_params_nfcv_passive_poll(ndev,
582                                 &(ntf.rf_tech_specific_params.nfcv_poll), data);
583                         break;
584
585                 case NCI_NFC_A_PASSIVE_LISTEN_MODE:
586                         /* no RF technology specific parameters */
587                         break;
588
589                 case NCI_NFC_F_PASSIVE_LISTEN_MODE:
590                         data = nci_extract_rf_params_nfcf_passive_listen(ndev,
591                                 &(ntf.rf_tech_specific_params.nfcf_listen),
592                                 data);
593                         break;
594
595                 default:
596                         pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
597                                ntf.activation_rf_tech_and_mode);
598                         err = NCI_STATUS_RF_PROTOCOL_ERROR;
599                         goto exit;
600                 }
601         }
602
603         ntf.data_exch_rf_tech_and_mode = *data++;
604         ntf.data_exch_tx_bit_rate = *data++;
605         ntf.data_exch_rx_bit_rate = *data++;
606         ntf.activation_params_len = *data++;
607
608         pr_debug("data_exch_rf_tech_and_mode 0x%x\n",
609                  ntf.data_exch_rf_tech_and_mode);
610         pr_debug("data_exch_tx_bit_rate 0x%x\n", ntf.data_exch_tx_bit_rate);
611         pr_debug("data_exch_rx_bit_rate 0x%x\n", ntf.data_exch_rx_bit_rate);
612         pr_debug("activation_params_len %d\n", ntf.activation_params_len);
613
614         if (ntf.activation_params_len > 0) {
615                 switch (ntf.rf_interface) {
616                 case NCI_RF_INTERFACE_ISO_DEP:
617                         err = nci_extract_activation_params_iso_dep(ndev,
618                                                                     &ntf, data);
619                         break;
620
621                 case NCI_RF_INTERFACE_NFC_DEP:
622                         err = nci_extract_activation_params_nfc_dep(ndev,
623                                                                     &ntf, data);
624                         break;
625
626                 case NCI_RF_INTERFACE_FRAME:
627                         /* no activation params */
628                         break;
629
630                 default:
631                         pr_err("unsupported rf_interface 0x%x\n",
632                                ntf.rf_interface);
633                         err = NCI_STATUS_RF_PROTOCOL_ERROR;
634                         break;
635                 }
636         }
637
638 exit:
639         if (err == NCI_STATUS_OK) {
640                 conn_info = ndev->rf_conn_info;
641                 if (!conn_info)
642                         return;
643
644                 conn_info->max_pkt_payload_len = ntf.max_data_pkt_payload_size;
645                 conn_info->initial_num_credits = ntf.initial_num_credits;
646
647                 /* set the available credits to initial value */
648                 atomic_set(&conn_info->credits_cnt,
649                            conn_info->initial_num_credits);
650
651                 /* store general bytes to be reported later in dep_link_up */
652                 if (ntf.rf_interface == NCI_RF_INTERFACE_NFC_DEP) {
653                         err = nci_store_general_bytes_nfc_dep(ndev, &ntf);
654                         if (err != NCI_STATUS_OK)
655                                 pr_err("unable to store general bytes\n");
656                 }
657         }
658
659         if (!(ntf.activation_rf_tech_and_mode & NCI_RF_TECH_MODE_LISTEN_MASK)) {
660                 /* Poll mode */
661                 if (atomic_read(&ndev->state) == NCI_DISCOVERY) {
662                         /* A single target was found and activated
663                          * automatically */
664                         atomic_set(&ndev->state, NCI_POLL_ACTIVE);
665                         if (err == NCI_STATUS_OK)
666                                 nci_target_auto_activated(ndev, &ntf);
667                 } else {        /* ndev->state == NCI_W4_HOST_SELECT */
668                         /* A selected target was activated, so complete the
669                          * request */
670                         atomic_set(&ndev->state, NCI_POLL_ACTIVE);
671                         nci_req_complete(ndev, err);
672                 }
673         } else {
674 listen:
675                 /* Listen mode */
676                 atomic_set(&ndev->state, NCI_LISTEN_ACTIVE);
677                 if (err == NCI_STATUS_OK &&
678                     ntf.rf_protocol == NCI_RF_PROTOCOL_NFC_DEP) {
679                         err = nfc_tm_activated(ndev->nfc_dev,
680                                                NFC_PROTO_NFC_DEP_MASK,
681                                                NFC_COMM_PASSIVE,
682                                                ndev->remote_gb,
683                                                ndev->remote_gb_len);
684                         if (err != NCI_STATUS_OK)
685                                 pr_err("error when signaling tm activation\n");
686                 }
687         }
688 }
689
690 static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev,
691                                          const struct sk_buff *skb)
692 {
693         const struct nci_conn_info *conn_info;
694         const struct nci_rf_deactivate_ntf *ntf = (void *)skb->data;
695
696         pr_debug("entry, type 0x%x, reason 0x%x\n", ntf->type, ntf->reason);
697
698         conn_info = ndev->rf_conn_info;
699         if (!conn_info)
700                 return;
701
702         /* drop tx data queue */
703         skb_queue_purge(&ndev->tx_q);
704
705         /* drop partial rx data packet */
706         if (ndev->rx_data_reassembly) {
707                 kfree_skb(ndev->rx_data_reassembly);
708                 ndev->rx_data_reassembly = NULL;
709         }
710
711         /* complete the data exchange transaction, if exists */
712         if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
713                 nci_data_exchange_complete(ndev, NULL, NCI_STATIC_RF_CONN_ID,
714                                            -EIO);
715
716         switch (ntf->type) {
717         case NCI_DEACTIVATE_TYPE_IDLE_MODE:
718                 nci_clear_target_list(ndev);
719                 atomic_set(&ndev->state, NCI_IDLE);
720                 break;
721         case NCI_DEACTIVATE_TYPE_SLEEP_MODE:
722         case NCI_DEACTIVATE_TYPE_SLEEP_AF_MODE:
723                 atomic_set(&ndev->state, NCI_W4_HOST_SELECT);
724                 break;
725         case NCI_DEACTIVATE_TYPE_DISCOVERY:
726                 nci_clear_target_list(ndev);
727                 atomic_set(&ndev->state, NCI_DISCOVERY);
728                 break;
729         }
730
731         nci_req_complete(ndev, NCI_STATUS_OK);
732 }
733
734 static void nci_nfcee_discover_ntf_packet(struct nci_dev *ndev,
735                                           const struct sk_buff *skb)
736 {
737         u8 status = NCI_STATUS_OK;
738         const struct nci_nfcee_discover_ntf *nfcee_ntf =
739                                 (struct nci_nfcee_discover_ntf *)skb->data;
740
741         pr_debug("\n");
742
743         /* NFCForum NCI 9.2.1 HCI Network Specific Handling
744          * If the NFCC supports the HCI Network, it SHALL return one,
745          * and only one, NFCEE_DISCOVER_NTF with a Protocol type of
746          * “HCI Access”, even if the HCI Network contains multiple NFCEEs.
747          */
748         ndev->hci_dev->nfcee_id = nfcee_ntf->nfcee_id;
749         ndev->cur_params.id = nfcee_ntf->nfcee_id;
750
751         nci_req_complete(ndev, status);
752 }
753
754 static void nci_nfcee_action_ntf_packet(struct nci_dev *ndev,
755                                         const struct sk_buff *skb)
756 {
757         pr_debug("\n");
758 }
759
760 void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb)
761 {
762         __u16 ntf_opcode = nci_opcode(skb->data);
763
764         pr_debug("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
765                  nci_pbf(skb->data),
766                  nci_opcode_gid(ntf_opcode),
767                  nci_opcode_oid(ntf_opcode),
768                  nci_plen(skb->data));
769
770         /* strip the nci control header */
771         skb_pull(skb, NCI_CTRL_HDR_SIZE);
772
773         if (nci_opcode_gid(ntf_opcode) == NCI_GID_PROPRIETARY) {
774                 if (nci_prop_ntf_packet(ndev, ntf_opcode, skb) == -ENOTSUPP) {
775                         pr_err("unsupported ntf opcode 0x%x\n",
776                                ntf_opcode);
777                 }
778
779                 goto end;
780         }
781
782         switch (ntf_opcode) {
783         case NCI_OP_CORE_RESET_NTF:
784                 nci_core_reset_ntf_packet(ndev, skb);
785                 break;
786
787         case NCI_OP_CORE_CONN_CREDITS_NTF:
788                 nci_core_conn_credits_ntf_packet(ndev, skb);
789                 break;
790
791         case NCI_OP_CORE_GENERIC_ERROR_NTF:
792                 nci_core_generic_error_ntf_packet(ndev, skb);
793                 break;
794
795         case NCI_OP_CORE_INTF_ERROR_NTF:
796                 nci_core_conn_intf_error_ntf_packet(ndev, skb);
797                 break;
798
799         case NCI_OP_RF_DISCOVER_NTF:
800                 nci_rf_discover_ntf_packet(ndev, skb);
801                 break;
802
803         case NCI_OP_RF_INTF_ACTIVATED_NTF:
804                 nci_rf_intf_activated_ntf_packet(ndev, skb);
805                 break;
806
807         case NCI_OP_RF_DEACTIVATE_NTF:
808                 nci_rf_deactivate_ntf_packet(ndev, skb);
809                 break;
810
811         case NCI_OP_NFCEE_DISCOVER_NTF:
812                 nci_nfcee_discover_ntf_packet(ndev, skb);
813                 break;
814
815         case NCI_OP_RF_NFCEE_ACTION_NTF:
816                 nci_nfcee_action_ntf_packet(ndev, skb);
817                 break;
818
819         default:
820                 pr_err("unknown ntf opcode 0x%x\n", ntf_opcode);
821                 break;
822         }
823
824         nci_core_ntf_packet(ndev, ntf_opcode, skb);
825 end:
826         kfree_skb(skb);
827 }