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