xref: /openbmc/linux/net/nfc/nci/ntf.c (revision d5a2ca60)
1 /*
2  *  The NFC Controller Interface is the communication protocol between an
3  *  NFC Controller (NFCC) and a Device Host (DH).
4  *
5  *  Copyright (C) 2011 Texas Instruments, Inc.
6  *
7  *  Written by Ilan Elias <ilane@ti.com>
8  *
9  *  Acknowledgements:
10  *  This file is based on hci_event.c, which was written
11  *  by Maxim Krasnyansky.
12  *
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License version 2
15  *  as published by the Free Software Foundation
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  *
26  */
27 
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_conn_intf_error_ntf_packet(struct nci_dev *ndev,
75 						struct sk_buff *skb)
76 {
77 	struct nci_core_intf_error_ntf *ntf = (void *) skb->data;
78 
79 	ntf->conn_id = nci_conn_id(&ntf->conn_id);
80 
81 	pr_debug("status 0x%x, conn_id %d\n", ntf->status, ntf->conn_id);
82 
83 	/* complete the data exchange transaction, if exists */
84 	if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
85 		nci_data_exchange_complete(ndev, NULL, -EIO);
86 }
87 
88 static __u8 *nci_extract_rf_params_nfca_passive_poll(struct nci_dev *ndev,
89 			struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
90 {
91 	struct rf_tech_specific_params_nfca_poll *nfca_poll;
92 
93 	nfca_poll = &ntf->rf_tech_specific_params.nfca_poll;
94 
95 	nfca_poll->sens_res = __le16_to_cpu(*((__u16 *)data));
96 	data += 2;
97 
98 	nfca_poll->nfcid1_len = *data++;
99 
100 	pr_debug("sens_res 0x%x, nfcid1_len %d\n",
101 		 nfca_poll->sens_res, nfca_poll->nfcid1_len);
102 
103 	memcpy(nfca_poll->nfcid1, data, nfca_poll->nfcid1_len);
104 	data += nfca_poll->nfcid1_len;
105 
106 	nfca_poll->sel_res_len = *data++;
107 
108 	if (nfca_poll->sel_res_len != 0)
109 		nfca_poll->sel_res = *data++;
110 
111 	pr_debug("sel_res_len %d, sel_res 0x%x\n",
112 		 nfca_poll->sel_res_len,
113 		 nfca_poll->sel_res);
114 
115 	return data;
116 }
117 
118 static __u8 *nci_extract_rf_params_nfcb_passive_poll(struct nci_dev *ndev,
119 			struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
120 {
121 	struct rf_tech_specific_params_nfcb_poll *nfcb_poll;
122 
123 	nfcb_poll = &ntf->rf_tech_specific_params.nfcb_poll;
124 
125 	nfcb_poll->sensb_res_len = *data++;
126 
127 	pr_debug("sensb_res_len %d\n", nfcb_poll->sensb_res_len);
128 
129 	memcpy(nfcb_poll->sensb_res, data, nfcb_poll->sensb_res_len);
130 	data += nfcb_poll->sensb_res_len;
131 
132 	return data;
133 }
134 
135 static __u8 *nci_extract_rf_params_nfcf_passive_poll(struct nci_dev *ndev,
136 			struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
137 {
138 	struct rf_tech_specific_params_nfcf_poll *nfcf_poll;
139 
140 	nfcf_poll = &ntf->rf_tech_specific_params.nfcf_poll;
141 
142 	nfcf_poll->bit_rate = *data++;
143 	nfcf_poll->sensf_res_len = *data++;
144 
145 	pr_debug("bit_rate %d, sensf_res_len %d\n",
146 		nfcf_poll->bit_rate, nfcf_poll->sensf_res_len);
147 
148 	memcpy(nfcf_poll->sensf_res, data, nfcf_poll->sensf_res_len);
149 	data += nfcf_poll->sensf_res_len;
150 
151 	return data;
152 }
153 
154 static int nci_extract_activation_params_iso_dep(struct nci_dev *ndev,
155 			struct nci_rf_intf_activated_ntf *ntf, __u8 *data)
156 {
157 	struct activation_params_nfca_poll_iso_dep *nfca_poll;
158 	struct activation_params_nfcb_poll_iso_dep *nfcb_poll;
159 
160 	switch (ntf->activation_rf_tech_and_mode) {
161 	case NCI_NFC_A_PASSIVE_POLL_MODE:
162 		nfca_poll = &ntf->activation_params.nfca_poll_iso_dep;
163 		nfca_poll->rats_res_len = *data++;
164 		pr_debug("rats_res_len %d\n", nfca_poll->rats_res_len);
165 		if (nfca_poll->rats_res_len > 0) {
166 			memcpy(nfca_poll->rats_res,
167 				data,
168 				nfca_poll->rats_res_len);
169 		}
170 		break;
171 
172 	case NCI_NFC_B_PASSIVE_POLL_MODE:
173 		nfcb_poll = &ntf->activation_params.nfcb_poll_iso_dep;
174 		nfcb_poll->attrib_res_len = *data++;
175 		pr_debug("attrib_res_len %d\n",
176 			nfcb_poll->attrib_res_len);
177 		if (nfcb_poll->attrib_res_len > 0) {
178 			memcpy(nfcb_poll->attrib_res,
179 				data,
180 				nfcb_poll->attrib_res_len);
181 		}
182 		break;
183 
184 	default:
185 		pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
186 		       ntf->activation_rf_tech_and_mode);
187 		return -EPROTO;
188 	}
189 
190 	return 0;
191 }
192 
193 static void nci_target_found(struct nci_dev *ndev,
194 				struct nci_rf_intf_activated_ntf *ntf)
195 {
196 	struct nfc_target nfc_tgt;
197 
198 	memset(&nfc_tgt, 0, sizeof(nfc_tgt));
199 
200 	if (ntf->rf_protocol == NCI_RF_PROTOCOL_T2T)
201 		nfc_tgt.supported_protocols = NFC_PROTO_MIFARE_MASK;
202 	else if (ntf->rf_protocol == NCI_RF_PROTOCOL_ISO_DEP)
203 		nfc_tgt.supported_protocols = NFC_PROTO_ISO14443_MASK;
204 	else if (ntf->rf_protocol == NCI_RF_PROTOCOL_T3T)
205 		nfc_tgt.supported_protocols = NFC_PROTO_FELICA_MASK;
206 
207 	if (!(nfc_tgt.supported_protocols & ndev->poll_prots)) {
208 		pr_debug("the target found does not have the desired protocol\n");
209 		return;
210 	}
211 
212 	pr_debug("new target found,  supported_protocols 0x%x\n",
213 		 nfc_tgt.supported_protocols);
214 
215 	if (ntf->activation_rf_tech_and_mode == NCI_NFC_A_PASSIVE_POLL_MODE) {
216 		nfc_tgt.sens_res =
217 			ntf->rf_tech_specific_params.nfca_poll.sens_res;
218 		nfc_tgt.sel_res =
219 			ntf->rf_tech_specific_params.nfca_poll.sel_res;
220 		nfc_tgt.nfcid1_len =
221 			ntf->rf_tech_specific_params.nfca_poll.nfcid1_len;
222 		if (nfc_tgt.nfcid1_len > 0) {
223 			memcpy(nfc_tgt.nfcid1,
224 				ntf->rf_tech_specific_params.nfca_poll.nfcid1,
225 				nfc_tgt.nfcid1_len);
226 		}
227 	} else if (ntf->activation_rf_tech_and_mode ==
228 						NCI_NFC_B_PASSIVE_POLL_MODE) {
229 		nfc_tgt.sensb_res_len =
230 			ntf->rf_tech_specific_params.nfcb_poll.sensb_res_len;
231 		if (nfc_tgt.sensb_res_len > 0) {
232 			memcpy(nfc_tgt.sensb_res,
233 			       ntf->rf_tech_specific_params.nfcb_poll.sensb_res,
234 			       nfc_tgt.sensb_res_len);
235 		}
236 	} else if (ntf->activation_rf_tech_and_mode ==
237 						NCI_NFC_F_PASSIVE_POLL_MODE) {
238 		nfc_tgt.sensf_res_len =
239 			ntf->rf_tech_specific_params.nfcf_poll.sensf_res_len;
240 		if (nfc_tgt.sensf_res_len > 0) {
241 			memcpy(nfc_tgt.sensf_res,
242 			       ntf->rf_tech_specific_params.nfcf_poll.sensf_res,
243 			       nfc_tgt.sensf_res_len);
244 		}
245 	}
246 
247 	ndev->target_available_prots = nfc_tgt.supported_protocols;
248 	ndev->max_data_pkt_payload_size = ntf->max_data_pkt_payload_size;
249 	ndev->initial_num_credits = ntf->initial_num_credits;
250 
251 	/* set the available credits to initial value */
252 	atomic_set(&ndev->credits_cnt, ndev->initial_num_credits);
253 
254 	nfc_targets_found(ndev->nfc_dev, &nfc_tgt, 1);
255 }
256 
257 static void nci_rf_intf_activated_ntf_packet(struct nci_dev *ndev,
258 						struct sk_buff *skb)
259 {
260 	struct nci_rf_intf_activated_ntf ntf;
261 	__u8 *data = skb->data;
262 	int err = 0;
263 
264 	clear_bit(NCI_DISCOVERY, &ndev->flags);
265 	set_bit(NCI_POLL_ACTIVE, &ndev->flags);
266 
267 	ntf.rf_discovery_id = *data++;
268 	ntf.rf_interface = *data++;
269 	ntf.rf_protocol = *data++;
270 	ntf.activation_rf_tech_and_mode = *data++;
271 	ntf.max_data_pkt_payload_size = *data++;
272 	ntf.initial_num_credits = *data++;
273 	ntf.rf_tech_specific_params_len = *data++;
274 
275 	pr_debug("rf_discovery_id %d\n", ntf.rf_discovery_id);
276 	pr_debug("rf_interface 0x%x\n", ntf.rf_interface);
277 	pr_debug("rf_protocol 0x%x\n", ntf.rf_protocol);
278 	pr_debug("activation_rf_tech_and_mode 0x%x\n",
279 		 ntf.activation_rf_tech_and_mode);
280 	pr_debug("max_data_pkt_payload_size 0x%x\n",
281 		 ntf.max_data_pkt_payload_size);
282 	pr_debug("initial_num_credits 0x%x\n", ntf.initial_num_credits);
283 	pr_debug("rf_tech_specific_params_len %d\n",
284 		 ntf.rf_tech_specific_params_len);
285 
286 	if (ntf.rf_tech_specific_params_len > 0) {
287 		switch (ntf.activation_rf_tech_and_mode) {
288 		case NCI_NFC_A_PASSIVE_POLL_MODE:
289 			data = nci_extract_rf_params_nfca_passive_poll(ndev,
290 				&ntf, data);
291 			break;
292 
293 		case NCI_NFC_B_PASSIVE_POLL_MODE:
294 			data = nci_extract_rf_params_nfcb_passive_poll(ndev,
295 				&ntf, data);
296 			break;
297 
298 		case NCI_NFC_F_PASSIVE_POLL_MODE:
299 			data = nci_extract_rf_params_nfcf_passive_poll(ndev,
300 				&ntf, data);
301 			break;
302 
303 		default:
304 			pr_err("unsupported activation_rf_tech_and_mode 0x%x\n",
305 			       ntf.activation_rf_tech_and_mode);
306 			return;
307 		}
308 	}
309 
310 	ntf.data_exch_rf_tech_and_mode = *data++;
311 	ntf.data_exch_tx_bit_rate = *data++;
312 	ntf.data_exch_rx_bit_rate = *data++;
313 	ntf.activation_params_len = *data++;
314 
315 	pr_debug("data_exch_rf_tech_and_mode 0x%x\n",
316 		 ntf.data_exch_rf_tech_and_mode);
317 	pr_debug("data_exch_tx_bit_rate 0x%x\n",
318 		 ntf.data_exch_tx_bit_rate);
319 	pr_debug("data_exch_rx_bit_rate 0x%x\n",
320 		 ntf.data_exch_rx_bit_rate);
321 	pr_debug("activation_params_len %d\n",
322 		 ntf.activation_params_len);
323 
324 	if (ntf.activation_params_len > 0) {
325 		switch (ntf.rf_interface) {
326 		case NCI_RF_INTERFACE_ISO_DEP:
327 			err = nci_extract_activation_params_iso_dep(ndev,
328 				&ntf, data);
329 			break;
330 
331 		case NCI_RF_INTERFACE_FRAME:
332 			/* no activation params */
333 			break;
334 
335 		default:
336 			pr_err("unsupported rf_interface 0x%x\n",
337 			       ntf.rf_interface);
338 			return;
339 		}
340 	}
341 
342 	if (!err)
343 		nci_target_found(ndev, &ntf);
344 }
345 
346 static void nci_rf_deactivate_ntf_packet(struct nci_dev *ndev,
347 					struct sk_buff *skb)
348 {
349 	struct nci_rf_deactivate_ntf *ntf = (void *) skb->data;
350 
351 	pr_debug("entry, type 0x%x, reason 0x%x\n", ntf->type, ntf->reason);
352 
353 	clear_bit(NCI_POLL_ACTIVE, &ndev->flags);
354 	ndev->target_active_prot = 0;
355 
356 	/* drop tx data queue */
357 	skb_queue_purge(&ndev->tx_q);
358 
359 	/* drop partial rx data packet */
360 	if (ndev->rx_data_reassembly) {
361 		kfree_skb(ndev->rx_data_reassembly);
362 		ndev->rx_data_reassembly = 0;
363 	}
364 
365 	/* complete the data exchange transaction, if exists */
366 	if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
367 		nci_data_exchange_complete(ndev, NULL, -EIO);
368 
369 	nci_req_complete(ndev, NCI_STATUS_OK);
370 }
371 
372 void nci_ntf_packet(struct nci_dev *ndev, struct sk_buff *skb)
373 {
374 	__u16 ntf_opcode = nci_opcode(skb->data);
375 
376 	pr_debug("NCI RX: MT=ntf, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
377 		 nci_pbf(skb->data),
378 		 nci_opcode_gid(ntf_opcode),
379 		 nci_opcode_oid(ntf_opcode),
380 		 nci_plen(skb->data));
381 
382 	/* strip the nci control header */
383 	skb_pull(skb, NCI_CTRL_HDR_SIZE);
384 
385 	switch (ntf_opcode) {
386 	case NCI_OP_CORE_CONN_CREDITS_NTF:
387 		nci_core_conn_credits_ntf_packet(ndev, skb);
388 		break;
389 
390 	case NCI_OP_CORE_INTF_ERROR_NTF:
391 		nci_core_conn_intf_error_ntf_packet(ndev, skb);
392 		break;
393 
394 	case NCI_OP_RF_INTF_ACTIVATED_NTF:
395 		nci_rf_intf_activated_ntf_packet(ndev, skb);
396 		break;
397 
398 	case NCI_OP_RF_DEACTIVATE_NTF:
399 		nci_rf_deactivate_ntf_packet(ndev, skb);
400 		break;
401 
402 	default:
403 		pr_err("unknown ntf opcode 0x%x\n", ntf_opcode);
404 		break;
405 	}
406 
407 	kfree_skb(skb);
408 }
409