1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2014  STMicroelectronics SAS. All rights reserved.
4  */
5 
6 #include <net/nfc/hci.h>
7 
8 #include "st21nfca.h"
9 
10 #define ST21NFCA_NFCIP1_INITIATOR 0x00
11 #define ST21NFCA_NFCIP1_REQ 0xd4
12 #define ST21NFCA_NFCIP1_RES 0xd5
13 #define ST21NFCA_NFCIP1_ATR_REQ 0x00
14 #define ST21NFCA_NFCIP1_ATR_RES 0x01
15 #define ST21NFCA_NFCIP1_PSL_REQ 0x04
16 #define ST21NFCA_NFCIP1_PSL_RES 0x05
17 #define ST21NFCA_NFCIP1_DEP_REQ 0x06
18 #define ST21NFCA_NFCIP1_DEP_RES 0x07
19 
20 #define ST21NFCA_NFC_DEP_PFB_PNI(pfb)     ((pfb) & 0x03)
21 #define ST21NFCA_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
22 #define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
23 				((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT)
24 #define ST21NFCA_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04)
25 #define ST21NFCA_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
26 #define ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT 0x10
27 
28 #define ST21NFCA_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
29 				((pfb) & ST21NFCA_NFC_DEP_PFB_TIMEOUT_BIT)
30 
31 #define ST21NFCA_NFC_DEP_PFB_I_PDU          0x00
32 #define ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU   0x40
33 #define ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
34 
35 #define ST21NFCA_ATR_REQ_MIN_SIZE 17
36 #define ST21NFCA_ATR_REQ_MAX_SIZE 65
37 #define ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B 0x30
38 #define ST21NFCA_GB_BIT  0x02
39 
40 #define ST21NFCA_EVT_SEND_DATA		0x10
41 #define ST21NFCA_EVT_FIELD_ON           0x11
42 #define ST21NFCA_EVT_CARD_DEACTIVATED   0x12
43 #define ST21NFCA_EVT_CARD_ACTIVATED     0x13
44 #define ST21NFCA_EVT_FIELD_OFF          0x14
45 
46 #define ST21NFCA_EVT_CARD_F_BITRATE 0x16
47 #define ST21NFCA_EVT_READER_F_BITRATE 0x13
48 #define	ST21NFCA_PSL_REQ_SEND_SPEED(brs) (brs & 0x38)
49 #define ST21NFCA_PSL_REQ_RECV_SPEED(brs) (brs & 0x07)
50 #define ST21NFCA_PP2LRI(pp) ((pp & 0x30) >> 4)
51 #define ST21NFCA_CARD_BITRATE_212 0x01
52 #define ST21NFCA_CARD_BITRATE_424 0x02
53 
54 #define ST21NFCA_DEFAULT_TIMEOUT 0x0a
55 
56 
57 #define PROTOCOL_ERR(req) pr_err("%d: ST21NFCA Protocol error: %s\n", \
58 				 __LINE__, req)
59 
60 struct st21nfca_atr_req {
61 	u8 length;
62 	u8 cmd0;
63 	u8 cmd1;
64 	u8 nfcid3[NFC_NFCID3_MAXSIZE];
65 	u8 did;
66 	u8 bsi;
67 	u8 bri;
68 	u8 ppi;
69 	u8 gbi[];
70 } __packed;
71 
72 struct st21nfca_atr_res {
73 	u8 length;
74 	u8 cmd0;
75 	u8 cmd1;
76 	u8 nfcid3[NFC_NFCID3_MAXSIZE];
77 	u8 did;
78 	u8 bsi;
79 	u8 bri;
80 	u8 to;
81 	u8 ppi;
82 	u8 gbi[];
83 } __packed;
84 
85 struct st21nfca_psl_req {
86 	u8 length;
87 	u8 cmd0;
88 	u8 cmd1;
89 	u8 did;
90 	u8 brs;
91 	u8 fsl;
92 } __packed;
93 
94 struct st21nfca_psl_res {
95 	u8 length;
96 	u8 cmd0;
97 	u8 cmd1;
98 	u8 did;
99 } __packed;
100 
101 struct st21nfca_dep_req_res {
102 	u8 length;
103 	u8 cmd0;
104 	u8 cmd1;
105 	u8 pfb;
106 	u8 did;
107 	u8 nad;
108 } __packed;
109 
st21nfca_tx_work(struct work_struct * work)110 static void st21nfca_tx_work(struct work_struct *work)
111 {
112 	struct st21nfca_hci_info *info = container_of(work,
113 						struct st21nfca_hci_info,
114 						dep_info.tx_work);
115 
116 	struct nfc_dev *dev;
117 	struct sk_buff *skb;
118 
119 	if (info) {
120 		dev = info->hdev->ndev;
121 		skb = info->dep_info.tx_pending;
122 
123 		device_lock(&dev->dev);
124 
125 		nfc_hci_send_cmd_async(info->hdev, ST21NFCA_RF_READER_F_GATE,
126 				ST21NFCA_WR_XCHG_DATA, skb->data, skb->len,
127 				info->async_cb, info);
128 		device_unlock(&dev->dev);
129 		kfree_skb(skb);
130 	}
131 }
132 
st21nfca_im_send_pdu(struct st21nfca_hci_info * info,struct sk_buff * skb)133 static void st21nfca_im_send_pdu(struct st21nfca_hci_info *info,
134 						struct sk_buff *skb)
135 {
136 	info->dep_info.tx_pending = skb;
137 	schedule_work(&info->dep_info.tx_work);
138 }
139 
st21nfca_tm_send_atr_res(struct nfc_hci_dev * hdev,struct st21nfca_atr_req * atr_req)140 static int st21nfca_tm_send_atr_res(struct nfc_hci_dev *hdev,
141 				    struct st21nfca_atr_req *atr_req)
142 {
143 	struct st21nfca_atr_res *atr_res;
144 	struct sk_buff *skb;
145 	size_t gb_len;
146 	int r;
147 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
148 
149 	gb_len = atr_req->length - sizeof(struct st21nfca_atr_req);
150 	skb = alloc_skb(atr_req->length + 1, GFP_KERNEL);
151 	if (!skb)
152 		return -ENOMEM;
153 
154 	skb_put(skb, sizeof(struct st21nfca_atr_res));
155 
156 	atr_res = (struct st21nfca_atr_res *)skb->data;
157 	memset(atr_res, 0, sizeof(struct st21nfca_atr_res));
158 
159 	atr_res->length = atr_req->length + 1;
160 	atr_res->cmd0 = ST21NFCA_NFCIP1_RES;
161 	atr_res->cmd1 = ST21NFCA_NFCIP1_ATR_RES;
162 
163 	memcpy(atr_res->nfcid3, atr_req->nfcid3, 6);
164 	atr_res->bsi = 0x00;
165 	atr_res->bri = 0x00;
166 	atr_res->to = ST21NFCA_DEFAULT_TIMEOUT;
167 	atr_res->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B;
168 
169 	if (gb_len) {
170 		skb_put(skb, gb_len);
171 
172 		atr_res->ppi |= ST21NFCA_GB_BIT;
173 		memcpy(atr_res->gbi, atr_req->gbi, gb_len);
174 		r = nfc_set_remote_general_bytes(hdev->ndev, atr_res->gbi,
175 						  gb_len);
176 		if (r < 0) {
177 			kfree_skb(skb);
178 			return r;
179 		}
180 	}
181 
182 	info->dep_info.curr_nfc_dep_pni = 0;
183 
184 	r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
185 				ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
186 	kfree_skb(skb);
187 	return r;
188 }
189 
st21nfca_tm_recv_atr_req(struct nfc_hci_dev * hdev,struct sk_buff * skb)190 static int st21nfca_tm_recv_atr_req(struct nfc_hci_dev *hdev,
191 				    struct sk_buff *skb)
192 {
193 	struct st21nfca_atr_req *atr_req;
194 	size_t gb_len;
195 	int r;
196 
197 	skb_trim(skb, skb->len - 1);
198 
199 	if (!skb->len) {
200 		r = -EIO;
201 		goto exit;
202 	}
203 
204 	if (skb->len < ST21NFCA_ATR_REQ_MIN_SIZE) {
205 		r = -EPROTO;
206 		goto exit;
207 	}
208 
209 	atr_req = (struct st21nfca_atr_req *)skb->data;
210 
211 	if (atr_req->length < sizeof(struct st21nfca_atr_req)) {
212 		r = -EPROTO;
213 		goto exit;
214 	}
215 
216 	r = st21nfca_tm_send_atr_res(hdev, atr_req);
217 	if (r)
218 		goto exit;
219 
220 	gb_len = skb->len - sizeof(struct st21nfca_atr_req);
221 
222 	r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
223 			      NFC_COMM_PASSIVE, atr_req->gbi, gb_len);
224 	if (r)
225 		goto exit;
226 
227 	r = 0;
228 
229 exit:
230 	return r;
231 }
232 
st21nfca_tm_send_psl_res(struct nfc_hci_dev * hdev,struct st21nfca_psl_req * psl_req)233 static int st21nfca_tm_send_psl_res(struct nfc_hci_dev *hdev,
234 				    struct st21nfca_psl_req *psl_req)
235 {
236 	struct st21nfca_psl_res *psl_res;
237 	struct sk_buff *skb;
238 	u8 bitrate[2] = {0, 0};
239 	int r;
240 
241 	skb = alloc_skb(sizeof(struct st21nfca_psl_res), GFP_KERNEL);
242 	if (!skb)
243 		return -ENOMEM;
244 	skb_put(skb, sizeof(struct st21nfca_psl_res));
245 
246 	psl_res = (struct st21nfca_psl_res *)skb->data;
247 
248 	psl_res->length = sizeof(struct st21nfca_psl_res);
249 	psl_res->cmd0 = ST21NFCA_NFCIP1_RES;
250 	psl_res->cmd1 = ST21NFCA_NFCIP1_PSL_RES;
251 	psl_res->did = psl_req->did;
252 
253 	r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
254 				ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
255 	if (r < 0)
256 		goto error;
257 
258 	/*
259 	 * ST21NFCA only support P2P passive.
260 	 * PSL_REQ BRS value != 0 has only a meaning to
261 	 * change technology to type F.
262 	 * We change to BITRATE 424Kbits.
263 	 * In other case switch to BITRATE 106Kbits.
264 	 */
265 	if (ST21NFCA_PSL_REQ_SEND_SPEED(psl_req->brs) &&
266 	    ST21NFCA_PSL_REQ_RECV_SPEED(psl_req->brs)) {
267 		bitrate[0] = ST21NFCA_CARD_BITRATE_424;
268 		bitrate[1] = ST21NFCA_CARD_BITRATE_424;
269 	}
270 
271 	/* Send an event to change bitrate change event to card f */
272 	r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
273 			ST21NFCA_EVT_CARD_F_BITRATE, bitrate, 2);
274 error:
275 	kfree_skb(skb);
276 	return r;
277 }
278 
st21nfca_tm_recv_psl_req(struct nfc_hci_dev * hdev,struct sk_buff * skb)279 static int st21nfca_tm_recv_psl_req(struct nfc_hci_dev *hdev,
280 				    struct sk_buff *skb)
281 {
282 	struct st21nfca_psl_req *psl_req;
283 	int r;
284 
285 	skb_trim(skb, skb->len - 1);
286 
287 	if (!skb->len) {
288 		r = -EIO;
289 		goto exit;
290 	}
291 
292 	psl_req = (struct st21nfca_psl_req *)skb->data;
293 
294 	if (skb->len < sizeof(struct st21nfca_psl_req)) {
295 		r = -EIO;
296 		goto exit;
297 	}
298 
299 	r = st21nfca_tm_send_psl_res(hdev, psl_req);
300 exit:
301 	return r;
302 }
303 
st21nfca_tm_send_dep_res(struct nfc_hci_dev * hdev,struct sk_buff * skb)304 int st21nfca_tm_send_dep_res(struct nfc_hci_dev *hdev, struct sk_buff *skb)
305 {
306 	int r;
307 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
308 
309 	*(u8 *)skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
310 	*(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_RES;
311 	*(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_RES;
312 	*(u8 *)skb_push(skb, 1) = skb->len;
313 
314 	r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
315 			ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
316 	kfree_skb(skb);
317 
318 	return r;
319 }
320 EXPORT_SYMBOL(st21nfca_tm_send_dep_res);
321 
st21nfca_tm_recv_dep_req(struct nfc_hci_dev * hdev,struct sk_buff * skb)322 static int st21nfca_tm_recv_dep_req(struct nfc_hci_dev *hdev,
323 				    struct sk_buff *skb)
324 {
325 	struct st21nfca_dep_req_res *dep_req;
326 	u8 size;
327 	int r;
328 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
329 
330 	skb_trim(skb, skb->len - 1);
331 
332 	size = 4;
333 
334 	dep_req = (struct st21nfca_dep_req_res *)skb->data;
335 	if (skb->len < size) {
336 		r = -EIO;
337 		goto exit;
338 	}
339 
340 	if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_req->pfb))
341 		size++;
342 	if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_req->pfb))
343 		size++;
344 
345 	if (skb->len < size) {
346 		r = -EIO;
347 		goto exit;
348 	}
349 
350 	/* Receiving DEP_REQ - Decoding */
351 	switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_req->pfb)) {
352 	case ST21NFCA_NFC_DEP_PFB_I_PDU:
353 		info->dep_info.curr_nfc_dep_pni =
354 				ST21NFCA_NFC_DEP_PFB_PNI(dep_req->pfb);
355 		break;
356 	case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
357 		pr_err("Received a ACK/NACK PDU\n");
358 		break;
359 	case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
360 		pr_err("Received a SUPERVISOR PDU\n");
361 		break;
362 	}
363 
364 	skb_pull(skb, size);
365 
366 	return nfc_tm_data_received(hdev->ndev, skb);
367 exit:
368 	return r;
369 }
370 
st21nfca_tm_event_send_data(struct nfc_hci_dev * hdev,struct sk_buff * skb)371 static int st21nfca_tm_event_send_data(struct nfc_hci_dev *hdev,
372 				struct sk_buff *skb)
373 {
374 	u8 cmd0, cmd1;
375 	int r;
376 
377 	cmd0 = skb->data[1];
378 	switch (cmd0) {
379 	case ST21NFCA_NFCIP1_REQ:
380 		cmd1 = skb->data[2];
381 		switch (cmd1) {
382 		case ST21NFCA_NFCIP1_ATR_REQ:
383 			r = st21nfca_tm_recv_atr_req(hdev, skb);
384 			break;
385 		case ST21NFCA_NFCIP1_PSL_REQ:
386 			r = st21nfca_tm_recv_psl_req(hdev, skb);
387 			break;
388 		case ST21NFCA_NFCIP1_DEP_REQ:
389 			r = st21nfca_tm_recv_dep_req(hdev, skb);
390 			break;
391 		default:
392 			return 1;
393 		}
394 		break;
395 	default:
396 		return 1;
397 	}
398 	return r;
399 }
400 
401 /*
402  * Returns:
403  * <= 0: driver handled the event, skb consumed
404  *    1: driver does not handle the event, please do standard processing
405  */
st21nfca_dep_event_received(struct nfc_hci_dev * hdev,u8 event,struct sk_buff * skb)406 int st21nfca_dep_event_received(struct nfc_hci_dev *hdev,
407 				u8 event, struct sk_buff *skb)
408 {
409 	int r = 0;
410 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
411 
412 	pr_debug("dep event: %d\n", event);
413 
414 	switch (event) {
415 	case ST21NFCA_EVT_CARD_ACTIVATED:
416 		info->dep_info.curr_nfc_dep_pni = 0;
417 		break;
418 	case ST21NFCA_EVT_CARD_DEACTIVATED:
419 		break;
420 	case ST21NFCA_EVT_FIELD_ON:
421 		break;
422 	case ST21NFCA_EVT_FIELD_OFF:
423 		break;
424 	case ST21NFCA_EVT_SEND_DATA:
425 		r = st21nfca_tm_event_send_data(hdev, skb);
426 		if (r < 0)
427 			return r;
428 		return 0;
429 	default:
430 		nfc_err(&hdev->ndev->dev, "Unexpected event on card f gate\n");
431 		return 1;
432 	}
433 	kfree_skb(skb);
434 	return r;
435 }
436 EXPORT_SYMBOL(st21nfca_dep_event_received);
437 
st21nfca_im_send_psl_req(struct nfc_hci_dev * hdev,u8 did,u8 bsi,u8 bri,u8 lri)438 static void st21nfca_im_send_psl_req(struct nfc_hci_dev *hdev, u8 did, u8 bsi,
439 				     u8 bri, u8 lri)
440 {
441 	struct sk_buff *skb;
442 	struct st21nfca_psl_req *psl_req;
443 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
444 
445 	skb =
446 	    alloc_skb(sizeof(struct st21nfca_psl_req) + 1, GFP_KERNEL);
447 	if (!skb)
448 		return;
449 	skb_reserve(skb, 1);
450 
451 	skb_put(skb, sizeof(struct st21nfca_psl_req));
452 	psl_req = (struct st21nfca_psl_req *) skb->data;
453 
454 	psl_req->length = sizeof(struct st21nfca_psl_req);
455 	psl_req->cmd0 = ST21NFCA_NFCIP1_REQ;
456 	psl_req->cmd1 = ST21NFCA_NFCIP1_PSL_REQ;
457 	psl_req->did = did;
458 	psl_req->brs = (0x30 & bsi << 4) | (bri & 0x03);
459 	psl_req->fsl = lri;
460 
461 	*(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
462 
463 	st21nfca_im_send_pdu(info, skb);
464 }
465 
466 #define ST21NFCA_CB_TYPE_READER_F 1
st21nfca_im_recv_atr_res_cb(void * context,struct sk_buff * skb,int err)467 static void st21nfca_im_recv_atr_res_cb(void *context, struct sk_buff *skb,
468 					int err)
469 {
470 	struct st21nfca_hci_info *info = context;
471 	struct st21nfca_atr_res *atr_res;
472 	int r;
473 
474 	if (err != 0)
475 		return;
476 
477 	if (!skb)
478 		return;
479 
480 	switch (info->async_cb_type) {
481 	case ST21NFCA_CB_TYPE_READER_F:
482 		skb_trim(skb, skb->len - 1);
483 		atr_res = (struct st21nfca_atr_res *)skb->data;
484 		r = nfc_set_remote_general_bytes(info->hdev->ndev,
485 				atr_res->gbi,
486 				skb->len - sizeof(struct st21nfca_atr_res));
487 		if (r < 0)
488 			return;
489 
490 		if (atr_res->to >= 0x0e)
491 			info->dep_info.to = 0x0e;
492 		else
493 			info->dep_info.to = atr_res->to + 1;
494 
495 		info->dep_info.to |= 0x10;
496 
497 		r = nfc_dep_link_is_up(info->hdev->ndev, info->dep_info.idx,
498 					NFC_COMM_PASSIVE, NFC_RF_INITIATOR);
499 		if (r < 0)
500 			return;
501 
502 		info->dep_info.curr_nfc_dep_pni = 0;
503 		if (ST21NFCA_PP2LRI(atr_res->ppi) != info->dep_info.lri)
504 			st21nfca_im_send_psl_req(info->hdev, atr_res->did,
505 						atr_res->bsi, atr_res->bri,
506 						ST21NFCA_PP2LRI(atr_res->ppi));
507 		break;
508 	default:
509 		kfree_skb(skb);
510 		break;
511 	}
512 }
513 
st21nfca_im_send_atr_req(struct nfc_hci_dev * hdev,u8 * gb,size_t gb_len)514 int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len)
515 {
516 	struct sk_buff *skb;
517 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
518 	struct st21nfca_atr_req *atr_req;
519 	struct nfc_target *target;
520 	uint size;
521 
522 	info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
523 	size = ST21NFCA_ATR_REQ_MIN_SIZE + gb_len;
524 	if (size > ST21NFCA_ATR_REQ_MAX_SIZE) {
525 		PROTOCOL_ERR("14.6.1.1");
526 		return -EINVAL;
527 	}
528 
529 	skb =
530 	    alloc_skb(sizeof(struct st21nfca_atr_req) + gb_len + 1, GFP_KERNEL);
531 	if (!skb)
532 		return -ENOMEM;
533 
534 	skb_reserve(skb, 1);
535 
536 	skb_put(skb, sizeof(struct st21nfca_atr_req));
537 
538 	atr_req = (struct st21nfca_atr_req *)skb->data;
539 	memset(atr_req, 0, sizeof(struct st21nfca_atr_req));
540 
541 	atr_req->cmd0 = ST21NFCA_NFCIP1_REQ;
542 	atr_req->cmd1 = ST21NFCA_NFCIP1_ATR_REQ;
543 	memset(atr_req->nfcid3, 0, NFC_NFCID3_MAXSIZE);
544 	target = hdev->ndev->targets;
545 
546 	if (target->sensf_res_len > 0)
547 		memcpy(atr_req->nfcid3, target->sensf_res,
548 				target->sensf_res_len);
549 	else
550 		get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
551 
552 	atr_req->did = 0x0;
553 
554 	atr_req->bsi = 0x00;
555 	atr_req->bri = 0x00;
556 	atr_req->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B;
557 	if (gb_len) {
558 		atr_req->ppi |= ST21NFCA_GB_BIT;
559 		skb_put_data(skb, gb, gb_len);
560 	}
561 	atr_req->length = sizeof(struct st21nfca_atr_req) + hdev->gb_len;
562 
563 	*(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10; /* timeout */
564 
565 	info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
566 	info->async_cb_context = info;
567 	info->async_cb = st21nfca_im_recv_atr_res_cb;
568 	info->dep_info.bri = atr_req->bri;
569 	info->dep_info.bsi = atr_req->bsi;
570 	info->dep_info.lri = ST21NFCA_PP2LRI(atr_req->ppi);
571 
572 	return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
573 				ST21NFCA_WR_XCHG_DATA, skb->data,
574 				skb->len, info->async_cb, info);
575 }
576 EXPORT_SYMBOL(st21nfca_im_send_atr_req);
577 
st21nfca_im_recv_dep_res_cb(void * context,struct sk_buff * skb,int err)578 static void st21nfca_im_recv_dep_res_cb(void *context, struct sk_buff *skb,
579 					int err)
580 {
581 	struct st21nfca_hci_info *info = context;
582 	struct st21nfca_dep_req_res *dep_res;
583 
584 	int size;
585 
586 	if (err != 0)
587 		return;
588 
589 	if (!skb)
590 		return;
591 
592 	switch (info->async_cb_type) {
593 	case ST21NFCA_CB_TYPE_READER_F:
594 		dep_res = (struct st21nfca_dep_req_res *)skb->data;
595 
596 		size = 3;
597 		if (skb->len < size)
598 			goto exit;
599 
600 		if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_res->pfb))
601 			size++;
602 		if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_res->pfb))
603 			size++;
604 
605 		if (skb->len < size)
606 			goto exit;
607 
608 		skb_trim(skb, skb->len - 1);
609 
610 		/* Receiving DEP_REQ - Decoding */
611 		switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_res->pfb)) {
612 		case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
613 			pr_err("Received a ACK/NACK PDU\n");
614 			fallthrough;
615 		case ST21NFCA_NFC_DEP_PFB_I_PDU:
616 			info->dep_info.curr_nfc_dep_pni =
617 			    ST21NFCA_NFC_DEP_PFB_PNI(dep_res->pfb + 1);
618 			size++;
619 			skb_pull(skb, size);
620 			nfc_tm_data_received(info->hdev->ndev, skb);
621 			break;
622 		case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
623 			pr_err("Received a SUPERVISOR PDU\n");
624 			skb_pull(skb, size);
625 			*(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
626 			*(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
627 			*(u8 *)skb_push(skb, 1) = skb->len;
628 			*(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
629 
630 			st21nfca_im_send_pdu(info, skb);
631 			break;
632 		}
633 
634 		return;
635 	default:
636 		break;
637 	}
638 
639 exit:
640 	kfree_skb(skb);
641 }
642 
st21nfca_im_send_dep_req(struct nfc_hci_dev * hdev,struct sk_buff * skb)643 int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb)
644 {
645 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
646 
647 	info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
648 	info->async_cb_context = info;
649 	info->async_cb = st21nfca_im_recv_dep_res_cb;
650 
651 	*(u8 *)skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
652 	*(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
653 	*(u8 *)skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
654 	*(u8 *)skb_push(skb, 1) = skb->len;
655 
656 	*(u8 *)skb_push(skb, 1) = info->dep_info.to | 0x10;
657 
658 	return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
659 				      ST21NFCA_WR_XCHG_DATA,
660 				      skb->data, skb->len,
661 				      info->async_cb, info);
662 }
663 EXPORT_SYMBOL(st21nfca_im_send_dep_req);
664 
st21nfca_dep_init(struct nfc_hci_dev * hdev)665 void st21nfca_dep_init(struct nfc_hci_dev *hdev)
666 {
667 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
668 
669 	INIT_WORK(&info->dep_info.tx_work, st21nfca_tx_work);
670 	info->dep_info.curr_nfc_dep_pni = 0;
671 	info->dep_info.idx = 0;
672 	info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
673 }
674 EXPORT_SYMBOL(st21nfca_dep_init);
675 
st21nfca_dep_deinit(struct nfc_hci_dev * hdev)676 void st21nfca_dep_deinit(struct nfc_hci_dev *hdev)
677 {
678 	struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
679 
680 	cancel_work_sync(&info->dep_info.tx_work);
681 }
682 EXPORT_SYMBOL(st21nfca_dep_deinit);
683