2 * The NFC Controller Interface is the communication protocol between an
3 * NFC Controller (NFCC) and a Device Host (DH).
5 * Copyright (C) 2011 Texas Instruments, Inc.
7 * Written by Ilan Elias <ilane@ti.com>
10 * This file is based on hci_event.c, which was written
11 * by Maxim Krasnyansky.
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
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.
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
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 #include <linux/types.h>
31 #include <linux/interrupt.h>
32 #include <linux/bitops.h>
33 #include <linux/skbuff.h>
36 #include <net/nfc/nci.h>
37 #include <net/nfc/nci_core.h>
38 #include <linux/nfc.h>
40 /* Handle NCI Notification packets */
42 static void nci_core_conn_credits_ntf_packet(struct nci_dev
*ndev
,
45 struct nci_core_conn_credit_ntf
*ntf
= (void *) skb
->data
;
48 nfc_dbg("entry, num_entries %d", ntf
->num_entries
);
50 if (ntf
->num_entries
> NCI_MAX_NUM_CONN
)
51 ntf
->num_entries
= NCI_MAX_NUM_CONN
;
53 /* update the credits */
54 for (i
= 0; i
< ntf
->num_entries
; i
++) {
55 nfc_dbg("entry[%d]: conn_id %d, credits %d", i
,
56 ntf
->conn_entries
[i
].conn_id
,
57 ntf
->conn_entries
[i
].credits
);
59 if (ntf
->conn_entries
[i
].conn_id
== NCI_STATIC_RF_CONN_ID
) {
60 /* found static rf connection */
61 atomic_add(ntf
->conn_entries
[i
].credits
,
66 /* trigger the next tx */
67 if (!skb_queue_empty(&ndev
->tx_q
))
68 queue_work(ndev
->tx_wq
, &ndev
->tx_work
);
71 static __u8
*nci_extract_rf_params_nfca_passive_poll(struct nci_dev
*ndev
,
72 struct nci_rf_intf_activated_ntf
*ntf
, __u8
*data
)
74 struct rf_tech_specific_params_nfca_poll
*nfca_poll
;
76 nfca_poll
= &ntf
->rf_tech_specific_params
.nfca_poll
;
78 nfca_poll
->sens_res
= __le16_to_cpu(*((__u16
*)data
));
81 nfca_poll
->nfcid1_len
= *data
++;
83 nfc_dbg("sens_res 0x%x, nfcid1_len %d",
85 nfca_poll
->nfcid1_len
);
87 memcpy(nfca_poll
->nfcid1
, data
, nfca_poll
->nfcid1_len
);
88 data
+= nfca_poll
->nfcid1_len
;
90 nfca_poll
->sel_res_len
= *data
++;
92 if (nfca_poll
->sel_res_len
!= 0)
93 nfca_poll
->sel_res
= *data
++;
95 nfc_dbg("sel_res_len %d, sel_res 0x%x",
96 nfca_poll
->sel_res_len
,
102 static int nci_extract_activation_params_iso_dep(struct nci_dev
*ndev
,
103 struct nci_rf_intf_activated_ntf
*ntf
, __u8
*data
)
105 struct activation_params_nfca_poll_iso_dep
*nfca_poll
;
107 switch (ntf
->activation_rf_tech_and_mode
) {
108 case NCI_NFC_A_PASSIVE_POLL_MODE
:
109 nfca_poll
= &ntf
->activation_params
.nfca_poll_iso_dep
;
110 nfca_poll
->rats_res_len
= *data
++;
111 if (nfca_poll
->rats_res_len
> 0) {
112 memcpy(nfca_poll
->rats_res
,
114 nfca_poll
->rats_res_len
);
119 pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
120 ntf
->activation_rf_tech_and_mode
);
127 static void nci_target_found(struct nci_dev
*ndev
,
128 struct nci_rf_intf_activated_ntf
*ntf
)
130 struct nfc_target nfc_tgt
;
132 if (ntf
->rf_protocol
== NCI_RF_PROTOCOL_T2T
) /* T2T MifareUL */
133 nfc_tgt
.supported_protocols
= NFC_PROTO_MIFARE_MASK
;
134 else if (ntf
->rf_protocol
== NCI_RF_PROTOCOL_ISO_DEP
) /* 4A */
135 nfc_tgt
.supported_protocols
= NFC_PROTO_ISO14443_MASK
;
137 nfc_tgt
.supported_protocols
= 0;
139 nfc_tgt
.sens_res
= ntf
->rf_tech_specific_params
.nfca_poll
.sens_res
;
140 nfc_tgt
.sel_res
= ntf
->rf_tech_specific_params
.nfca_poll
.sel_res
;
142 if (!(nfc_tgt
.supported_protocols
& ndev
->poll_prots
)) {
143 nfc_dbg("the target found does not have the desired protocol");
147 nfc_dbg("new target found, supported_protocols 0x%x",
148 nfc_tgt
.supported_protocols
);
150 ndev
->target_available_prots
= nfc_tgt
.supported_protocols
;
152 nfc_targets_found(ndev
->nfc_dev
, &nfc_tgt
, 1);
155 static void nci_rf_intf_activated_ntf_packet(struct nci_dev
*ndev
,
158 struct nci_rf_intf_activated_ntf ntf
;
159 __u8
*data
= skb
->data
;
162 clear_bit(NCI_DISCOVERY
, &ndev
->flags
);
163 set_bit(NCI_POLL_ACTIVE
, &ndev
->flags
);
165 ntf
.rf_discovery_id
= *data
++;
166 ntf
.rf_interface_type
= *data
++;
167 ntf
.rf_protocol
= *data
++;
168 ntf
.activation_rf_tech_and_mode
= *data
++;
169 ntf
.rf_tech_specific_params_len
= *data
++;
171 nfc_dbg("rf_discovery_id %d", ntf
.rf_discovery_id
);
172 nfc_dbg("rf_interface_type 0x%x", ntf
.rf_interface_type
);
173 nfc_dbg("rf_protocol 0x%x", ntf
.rf_protocol
);
174 nfc_dbg("activation_rf_tech_and_mode 0x%x",
175 ntf
.activation_rf_tech_and_mode
);
176 nfc_dbg("rf_tech_specific_params_len %d",
177 ntf
.rf_tech_specific_params_len
);
179 if (ntf
.rf_tech_specific_params_len
> 0) {
180 switch (ntf
.activation_rf_tech_and_mode
) {
181 case NCI_NFC_A_PASSIVE_POLL_MODE
:
182 data
= nci_extract_rf_params_nfca_passive_poll(ndev
,
187 pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
188 ntf
.activation_rf_tech_and_mode
);
193 ntf
.data_exch_rf_tech_and_mode
= *data
++;
194 ntf
.data_exch_tx_bit_rate
= *data
++;
195 ntf
.data_exch_rx_bit_rate
= *data
++;
196 ntf
.activation_params_len
= *data
++;
198 nfc_dbg("data_exch_rf_tech_and_mode 0x%x",
199 ntf
.data_exch_rf_tech_and_mode
);
200 nfc_dbg("data_exch_tx_bit_rate 0x%x",
201 ntf
.data_exch_tx_bit_rate
);
202 nfc_dbg("data_exch_rx_bit_rate 0x%x",
203 ntf
.data_exch_rx_bit_rate
);
204 nfc_dbg("activation_params_len %d",
205 ntf
.activation_params_len
);
207 if (ntf
.activation_params_len
> 0) {
208 switch (ntf
.rf_interface_type
) {
209 case NCI_RF_INTERFACE_ISO_DEP
:
210 err
= nci_extract_activation_params_iso_dep(ndev
,
214 case NCI_RF_INTERFACE_FRAME
:
215 /* no activation params */
219 pr_err("unsupported rf_interface_type 0x%x\n",
220 ntf
.rf_interface_type
);
226 nci_target_found(ndev
, &ntf
);
229 static void nci_rf_deactivate_ntf_packet(struct nci_dev
*ndev
,
232 struct nci_rf_deactivate_ntf
*ntf
= (void *) skb
->data
;
234 nfc_dbg("entry, type 0x%x, reason 0x%x", ntf
->type
, ntf
->reason
);
236 clear_bit(NCI_POLL_ACTIVE
, &ndev
->flags
);
237 ndev
->target_active_prot
= 0;
239 /* drop tx data queue */
240 skb_queue_purge(&ndev
->tx_q
);
242 /* drop partial rx data packet */
243 if (ndev
->rx_data_reassembly
) {
244 kfree_skb(ndev
->rx_data_reassembly
);
245 ndev
->rx_data_reassembly
= 0;
248 /* set the available credits to initial value */
249 atomic_set(&ndev
->credits_cnt
, ndev
->initial_num_credits
);
251 /* complete the data exchange transaction, if exists */
252 if (test_bit(NCI_DATA_EXCHANGE
, &ndev
->flags
))
253 nci_data_exchange_complete(ndev
, NULL
, -EIO
);
256 void nci_ntf_packet(struct nci_dev
*ndev
, struct sk_buff
*skb
)
258 __u16 ntf_opcode
= nci_opcode(skb
->data
);
260 nfc_dbg("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d",
262 nci_opcode_gid(ntf_opcode
),
263 nci_opcode_oid(ntf_opcode
),
264 nci_plen(skb
->data
));
266 /* strip the nci control header */
267 skb_pull(skb
, NCI_CTRL_HDR_SIZE
);
269 switch (ntf_opcode
) {
270 case NCI_OP_CORE_CONN_CREDITS_NTF
:
271 nci_core_conn_credits_ntf_packet(ndev
, skb
);
274 case NCI_OP_RF_INTF_ACTIVATED_NTF
:
275 nci_rf_intf_activated_ntf_packet(ndev
, skb
);
278 case NCI_OP_RF_DEACTIVATE_NTF
:
279 nci_rf_deactivate_ntf_packet(ndev
, skb
);
283 pr_err("unknown ntf opcode 0x%x\n", ntf_opcode
);