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