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