blob: 206285210ab5a8610064be6acaeb85ddb1528741 [file] [log] [blame]
Christophe Ricard1892bf82014-05-20 22:21:59 +02001/*
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"
Christophe Ricard1892bf82014-05-20 22:21:59 +020020
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
Christophe Ricarda4415e72014-11-13 00:30:39 +010051#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
Christophe Ricard1892bf82014-05-20 22:21:59 +020057#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
71struct 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
83struct 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
96struct st21nfca_psl_req {
97 u8 length;
98 u8 cmd0;
99 u8 cmd1;
100 u8 did;
101 u8 brs;
102 u8 fsl;
103} __packed;
104
105struct st21nfca_psl_res {
106 u8 length;
107 u8 cmd0;
108 u8 cmd1;
109 u8 did;
110} __packed;
111
112struct 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
121static 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;
Christophe Ricard3e6df9192014-07-28 18:11:31 +0200129
Christophe Ricard1892bf82014-05-20 22:21:59 +0200130 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,
Christophe Ricardcc3faac2014-09-13 10:28:43 +0200137 ST21NFCA_WR_XCHG_DATA, skb->data, skb->len,
138 info->async_cb, info);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200139 device_unlock(&dev->dev);
140 kfree_skb(skb);
141 }
142}
143
144static 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
151static 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 return r;
189 }
190
191 info->dep_info.curr_nfc_dep_pni = 0;
192
Christophe Ricardecc65222014-09-13 10:28:44 +0200193 r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
Christophe Ricard1892bf82014-05-20 22:21:59 +0200194 ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
Christophe Ricardecc65222014-09-13 10:28:44 +0200195 kfree_skb(skb);
196 return r;
Christophe Ricard1892bf82014-05-20 22:21:59 +0200197}
198
199static int st21nfca_tm_recv_atr_req(struct nfc_hci_dev *hdev,
200 struct sk_buff *skb)
201{
202 struct st21nfca_atr_req *atr_req;
203 size_t gb_len;
204 int r;
205
206 skb_trim(skb, skb->len - 1);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200207
208 if (!skb->len) {
209 r = -EIO;
210 goto exit;
211 }
212
213 if (skb->len < ST21NFCA_ATR_REQ_MIN_SIZE) {
214 r = -EPROTO;
215 goto exit;
216 }
217
218 atr_req = (struct st21nfca_atr_req *)skb->data;
219
Suren Baghdasaryan85e5fc72017-08-17 10:43:14 -0700220 if (atr_req->length < sizeof(struct st21nfca_atr_req) ||
221 atr_req->length > skb->len) {
Christophe Ricard56f1ffc2014-08-11 00:04:56 +0200222 r = -EPROTO;
223 goto exit;
224 }
225
Christophe Ricard1892bf82014-05-20 22:21:59 +0200226 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
239exit:
240 return r;
241}
242
243static 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};
Christophe Ricard1892bf82014-05-20 22:21:59 +0200249 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);
Christophe Ricardecc65222014-09-13 10:28:44 +0200265 if (r < 0)
266 goto error;
Christophe Ricard1892bf82014-05-20 22:21:59 +0200267
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 */
Christophe Ricardecc65222014-09-13 10:28:44 +0200282 r = nfc_hci_send_event(hdev, ST21NFCA_RF_CARD_F_GATE,
Christophe Ricard1892bf82014-05-20 22:21:59 +0200283 ST21NFCA_EVT_CARD_F_BITRATE, bitrate, 2);
Christophe Ricardecc65222014-09-13 10:28:44 +0200284error:
285 kfree_skb(skb);
286 return r;
Christophe Ricard1892bf82014-05-20 22:21:59 +0200287}
288
289static 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);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200296
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);
310exit:
311 return r;
312}
313
314int 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,
Christophe Ricardcc3faac2014-09-13 10:28:43 +0200325 ST21NFCA_EVT_SEND_DATA, skb->data, skb->len);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200326 kfree_skb(skb);
327
328 return r;
329}
330EXPORT_SYMBOL(st21nfca_tm_send_dep_res);
331
332static 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);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200341
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
Christophe Ricard1892bf82014-05-20 22:21:59 +0200374 skb_pull(skb, size);
375
376 return nfc_tm_data_received(hdev->ndev, skb);
377exit:
378 return r;
379}
380
Christophe Ricarda4415e72014-11-13 00:30:39 +0100381static int st21nfca_tm_event_send_data(struct nfc_hci_dev *hdev,
382 struct sk_buff *skb)
Christophe Ricard1892bf82014-05-20 22:21:59 +0200383{
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}
Christophe Ricarda4415e72014-11-13 00:30:39 +0100409
410/*
411 * Returns:
412 * <= 0: driver handled the event, skb consumed
413 * 1: driver does not handle the event, please do standard processing
414 */
415int 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:
Christophe Ricarda9e062d2015-10-25 22:54:47 +0100439 nfc_err(&hdev->ndev->dev, "Unexpected event on card f gate\n");
Christophe Ricarda4415e72014-11-13 00:30:39 +0100440 return 1;
441 }
442 kfree_skb(skb);
443 return r;
444}
445EXPORT_SYMBOL(st21nfca_dep_event_received);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200446
447static void st21nfca_im_send_psl_req(struct nfc_hci_dev *hdev, u8 did, u8 bsi,
448 u8 bri, u8 lri)
449{
450 struct sk_buff *skb;
451 struct st21nfca_psl_req *psl_req;
452 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
453
454 skb =
455 alloc_skb(sizeof(struct st21nfca_psl_req) + 1, GFP_KERNEL);
456 if (!skb)
457 return;
458 skb_reserve(skb, 1);
459
460 skb_put(skb, sizeof(struct st21nfca_psl_req));
461 psl_req = (struct st21nfca_psl_req *) skb->data;
462
463 psl_req->length = sizeof(struct st21nfca_psl_req);
464 psl_req->cmd0 = ST21NFCA_NFCIP1_REQ;
465 psl_req->cmd1 = ST21NFCA_NFCIP1_PSL_REQ;
466 psl_req->did = did;
467 psl_req->brs = (0x30 & bsi << 4) | (bri & 0x03);
468 psl_req->fsl = lri;
469
470 *skb_push(skb, 1) = info->dep_info.to | 0x10;
471
472 st21nfca_im_send_pdu(info, skb);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200473}
474
475#define ST21NFCA_CB_TYPE_READER_F 1
476static void st21nfca_im_recv_atr_res_cb(void *context, struct sk_buff *skb,
477 int err)
478{
479 struct st21nfca_hci_info *info = context;
480 struct st21nfca_atr_res *atr_res;
481 int r;
482
483 if (err != 0)
484 return;
485
Christophe Ricard459e7942014-09-13 10:28:52 +0200486 if (!skb)
Christophe Ricard1892bf82014-05-20 22:21:59 +0200487 return;
488
489 switch (info->async_cb_type) {
490 case ST21NFCA_CB_TYPE_READER_F:
491 skb_trim(skb, skb->len - 1);
492 atr_res = (struct st21nfca_atr_res *)skb->data;
493 r = nfc_set_remote_general_bytes(info->hdev->ndev,
494 atr_res->gbi,
495 skb->len - sizeof(struct st21nfca_atr_res));
496 if (r < 0)
497 return;
498
499 if (atr_res->to >= 0x0e)
500 info->dep_info.to = 0x0e;
501 else
502 info->dep_info.to = atr_res->to + 1;
503
504 info->dep_info.to |= 0x10;
505
506 r = nfc_dep_link_is_up(info->hdev->ndev, info->dep_info.idx,
507 NFC_COMM_PASSIVE, NFC_RF_INITIATOR);
508 if (r < 0)
509 return;
510
511 info->dep_info.curr_nfc_dep_pni = 0;
512 if (ST21NFCA_PP2LRI(atr_res->ppi) != info->dep_info.lri)
513 st21nfca_im_send_psl_req(info->hdev, atr_res->did,
514 atr_res->bsi, atr_res->bri,
515 ST21NFCA_PP2LRI(atr_res->ppi));
516 break;
517 default:
Christophe Ricard32b41d82014-08-11 00:04:53 +0200518 kfree_skb(skb);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200519 break;
520 }
521}
522
523int st21nfca_im_send_atr_req(struct nfc_hci_dev *hdev, u8 *gb, size_t gb_len)
524{
525 struct sk_buff *skb;
526 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
527 struct st21nfca_atr_req *atr_req;
528 struct nfc_target *target;
529 uint size;
530
531 info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
532 size = ST21NFCA_ATR_REQ_MIN_SIZE + gb_len;
533 if (size > ST21NFCA_ATR_REQ_MAX_SIZE) {
534 PROTOCOL_ERR("14.6.1.1");
535 return -EINVAL;
536 }
537
538 skb =
539 alloc_skb(sizeof(struct st21nfca_atr_req) + gb_len + 1, GFP_KERNEL);
540 if (!skb)
541 return -ENOMEM;
542
543 skb_reserve(skb, 1);
544
545 skb_put(skb, sizeof(struct st21nfca_atr_req));
546
547 atr_req = (struct st21nfca_atr_req *)skb->data;
548 memset(atr_req, 0, sizeof(struct st21nfca_atr_req));
549
550 atr_req->cmd0 = ST21NFCA_NFCIP1_REQ;
551 atr_req->cmd1 = ST21NFCA_NFCIP1_ATR_REQ;
552 memset(atr_req->nfcid3, 0, NFC_NFCID3_MAXSIZE);
553 target = hdev->ndev->targets;
554
Christophe Ricard72030a22014-08-11 00:04:52 +0200555 if (target->sensf_res_len > 0)
Christophe Ricard1892bf82014-05-20 22:21:59 +0200556 memcpy(atr_req->nfcid3, target->sensf_res,
557 target->sensf_res_len);
558 else
559 get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
560
561 atr_req->did = 0x0;
562
563 atr_req->bsi = 0x00;
564 atr_req->bri = 0x00;
565 atr_req->ppi = ST21NFCA_LR_BITS_PAYLOAD_SIZE_254B;
566 if (gb_len) {
567 atr_req->ppi |= ST21NFCA_GB_BIT;
568 memcpy(skb_put(skb, gb_len), gb, gb_len);
569 }
570 atr_req->length = sizeof(struct st21nfca_atr_req) + hdev->gb_len;
571
572 *skb_push(skb, 1) = info->dep_info.to | 0x10; /* timeout */
573
574 info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
575 info->async_cb_context = info;
576 info->async_cb = st21nfca_im_recv_atr_res_cb;
577 info->dep_info.bri = atr_req->bri;
578 info->dep_info.bsi = atr_req->bsi;
579 info->dep_info.lri = ST21NFCA_PP2LRI(atr_req->ppi);
580
581 return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
582 ST21NFCA_WR_XCHG_DATA, skb->data,
583 skb->len, info->async_cb, info);
584}
585EXPORT_SYMBOL(st21nfca_im_send_atr_req);
586
587static void st21nfca_im_recv_dep_res_cb(void *context, struct sk_buff *skb,
588 int err)
589{
590 struct st21nfca_hci_info *info = context;
591 struct st21nfca_dep_req_res *dep_res;
592
593 int size;
594
595 if (err != 0)
596 return;
597
Christophe Ricard459e7942014-09-13 10:28:52 +0200598 if (!skb)
Christophe Ricard1892bf82014-05-20 22:21:59 +0200599 return;
600
601 switch (info->async_cb_type) {
602 case ST21NFCA_CB_TYPE_READER_F:
603 dep_res = (struct st21nfca_dep_req_res *)skb->data;
604
605 size = 3;
606 if (skb->len < size)
607 goto exit;
608
609 if (ST21NFCA_NFC_DEP_DID_BIT_SET(dep_res->pfb))
610 size++;
611 if (ST21NFCA_NFC_DEP_NAD_BIT_SET(dep_res->pfb))
612 size++;
613
614 if (skb->len < size)
615 goto exit;
616
617 skb_trim(skb, skb->len - 1);
618
619 /* Receiving DEP_REQ - Decoding */
620 switch (ST21NFCA_NFC_DEP_PFB_TYPE(dep_res->pfb)) {
621 case ST21NFCA_NFC_DEP_PFB_ACK_NACK_PDU:
622 pr_err("Received a ACK/NACK PDU\n");
623 case ST21NFCA_NFC_DEP_PFB_I_PDU:
624 info->dep_info.curr_nfc_dep_pni =
625 ST21NFCA_NFC_DEP_PFB_PNI(dep_res->pfb + 1);
626 size++;
627 skb_pull(skb, size);
628 nfc_tm_data_received(info->hdev->ndev, skb);
629 break;
630 case ST21NFCA_NFC_DEP_PFB_SUPERVISOR_PDU:
631 pr_err("Received a SUPERVISOR PDU\n");
632 skb_pull(skb, size);
633 *skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
634 *skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
635 *skb_push(skb, 1) = skb->len;
636 *skb_push(skb, 1) = info->dep_info.to | 0x10;
637
638 st21nfca_im_send_pdu(info, skb);
639 break;
640 }
641
642 return;
643 default:
644 break;
645 }
646
647exit:
Christophe Ricard32b41d82014-08-11 00:04:53 +0200648 kfree_skb(skb);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200649}
650
651int st21nfca_im_send_dep_req(struct nfc_hci_dev *hdev, struct sk_buff *skb)
652{
653 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
654
655 info->async_cb_type = ST21NFCA_CB_TYPE_READER_F;
656 info->async_cb_context = info;
657 info->async_cb = st21nfca_im_recv_dep_res_cb;
658
659 *skb_push(skb, 1) = info->dep_info.curr_nfc_dep_pni;
660 *skb_push(skb, 1) = ST21NFCA_NFCIP1_DEP_REQ;
661 *skb_push(skb, 1) = ST21NFCA_NFCIP1_REQ;
662 *skb_push(skb, 1) = skb->len;
663
664 *skb_push(skb, 1) = info->dep_info.to | 0x10;
665
666 return nfc_hci_send_cmd_async(hdev, ST21NFCA_RF_READER_F_GATE,
667 ST21NFCA_WR_XCHG_DATA,
668 skb->data, skb->len,
669 info->async_cb, info);
670}
671EXPORT_SYMBOL(st21nfca_im_send_dep_req);
672
673void st21nfca_dep_init(struct nfc_hci_dev *hdev)
674{
675 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
676
677 INIT_WORK(&info->dep_info.tx_work, st21nfca_tx_work);
678 info->dep_info.curr_nfc_dep_pni = 0;
679 info->dep_info.idx = 0;
680 info->dep_info.to = ST21NFCA_DEFAULT_TIMEOUT;
681}
682EXPORT_SYMBOL(st21nfca_dep_init);
683
684void st21nfca_dep_deinit(struct nfc_hci_dev *hdev)
685{
686 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
687
688 cancel_work_sync(&info->dep_info.tx_work);
689}
690EXPORT_SYMBOL(st21nfca_dep_deinit);