• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * HCI based Driver for STMicroelectronics NFC Chip
3  *
4  * Copyright (C) 2014  STMicroelectronics SAS. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <linux/module.h>
20 #include <linux/nfc.h>
21 #include <net/nfc/hci.h>
22 #include <net/nfc/llc.h>
23 
24 #include "st21nfca.h"
25 #include "st21nfca_dep.h"
26 
27 #define DRIVER_DESC "HCI NFC driver for ST21NFCA"
28 
29 #define FULL_VERSION_LEN 3
30 
31 /* Proprietary gates, events, commands and registers */
32 
33 /* Commands that apply to all RF readers */
34 #define ST21NFCA_RF_READER_CMD_PRESENCE_CHECK	0x30
35 
36 #define ST21NFCA_RF_READER_ISO15693_GATE	0x12
37 #define ST21NFCA_RF_READER_ISO15693_INVENTORY	0x01
38 
39 /*
40  * Reader gate for communication with contact-less cards using Type A
41  * protocol ISO14443-3 but not compliant with ISO14443-4
42  */
43 #define ST21NFCA_RF_READER_14443_3_A_GATE	0x15
44 #define ST21NFCA_RF_READER_14443_3_A_UID	0x02
45 #define ST21NFCA_RF_READER_14443_3_A_ATQA	0x03
46 #define ST21NFCA_RF_READER_14443_3_A_SAK	0x04
47 
48 #define ST21NFCA_RF_READER_F_DATARATE		0x01
49 #define ST21NFCA_RF_READER_F_DATARATE_106	0x01
50 #define ST21NFCA_RF_READER_F_DATARATE_212	0x02
51 #define ST21NFCA_RF_READER_F_DATARATE_424	0x04
52 #define ST21NFCA_RF_READER_F_POL_REQ		0x02
53 #define ST21NFCA_RF_READER_F_POL_REQ_DEFAULT	0xffff0000
54 #define ST21NFCA_RF_READER_F_NFCID2		0x03
55 #define ST21NFCA_RF_READER_F_NFCID1		0x04
56 
57 #define ST21NFCA_RF_CARD_F_MODE			0x01
58 #define ST21NFCA_RF_CARD_F_NFCID2_LIST		0x04
59 #define ST21NFCA_RF_CARD_F_NFCID1		0x05
60 #define ST21NFCA_RF_CARD_F_SENS_RES		0x06
61 #define ST21NFCA_RF_CARD_F_SEL_RES		0x07
62 #define ST21NFCA_RF_CARD_F_DATARATE		0x08
63 #define ST21NFCA_RF_CARD_F_DATARATE_212_424	0x01
64 
65 #define ST21NFCA_DEVICE_MGNT_GATE		0x01
66 #define ST21NFCA_DEVICE_MGNT_PIPE		0x02
67 
68 #define ST21NFCA_DM_GETINFO			0x13
69 #define ST21NFCA_DM_GETINFO_PIPE_LIST		0x02
70 #define ST21NFCA_DM_GETINFO_PIPE_INFO		0x01
71 #define ST21NFCA_DM_PIPE_CREATED		0x02
72 #define ST21NFCA_DM_PIPE_OPEN			0x04
73 #define ST21NFCA_DM_RF_ACTIVE			0x80
74 #define ST21NFCA_DM_DISCONNECT			0x30
75 
76 #define ST21NFCA_DM_IS_PIPE_OPEN(p) \
77 	((p & 0x0f) == (ST21NFCA_DM_PIPE_CREATED | ST21NFCA_DM_PIPE_OPEN))
78 
79 #define ST21NFCA_NFC_MODE			0x03	/* NFC_MODE parameter*/
80 #define ST21NFCA_EVT_FIELD_ON			0x11
81 #define ST21NFCA_EVT_CARD_DEACTIVATED		0x12
82 #define ST21NFCA_EVT_CARD_ACTIVATED		0x13
83 #define ST21NFCA_EVT_FIELD_OFF			0x14
84 
85 static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
86 
87 static struct nfc_hci_gate st21nfca_gates[] = {
88 	{NFC_HCI_ADMIN_GATE, NFC_HCI_ADMIN_PIPE},
89 	{NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
90 	{NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
91 	{NFC_HCI_LINK_MGMT_GATE, NFC_HCI_LINK_MGMT_PIPE},
92 	{NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
93 	{NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
94 	{ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
95 	{ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
96 	{ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
97 	{ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
98 	{ST21NFCA_RF_CARD_F_GATE, NFC_HCI_INVALID_PIPE},
99 };
100 
101 struct st21nfca_pipe_info {
102 	u8 pipe_state;
103 	u8 src_host_id;
104 	u8 src_gate_id;
105 	u8 dst_host_id;
106 	u8 dst_gate_id;
107 } __packed;
108 
109 /* Largest headroom needed for outgoing custom commands */
110 #define ST21NFCA_CMDS_HEADROOM  7
111 
st21nfca_hci_load_session(struct nfc_hci_dev * hdev)112 static int st21nfca_hci_load_session(struct nfc_hci_dev *hdev)
113 {
114 	int i, j, r;
115 	struct sk_buff *skb_pipe_list, *skb_pipe_info;
116 	struct st21nfca_pipe_info *info;
117 
118 	u8 pipe_list[] = { ST21NFCA_DM_GETINFO_PIPE_LIST,
119 		NFC_HCI_TERMINAL_HOST_ID
120 	};
121 	u8 pipe_info[] = { ST21NFCA_DM_GETINFO_PIPE_INFO,
122 		NFC_HCI_TERMINAL_HOST_ID, 0
123 	};
124 
125 	skb_pipe_list = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
126 	if (!skb_pipe_list) {
127 		r = -ENOMEM;
128 		goto free_list;
129 	}
130 
131 	skb_pipe_info = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
132 	if (!skb_pipe_info) {
133 		r = -ENOMEM;
134 		goto free_info;
135 	}
136 
137 	/* On ST21NFCA device pipes number are dynamics
138 	 * A maximum of 16 pipes can be created at the same time
139 	 * If pipes are already created, hci_dev_up will fail.
140 	 * Doing a clear all pipe is a bad idea because:
141 	 * - It does useless EEPROM cycling
142 	 * - It might cause issue for secure elements support
143 	 * (such as removing connectivity or APDU reader pipe)
144 	 * A better approach on ST21NFCA is to:
145 	 * - get a pipe list for each host.
146 	 * (eg: NFC_HCI_HOST_CONTROLLER_ID for now).
147 	 * (TODO Later on UICC HOST and eSE HOST)
148 	 * - get pipe information
149 	 * - match retrieved pipe list in st21nfca_gates
150 	 * ST21NFCA_DEVICE_MGNT_GATE is a proprietary gate
151 	 * with ST21NFCA_DEVICE_MGNT_PIPE.
152 	 * Pipe can be closed and need to be open.
153 	 */
154 	r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
155 		ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE);
156 	if (r < 0)
157 		goto free_info;
158 
159 	/* Get pipe list */
160 	r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
161 			ST21NFCA_DM_GETINFO, pipe_list, sizeof(pipe_list),
162 			&skb_pipe_list);
163 	if (r < 0)
164 		goto free_info;
165 
166 	/* Complete the existing gate_pipe table */
167 	for (i = 0; i < skb_pipe_list->len; i++) {
168 		pipe_info[2] = skb_pipe_list->data[i];
169 		r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
170 					ST21NFCA_DM_GETINFO, pipe_info,
171 					sizeof(pipe_info), &skb_pipe_info);
172 
173 		if (r)
174 			continue;
175 
176 		/*
177 		 * Match pipe ID and gate ID
178 		 * Output format from ST21NFC_DM_GETINFO is:
179 		 * - pipe state (1byte)
180 		 * - source hid (1byte)
181 		 * - source gid (1byte)
182 		 * - destination hid (1byte)
183 		 * - destination gid (1byte)
184 		 */
185 		info = (struct st21nfca_pipe_info *) skb_pipe_info->data;
186 		for (j = 0; (j < ARRAY_SIZE(st21nfca_gates)) &&
187 			(st21nfca_gates[j].gate != info->dst_gate_id);
188 			j++)
189 			;
190 
191 		if (j < ARRAY_SIZE(st21nfca_gates) &&
192 			st21nfca_gates[j].gate == info->dst_gate_id &&
193 			ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) {
194 			st21nfca_gates[j].pipe = pipe_info[2];
195 			hdev->gate2pipe[st21nfca_gates[j].gate] =
196 				st21nfca_gates[j].pipe;
197 		}
198 	}
199 
200 	/*
201 	 * 3 gates have a well known pipe ID.
202 	 * They will never appear in the pipe list
203 	 */
204 	if (skb_pipe_list->len + 3 < ARRAY_SIZE(st21nfca_gates)) {
205 		for (i = skb_pipe_list->len + 3;
206 				i < ARRAY_SIZE(st21nfca_gates); i++) {
207 			r = nfc_hci_connect_gate(hdev,
208 					NFC_HCI_HOST_CONTROLLER_ID,
209 					st21nfca_gates[i].gate,
210 					st21nfca_gates[i].pipe);
211 			if (r < 0)
212 				goto free_info;
213 		}
214 	}
215 
216 	memcpy(hdev->init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
217 free_info:
218 	kfree_skb(skb_pipe_info);
219 free_list:
220 	kfree_skb(skb_pipe_list);
221 	return r;
222 }
223 
st21nfca_hci_open(struct nfc_hci_dev * hdev)224 static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
225 {
226 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
227 	int r;
228 
229 	mutex_lock(&info->info_lock);
230 
231 	if (info->state != ST21NFCA_ST_COLD) {
232 		r = -EBUSY;
233 		goto out;
234 	}
235 
236 	r = info->phy_ops->enable(info->phy_id);
237 
238 	if (r == 0)
239 		info->state = ST21NFCA_ST_READY;
240 
241 out:
242 	mutex_unlock(&info->info_lock);
243 	return r;
244 }
245 
st21nfca_hci_close(struct nfc_hci_dev * hdev)246 static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
247 {
248 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
249 
250 	mutex_lock(&info->info_lock);
251 
252 	if (info->state == ST21NFCA_ST_COLD)
253 		goto out;
254 
255 	info->phy_ops->disable(info->phy_id);
256 	info->state = ST21NFCA_ST_COLD;
257 
258 out:
259 	mutex_unlock(&info->info_lock);
260 }
261 
st21nfca_hci_ready(struct nfc_hci_dev * hdev)262 static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
263 {
264 	struct sk_buff *skb;
265 
266 	u8 param;
267 	int r;
268 
269 	param = NFC_HCI_UICC_HOST_ID;
270 	r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
271 			      NFC_HCI_ADMIN_WHITELIST, &param, 1);
272 	if (r < 0)
273 		return r;
274 
275 	/* Set NFC_MODE in device management gate to enable */
276 	r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
277 			      ST21NFCA_NFC_MODE, &skb);
278 	if (r < 0)
279 		return r;
280 
281 	if (skb->data[0] == 0) {
282 		kfree_skb(skb);
283 		param = 1;
284 
285 		r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
286 					ST21NFCA_NFC_MODE, &param, 1);
287 		if (r < 0)
288 			return r;
289 	}
290 
291 	r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
292 			       NFC_HCI_EVT_END_OPERATION, NULL, 0);
293 	if (r < 0)
294 		return r;
295 
296 	r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
297 			      NFC_HCI_ID_MGMT_VERSION_SW, &skb);
298 	if (r < 0)
299 		return r;
300 
301 	if (skb->len != FULL_VERSION_LEN) {
302 		kfree_skb(skb);
303 		return -EINVAL;
304 	}
305 
306 	print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
307 		       DUMP_PREFIX_NONE, 16, 1,
308 		       skb->data, FULL_VERSION_LEN, false);
309 
310 	kfree_skb(skb);
311 
312 	return 0;
313 }
314 
st21nfca_hci_xmit(struct nfc_hci_dev * hdev,struct sk_buff * skb)315 static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
316 {
317 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
318 
319 	return info->phy_ops->write(info->phy_id, skb);
320 }
321 
st21nfca_hci_start_poll(struct nfc_hci_dev * hdev,u32 im_protocols,u32 tm_protocols)322 static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
323 				   u32 im_protocols, u32 tm_protocols)
324 {
325 	int r;
326 	u32 pol_req;
327 	u8 param[19];
328 	struct sk_buff *datarate_skb;
329 
330 	pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
331 		__func__, im_protocols, tm_protocols);
332 
333 	r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
334 			       NFC_HCI_EVT_END_OPERATION, NULL, 0);
335 	if (r < 0)
336 		return r;
337 	if (im_protocols) {
338 		/*
339 		 * enable polling according to im_protocols & tm_protocols
340 		 * - CLOSE pipe according to im_protocols & tm_protocols
341 		 */
342 		if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
343 			r = nfc_hci_disconnect_gate(hdev,
344 					NFC_HCI_RF_READER_B_GATE);
345 			if (r < 0)
346 				return r;
347 		}
348 
349 		if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
350 			r = nfc_hci_disconnect_gate(hdev,
351 					NFC_HCI_RF_READER_A_GATE);
352 			if (r < 0)
353 				return r;
354 		}
355 
356 		if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
357 			r = nfc_hci_disconnect_gate(hdev,
358 					ST21NFCA_RF_READER_F_GATE);
359 			if (r < 0)
360 				return r;
361 		} else {
362 			hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
363 							       &hdev->gb_len);
364 
365 			if (hdev->gb == NULL || hdev->gb_len == 0) {
366 				im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
367 				tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
368 			}
369 
370 			param[0] = ST21NFCA_RF_READER_F_DATARATE_106 |
371 			    ST21NFCA_RF_READER_F_DATARATE_212 |
372 			    ST21NFCA_RF_READER_F_DATARATE_424;
373 			r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
374 					      ST21NFCA_RF_READER_F_DATARATE,
375 					      param, 1);
376 			if (r < 0)
377 				return r;
378 
379 			pol_req = be32_to_cpu((__force __be32)
380 					ST21NFCA_RF_READER_F_POL_REQ_DEFAULT);
381 			r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
382 					      ST21NFCA_RF_READER_F_POL_REQ,
383 					      (u8 *) &pol_req, 4);
384 			if (r < 0)
385 				return r;
386 		}
387 
388 		if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
389 			r = nfc_hci_disconnect_gate(hdev,
390 					ST21NFCA_RF_READER_14443_3_A_GATE);
391 			if (r < 0)
392 				return r;
393 		}
394 
395 		if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
396 			r = nfc_hci_disconnect_gate(hdev,
397 					ST21NFCA_RF_READER_ISO15693_GATE);
398 			if (r < 0)
399 				return r;
400 		}
401 
402 		r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
403 				       NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
404 		if (r < 0)
405 			nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
406 					   NFC_HCI_EVT_END_OPERATION, NULL, 0);
407 	}
408 
409 	if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
410 		r = nfc_hci_get_param(hdev, ST21NFCA_RF_CARD_F_GATE,
411 				      ST21NFCA_RF_CARD_F_DATARATE,
412 				      &datarate_skb);
413 		if (r < 0)
414 			return r;
415 
416 		/* Configure the maximum supported datarate to 424Kbps */
417 		if (datarate_skb->len > 0 &&
418 		    datarate_skb->data[0] !=
419 		    ST21NFCA_RF_CARD_F_DATARATE_212_424) {
420 			param[0] = ST21NFCA_RF_CARD_F_DATARATE_212_424;
421 			r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
422 					      ST21NFCA_RF_CARD_F_DATARATE,
423 					      param, 1);
424 			if (r < 0)
425 				return r;
426 		}
427 
428 		/*
429 		 * Configure sens_res
430 		 *
431 		 * NFC Forum Digital Spec Table 7:
432 		 * NFCID1 size: triple (10 bytes)
433 		 */
434 		param[0] = 0x00;
435 		param[1] = 0x08;
436 		r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
437 				      ST21NFCA_RF_CARD_F_SENS_RES, param, 2);
438 		if (r < 0)
439 			return r;
440 
441 		/*
442 		 * Configure sel_res
443 		 *
444 		 * NFC Forum Digistal Spec Table 17:
445 		 * b3 set to 0b (value b7-b6):
446 		 * - 10b: Configured for NFC-DEP Protocol
447 		 */
448 		param[0] = 0x40;
449 		r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
450 				      ST21NFCA_RF_CARD_F_SEL_RES, param, 1);
451 		if (r < 0)
452 			return r;
453 
454 		/* Configure NFCID1 Random uid */
455 		r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
456 				      ST21NFCA_RF_CARD_F_NFCID1, NULL, 0);
457 		if (r < 0)
458 			return r;
459 
460 		/* Configure NFCID2_LIST */
461 		/* System Code */
462 		param[0] = 0x00;
463 		param[1] = 0x00;
464 		/* NFCID2 */
465 		param[2] = 0x01;
466 		param[3] = 0xfe;
467 		param[4] = 'S';
468 		param[5] = 'T';
469 		param[6] = 'M';
470 		param[7] = 'i';
471 		param[8] = 'c';
472 		param[9] = 'r';
473 		/* 8 byte Pad bytes used for polling respone frame */
474 
475 		/*
476 		 * Configuration byte:
477 		 * - bit 0: define the default NFCID2 entry used when the
478 		 * system code is equal to 'FFFF'
479 		 * - bit 1: use a random value for lowest 6 bytes of
480 		 * NFCID2 value
481 		 * - bit 2: ignore polling request frame if request code
482 		 * is equal to '01'
483 		 * - Other bits are RFU
484 		 */
485 		param[18] = 0x01;
486 		r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
487 				      ST21NFCA_RF_CARD_F_NFCID2_LIST, param,
488 				      19);
489 		if (r < 0)
490 			return r;
491 
492 		param[0] = 0x02;
493 		r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
494 				      ST21NFCA_RF_CARD_F_MODE, param, 1);
495 	}
496 
497 	return r;
498 }
499 
st21nfca_hci_stop_poll(struct nfc_hci_dev * hdev)500 static void st21nfca_hci_stop_poll(struct nfc_hci_dev *hdev)
501 {
502 	nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
503 			ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
504 }
505 
st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev * hdev,u16 * atqa)506 static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
507 {
508 	int r;
509 	struct sk_buff *atqa_skb = NULL;
510 
511 	r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
512 			      ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
513 	if (r < 0)
514 		goto exit;
515 
516 	if (atqa_skb->len != 2) {
517 		r = -EPROTO;
518 		goto exit;
519 	}
520 
521 	*atqa = be16_to_cpu(*(__be16 *) atqa_skb->data);
522 
523 exit:
524 	kfree_skb(atqa_skb);
525 	return r;
526 }
527 
st21nfca_get_iso14443_3_sak(struct nfc_hci_dev * hdev,u8 * sak)528 static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
529 {
530 	int r;
531 	struct sk_buff *sak_skb = NULL;
532 
533 	r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
534 			      ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
535 	if (r < 0)
536 		goto exit;
537 
538 	if (sak_skb->len != 1) {
539 		r = -EPROTO;
540 		goto exit;
541 	}
542 
543 	*sak = sak_skb->data[0];
544 
545 exit:
546 	kfree_skb(sak_skb);
547 	return r;
548 }
549 
st21nfca_get_iso14443_3_uid(struct nfc_hci_dev * hdev,u8 * gate,int * len)550 static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *gate,
551 				       int *len)
552 {
553 	int r;
554 	struct sk_buff *uid_skb = NULL;
555 
556 	r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
557 			      ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
558 	if (r < 0)
559 		goto exit;
560 
561 	if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
562 		r = -EPROTO;
563 		goto exit;
564 	}
565 
566 	gate = uid_skb->data;
567 	*len = uid_skb->len;
568 exit:
569 	kfree_skb(uid_skb);
570 	return r;
571 }
572 
st21nfca_get_iso15693_inventory(struct nfc_hci_dev * hdev,struct nfc_target * target)573 static int st21nfca_get_iso15693_inventory(struct nfc_hci_dev *hdev,
574 					   struct nfc_target *target)
575 {
576 	int r;
577 	struct sk_buff *inventory_skb = NULL;
578 
579 	r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_ISO15693_GATE,
580 			      ST21NFCA_RF_READER_ISO15693_INVENTORY,
581 			      &inventory_skb);
582 	if (r < 0)
583 		goto exit;
584 
585 	skb_pull(inventory_skb, 2);
586 
587 	if (inventory_skb->len == 0 ||
588 	    inventory_skb->len > NFC_ISO15693_UID_MAXSIZE) {
589 		r = -EPROTO;
590 		goto exit;
591 	}
592 
593 	memcpy(target->iso15693_uid, inventory_skb->data, inventory_skb->len);
594 	target->iso15693_dsfid	= inventory_skb->data[1];
595 	target->is_iso15693 = 1;
596 exit:
597 	kfree_skb(inventory_skb);
598 	return r;
599 }
600 
st21nfca_hci_dep_link_up(struct nfc_hci_dev * hdev,struct nfc_target * target,u8 comm_mode,u8 * gb,size_t gb_len)601 static int st21nfca_hci_dep_link_up(struct nfc_hci_dev *hdev,
602 				    struct nfc_target *target, u8 comm_mode,
603 				    u8 *gb, size_t gb_len)
604 {
605 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
606 
607 	info->dep_info.idx = target->idx;
608 	return st21nfca_im_send_atr_req(hdev, gb, gb_len);
609 }
610 
st21nfca_hci_dep_link_down(struct nfc_hci_dev * hdev)611 static int st21nfca_hci_dep_link_down(struct nfc_hci_dev *hdev)
612 {
613 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
614 
615 	info->state = ST21NFCA_ST_READY;
616 
617 	return nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
618 				ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
619 }
620 
st21nfca_hci_target_from_gate(struct nfc_hci_dev * hdev,u8 gate,struct nfc_target * target)621 static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
622 					 struct nfc_target *target)
623 {
624 	int r, len;
625 	u16 atqa;
626 	u8 sak;
627 	u8 uid[NFC_NFCID1_MAXSIZE];
628 
629 	switch (gate) {
630 	case ST21NFCA_RF_READER_F_GATE:
631 		target->supported_protocols = NFC_PROTO_FELICA_MASK;
632 		break;
633 	case ST21NFCA_RF_READER_14443_3_A_GATE:
634 		/* ISO14443-3 type 1 or 2 tags */
635 		r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
636 		if (r < 0)
637 			return r;
638 		if (atqa == 0x000c) {
639 			target->supported_protocols = NFC_PROTO_JEWEL_MASK;
640 			target->sens_res = 0x0c00;
641 		} else {
642 			r = st21nfca_get_iso14443_3_sak(hdev, &sak);
643 			if (r < 0)
644 				return r;
645 
646 			r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
647 			if (r < 0)
648 				return r;
649 
650 			target->supported_protocols =
651 			    nfc_hci_sak_to_protocol(sak);
652 			if (target->supported_protocols == 0xffffffff)
653 				return -EPROTO;
654 
655 			target->sens_res = atqa;
656 			target->sel_res = sak;
657 			memcpy(target->nfcid1, uid, len);
658 			target->nfcid1_len = len;
659 		}
660 
661 		break;
662 	case ST21NFCA_RF_READER_ISO15693_GATE:
663 		target->supported_protocols = NFC_PROTO_ISO15693_MASK;
664 		r = st21nfca_get_iso15693_inventory(hdev, target);
665 		if (r < 0)
666 			return r;
667 		break;
668 	default:
669 		return -EPROTO;
670 	}
671 
672 	return 0;
673 }
674 
st21nfca_hci_complete_target_discovered(struct nfc_hci_dev * hdev,u8 gate,struct nfc_target * target)675 static int st21nfca_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
676 						u8 gate,
677 						struct nfc_target *target)
678 {
679 	int r;
680 	struct sk_buff *nfcid2_skb = NULL, *nfcid1_skb;
681 
682 	if (gate == ST21NFCA_RF_READER_F_GATE) {
683 		r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
684 				ST21NFCA_RF_READER_F_NFCID2, &nfcid2_skb);
685 		if (r < 0)
686 			goto exit;
687 
688 		if (nfcid2_skb->len > NFC_SENSF_RES_MAXSIZE) {
689 			r = -EPROTO;
690 			goto exit;
691 		}
692 
693 		/*
694 		 * - After the recepton of polling response for type F frame
695 		 * at 212 or 424 Kbit/s, NFCID2 registry parameters will be
696 		 * updated.
697 		 * - After the reception of SEL_RES with NFCIP-1 compliant bit
698 		 * set for type A frame NFCID1 will be updated
699 		 */
700 		if (nfcid2_skb->len > 0) {
701 			/* P2P in type F */
702 			memcpy(target->sensf_res, nfcid2_skb->data,
703 				nfcid2_skb->len);
704 			target->sensf_res_len = nfcid2_skb->len;
705 			/* NFC Forum Digital Protocol Table 44 */
706 			if (target->sensf_res[0] == 0x01 &&
707 			    target->sensf_res[1] == 0xfe)
708 				target->supported_protocols =
709 							NFC_PROTO_NFC_DEP_MASK;
710 			else
711 				target->supported_protocols =
712 							NFC_PROTO_FELICA_MASK;
713 		} else {
714 			/* P2P in type A */
715 			r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
716 					ST21NFCA_RF_READER_F_NFCID1,
717 					&nfcid1_skb);
718 			if (r < 0)
719 				goto exit;
720 
721 			if (nfcid1_skb->len > NFC_NFCID1_MAXSIZE) {
722 				r = -EPROTO;
723 				goto exit;
724 			}
725 			memcpy(target->sensf_res, nfcid1_skb->data,
726 				nfcid1_skb->len);
727 			target->sensf_res_len = nfcid1_skb->len;
728 			target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
729 		}
730 		target->hci_reader_gate = ST21NFCA_RF_READER_F_GATE;
731 	}
732 	r = 1;
733 exit:
734 	kfree_skb(nfcid2_skb);
735 	return r;
736 }
737 
738 #define ST21NFCA_CB_TYPE_READER_ISO15693 1
st21nfca_hci_data_exchange_cb(void * context,struct sk_buff * skb,int err)739 static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
740 					  int err)
741 {
742 	struct st21nfca_hci_info *info = context;
743 
744 	switch (info->async_cb_type) {
745 	case ST21NFCA_CB_TYPE_READER_ISO15693:
746 		if (err == 0)
747 			skb_trim(skb, skb->len - 1);
748 		info->async_cb(info->async_cb_context, skb, err);
749 		break;
750 	default:
751 		if (err == 0)
752 			kfree_skb(skb);
753 		break;
754 	}
755 }
756 
757 /*
758  * Returns:
759  * <= 0: driver handled the data exchange
760  *    1: driver doesn't especially handle, please do standard processing
761  */
st21nfca_hci_im_transceive(struct nfc_hci_dev * hdev,struct nfc_target * target,struct sk_buff * skb,data_exchange_cb_t cb,void * cb_context)762 static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
763 				      struct nfc_target *target,
764 				      struct sk_buff *skb,
765 				      data_exchange_cb_t cb, void *cb_context)
766 {
767 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
768 
769 	pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
770 		target->hci_reader_gate, skb->len);
771 
772 	switch (target->hci_reader_gate) {
773 	case ST21NFCA_RF_READER_F_GATE:
774 		if (target->supported_protocols == NFC_PROTO_NFC_DEP_MASK)
775 			return st21nfca_im_send_dep_req(hdev, skb);
776 
777 		*skb_push(skb, 1) = 0x1a;
778 		return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
779 					      ST21NFCA_WR_XCHG_DATA, skb->data,
780 					      skb->len, cb, cb_context);
781 	case ST21NFCA_RF_READER_14443_3_A_GATE:
782 		*skb_push(skb, 1) = 0x1a;	/* CTR, see spec:10.2.2.1 */
783 
784 		return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
785 					      ST21NFCA_WR_XCHG_DATA, skb->data,
786 					      skb->len, cb, cb_context);
787 	case ST21NFCA_RF_READER_ISO15693_GATE:
788 		info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
789 		info->async_cb = cb;
790 		info->async_cb_context = cb_context;
791 
792 		*skb_push(skb, 1) = 0x17;
793 
794 		return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
795 					      ST21NFCA_WR_XCHG_DATA, skb->data,
796 					      skb->len,
797 					      st21nfca_hci_data_exchange_cb,
798 					      info);
799 		break;
800 	default:
801 		return 1;
802 	}
803 }
804 
st21nfca_hci_tm_send(struct nfc_hci_dev * hdev,struct sk_buff * skb)805 static int st21nfca_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
806 {
807 	return st21nfca_tm_send_dep_res(hdev, skb);
808 }
809 
st21nfca_hci_check_presence(struct nfc_hci_dev * hdev,struct nfc_target * target)810 static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
811 				       struct nfc_target *target)
812 {
813 	u8 fwi = 0x11;
814 
815 	switch (target->hci_reader_gate) {
816 	case NFC_HCI_RF_READER_A_GATE:
817 	case NFC_HCI_RF_READER_B_GATE:
818 		/*
819 		 * PRESENCE_CHECK on those gates is available
820 		 * However, the answer to this command is taking 3 * fwi
821 		 * if the card is no present.
822 		 * Instead, we send an empty I-Frame with a very short
823 		 * configurable fwi ~604µs.
824 		 */
825 		return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
826 					ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
827 	case ST21NFCA_RF_READER_14443_3_A_GATE:
828 		return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
829 					ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
830 					NULL, 0, NULL);
831 	default:
832 		return -EOPNOTSUPP;
833 	}
834 }
835 
836 /*
837  * Returns:
838  * <= 0: driver handled the event, skb consumed
839  *    1: driver does not handle the event, please do standard processing
840  */
st21nfca_hci_event_received(struct nfc_hci_dev * hdev,u8 gate,u8 event,struct sk_buff * skb)841 static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 gate,
842 				       u8 event, struct sk_buff *skb)
843 {
844 	int r;
845 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
846 
847 	pr_debug("hci event: %d\n", event);
848 
849 	switch (event) {
850 	case ST21NFCA_EVT_CARD_ACTIVATED:
851 		if (gate == ST21NFCA_RF_CARD_F_GATE)
852 			info->dep_info.curr_nfc_dep_pni = 0;
853 		break;
854 	case ST21NFCA_EVT_CARD_DEACTIVATED:
855 		break;
856 	case ST21NFCA_EVT_FIELD_ON:
857 		break;
858 	case ST21NFCA_EVT_FIELD_OFF:
859 		break;
860 	case ST21NFCA_EVT_SEND_DATA:
861 		if (gate == ST21NFCA_RF_CARD_F_GATE) {
862 			r = st21nfca_tm_event_send_data(hdev, skb, gate);
863 			if (r < 0)
864 				return r;
865 			return 0;
866 		}
867 		info->dep_info.curr_nfc_dep_pni = 0;
868 		return 1;
869 	default:
870 		return 1;
871 	}
872 	kfree_skb(skb);
873 	return 0;
874 }
875 
876 static struct nfc_hci_ops st21nfca_hci_ops = {
877 	.open = st21nfca_hci_open,
878 	.close = st21nfca_hci_close,
879 	.load_session = st21nfca_hci_load_session,
880 	.hci_ready = st21nfca_hci_ready,
881 	.xmit = st21nfca_hci_xmit,
882 	.start_poll = st21nfca_hci_start_poll,
883 	.stop_poll = st21nfca_hci_stop_poll,
884 	.dep_link_up = st21nfca_hci_dep_link_up,
885 	.dep_link_down = st21nfca_hci_dep_link_down,
886 	.target_from_gate = st21nfca_hci_target_from_gate,
887 	.complete_target_discovered = st21nfca_hci_complete_target_discovered,
888 	.im_transceive = st21nfca_hci_im_transceive,
889 	.tm_send = st21nfca_hci_tm_send,
890 	.check_presence = st21nfca_hci_check_presence,
891 	.event_received = st21nfca_hci_event_received,
892 };
893 
st21nfca_hci_probe(void * phy_id,struct nfc_phy_ops * phy_ops,char * llc_name,int phy_headroom,int phy_tailroom,int phy_payload,struct nfc_hci_dev ** hdev)894 int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
895 		       char *llc_name, int phy_headroom, int phy_tailroom,
896 		       int phy_payload, struct nfc_hci_dev **hdev)
897 {
898 	struct st21nfca_hci_info *info;
899 	int r = 0;
900 	int dev_num;
901 	u32 protocols;
902 	struct nfc_hci_init_data init_data;
903 	unsigned long quirks = 0;
904 
905 	info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
906 	if (!info) {
907 		r = -ENOMEM;
908 		goto err_alloc_hdev;
909 	}
910 
911 	info->phy_ops = phy_ops;
912 	info->phy_id = phy_id;
913 	info->state = ST21NFCA_ST_COLD;
914 	mutex_init(&info->info_lock);
915 
916 	init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
917 
918 	memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
919 
920 	/*
921 	 * Session id must include the driver name + i2c bus addr
922 	 * persistent info to discriminate 2 identical chips
923 	 */
924 	dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
925 
926 	if (dev_num >= ST21NFCA_NUM_DEVICES)
927 		return -ENODEV;
928 
929 	set_bit(dev_num, dev_mask);
930 
931 	scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
932 		  "ST21AH", dev_num);
933 
934 	protocols = NFC_PROTO_JEWEL_MASK |
935 	    NFC_PROTO_MIFARE_MASK |
936 	    NFC_PROTO_FELICA_MASK |
937 	    NFC_PROTO_ISO14443_MASK |
938 	    NFC_PROTO_ISO14443_B_MASK |
939 	    NFC_PROTO_ISO15693_MASK |
940 	    NFC_PROTO_NFC_DEP_MASK;
941 
942 	set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
943 
944 	info->hdev =
945 	    nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
946 				    protocols, llc_name,
947 				    phy_headroom + ST21NFCA_CMDS_HEADROOM,
948 				    phy_tailroom, phy_payload);
949 
950 	if (!info->hdev) {
951 		pr_err("Cannot allocate nfc hdev.\n");
952 		r = -ENOMEM;
953 		goto err_alloc_hdev;
954 	}
955 
956 	nfc_hci_set_clientdata(info->hdev, info);
957 
958 	r = nfc_hci_register_device(info->hdev);
959 	if (r)
960 		goto err_regdev;
961 
962 	*hdev = info->hdev;
963 	st21nfca_dep_init(info->hdev);
964 
965 	return 0;
966 
967 err_regdev:
968 	nfc_hci_free_device(info->hdev);
969 
970 err_alloc_hdev:
971 	kfree(info);
972 
973 	return r;
974 }
975 EXPORT_SYMBOL(st21nfca_hci_probe);
976 
st21nfca_hci_remove(struct nfc_hci_dev * hdev)977 void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
978 {
979 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
980 
981 	st21nfca_dep_deinit(hdev);
982 	nfc_hci_unregister_device(hdev);
983 	nfc_hci_free_device(hdev);
984 	kfree(info);
985 }
986 EXPORT_SYMBOL(st21nfca_hci_remove);
987 
988 MODULE_LICENSE("GPL");
989 MODULE_DESCRIPTION(DRIVER_DESC);
990