1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HCI based Driver for Inside Secure microread NFC Chip
4  *
5  * Copyright (C) 2013  Intel Corporation. All rights reserved.
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/module.h>
11 #include <linux/delay.h>
12 #include <linux/slab.h>
13 #include <linux/crc-ccitt.h>
14 
15 #include <linux/nfc.h>
16 #include <net/nfc/nfc.h>
17 #include <net/nfc/hci.h>
18 #include <net/nfc/llc.h>
19 
20 #include "microread.h"
21 
22 /* Proprietary gates, events, commands and registers */
23 /* Admin */
24 #define MICROREAD_GATE_ID_ADM NFC_HCI_ADMIN_GATE
25 #define MICROREAD_GATE_ID_MGT 0x01
26 #define MICROREAD_GATE_ID_OS 0x02
27 #define MICROREAD_GATE_ID_TESTRF 0x03
28 #define MICROREAD_GATE_ID_LOOPBACK NFC_HCI_LOOPBACK_GATE
29 #define MICROREAD_GATE_ID_IDT NFC_HCI_ID_MGMT_GATE
30 #define MICROREAD_GATE_ID_LMS NFC_HCI_LINK_MGMT_GATE
31 
32 /* Reader */
33 #define MICROREAD_GATE_ID_MREAD_GEN 0x10
34 #define MICROREAD_GATE_ID_MREAD_ISO_B NFC_HCI_RF_READER_B_GATE
35 #define MICROREAD_GATE_ID_MREAD_NFC_T1 0x12
36 #define MICROREAD_GATE_ID_MREAD_ISO_A NFC_HCI_RF_READER_A_GATE
37 #define MICROREAD_GATE_ID_MREAD_NFC_T3 0x14
38 #define MICROREAD_GATE_ID_MREAD_ISO_15_3 0x15
39 #define MICROREAD_GATE_ID_MREAD_ISO_15_2 0x16
40 #define MICROREAD_GATE_ID_MREAD_ISO_B_3 0x17
41 #define MICROREAD_GATE_ID_MREAD_BPRIME 0x18
42 #define MICROREAD_GATE_ID_MREAD_ISO_A_3 0x19
43 
44 /* Card */
45 #define MICROREAD_GATE_ID_MCARD_GEN 0x20
46 #define MICROREAD_GATE_ID_MCARD_ISO_B 0x21
47 #define MICROREAD_GATE_ID_MCARD_BPRIME 0x22
48 #define MICROREAD_GATE_ID_MCARD_ISO_A 0x23
49 #define MICROREAD_GATE_ID_MCARD_NFC_T3 0x24
50 #define MICROREAD_GATE_ID_MCARD_ISO_15_3 0x25
51 #define MICROREAD_GATE_ID_MCARD_ISO_15_2 0x26
52 #define MICROREAD_GATE_ID_MCARD_ISO_B_2 0x27
53 #define MICROREAD_GATE_ID_MCARD_ISO_CUSTOM 0x28
54 #define MICROREAD_GATE_ID_SECURE_ELEMENT 0x2F
55 
56 /* P2P */
57 #define MICROREAD_GATE_ID_P2P_GEN 0x30
58 #define MICROREAD_GATE_ID_P2P_TARGET 0x31
59 #define MICROREAD_PAR_P2P_TARGET_MODE 0x01
60 #define MICROREAD_PAR_P2P_TARGET_GT 0x04
61 #define MICROREAD_GATE_ID_P2P_INITIATOR 0x32
62 #define MICROREAD_PAR_P2P_INITIATOR_GI 0x01
63 #define MICROREAD_PAR_P2P_INITIATOR_GT 0x03
64 
65 /* Those pipes are created/opened by default in the chip */
66 #define MICROREAD_PIPE_ID_LMS 0x00
67 #define MICROREAD_PIPE_ID_ADMIN 0x01
68 #define MICROREAD_PIPE_ID_MGT 0x02
69 #define MICROREAD_PIPE_ID_OS 0x03
70 #define MICROREAD_PIPE_ID_HDS_LOOPBACK 0x04
71 #define MICROREAD_PIPE_ID_HDS_IDT 0x05
72 #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_B 0x08
73 #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_BPRIME 0x09
74 #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_A 0x0A
75 #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_15_3 0x0B
76 #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_15_2 0x0C
77 #define MICROREAD_PIPE_ID_HDS_MCARD_NFC_T3 0x0D
78 #define MICROREAD_PIPE_ID_HDS_MCARD_ISO_B_2 0x0E
79 #define MICROREAD_PIPE_ID_HDS_MCARD_CUSTOM 0x0F
80 #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_B 0x10
81 #define MICROREAD_PIPE_ID_HDS_MREAD_NFC_T1 0x11
82 #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_A 0x12
83 #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_15_3 0x13
84 #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_15_2 0x14
85 #define MICROREAD_PIPE_ID_HDS_MREAD_NFC_T3 0x15
86 #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_B_3 0x16
87 #define MICROREAD_PIPE_ID_HDS_MREAD_BPRIME 0x17
88 #define MICROREAD_PIPE_ID_HDS_MREAD_ISO_A_3 0x18
89 #define MICROREAD_PIPE_ID_HDS_MREAD_GEN 0x1B
90 #define MICROREAD_PIPE_ID_HDS_STACKED_ELEMENT 0x1C
91 #define MICROREAD_PIPE_ID_HDS_INSTANCES 0x1D
92 #define MICROREAD_PIPE_ID_HDS_TESTRF 0x1E
93 #define MICROREAD_PIPE_ID_HDS_P2P_TARGET 0x1F
94 #define MICROREAD_PIPE_ID_HDS_P2P_INITIATOR 0x20
95 
96 /* Events */
97 #define MICROREAD_EVT_MREAD_DISCOVERY_OCCURED NFC_HCI_EVT_TARGET_DISCOVERED
98 #define MICROREAD_EVT_MREAD_CARD_FOUND 0x3D
99 #define MICROREAD_EMCF_A_ATQA 0
100 #define MICROREAD_EMCF_A_SAK 2
101 #define MICROREAD_EMCF_A_LEN 3
102 #define MICROREAD_EMCF_A_UID 4
103 #define MICROREAD_EMCF_A3_ATQA 0
104 #define MICROREAD_EMCF_A3_SAK 2
105 #define MICROREAD_EMCF_A3_LEN 3
106 #define MICROREAD_EMCF_A3_UID 4
107 #define MICROREAD_EMCF_B_UID 0
108 #define MICROREAD_EMCF_T1_ATQA 0
109 #define MICROREAD_EMCF_T1_UID 4
110 #define MICROREAD_EMCF_T3_UID 0
111 #define MICROREAD_EVT_MREAD_DISCOVERY_START NFC_HCI_EVT_READER_REQUESTED
112 #define MICROREAD_EVT_MREAD_DISCOVERY_START_SOME 0x3E
113 #define MICROREAD_EVT_MREAD_DISCOVERY_STOP NFC_HCI_EVT_END_OPERATION
114 #define MICROREAD_EVT_MREAD_SIM_REQUESTS 0x3F
115 #define MICROREAD_EVT_MCARD_EXCHANGE NFC_HCI_EVT_TARGET_DISCOVERED
116 #define MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_TO_RF 0x20
117 #define MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_FROM_RF 0x21
118 #define MICROREAD_EVT_MCARD_FIELD_ON 0x11
119 #define MICROREAD_EVT_P2P_TARGET_ACTIVATED 0x13
120 #define MICROREAD_EVT_P2P_TARGET_DEACTIVATED 0x12
121 #define MICROREAD_EVT_MCARD_FIELD_OFF 0x14
122 
123 /* Commands */
124 #define MICROREAD_CMD_MREAD_EXCHANGE 0x10
125 #define MICROREAD_CMD_MREAD_SUBSCRIBE 0x3F
126 
127 /* Hosts IDs */
128 #define MICROREAD_ELT_ID_HDS NFC_HCI_TERMINAL_HOST_ID
129 #define MICROREAD_ELT_ID_SIM NFC_HCI_UICC_HOST_ID
130 #define MICROREAD_ELT_ID_SE1 0x03
131 #define MICROREAD_ELT_ID_SE2 0x04
132 #define MICROREAD_ELT_ID_SE3 0x05
133 
134 static struct nfc_hci_gate microread_gates[] = {
135 	{MICROREAD_GATE_ID_ADM, MICROREAD_PIPE_ID_ADMIN},
136 	{MICROREAD_GATE_ID_LOOPBACK, MICROREAD_PIPE_ID_HDS_LOOPBACK},
137 	{MICROREAD_GATE_ID_IDT, MICROREAD_PIPE_ID_HDS_IDT},
138 	{MICROREAD_GATE_ID_LMS, MICROREAD_PIPE_ID_LMS},
139 	{MICROREAD_GATE_ID_MREAD_ISO_B, MICROREAD_PIPE_ID_HDS_MREAD_ISO_B},
140 	{MICROREAD_GATE_ID_MREAD_ISO_A, MICROREAD_PIPE_ID_HDS_MREAD_ISO_A},
141 	{MICROREAD_GATE_ID_MREAD_ISO_A_3, MICROREAD_PIPE_ID_HDS_MREAD_ISO_A_3},
142 	{MICROREAD_GATE_ID_MGT, MICROREAD_PIPE_ID_MGT},
143 	{MICROREAD_GATE_ID_OS, MICROREAD_PIPE_ID_OS},
144 	{MICROREAD_GATE_ID_MREAD_NFC_T1, MICROREAD_PIPE_ID_HDS_MREAD_NFC_T1},
145 	{MICROREAD_GATE_ID_MREAD_NFC_T3, MICROREAD_PIPE_ID_HDS_MREAD_NFC_T3},
146 	{MICROREAD_GATE_ID_P2P_TARGET, MICROREAD_PIPE_ID_HDS_P2P_TARGET},
147 	{MICROREAD_GATE_ID_P2P_INITIATOR, MICROREAD_PIPE_ID_HDS_P2P_INITIATOR}
148 };
149 
150 /* Largest headroom needed for outgoing custom commands */
151 #define MICROREAD_CMDS_HEADROOM	2
152 #define MICROREAD_CMD_TAILROOM	2
153 
154 struct microread_info {
155 	struct nfc_phy_ops *phy_ops;
156 	void *phy_id;
157 
158 	struct nfc_hci_dev *hdev;
159 
160 	int async_cb_type;
161 	data_exchange_cb_t async_cb;
162 	void *async_cb_context;
163 };
164 
165 static int microread_open(struct nfc_hci_dev *hdev)
166 {
167 	struct microread_info *info = nfc_hci_get_clientdata(hdev);
168 
169 	return info->phy_ops->enable(info->phy_id);
170 }
171 
172 static void microread_close(struct nfc_hci_dev *hdev)
173 {
174 	struct microread_info *info = nfc_hci_get_clientdata(hdev);
175 
176 	info->phy_ops->disable(info->phy_id);
177 }
178 
179 static int microread_hci_ready(struct nfc_hci_dev *hdev)
180 {
181 	int r;
182 	u8 param[4];
183 
184 	param[0] = 0x03;
185 	r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_A,
186 			     MICROREAD_CMD_MREAD_SUBSCRIBE, param, 1, NULL);
187 	if (r)
188 		return r;
189 
190 	r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_A_3,
191 			     MICROREAD_CMD_MREAD_SUBSCRIBE, NULL, 0, NULL);
192 	if (r)
193 		return r;
194 
195 	param[0] = 0x00;
196 	param[1] = 0x03;
197 	param[2] = 0x00;
198 	r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_B,
199 			     MICROREAD_CMD_MREAD_SUBSCRIBE, param, 3, NULL);
200 	if (r)
201 		return r;
202 
203 	r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_NFC_T1,
204 			     MICROREAD_CMD_MREAD_SUBSCRIBE, NULL, 0, NULL);
205 	if (r)
206 		return r;
207 
208 	param[0] = 0xFF;
209 	param[1] = 0xFF;
210 	param[2] = 0x00;
211 	param[3] = 0x00;
212 	r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_NFC_T3,
213 			     MICROREAD_CMD_MREAD_SUBSCRIBE, param, 4, NULL);
214 
215 	return r;
216 }
217 
218 static int microread_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
219 {
220 	struct microread_info *info = nfc_hci_get_clientdata(hdev);
221 
222 	return info->phy_ops->write(info->phy_id, skb);
223 }
224 
225 static int microread_start_poll(struct nfc_hci_dev *hdev,
226 				u32 im_protocols, u32 tm_protocols)
227 {
228 	int r;
229 
230 	u8 param[2];
231 	u8 mode;
232 
233 	param[0] = 0x00;
234 	param[1] = 0x00;
235 
236 	if (im_protocols & NFC_PROTO_ISO14443_MASK)
237 		param[0] |= (1 << 2);
238 
239 	if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
240 		param[0] |= 1;
241 
242 	if (im_protocols & NFC_PROTO_MIFARE_MASK)
243 		param[1] |= 1;
244 
245 	if (im_protocols & NFC_PROTO_JEWEL_MASK)
246 		param[0] |= (1 << 1);
247 
248 	if (im_protocols & NFC_PROTO_FELICA_MASK)
249 		param[0] |= (1 << 5);
250 
251 	if (im_protocols & NFC_PROTO_NFC_DEP_MASK)
252 		param[1] |= (1 << 1);
253 
254 	if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
255 		hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
256 						       &hdev->gb_len);
257 		if (hdev->gb == NULL || hdev->gb_len == 0) {
258 			im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
259 			tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
260 		}
261 	}
262 
263 	r = nfc_hci_send_event(hdev, MICROREAD_GATE_ID_MREAD_ISO_A,
264 			       MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL, 0);
265 	if (r)
266 		return r;
267 
268 	mode = 0xff;
269 	r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
270 			      MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1);
271 	if (r)
272 		return r;
273 
274 	if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
275 		r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_INITIATOR,
276 				      MICROREAD_PAR_P2P_INITIATOR_GI,
277 				      hdev->gb, hdev->gb_len);
278 		if (r)
279 			return r;
280 	}
281 
282 	if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
283 		r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
284 				      MICROREAD_PAR_P2P_TARGET_GT,
285 				      hdev->gb, hdev->gb_len);
286 		if (r)
287 			return r;
288 
289 		mode = 0x02;
290 		r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
291 				      MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1);
292 		if (r)
293 			return r;
294 	}
295 
296 	return nfc_hci_send_event(hdev, MICROREAD_GATE_ID_MREAD_ISO_A,
297 				  MICROREAD_EVT_MREAD_DISCOVERY_START_SOME,
298 				  param, 2);
299 }
300 
301 static int microread_dep_link_up(struct nfc_hci_dev *hdev,
302 				struct nfc_target *target, u8 comm_mode,
303 				u8 *gb, size_t gb_len)
304 {
305 	struct sk_buff *rgb_skb = NULL;
306 	int r;
307 
308 	r = nfc_hci_get_param(hdev, target->hci_reader_gate,
309 			      MICROREAD_PAR_P2P_INITIATOR_GT, &rgb_skb);
310 	if (r < 0)
311 		return r;
312 
313 	if (rgb_skb->len == 0 || rgb_skb->len > NFC_GB_MAXSIZE) {
314 		r = -EPROTO;
315 		goto exit;
316 	}
317 
318 	r = nfc_set_remote_general_bytes(hdev->ndev, rgb_skb->data,
319 					 rgb_skb->len);
320 	if (r == 0)
321 		r = nfc_dep_link_is_up(hdev->ndev, target->idx, comm_mode,
322 				       NFC_RF_INITIATOR);
323 exit:
324 	kfree_skb(rgb_skb);
325 
326 	return r;
327 }
328 
329 static int microread_dep_link_down(struct nfc_hci_dev *hdev)
330 {
331 	return nfc_hci_send_event(hdev, MICROREAD_GATE_ID_P2P_INITIATOR,
332 				  MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL, 0);
333 }
334 
335 static int microread_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
336 				      struct nfc_target *target)
337 {
338 	switch (gate) {
339 	case MICROREAD_GATE_ID_P2P_INITIATOR:
340 		target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
341 		break;
342 	default:
343 		return -EPROTO;
344 	}
345 
346 	return 0;
347 }
348 
349 static int microread_complete_target_discovered(struct nfc_hci_dev *hdev,
350 						u8 gate,
351 						struct nfc_target *target)
352 {
353 	return 0;
354 }
355 
356 #define MICROREAD_CB_TYPE_READER_ALL 1
357 
358 static void microread_im_transceive_cb(void *context, struct sk_buff *skb,
359 				       int err)
360 {
361 	struct microread_info *info = context;
362 
363 	switch (info->async_cb_type) {
364 	case MICROREAD_CB_TYPE_READER_ALL:
365 		if (err == 0) {
366 			if (skb->len == 0) {
367 				err = -EPROTO;
368 				kfree_skb(skb);
369 				info->async_cb(info->async_cb_context, NULL,
370 					       -EPROTO);
371 				return;
372 			}
373 
374 			if (skb->data[skb->len - 1] != 0) {
375 				err = nfc_hci_result_to_errno(
376 						       skb->data[skb->len - 1]);
377 				kfree_skb(skb);
378 				info->async_cb(info->async_cb_context, NULL,
379 					       err);
380 				return;
381 			}
382 
383 			skb_trim(skb, skb->len - 1);	/* RF Error ind. */
384 		}
385 		info->async_cb(info->async_cb_context, skb, err);
386 		break;
387 	default:
388 		if (err == 0)
389 			kfree_skb(skb);
390 		break;
391 	}
392 }
393 
394 /*
395  * Returns:
396  * <= 0: driver handled the data exchange
397  *    1: driver doesn't especially handle, please do standard processing
398  */
399 static int microread_im_transceive(struct nfc_hci_dev *hdev,
400 				   struct nfc_target *target,
401 				   struct sk_buff *skb, data_exchange_cb_t cb,
402 				   void *cb_context)
403 {
404 	struct microread_info *info = nfc_hci_get_clientdata(hdev);
405 	u8 control_bits;
406 	u16 crc;
407 
408 	pr_info("data exchange to gate 0x%x\n", target->hci_reader_gate);
409 
410 	if (target->hci_reader_gate == MICROREAD_GATE_ID_P2P_INITIATOR) {
411 		*(u8 *)skb_push(skb, 1) = 0;
412 
413 		return nfc_hci_send_event(hdev, target->hci_reader_gate,
414 				     MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_TO_RF,
415 				     skb->data, skb->len);
416 	}
417 
418 	switch (target->hci_reader_gate) {
419 	case MICROREAD_GATE_ID_MREAD_ISO_A:
420 		control_bits = 0xCB;
421 		break;
422 	case MICROREAD_GATE_ID_MREAD_ISO_A_3:
423 		control_bits = 0xCB;
424 		break;
425 	case MICROREAD_GATE_ID_MREAD_ISO_B:
426 		control_bits = 0xCB;
427 		break;
428 	case MICROREAD_GATE_ID_MREAD_NFC_T1:
429 		control_bits = 0x1B;
430 
431 		crc = crc_ccitt(0xffff, skb->data, skb->len);
432 		crc = ~crc;
433 		skb_put_u8(skb, crc & 0xff);
434 		skb_put_u8(skb, crc >> 8);
435 		break;
436 	case MICROREAD_GATE_ID_MREAD_NFC_T3:
437 		control_bits = 0xDB;
438 		break;
439 	default:
440 		pr_info("Abort im_transceive to invalid gate 0x%x\n",
441 			target->hci_reader_gate);
442 		return 1;
443 	}
444 
445 	*(u8 *)skb_push(skb, 1) = control_bits;
446 
447 	info->async_cb_type = MICROREAD_CB_TYPE_READER_ALL;
448 	info->async_cb = cb;
449 	info->async_cb_context = cb_context;
450 
451 	return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
452 				      MICROREAD_CMD_MREAD_EXCHANGE,
453 				      skb->data, skb->len,
454 				      microread_im_transceive_cb, info);
455 }
456 
457 static int microread_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
458 {
459 	int r;
460 
461 	r = nfc_hci_send_event(hdev, MICROREAD_GATE_ID_P2P_TARGET,
462 			       MICROREAD_EVT_MCARD_EXCHANGE,
463 			       skb->data, skb->len);
464 
465 	kfree_skb(skb);
466 
467 	return r;
468 }
469 
470 static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate,
471 					struct sk_buff *skb)
472 {
473 	struct nfc_target *targets;
474 	int r = 0;
475 
476 	pr_info("target discovered to gate 0x%x\n", gate);
477 
478 	targets = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
479 	if (targets == NULL) {
480 		r = -ENOMEM;
481 		goto exit;
482 	}
483 
484 	targets->hci_reader_gate = gate;
485 
486 	switch (gate) {
487 	case MICROREAD_GATE_ID_MREAD_ISO_A:
488 		targets->supported_protocols =
489 		      nfc_hci_sak_to_protocol(skb->data[MICROREAD_EMCF_A_SAK]);
490 		targets->sens_res =
491 			 be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A_ATQA]);
492 		targets->sel_res = skb->data[MICROREAD_EMCF_A_SAK];
493 		targets->nfcid1_len = skb->data[MICROREAD_EMCF_A_LEN];
494 		if (targets->nfcid1_len > sizeof(targets->nfcid1)) {
495 			r = -EINVAL;
496 			goto exit_free;
497 		}
498 		memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A_UID],
499 		       targets->nfcid1_len);
500 		break;
501 	case MICROREAD_GATE_ID_MREAD_ISO_A_3:
502 		targets->supported_protocols =
503 		      nfc_hci_sak_to_protocol(skb->data[MICROREAD_EMCF_A3_SAK]);
504 		targets->sens_res =
505 			 be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A3_ATQA]);
506 		targets->sel_res = skb->data[MICROREAD_EMCF_A3_SAK];
507 		targets->nfcid1_len = skb->data[MICROREAD_EMCF_A3_LEN];
508 		if (targets->nfcid1_len > sizeof(targets->nfcid1)) {
509 			r = -EINVAL;
510 			goto exit_free;
511 		}
512 		memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A3_UID],
513 		       targets->nfcid1_len);
514 		break;
515 	case MICROREAD_GATE_ID_MREAD_ISO_B:
516 		targets->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
517 		memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_B_UID], 4);
518 		targets->nfcid1_len = 4;
519 		break;
520 	case MICROREAD_GATE_ID_MREAD_NFC_T1:
521 		targets->supported_protocols = NFC_PROTO_JEWEL_MASK;
522 		targets->sens_res =
523 			le16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_T1_ATQA]);
524 		memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_T1_UID], 4);
525 		targets->nfcid1_len = 4;
526 		break;
527 	case MICROREAD_GATE_ID_MREAD_NFC_T3:
528 		targets->supported_protocols = NFC_PROTO_FELICA_MASK;
529 		memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_T3_UID], 8);
530 		targets->nfcid1_len = 8;
531 		break;
532 	default:
533 		pr_info("discard target discovered to gate 0x%x\n", gate);
534 		goto exit_free;
535 	}
536 
537 	r = nfc_targets_found(hdev->ndev, targets, 1);
538 
539 exit_free:
540 	kfree(targets);
541 
542 exit:
543 	kfree_skb(skb);
544 
545 	if (r)
546 		pr_err("Failed to handle discovered target err=%d\n", r);
547 }
548 
549 static int microread_event_received(struct nfc_hci_dev *hdev, u8 pipe,
550 				     u8 event, struct sk_buff *skb)
551 {
552 	int r;
553 	u8 gate = hdev->pipes[pipe].gate;
554 	u8 mode;
555 
556 	pr_info("Microread received event 0x%x to gate 0x%x\n", event, gate);
557 
558 	switch (event) {
559 	case MICROREAD_EVT_MREAD_CARD_FOUND:
560 		microread_target_discovered(hdev, gate, skb);
561 		return 0;
562 
563 	case MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_FROM_RF:
564 		if (skb->len < 1) {
565 			kfree_skb(skb);
566 			return -EPROTO;
567 		}
568 
569 		if (skb->data[skb->len - 1]) {
570 			kfree_skb(skb);
571 			return -EIO;
572 		}
573 
574 		skb_trim(skb, skb->len - 1);
575 
576 		r = nfc_tm_data_received(hdev->ndev, skb);
577 		break;
578 
579 	case MICROREAD_EVT_MCARD_FIELD_ON:
580 	case MICROREAD_EVT_MCARD_FIELD_OFF:
581 		kfree_skb(skb);
582 		return 0;
583 
584 	case MICROREAD_EVT_P2P_TARGET_ACTIVATED:
585 		r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
586 				     NFC_COMM_PASSIVE, skb->data,
587 				     skb->len);
588 
589 		kfree_skb(skb);
590 		break;
591 
592 	case MICROREAD_EVT_MCARD_EXCHANGE:
593 		if (skb->len < 1) {
594 			kfree_skb(skb);
595 			return -EPROTO;
596 		}
597 
598 		if (skb->data[skb->len-1]) {
599 			kfree_skb(skb);
600 			return -EIO;
601 		}
602 
603 		skb_trim(skb, skb->len - 1);
604 
605 		r = nfc_tm_data_received(hdev->ndev, skb);
606 		break;
607 
608 	case MICROREAD_EVT_P2P_TARGET_DEACTIVATED:
609 		kfree_skb(skb);
610 
611 		mode = 0xff;
612 		r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
613 				      MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1);
614 		if (r)
615 			break;
616 
617 		r = nfc_hci_send_event(hdev, gate,
618 				       MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL,
619 				       0);
620 		break;
621 
622 	default:
623 		return 1;
624 	}
625 
626 	return r;
627 }
628 
629 static struct nfc_hci_ops microread_hci_ops = {
630 	.open = microread_open,
631 	.close = microread_close,
632 	.hci_ready = microread_hci_ready,
633 	.xmit = microread_xmit,
634 	.start_poll = microread_start_poll,
635 	.dep_link_up = microread_dep_link_up,
636 	.dep_link_down = microread_dep_link_down,
637 	.target_from_gate = microread_target_from_gate,
638 	.complete_target_discovered = microread_complete_target_discovered,
639 	.im_transceive = microread_im_transceive,
640 	.tm_send = microread_tm_send,
641 	.check_presence = NULL,
642 	.event_received = microread_event_received,
643 };
644 
645 int microread_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
646 		    int phy_headroom, int phy_tailroom, int phy_payload,
647 		    struct nfc_hci_dev **hdev)
648 {
649 	struct microread_info *info;
650 	unsigned long quirks = 0;
651 	u32 protocols;
652 	struct nfc_hci_init_data init_data;
653 	int r;
654 
655 	info = kzalloc(sizeof(struct microread_info), GFP_KERNEL);
656 	if (!info) {
657 		r = -ENOMEM;
658 		goto err_info_alloc;
659 	}
660 
661 	info->phy_ops = phy_ops;
662 	info->phy_id = phy_id;
663 
664 	init_data.gate_count = ARRAY_SIZE(microread_gates);
665 	memcpy(init_data.gates, microread_gates, sizeof(microread_gates));
666 
667 	strcpy(init_data.session_id, "MICROREA");
668 
669 	set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
670 
671 	protocols = NFC_PROTO_JEWEL_MASK |
672 		    NFC_PROTO_MIFARE_MASK |
673 		    NFC_PROTO_FELICA_MASK |
674 		    NFC_PROTO_ISO14443_MASK |
675 		    NFC_PROTO_ISO14443_B_MASK |
676 		    NFC_PROTO_NFC_DEP_MASK;
677 
678 	info->hdev = nfc_hci_allocate_device(&microread_hci_ops, &init_data,
679 					     quirks, protocols, llc_name,
680 					     phy_headroom +
681 					     MICROREAD_CMDS_HEADROOM,
682 					     phy_tailroom +
683 					     MICROREAD_CMD_TAILROOM,
684 					     phy_payload);
685 	if (!info->hdev) {
686 		pr_err("Cannot allocate nfc hdev\n");
687 		r = -ENOMEM;
688 		goto err_alloc_hdev;
689 	}
690 
691 	nfc_hci_set_clientdata(info->hdev, info);
692 
693 	r = nfc_hci_register_device(info->hdev);
694 	if (r)
695 		goto err_regdev;
696 
697 	*hdev = info->hdev;
698 
699 	return 0;
700 
701 err_regdev:
702 	nfc_hci_free_device(info->hdev);
703 
704 err_alloc_hdev:
705 	kfree(info);
706 
707 err_info_alloc:
708 	return r;
709 }
710 EXPORT_SYMBOL(microread_probe);
711 
712 void microread_remove(struct nfc_hci_dev *hdev)
713 {
714 	struct microread_info *info = nfc_hci_get_clientdata(hdev);
715 
716 	nfc_hci_unregister_device(hdev);
717 	nfc_hci_free_device(hdev);
718 	kfree(info);
719 }
720 EXPORT_SYMBOL(microread_remove);
721 
722 MODULE_LICENSE("GPL");
723 MODULE_DESCRIPTION(DRIVER_DESC);
724