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