blob: f864ce19e13dbc5d7734ca2ff8308047089c2d70 [file] [log] [blame]
Thierry Escande7d0911c2013-09-19 17:55:29 +02001/*
2 * NFC Digital Protocol stack
3 * Copyright (c) 2013, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
Samuel Ortizc5da0e42013-09-20 09:05:48 +020016#define pr_fmt(fmt) "digital: %s: " fmt, __func__
17
Thierry Escande7d0911c2013-09-19 17:55:29 +020018#include "digital.h"
19
Mark A. Greera80509c2014-09-23 16:38:11 -070020#define DIGITAL_NFC_DEP_N_RETRY_NACK 2
Mark A. Greer384ab1d2014-09-23 16:38:13 -070021#define DIGITAL_NFC_DEP_N_RETRY_ATN 2
Mark A. Greera80509c2014-09-23 16:38:11 -070022
Thierry Escande7d0911c2013-09-19 17:55:29 +020023#define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
24#define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5
25
26#define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0
27
28#define DIGITAL_CMD_ATR_REQ 0x00
29#define DIGITAL_CMD_ATR_RES 0x01
30#define DIGITAL_CMD_PSL_REQ 0x04
31#define DIGITAL_CMD_PSL_RES 0x05
32#define DIGITAL_CMD_DEP_REQ 0x06
33#define DIGITAL_CMD_DEP_RES 0x07
34
35#define DIGITAL_ATR_REQ_MIN_SIZE 16
36#define DIGITAL_ATR_REQ_MAX_SIZE 64
37
Thierry Escande1a09c562016-07-08 15:52:45 +020038#define DIGITAL_ATR_RES_TO_WT(s) ((s) & 0xF)
39
Mark A. Greer05afedc2014-09-23 16:38:05 -070040#define DIGITAL_DID_MAX 14
41
Mark A. Greerb08147c2014-09-23 16:38:08 -070042#define DIGITAL_PAYLOAD_SIZE_MAX 254
43#define DIGITAL_PAYLOAD_BITS_TO_PP(s) (((s) & 0x3) << 4)
44#define DIGITAL_PAYLOAD_PP_TO_BITS(s) (((s) >> 4) & 0x3)
45#define DIGITAL_PAYLOAD_BITS_TO_FSL(s) ((s) & 0x3)
46#define DIGITAL_PAYLOAD_FSL_TO_BITS(s) ((s) & 0x3)
47
Thierry Escande7d0911c2013-09-19 17:55:29 +020048#define DIGITAL_GB_BIT 0x02
49
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -070050#define DIGITAL_NFC_DEP_REQ_RES_HEADROOM 2 /* SoD: [SB (NFC-A)] + LEN */
51#define DIGITAL_NFC_DEP_REQ_RES_TAILROOM 2 /* EoD: 2-byte CRC */
52
Thierry Escande7d0911c2013-09-19 17:55:29 +020053#define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
54
55#define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -070056#define DIGITAL_NFC_DEP_PFB_MI_BIT 0x10
57#define DIGITAL_NFC_DEP_PFB_NACK_BIT 0x10
Mark A. Greer05afedc2014-09-23 16:38:05 -070058#define DIGITAL_NFC_DEP_PFB_DID_BIT 0x04
Thierry Escande7d0911c2013-09-19 17:55:29 +020059
60#define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
61 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -070062#define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_MI_BIT)
63#define DIGITAL_NFC_DEP_NACK_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_NACK_BIT)
Thierry Escande7d0911c2013-09-19 17:55:29 +020064#define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
Mark A. Greer05afedc2014-09-23 16:38:05 -070065#define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT)
Thierry Escande7d0911c2013-09-19 17:55:29 +020066#define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
67
Thierry Escanded85a3012016-07-08 15:52:46 +020068#define DIGITAL_NFC_DEP_RTOX_VALUE(data) ((data) & 0x3F)
69#define DIGITAL_NFC_DEP_RTOX_MAX 59
70
Thierry Escande7d0911c2013-09-19 17:55:29 +020071#define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
72#define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
73#define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
74
75struct digital_atr_req {
76 u8 dir;
77 u8 cmd;
78 u8 nfcid3[10];
79 u8 did;
80 u8 bs;
81 u8 br;
82 u8 pp;
83 u8 gb[0];
84} __packed;
85
86struct digital_atr_res {
87 u8 dir;
88 u8 cmd;
89 u8 nfcid3[10];
90 u8 did;
91 u8 bs;
92 u8 br;
93 u8 to;
94 u8 pp;
95 u8 gb[0];
96} __packed;
97
98struct digital_psl_req {
99 u8 dir;
100 u8 cmd;
101 u8 did;
102 u8 brs;
103 u8 fsl;
104} __packed;
105
106struct digital_psl_res {
107 u8 dir;
108 u8 cmd;
109 u8 did;
110} __packed;
111
112struct digital_dep_req_res {
113 u8 dir;
114 u8 cmd;
115 u8 pfb;
116} __packed;
117
118static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
119 struct sk_buff *resp);
Mark A. Greerc12715a2014-09-23 16:38:10 -0700120static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
121 struct sk_buff *resp);
Thierry Escande7d0911c2013-09-19 17:55:29 +0200122
Mark A. Greerb08147c2014-09-23 16:38:08 -0700123static const u8 digital_payload_bits_map[4] = {
124 [0] = 64,
125 [1] = 128,
126 [2] = 192,
127 [3] = 254
128};
129
Thierry Escande1a09c562016-07-08 15:52:45 +0200130/* Response Waiting Time for ATR_RES PDU in ms
131 *
132 * RWT(ATR_RES) = RWT(nfcdep,activation) + dRWT(nfcdep) + dT(nfcdep,initiator)
133 *
134 * with:
135 * RWT(nfcdep,activation) = 4096 * 2^12 / f(c) s
136 * dRWT(nfcdep) = 16 / f(c) s
137 * dT(nfcdep,initiator) = 100 ms
138 * f(c) = 13560000 Hz
139 */
140#define DIGITAL_ATR_RES_RWT 1337
141
142/* Response Waiting Time for other DEP PDUs in ms
143 *
144 * max_rwt = rwt + dRWT(nfcdep) + dT(nfcdep,initiator)
145 *
146 * with:
147 * rwt = (256 * 16 / f(c)) * 2^wt s
148 * dRWT(nfcdep) = 16 / f(c) s
149 * dT(nfcdep,initiator) = 100 ms
150 * f(c) = 13560000 Hz
151 * 0 <= wt <= 14 (given by the target by the TO field of ATR_RES response)
152 */
153#define DIGITAL_NFC_DEP_IN_MAX_WT 14
154#define DIGITAL_NFC_DEP_TG_MAX_WT 8
155static const u16 digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT + 1] = {
156 100, 101, 101, 102, 105,
157 110, 119, 139, 177, 255,
158 409, 719, 1337, 2575, 5049,
159};
160
Mark A. Greerb08147c2014-09-23 16:38:08 -0700161static u8 digital_payload_bits_to_size(u8 payload_bits)
162{
163 if (payload_bits >= ARRAY_SIZE(digital_payload_bits_map))
164 return 0;
165
166 return digital_payload_bits_map[payload_bits];
167}
168
169static u8 digital_payload_size_to_bits(u8 payload_size)
170{
171 int i;
172
173 for (i = 0; i < ARRAY_SIZE(digital_payload_bits_map); i++)
174 if (digital_payload_bits_map[i] == payload_size)
175 return i;
176
177 return 0xff;
178}
179
Thierry Escande7d0911c2013-09-19 17:55:29 +0200180static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
181 struct sk_buff *skb)
182{
183 skb_push(skb, sizeof(u8));
184
185 skb->data[0] = skb->len;
186
187 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
188 *skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB;
189}
190
191static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev,
192 struct sk_buff *skb)
193{
194 u8 size;
195
196 if (skb->len < 2)
197 return -EIO;
198
199 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
200 skb_pull(skb, sizeof(u8));
201
202 size = skb->data[0];
203 if (size != skb->len)
204 return -EIO;
205
206 skb_pull(skb, sizeof(u8));
207
208 return 0;
209}
210
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -0700211static struct sk_buff *
212digital_send_dep_data_prep(struct nfc_digital_dev *ddev, struct sk_buff *skb,
213 struct digital_dep_req_res *dep_req_res,
214 struct digital_data_exch *data_exch)
215{
216 struct sk_buff *new_skb;
217
218 if (skb->len > ddev->remote_payload_max) {
219 dep_req_res->pfb |= DIGITAL_NFC_DEP_PFB_MI_BIT;
220
221 new_skb = digital_skb_alloc(ddev, ddev->remote_payload_max);
222 if (!new_skb) {
223 kfree_skb(ddev->chaining_skb);
224 ddev->chaining_skb = NULL;
225
226 return ERR_PTR(-ENOMEM);
227 }
228
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -0700229 memcpy(skb_put(new_skb, ddev->remote_payload_max), skb->data,
230 ddev->remote_payload_max);
231 skb_pull(skb, ddev->remote_payload_max);
232
233 ddev->chaining_skb = skb;
234 ddev->data_exch = data_exch;
235 } else {
236 ddev->chaining_skb = NULL;
237 new_skb = skb;
238 }
239
240 return new_skb;
241}
242
Mark A. Greerc12715a2014-09-23 16:38:10 -0700243static struct sk_buff *
244digital_recv_dep_data_gather(struct nfc_digital_dev *ddev, u8 pfb,
245 struct sk_buff *resp,
246 int (*send_ack)(struct nfc_digital_dev *ddev,
247 struct digital_data_exch
248 *data_exch),
249 struct digital_data_exch *data_exch)
250{
251 struct sk_buff *new_skb;
252 int rc;
253
254 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb) && (!ddev->chaining_skb)) {
255 ddev->chaining_skb =
256 nfc_alloc_recv_skb(8 * ddev->local_payload_max,
257 GFP_KERNEL);
258 if (!ddev->chaining_skb) {
259 rc = -ENOMEM;
260 goto error;
261 }
262 }
263
264 if (ddev->chaining_skb) {
265 if (resp->len > skb_tailroom(ddev->chaining_skb)) {
266 new_skb = skb_copy_expand(ddev->chaining_skb,
267 skb_headroom(
268 ddev->chaining_skb),
269 8 * ddev->local_payload_max,
270 GFP_KERNEL);
271 if (!new_skb) {
272 rc = -ENOMEM;
273 goto error;
274 }
275
276 kfree_skb(ddev->chaining_skb);
277 ddev->chaining_skb = new_skb;
278 }
279
280 memcpy(skb_put(ddev->chaining_skb, resp->len), resp->data,
281 resp->len);
282
283 kfree_skb(resp);
284 resp = NULL;
285
286 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
287 rc = send_ack(ddev, data_exch);
288 if (rc)
289 goto error;
290
291 return NULL;
292 }
293
294 resp = ddev->chaining_skb;
295 ddev->chaining_skb = NULL;
296 }
297
298 return resp;
299
300error:
301 kfree_skb(resp);
302
303 kfree_skb(ddev->chaining_skb);
304 ddev->chaining_skb = NULL;
305
306 return ERR_PTR(rc);
307}
308
Mark A. Greerdddb3da2014-07-22 20:18:01 -0700309static void digital_in_recv_psl_res(struct nfc_digital_dev *ddev, void *arg,
310 struct sk_buff *resp)
311{
312 struct nfc_target *target = arg;
313 struct digital_psl_res *psl_res;
314 int rc;
315
316 if (IS_ERR(resp)) {
317 rc = PTR_ERR(resp);
318 resp = NULL;
319 goto exit;
320 }
321
322 rc = ddev->skb_check_crc(resp);
323 if (rc) {
324 PROTOCOL_ERR("14.4.1.6");
325 goto exit;
326 }
327
328 rc = digital_skb_pull_dep_sod(ddev, resp);
329 if (rc) {
330 PROTOCOL_ERR("14.4.1.2");
331 goto exit;
332 }
333
334 psl_res = (struct digital_psl_res *)resp->data;
335
336 if ((resp->len != sizeof(*psl_res)) ||
337 (psl_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN) ||
338 (psl_res->cmd != DIGITAL_CMD_PSL_RES)) {
339 rc = -EIO;
340 goto exit;
341 }
342
343 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
344 NFC_DIGITAL_RF_TECH_424F);
345 if (rc)
346 goto exit;
347
348 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
349 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
350 if (rc)
351 goto exit;
352
353 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev) &&
354 (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)) {
355 ddev->skb_add_crc = digital_skb_add_crc_f;
356 ddev->skb_check_crc = digital_skb_check_crc_f;
357 }
358
359 ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_424F;
360
361 nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
362 NFC_RF_INITIATOR);
363
364 ddev->curr_nfc_dep_pni = 0;
365
366exit:
367 dev_kfree_skb(resp);
368
369 if (rc)
370 ddev->curr_protocol = 0;
371}
372
373static int digital_in_send_psl_req(struct nfc_digital_dev *ddev,
374 struct nfc_target *target)
375{
376 struct sk_buff *skb;
377 struct digital_psl_req *psl_req;
Mark A. Greerb15829b2014-09-23 16:38:02 -0700378 int rc;
Mark A. Greerb08147c2014-09-23 16:38:08 -0700379 u8 payload_size, payload_bits;
Mark A. Greerdddb3da2014-07-22 20:18:01 -0700380
381 skb = digital_skb_alloc(ddev, sizeof(*psl_req));
382 if (!skb)
383 return -ENOMEM;
384
385 skb_put(skb, sizeof(*psl_req));
386
387 psl_req = (struct digital_psl_req *)skb->data;
388
389 psl_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
390 psl_req->cmd = DIGITAL_CMD_PSL_REQ;
391 psl_req->did = 0;
392 psl_req->brs = (0x2 << 3) | 0x2; /* 424F both directions */
Mark A. Greerb08147c2014-09-23 16:38:08 -0700393
394 payload_size = min(ddev->local_payload_max, ddev->remote_payload_max);
395 payload_bits = digital_payload_size_to_bits(payload_size);
396 psl_req->fsl = DIGITAL_PAYLOAD_BITS_TO_FSL(payload_bits);
397
398 ddev->local_payload_max = payload_size;
399 ddev->remote_payload_max = payload_size;
Mark A. Greerdddb3da2014-07-22 20:18:01 -0700400
401 digital_skb_push_dep_sod(ddev, skb);
402
403 ddev->skb_add_crc(skb);
404
Thierry Escande1a09c562016-07-08 15:52:45 +0200405 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
406 digital_in_recv_psl_res, target);
Mark A. Greerb15829b2014-09-23 16:38:02 -0700407 if (rc)
408 kfree_skb(skb);
409
410 return rc;
Mark A. Greerdddb3da2014-07-22 20:18:01 -0700411}
412
Thierry Escande7d0911c2013-09-19 17:55:29 +0200413static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg,
414 struct sk_buff *resp)
415{
416 struct nfc_target *target = arg;
417 struct digital_atr_res *atr_res;
Mark A. Greerb08147c2014-09-23 16:38:08 -0700418 u8 gb_len, payload_bits;
Thierry Escande1a09c562016-07-08 15:52:45 +0200419 u8 wt;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200420 int rc;
421
422 if (IS_ERR(resp)) {
423 rc = PTR_ERR(resp);
424 resp = NULL;
425 goto exit;
426 }
427
428 rc = ddev->skb_check_crc(resp);
429 if (rc) {
430 PROTOCOL_ERR("14.4.1.6");
431 goto exit;
432 }
433
434 rc = digital_skb_pull_dep_sod(ddev, resp);
435 if (rc) {
436 PROTOCOL_ERR("14.4.1.2");
437 goto exit;
438 }
439
440 if (resp->len < sizeof(struct digital_atr_res)) {
441 rc = -EIO;
442 goto exit;
443 }
444
445 gb_len = resp->len - sizeof(struct digital_atr_res);
446
447 atr_res = (struct digital_atr_res *)resp->data;
448
Thierry Escande1a09c562016-07-08 15:52:45 +0200449 wt = DIGITAL_ATR_RES_TO_WT(atr_res->to);
450 if (wt > DIGITAL_NFC_DEP_IN_MAX_WT)
451 wt = DIGITAL_NFC_DEP_IN_MAX_WT;
452 ddev->dep_rwt = digital_rwt_map[wt];
453
Mark A. Greerb08147c2014-09-23 16:38:08 -0700454 payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_res->pp);
455 ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
456
457 if (!ddev->remote_payload_max) {
458 rc = -EINVAL;
459 goto exit;
460 }
461
Thierry Escande7d0911c2013-09-19 17:55:29 +0200462 rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
463 if (rc)
464 goto exit;
465
Mark A. Greerdddb3da2014-07-22 20:18:01 -0700466 if ((ddev->protocols & NFC_PROTO_FELICA_MASK) &&
467 (ddev->curr_rf_tech != NFC_DIGITAL_RF_TECH_424F)) {
468 rc = digital_in_send_psl_req(ddev, target);
469 if (!rc)
470 goto exit;
471 }
472
Thierry Escande7d0911c2013-09-19 17:55:29 +0200473 rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
474 NFC_RF_INITIATOR);
475
476 ddev->curr_nfc_dep_pni = 0;
477
478exit:
479 dev_kfree_skb(resp);
480
481 if (rc)
482 ddev->curr_protocol = 0;
483}
484
485int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
486 struct nfc_target *target, __u8 comm_mode, __u8 *gb,
487 size_t gb_len)
488{
489 struct sk_buff *skb;
490 struct digital_atr_req *atr_req;
491 uint size;
Mark A. Greerb15829b2014-09-23 16:38:02 -0700492 int rc;
Mark A. Greerb08147c2014-09-23 16:38:08 -0700493 u8 payload_bits;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200494
495 size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
496
497 if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
498 PROTOCOL_ERR("14.6.1.1");
499 return -EINVAL;
500 }
501
502 skb = digital_skb_alloc(ddev, size);
503 if (!skb)
504 return -ENOMEM;
505
506 skb_put(skb, sizeof(struct digital_atr_req));
507
508 atr_req = (struct digital_atr_req *)skb->data;
509 memset(atr_req, 0, sizeof(struct digital_atr_req));
510
511 atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
512 atr_req->cmd = DIGITAL_CMD_ATR_REQ;
513 if (target->nfcid2_len)
Thierry Escande4f319e32014-01-02 11:58:14 +0100514 memcpy(atr_req->nfcid3, target->nfcid2, NFC_NFCID2_MAXSIZE);
Thierry Escande7d0911c2013-09-19 17:55:29 +0200515 else
Thierry Escande4f319e32014-01-02 11:58:14 +0100516 get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
Thierry Escande7d0911c2013-09-19 17:55:29 +0200517
518 atr_req->did = 0;
519 atr_req->bs = 0;
520 atr_req->br = 0;
521
Mark A. Greerb08147c2014-09-23 16:38:08 -0700522 ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
523 payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
524 atr_req->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
Thierry Escande7d0911c2013-09-19 17:55:29 +0200525
526 if (gb_len) {
527 atr_req->pp |= DIGITAL_GB_BIT;
528 memcpy(skb_put(skb, gb_len), gb, gb_len);
529 }
530
531 digital_skb_push_dep_sod(ddev, skb);
532
533 ddev->skb_add_crc(skb);
534
Thierry Escande1a09c562016-07-08 15:52:45 +0200535 rc = digital_in_send_cmd(ddev, skb, DIGITAL_ATR_RES_RWT,
536 digital_in_recv_atr_res, target);
Mark A. Greerb15829b2014-09-23 16:38:02 -0700537 if (rc)
538 kfree_skb(skb);
539
540 return rc;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200541}
542
Mark A. Greerc12715a2014-09-23 16:38:10 -0700543static int digital_in_send_ack(struct nfc_digital_dev *ddev,
544 struct digital_data_exch *data_exch)
545{
546 struct digital_dep_req_res *dep_req;
547 struct sk_buff *skb;
548 int rc;
549
550 skb = digital_skb_alloc(ddev, 1);
551 if (!skb)
552 return -ENOMEM;
553
554 skb_push(skb, sizeof(struct digital_dep_req_res));
555
556 dep_req = (struct digital_dep_req_res *)skb->data;
557
558 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
559 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
560 dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
561 ddev->curr_nfc_dep_pni;
562
563 digital_skb_push_dep_sod(ddev, skb);
564
565 ddev->skb_add_crc(skb);
566
Thierry Escande1d984c22016-07-08 15:52:39 +0200567 ddev->saved_skb = pskb_copy(skb, GFP_KERNEL);
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700568
Thierry Escande1a09c562016-07-08 15:52:45 +0200569 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
570 digital_in_recv_dep_res, data_exch);
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700571 if (rc) {
Mark A. Greerc12715a2014-09-23 16:38:10 -0700572 kfree_skb(skb);
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700573 kfree_skb(ddev->saved_skb);
574 ddev->saved_skb = NULL;
575 }
Mark A. Greerc12715a2014-09-23 16:38:10 -0700576
577 return rc;
578}
579
Mark A. Greera80509c2014-09-23 16:38:11 -0700580static int digital_in_send_nack(struct nfc_digital_dev *ddev,
581 struct digital_data_exch *data_exch)
582{
583 struct digital_dep_req_res *dep_req;
584 struct sk_buff *skb;
585 int rc;
586
587 skb = digital_skb_alloc(ddev, 1);
588 if (!skb)
589 return -ENOMEM;
590
591 skb_push(skb, sizeof(struct digital_dep_req_res));
592
593 dep_req = (struct digital_dep_req_res *)skb->data;
594
595 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
596 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
597 dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
598 DIGITAL_NFC_DEP_PFB_NACK_BIT | ddev->curr_nfc_dep_pni;
599
600 digital_skb_push_dep_sod(ddev, skb);
601
602 ddev->skb_add_crc(skb);
603
Thierry Escande1a09c562016-07-08 15:52:45 +0200604 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
605 digital_in_recv_dep_res, data_exch);
Mark A. Greera80509c2014-09-23 16:38:11 -0700606 if (rc)
607 kfree_skb(skb);
608
609 return rc;
610}
611
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700612static int digital_in_send_atn(struct nfc_digital_dev *ddev,
613 struct digital_data_exch *data_exch)
614{
615 struct digital_dep_req_res *dep_req;
616 struct sk_buff *skb;
617 int rc;
618
619 skb = digital_skb_alloc(ddev, 1);
620 if (!skb)
621 return -ENOMEM;
622
623 skb_push(skb, sizeof(struct digital_dep_req_res));
624
625 dep_req = (struct digital_dep_req_res *)skb->data;
626
627 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
628 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
629 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
630
631 digital_skb_push_dep_sod(ddev, skb);
632
633 ddev->skb_add_crc(skb);
634
Thierry Escande1a09c562016-07-08 15:52:45 +0200635 rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
636 digital_in_recv_dep_res, data_exch);
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700637 if (rc)
638 kfree_skb(skb);
639
640 return rc;
641}
642
Thierry Escande7d0911c2013-09-19 17:55:29 +0200643static int digital_in_send_rtox(struct nfc_digital_dev *ddev,
644 struct digital_data_exch *data_exch, u8 rtox)
645{
646 struct digital_dep_req_res *dep_req;
647 struct sk_buff *skb;
648 int rc;
Thierry Escanded85a3012016-07-08 15:52:46 +0200649 u16 rwt_int;
650
651 rwt_int = ddev->dep_rwt * rtox;
652 if (rwt_int > digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT])
653 rwt_int = digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT];
Thierry Escande7d0911c2013-09-19 17:55:29 +0200654
655 skb = digital_skb_alloc(ddev, 1);
656 if (!skb)
657 return -ENOMEM;
658
659 *skb_put(skb, 1) = rtox;
660
661 skb_push(skb, sizeof(struct digital_dep_req_res));
662
663 dep_req = (struct digital_dep_req_res *)skb->data;
664
665 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
666 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
667 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU |
668 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT;
669
670 digital_skb_push_dep_sod(ddev, skb);
671
672 ddev->skb_add_crc(skb);
673
Thierry Escanded85a3012016-07-08 15:52:46 +0200674 rc = digital_in_send_cmd(ddev, skb, rwt_int,
Thierry Escande1a09c562016-07-08 15:52:45 +0200675 digital_in_recv_dep_res, data_exch);
Thierry Escande1d984c22016-07-08 15:52:39 +0200676 if (rc)
Mark A. Greerb15829b2014-09-23 16:38:02 -0700677 kfree_skb(skb);
Thierry Escande7d0911c2013-09-19 17:55:29 +0200678
679 return rc;
680}
681
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700682static int digital_in_send_saved_skb(struct nfc_digital_dev *ddev,
683 struct digital_data_exch *data_exch)
684{
Thierry Escande1d984c22016-07-08 15:52:39 +0200685 int rc;
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700686
Thierry Escande1d984c22016-07-08 15:52:39 +0200687 if (!ddev->saved_skb)
688 return -EINVAL;
689
690 skb_get(ddev->saved_skb);
691
Thierry Escande1a09c562016-07-08 15:52:45 +0200692 rc = digital_in_send_cmd(ddev, ddev->saved_skb, ddev->dep_rwt,
Thierry Escande1d984c22016-07-08 15:52:39 +0200693 digital_in_recv_dep_res, data_exch);
694 if (rc)
695 kfree_skb(ddev->saved_skb);
696
697 return rc;
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700698}
699
Thierry Escande7d0911c2013-09-19 17:55:29 +0200700static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
701 struct sk_buff *resp)
702{
703 struct digital_data_exch *data_exch = arg;
704 struct digital_dep_req_res *dep_res;
705 u8 pfb;
706 uint size;
707 int rc;
Thierry Escanded85a3012016-07-08 15:52:46 +0200708 u8 rtox;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200709
710 if (IS_ERR(resp)) {
711 rc = PTR_ERR(resp);
712 resp = NULL;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200713
Thierry Escande3f89fea2016-06-16 20:24:42 +0200714 if ((rc == -EIO || (rc == -ETIMEDOUT && ddev->nack_count)) &&
Mark A. Greera80509c2014-09-23 16:38:11 -0700715 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700716 ddev->atn_count = 0;
717
Mark A. Greera80509c2014-09-23 16:38:11 -0700718 rc = digital_in_send_nack(ddev, data_exch);
719 if (rc)
720 goto error;
721
722 return;
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700723 } else if ((rc == -ETIMEDOUT) &&
724 (ddev->atn_count++ < DIGITAL_NFC_DEP_N_RETRY_ATN)) {
725 ddev->nack_count = 0;
726
727 rc = digital_in_send_atn(ddev, data_exch);
728 if (rc)
729 goto error;
730
731 return;
Mark A. Greera80509c2014-09-23 16:38:11 -0700732 }
733
734 goto exit;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200735 }
736
737 rc = digital_skb_pull_dep_sod(ddev, resp);
738 if (rc) {
739 PROTOCOL_ERR("14.4.1.2");
740 goto exit;
741 }
742
Mark A. Greera80509c2014-09-23 16:38:11 -0700743 rc = ddev->skb_check_crc(resp);
744 if (rc) {
745 if ((resp->len >= 4) &&
746 (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700747 ddev->atn_count = 0;
748
Mark A. Greera80509c2014-09-23 16:38:11 -0700749 rc = digital_in_send_nack(ddev, data_exch);
750 if (rc)
751 goto error;
752
753 kfree_skb(resp);
754
755 return;
756 }
757
758 PROTOCOL_ERR("14.4.1.6");
759 goto error;
760 }
761
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700762 ddev->atn_count = 0;
Mark A. Greera80509c2014-09-23 16:38:11 -0700763 ddev->nack_count = 0;
764
Mark A. Greerb08147c2014-09-23 16:38:08 -0700765 if (resp->len > ddev->local_payload_max) {
766 rc = -EMSGSIZE;
767 goto exit;
768 }
769
Mark A. Greer6ce30662014-09-23 16:38:03 -0700770 size = sizeof(struct digital_dep_req_res);
Thierry Escande7d0911c2013-09-19 17:55:29 +0200771 dep_res = (struct digital_dep_req_res *)resp->data;
772
Mark A. Greer6ce30662014-09-23 16:38:03 -0700773 if (resp->len < size || dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
Thierry Escande7d0911c2013-09-19 17:55:29 +0200774 dep_res->cmd != DIGITAL_CMD_DEP_RES) {
775 rc = -EIO;
776 goto error;
777 }
778
779 pfb = dep_res->pfb;
780
Mark A. Greer3bc3f882014-09-23 16:38:04 -0700781 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
782 PROTOCOL_ERR("14.8.2.1");
783 rc = -EIO;
784 goto error;
785 }
Mark A. Greer6ce30662014-09-23 16:38:03 -0700786
Mark A. Greer3e6b0de2014-09-23 16:38:06 -0700787 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
788 rc = -EIO;
789 goto exit;
790 }
791
Mark A. Greer6ce30662014-09-23 16:38:03 -0700792 if (size > resp->len) {
793 rc = -EIO;
794 goto error;
795 }
796
797 skb_pull(resp, size);
798
Thierry Escande7d0911c2013-09-19 17:55:29 +0200799 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
800 case DIGITAL_NFC_DEP_PFB_I_PDU:
801 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
802 PROTOCOL_ERR("14.12.3.3");
803 rc = -EIO;
804 goto error;
805 }
806
807 ddev->curr_nfc_dep_pni =
808 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
Mark A. Greerc12715a2014-09-23 16:38:10 -0700809
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700810 kfree_skb(ddev->saved_skb);
811 ddev->saved_skb = NULL;
812
Mark A. Greerc12715a2014-09-23 16:38:10 -0700813 resp = digital_recv_dep_data_gather(ddev, pfb, resp,
814 digital_in_send_ack,
815 data_exch);
816 if (IS_ERR(resp)) {
817 rc = PTR_ERR(resp);
818 resp = NULL;
819 goto error;
820 }
821
822 /* If resp is NULL then we're still chaining so return and
823 * wait for the next part of the PDU. Else, the PDU is
824 * complete so pass it up.
825 */
826 if (!resp)
827 return;
828
Thierry Escande7d0911c2013-09-19 17:55:29 +0200829 rc = 0;
830 break;
831
832 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
Thierry Escandee073eb62016-07-08 15:52:43 +0200833 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) {
834 PROTOCOL_ERR("14.12.4.5");
835 rc = -EIO;
836 goto exit;
837 }
838
Mark A. Greer485fdc92014-09-23 16:38:07 -0700839 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
840 PROTOCOL_ERR("14.12.3.3");
841 rc = -EIO;
842 goto exit;
843 }
844
845 ddev->curr_nfc_dep_pni =
846 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
847
Thierry Escandee073eb62016-07-08 15:52:43 +0200848 if (!ddev->chaining_skb) {
849 PROTOCOL_ERR("14.12.4.3");
850 rc = -EIO;
851 goto exit;
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -0700852 }
853
Thierry Escandee073eb62016-07-08 15:52:43 +0200854 /* The initiator has received a valid ACK. Free the last sent
855 * PDU and keep on sending chained skb.
856 */
857 kfree_skb(ddev->saved_skb);
858 ddev->saved_skb = NULL;
859
860 rc = digital_in_send_dep_req(ddev, NULL,
861 ddev->chaining_skb,
862 ddev->data_exch);
863 if (rc)
864 goto error;
865
866 goto free_resp;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200867
868 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700869 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) { /* ATN */
870 rc = digital_in_send_saved_skb(ddev, data_exch);
Thierry Escande1d984c22016-07-08 15:52:39 +0200871 if (rc)
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700872 goto error;
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700873
Thierry Escandee200f002016-07-08 15:52:44 +0200874 goto free_resp;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200875 }
876
Thierry Escanded85a3012016-07-08 15:52:46 +0200877 if (ddev->atn_count || ddev->nack_count) {
878 PROTOCOL_ERR("14.12.4.4");
879 rc = -EIO;
880 goto error;
881 }
882
883 rtox = DIGITAL_NFC_DEP_RTOX_VALUE(resp->data[0]);
884 if (!rtox || rtox > DIGITAL_NFC_DEP_RTOX_MAX) {
885 PROTOCOL_ERR("14.8.4.1");
886 rc = -EIO;
887 goto error;
888 }
889
890 rc = digital_in_send_rtox(ddev, data_exch, rtox);
Thierry Escande7d0911c2013-09-19 17:55:29 +0200891 if (rc)
892 goto error;
893
Thierry Escandee200f002016-07-08 15:52:44 +0200894 goto free_resp;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200895 }
896
Thierry Escande7d0911c2013-09-19 17:55:29 +0200897exit:
898 data_exch->cb(data_exch->cb_context, resp, rc);
899
900error:
901 kfree(data_exch);
902
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -0700903 kfree_skb(ddev->chaining_skb);
904 ddev->chaining_skb = NULL;
905
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700906 kfree_skb(ddev->saved_skb);
907 ddev->saved_skb = NULL;
908
Thierry Escande7d0911c2013-09-19 17:55:29 +0200909 if (rc)
910 kfree_skb(resp);
Thierry Escandee073eb62016-07-08 15:52:43 +0200911
912 return;
913
914free_resp:
915 dev_kfree_skb(resp);
Thierry Escande7d0911c2013-09-19 17:55:29 +0200916}
917
918int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
919 struct nfc_target *target, struct sk_buff *skb,
920 struct digital_data_exch *data_exch)
921{
922 struct digital_dep_req_res *dep_req;
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -0700923 struct sk_buff *chaining_skb, *tmp_skb;
924 int rc;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200925
926 skb_push(skb, sizeof(struct digital_dep_req_res));
927
928 dep_req = (struct digital_dep_req_res *)skb->data;
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -0700929
Thierry Escande7d0911c2013-09-19 17:55:29 +0200930 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
931 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
932 dep_req->pfb = ddev->curr_nfc_dep_pni;
933
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700934 ddev->atn_count = 0;
Mark A. Greera80509c2014-09-23 16:38:11 -0700935 ddev->nack_count = 0;
936
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -0700937 chaining_skb = ddev->chaining_skb;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200938
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -0700939 tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_req, data_exch);
940 if (IS_ERR(tmp_skb))
941 return PTR_ERR(tmp_skb);
Thierry Escande7d0911c2013-09-19 17:55:29 +0200942
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -0700943 digital_skb_push_dep_sod(ddev, tmp_skb);
944
945 ddev->skb_add_crc(tmp_skb);
946
Thierry Escande1d984c22016-07-08 15:52:39 +0200947 ddev->saved_skb = pskb_copy(tmp_skb, GFP_KERNEL);
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700948
Thierry Escande1a09c562016-07-08 15:52:45 +0200949 rc = digital_in_send_cmd(ddev, tmp_skb, ddev->dep_rwt,
950 digital_in_recv_dep_res, data_exch);
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -0700951 if (rc) {
952 if (tmp_skb != skb)
953 kfree_skb(tmp_skb);
954
955 kfree_skb(chaining_skb);
956 ddev->chaining_skb = NULL;
Mark A. Greer384ab1d2014-09-23 16:38:13 -0700957
958 kfree_skb(ddev->saved_skb);
959 ddev->saved_skb = NULL;
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -0700960 }
961
962 return rc;
Thierry Escande7d0911c2013-09-19 17:55:29 +0200963}
Thierry Escande1c7a4c22013-09-19 17:55:30 +0200964
Thierry Escandeb711ad52014-01-06 23:34:48 +0100965static void digital_tg_set_rf_tech(struct nfc_digital_dev *ddev, u8 rf_tech)
966{
967 ddev->curr_rf_tech = rf_tech;
968
969 ddev->skb_add_crc = digital_skb_add_crc_none;
970 ddev->skb_check_crc = digital_skb_check_crc_none;
971
972 if (DIGITAL_DRV_CAPS_TG_CRC(ddev))
973 return;
974
975 switch (ddev->curr_rf_tech) {
976 case NFC_DIGITAL_RF_TECH_106A:
977 ddev->skb_add_crc = digital_skb_add_crc_a;
978 ddev->skb_check_crc = digital_skb_check_crc_a;
979 break;
980
981 case NFC_DIGITAL_RF_TECH_212F:
982 case NFC_DIGITAL_RF_TECH_424F:
983 ddev->skb_add_crc = digital_skb_add_crc_f;
984 ddev->skb_check_crc = digital_skb_check_crc_f;
985 break;
986
987 default:
988 break;
989 }
990}
991
Mark A. Greerc12715a2014-09-23 16:38:10 -0700992static int digital_tg_send_ack(struct nfc_digital_dev *ddev,
993 struct digital_data_exch *data_exch)
994{
995 struct digital_dep_req_res *dep_res;
996 struct sk_buff *skb;
997 int rc;
998
999 skb = digital_skb_alloc(ddev, 1);
1000 if (!skb)
1001 return -ENOMEM;
1002
1003 skb_push(skb, sizeof(struct digital_dep_req_res));
1004
1005 dep_res = (struct digital_dep_req_res *)skb->data;
1006
1007 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1008 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1009 dep_res->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
1010 ddev->curr_nfc_dep_pni;
1011
1012 if (ddev->did) {
1013 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1014
1015 memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
1016 sizeof(ddev->did));
1017 }
1018
1019 ddev->curr_nfc_dep_pni =
1020 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1021
1022 digital_skb_push_dep_sod(ddev, skb);
1023
1024 ddev->skb_add_crc(skb);
1025
Thierry Escande1d984c22016-07-08 15:52:39 +02001026 ddev->saved_skb = pskb_copy(skb, GFP_KERNEL);
Mark A. Greer49dbb142014-09-23 16:38:12 -07001027
Mark A. Greerc12715a2014-09-23 16:38:10 -07001028 rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
1029 data_exch);
Mark A. Greer49dbb142014-09-23 16:38:12 -07001030 if (rc) {
Mark A. Greerc12715a2014-09-23 16:38:10 -07001031 kfree_skb(skb);
Mark A. Greer49dbb142014-09-23 16:38:12 -07001032 kfree_skb(ddev->saved_skb);
1033 ddev->saved_skb = NULL;
1034 }
Mark A. Greerc12715a2014-09-23 16:38:10 -07001035
1036 return rc;
1037}
1038
Mark A. Greer9b5ec0f2014-09-23 16:38:14 -07001039static int digital_tg_send_atn(struct nfc_digital_dev *ddev)
1040{
1041 struct digital_dep_req_res *dep_res;
1042 struct sk_buff *skb;
1043 int rc;
1044
1045 skb = digital_skb_alloc(ddev, 1);
1046 if (!skb)
1047 return -ENOMEM;
1048
1049 skb_push(skb, sizeof(struct digital_dep_req_res));
1050
1051 dep_res = (struct digital_dep_req_res *)skb->data;
1052
1053 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1054 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1055 dep_res->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
1056
1057 if (ddev->did) {
1058 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1059
1060 memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
1061 sizeof(ddev->did));
1062 }
1063
1064 digital_skb_push_dep_sod(ddev, skb);
1065
1066 ddev->skb_add_crc(skb);
1067
1068 rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
1069 NULL);
1070 if (rc)
1071 kfree_skb(skb);
1072
1073 return rc;
1074}
1075
Mark A. Greer49dbb142014-09-23 16:38:12 -07001076static int digital_tg_send_saved_skb(struct nfc_digital_dev *ddev)
1077{
Thierry Escande1d984c22016-07-08 15:52:39 +02001078 int rc;
Mark A. Greer49dbb142014-09-23 16:38:12 -07001079
Thierry Escande1d984c22016-07-08 15:52:39 +02001080 if (!ddev->saved_skb)
1081 return -EINVAL;
1082
1083 skb_get(ddev->saved_skb);
1084
1085 rc = digital_tg_send_cmd(ddev, ddev->saved_skb, 1500,
1086 digital_tg_recv_dep_req, NULL);
1087 if (rc)
1088 kfree_skb(ddev->saved_skb);
1089
1090 return rc;
Mark A. Greer49dbb142014-09-23 16:38:12 -07001091}
1092
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001093static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
1094 struct sk_buff *resp)
1095{
1096 int rc;
1097 struct digital_dep_req_res *dep_req;
Mark A. Greer6ce30662014-09-23 16:38:03 -07001098 u8 pfb;
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001099 size_t size;
1100
1101 if (IS_ERR(resp)) {
1102 rc = PTR_ERR(resp);
1103 resp = NULL;
1104 goto exit;
1105 }
1106
1107 rc = ddev->skb_check_crc(resp);
1108 if (rc) {
1109 PROTOCOL_ERR("14.4.1.6");
1110 goto exit;
1111 }
1112
1113 rc = digital_skb_pull_dep_sod(ddev, resp);
1114 if (rc) {
1115 PROTOCOL_ERR("14.4.1.2");
1116 goto exit;
1117 }
1118
Mark A. Greerb08147c2014-09-23 16:38:08 -07001119 if (resp->len > ddev->local_payload_max) {
1120 rc = -EMSGSIZE;
1121 goto exit;
1122 }
1123
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001124 size = sizeof(struct digital_dep_req_res);
1125 dep_req = (struct digital_dep_req_res *)resp->data;
1126
1127 if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1128 dep_req->cmd != DIGITAL_CMD_DEP_REQ) {
1129 rc = -EIO;
1130 goto exit;
1131 }
1132
Mark A. Greer6ce30662014-09-23 16:38:03 -07001133 pfb = dep_req->pfb;
1134
Mark A. Greer05afedc2014-09-23 16:38:05 -07001135 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
1136 if (ddev->did && (ddev->did == resp->data[3])) {
1137 size++;
1138 } else {
1139 rc = -EIO;
1140 goto exit;
1141 }
1142 } else if (ddev->did) {
1143 rc = -EIO;
1144 goto exit;
1145 }
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001146
Mark A. Greer3e6b0de2014-09-23 16:38:06 -07001147 if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
1148 rc = -EIO;
1149 goto exit;
1150 }
1151
Mark A. Greer6ce30662014-09-23 16:38:03 -07001152 if (size > resp->len) {
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001153 rc = -EIO;
1154 goto exit;
1155 }
1156
Mark A. Greer6ce30662014-09-23 16:38:03 -07001157 skb_pull(resp, size);
1158
1159 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001160 case DIGITAL_NFC_DEP_PFB_I_PDU:
Samuel Ortiz26042532013-09-20 16:56:40 +02001161 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
Mark A. Greer485fdc92014-09-23 16:38:07 -07001162
Thierry Escandef23a9862016-07-08 15:52:41 +02001163 if (ddev->atn_count) {
1164 /* The target has received (and replied to) at least one
1165 * ATN DEP_REQ.
1166 */
1167 ddev->atn_count = 0;
1168
1169 /* pni of resp PDU equal to the target current pni - 1
1170 * means resp is the previous DEP_REQ PDU received from
1171 * the initiator so the target replies with saved_skb
1172 * which is the previous DEP_RES saved in
1173 * digital_tg_send_dep_res().
1174 */
1175 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) ==
1176 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni - 1)) {
1177 rc = digital_tg_send_saved_skb(ddev);
1178 if (rc)
1179 goto exit;
1180
1181 goto free_resp;
1182 }
1183
1184 /* atn_count > 0 and PDU pni != curr_nfc_dep_pni - 1
1185 * means the target probably did not received the last
1186 * DEP_REQ PDU sent by the initiator. The target
1187 * fallbacks to normal processing then.
1188 */
1189 }
1190
1191 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
Mark A. Greer485fdc92014-09-23 16:38:07 -07001192 PROTOCOL_ERR("14.12.3.4");
1193 rc = -EIO;
1194 goto exit;
1195 }
1196
Mark A. Greer49dbb142014-09-23 16:38:12 -07001197 kfree_skb(ddev->saved_skb);
1198 ddev->saved_skb = NULL;
1199
Mark A. Greerc12715a2014-09-23 16:38:10 -07001200 resp = digital_recv_dep_data_gather(ddev, pfb, resp,
1201 digital_tg_send_ack, NULL);
1202 if (IS_ERR(resp)) {
1203 rc = PTR_ERR(resp);
1204 resp = NULL;
1205 goto exit;
1206 }
1207
1208 /* If resp is NULL then we're still chaining so return and
1209 * wait for the next part of the PDU. Else, the PDU is
1210 * complete so pass it up.
1211 */
1212 if (!resp)
1213 return;
1214
Mark A. Greer485fdc92014-09-23 16:38:07 -07001215 rc = 0;
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001216 break;
1217 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
Thierry Escande482333b2016-07-08 15:52:42 +02001218 if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) { /* NACK */
1219 if (DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) !=
1220 ddev->curr_nfc_dep_pni) {
Mark A. Greer49dbb142014-09-23 16:38:12 -07001221 rc = -EIO;
1222 goto exit;
1223 }
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -07001224
Mark A. Greer9b5ec0f2014-09-23 16:38:14 -07001225 ddev->atn_count = 0;
1226
Mark A. Greer49dbb142014-09-23 16:38:12 -07001227 rc = digital_tg_send_saved_skb(ddev);
Thierry Escande1d984c22016-07-08 15:52:39 +02001228 if (rc)
Mark A. Greer49dbb142014-09-23 16:38:12 -07001229 goto exit;
Thierry Escande482333b2016-07-08 15:52:42 +02001230
1231 goto free_resp;
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -07001232 }
1233
Thierry Escande482333b2016-07-08 15:52:42 +02001234 /* ACK */
1235 if (ddev->atn_count) {
1236 /* The target has previously recevied one or more ATN
1237 * PDUs.
1238 */
1239 ddev->atn_count = 0;
1240
1241 /* If the ACK PNI is equal to the target PNI - 1 means
1242 * that the initiator did not receive the previous PDU
1243 * sent by the target so re-send it.
1244 */
1245 if (DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) ==
1246 ddev->curr_nfc_dep_pni) {
1247 rc = digital_tg_send_saved_skb(ddev);
1248 if (rc)
1249 goto exit;
1250
1251 goto free_resp;
1252 }
1253
1254 /* Otherwise, the target did not receive the previous
1255 * ACK PDU from the initiator. Fallback to normal
1256 * processing of chained PDU then.
1257 */
1258 }
1259
1260 /* Keep on sending chained PDU */
1261 if (!ddev->chaining_skb ||
1262 DIGITAL_NFC_DEP_PFB_PNI(pfb) !=
1263 ddev->curr_nfc_dep_pni) {
1264 rc = -EIO;
1265 goto exit;
1266 }
1267
1268 kfree_skb(ddev->saved_skb);
1269 ddev->saved_skb = NULL;
1270
1271 rc = digital_tg_send_dep_res(ddev, ddev->chaining_skb);
1272 if (rc)
1273 goto exit;
1274
1275 goto free_resp;
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001276 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
Mark A. Greer9b5ec0f2014-09-23 16:38:14 -07001277 if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
1278 rc = -EINVAL;
1279 goto exit;
1280 }
1281
1282 rc = digital_tg_send_atn(ddev);
1283 if (rc)
1284 goto exit;
1285
1286 ddev->atn_count++;
1287
Thierry Escandee200f002016-07-08 15:52:44 +02001288 goto free_resp;
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001289 }
1290
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001291 rc = nfc_tm_data_received(ddev->nfc_dev, resp);
1292
1293exit:
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -07001294 kfree_skb(ddev->chaining_skb);
1295 ddev->chaining_skb = NULL;
1296
Mark A. Greer9b5ec0f2014-09-23 16:38:14 -07001297 ddev->atn_count = 0;
1298
Mark A. Greer49dbb142014-09-23 16:38:12 -07001299 kfree_skb(ddev->saved_skb);
1300 ddev->saved_skb = NULL;
1301
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001302 if (rc)
1303 kfree_skb(resp);
Thierry Escandef23a9862016-07-08 15:52:41 +02001304
1305 return;
1306
1307free_resp:
1308 dev_kfree_skb(resp);
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001309}
1310
1311int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb)
1312{
1313 struct digital_dep_req_res *dep_res;
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -07001314 struct sk_buff *chaining_skb, *tmp_skb;
1315 int rc;
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001316
1317 skb_push(skb, sizeof(struct digital_dep_req_res));
Mark A. Greerb08147c2014-09-23 16:38:08 -07001318
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001319 dep_res = (struct digital_dep_req_res *)skb->data;
1320
1321 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1322 dep_res->cmd = DIGITAL_CMD_DEP_RES;
1323 dep_res->pfb = ddev->curr_nfc_dep_pni;
1324
Mark A. Greer05afedc2014-09-23 16:38:05 -07001325 if (ddev->did) {
1326 dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1327
1328 memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
1329 sizeof(ddev->did));
1330 }
1331
Mark A. Greer485fdc92014-09-23 16:38:07 -07001332 ddev->curr_nfc_dep_pni =
1333 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1334
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -07001335 chaining_skb = ddev->chaining_skb;
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001336
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -07001337 tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_res, NULL);
1338 if (IS_ERR(tmp_skb))
1339 return PTR_ERR(tmp_skb);
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001340
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -07001341 digital_skb_push_dep_sod(ddev, tmp_skb);
1342
1343 ddev->skb_add_crc(tmp_skb);
1344
Thierry Escande1d984c22016-07-08 15:52:39 +02001345 ddev->saved_skb = pskb_copy(tmp_skb, GFP_KERNEL);
Mark A. Greer49dbb142014-09-23 16:38:12 -07001346
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -07001347 rc = digital_tg_send_cmd(ddev, tmp_skb, 1500, digital_tg_recv_dep_req,
1348 NULL);
1349 if (rc) {
1350 if (tmp_skb != skb)
1351 kfree_skb(tmp_skb);
1352
1353 kfree_skb(chaining_skb);
1354 ddev->chaining_skb = NULL;
Mark A. Greer49dbb142014-09-23 16:38:12 -07001355
1356 kfree_skb(ddev->saved_skb);
1357 ddev->saved_skb = NULL;
Mark A. Greer3bd2a5b2014-09-23 16:38:09 -07001358 }
1359
1360 return rc;
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001361}
1362
1363static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev,
1364 void *arg, struct sk_buff *resp)
1365{
Thierry Escande67af1d72014-01-02 11:58:13 +01001366 u8 rf_tech = (unsigned long)arg;
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001367
1368 if (IS_ERR(resp))
1369 return;
1370
Thierry Escandeb711ad52014-01-06 23:34:48 +01001371 digital_tg_set_rf_tech(ddev, rf_tech);
1372
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001373 digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
1374
1375 digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL);
1376
1377 dev_kfree_skb(resp);
1378}
1379
1380static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did,
1381 u8 rf_tech)
1382{
1383 struct digital_psl_res *psl_res;
1384 struct sk_buff *skb;
1385 int rc;
1386
1387 skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
1388 if (!skb)
1389 return -ENOMEM;
1390
1391 skb_put(skb, sizeof(struct digital_psl_res));
1392
1393 psl_res = (struct digital_psl_res *)skb->data;
1394
1395 psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1396 psl_res->cmd = DIGITAL_CMD_PSL_RES;
1397 psl_res->did = did;
1398
1399 digital_skb_push_dep_sod(ddev, skb);
1400
1401 ddev->skb_add_crc(skb);
1402
Mark A. Greer485fdc92014-09-23 16:38:07 -07001403 ddev->curr_nfc_dep_pni = 0;
1404
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001405 rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete,
Thierry Escande67af1d72014-01-02 11:58:13 +01001406 (void *)(unsigned long)rf_tech);
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001407 if (rc)
1408 kfree_skb(skb);
1409
1410 return rc;
1411}
1412
1413static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
1414 struct sk_buff *resp)
1415{
1416 int rc;
1417 struct digital_psl_req *psl_req;
1418 u8 rf_tech;
Mark A. Greerb08147c2014-09-23 16:38:08 -07001419 u8 dsi, payload_size, payload_bits;
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001420
1421 if (IS_ERR(resp)) {
1422 rc = PTR_ERR(resp);
1423 resp = NULL;
1424 goto exit;
1425 }
1426
1427 rc = ddev->skb_check_crc(resp);
1428 if (rc) {
1429 PROTOCOL_ERR("14.4.1.6");
1430 goto exit;
1431 }
1432
1433 rc = digital_skb_pull_dep_sod(ddev, resp);
1434 if (rc) {
1435 PROTOCOL_ERR("14.4.1.2");
1436 goto exit;
1437 }
1438
1439 psl_req = (struct digital_psl_req *)resp->data;
1440
1441 if (resp->len != sizeof(struct digital_psl_req) ||
1442 psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1443 psl_req->cmd != DIGITAL_CMD_PSL_REQ) {
1444 rc = -EIO;
1445 goto exit;
1446 }
1447
1448 dsi = (psl_req->brs >> 3) & 0x07;
1449 switch (dsi) {
1450 case 0:
1451 rf_tech = NFC_DIGITAL_RF_TECH_106A;
1452 break;
1453 case 1:
1454 rf_tech = NFC_DIGITAL_RF_TECH_212F;
1455 break;
1456 case 2:
1457 rf_tech = NFC_DIGITAL_RF_TECH_424F;
1458 break;
1459 default:
Masanari Iida77d84ff2013-12-09 00:22:53 +09001460 pr_err("Unsupported dsi value %d\n", dsi);
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001461 goto exit;
1462 }
1463
Mark A. Greerb08147c2014-09-23 16:38:08 -07001464 payload_bits = DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req->fsl);
1465 payload_size = digital_payload_bits_to_size(payload_bits);
1466
1467 if (!payload_size || (payload_size > min(ddev->local_payload_max,
1468 ddev->remote_payload_max))) {
1469 rc = -EINVAL;
1470 goto exit;
1471 }
1472
1473 ddev->local_payload_max = payload_size;
1474 ddev->remote_payload_max = payload_size;
1475
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001476 rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech);
1477
1478exit:
1479 kfree_skb(resp);
1480}
1481
1482static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
1483 void *arg, struct sk_buff *resp)
1484{
1485 int offset;
1486
1487 if (IS_ERR(resp)) {
1488 digital_poll_next_tech(ddev);
1489 return;
1490 }
1491
1492 offset = 2;
1493 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
1494 offset++;
1495
Mark A. Greer9b5ec0f2014-09-23 16:38:14 -07001496 ddev->atn_count = 0;
1497
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001498 if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
1499 digital_tg_recv_psl_req(ddev, arg, resp);
1500 else
1501 digital_tg_recv_dep_req(ddev, arg, resp);
1502}
1503
1504static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev,
1505 struct digital_atr_req *atr_req)
1506{
1507 struct digital_atr_res *atr_res;
1508 struct sk_buff *skb;
Mark A. Greerb08147c2014-09-23 16:38:08 -07001509 u8 *gb, payload_bits;
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001510 size_t gb_len;
1511 int rc;
1512
1513 gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
1514 if (!gb)
1515 gb_len = 0;
1516
1517 skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
1518 if (!skb)
1519 return -ENOMEM;
1520
1521 skb_put(skb, sizeof(struct digital_atr_res));
1522 atr_res = (struct digital_atr_res *)skb->data;
1523
1524 memset(atr_res, 0, sizeof(struct digital_atr_res));
1525
1526 atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1527 atr_res->cmd = DIGITAL_CMD_ATR_RES;
1528 memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3));
Thierry Escande1a09c562016-07-08 15:52:45 +02001529 atr_res->to = DIGITAL_NFC_DEP_TG_MAX_WT;
Mark A. Greerb08147c2014-09-23 16:38:08 -07001530
1531 ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
1532 payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
1533 atr_res->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
1534
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001535 if (gb_len) {
1536 skb_put(skb, gb_len);
1537
1538 atr_res->pp |= DIGITAL_GB_BIT;
1539 memcpy(atr_res->gb, gb, gb_len);
1540 }
1541
1542 digital_skb_push_dep_sod(ddev, skb);
1543
1544 ddev->skb_add_crc(skb);
1545
Mark A. Greer485fdc92014-09-23 16:38:07 -07001546 ddev->curr_nfc_dep_pni = 0;
1547
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001548 rc = digital_tg_send_cmd(ddev, skb, 999,
1549 digital_tg_send_atr_res_complete, NULL);
Mark A. Greerb15829b2014-09-23 16:38:02 -07001550 if (rc)
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001551 kfree_skb(skb);
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001552
1553 return rc;
1554}
1555
1556void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
1557 struct sk_buff *resp)
1558{
1559 int rc;
1560 struct digital_atr_req *atr_req;
1561 size_t gb_len, min_size;
Mark A. Greerb08147c2014-09-23 16:38:08 -07001562 u8 poll_tech_count, payload_bits;
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001563
1564 if (IS_ERR(resp)) {
1565 rc = PTR_ERR(resp);
1566 resp = NULL;
1567 goto exit;
1568 }
1569
1570 if (!resp->len) {
1571 rc = -EIO;
1572 goto exit;
1573 }
1574
1575 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) {
1576 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2;
Thierry Escandeb711ad52014-01-06 23:34:48 +01001577 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A);
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001578 } else {
1579 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1;
Thierry Escandeb711ad52014-01-06 23:34:48 +01001580 digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F);
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001581 }
1582
1583 if (resp->len < min_size) {
1584 rc = -EIO;
1585 goto exit;
1586 }
1587
Thierry Escande48e10442014-01-06 23:34:37 +01001588 ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK;
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001589
1590 rc = ddev->skb_check_crc(resp);
1591 if (rc) {
1592 PROTOCOL_ERR("14.4.1.6");
1593 goto exit;
1594 }
1595
1596 rc = digital_skb_pull_dep_sod(ddev, resp);
1597 if (rc) {
1598 PROTOCOL_ERR("14.4.1.2");
1599 goto exit;
1600 }
1601
1602 atr_req = (struct digital_atr_req *)resp->data;
1603
1604 if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
Mark A. Greer05afedc2014-09-23 16:38:05 -07001605 atr_req->cmd != DIGITAL_CMD_ATR_REQ ||
1606 atr_req->did > DIGITAL_DID_MAX) {
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001607 rc = -EINVAL;
1608 goto exit;
1609 }
1610
Mark A. Greerb08147c2014-09-23 16:38:08 -07001611 payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_req->pp);
1612 ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
1613
1614 if (!ddev->remote_payload_max) {
1615 rc = -EINVAL;
1616 goto exit;
1617 }
1618
Mark A. Greer05afedc2014-09-23 16:38:05 -07001619 ddev->did = atr_req->did;
1620
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001621 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1622 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
1623 if (rc)
1624 goto exit;
1625
1626 rc = digital_tg_send_atr_res(ddev, atr_req);
1627 if (rc)
1628 goto exit;
1629
1630 gb_len = resp->len - sizeof(struct digital_atr_req);
Mark A. Greer0529a7a2014-07-02 09:03:49 -07001631
1632 poll_tech_count = ddev->poll_tech_count;
1633 ddev->poll_tech_count = 0;
1634
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001635 rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1636 NFC_COMM_PASSIVE, atr_req->gb, gb_len);
Mark A. Greer0529a7a2014-07-02 09:03:49 -07001637 if (rc) {
1638 ddev->poll_tech_count = poll_tech_count;
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001639 goto exit;
Mark A. Greer0529a7a2014-07-02 09:03:49 -07001640 }
Thierry Escande1c7a4c22013-09-19 17:55:30 +02001641
1642 rc = 0;
1643exit:
1644 if (rc)
1645 digital_poll_next_tech(ddev);
1646
1647 dev_kfree_skb(resp);
1648}