Commit | Line | Data |
---|---|---|
59ee2361 TE |
1 | /* |
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 | ||
c5da0e4a SO |
16 | #define pr_fmt(fmt) "digital: %s: " fmt, __func__ |
17 | ||
59ee2361 TE |
18 | #include "digital.h" |
19 | ||
20 | #define DIGITAL_CMD_SENS_REQ 0x26 | |
21 | #define DIGITAL_CMD_ALL_REQ 0x52 | |
22 | #define DIGITAL_CMD_SEL_REQ_CL1 0x93 | |
23 | #define DIGITAL_CMD_SEL_REQ_CL2 0x95 | |
24 | #define DIGITAL_CMD_SEL_REQ_CL3 0x97 | |
25 | ||
26 | #define DIGITAL_SDD_REQ_SEL_PAR 0x20 | |
27 | ||
28 | #define DIGITAL_SDD_RES_CT 0x88 | |
29 | #define DIGITAL_SDD_RES_LEN 5 | |
30 | ||
2c66daec TE |
31 | #define DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res) (!((sel_res) & 0x04)) |
32 | #define DIGITAL_SEL_RES_IS_T2T(sel_res) (!((sel_res) & 0x60)) | |
7d0911c0 | 33 | #define DIGITAL_SEL_RES_IS_NFC_DEP(sel_res) ((sel_res) & 0x40) |
2c66daec TE |
34 | |
35 | #define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x000C) == 0x000C) | |
36 | #define DIGITAL_SENS_RES_IS_VALID(sens_res) \ | |
37 | ((!((sens_res) & 0x1F00) && (((sens_res) & 0x000C) == 0x000C)) || \ | |
38 | (((sens_res) & 0x1F00) && ((sens_res) & 0x000C) != 0x000C)) | |
39 | ||
40 | #define DIGITAL_MIFARE_READ_RES_LEN 16 | |
41 | #define DIGITAL_MIFARE_ACK_RES 0x0A | |
42 | ||
8c0695e4 TE |
43 | #define DIGITAL_CMD_SENSF_REQ 0x00 |
44 | #define DIGITAL_CMD_SENSF_RES 0x01 | |
45 | ||
46 | #define DIGITAL_SENSF_RES_MIN_LENGTH 17 | |
47 | #define DIGITAL_SENSF_RES_RD_AP_B1 0x00 | |
48 | #define DIGITAL_SENSF_RES_RD_AP_B2 0x8F | |
49 | ||
50 | #define DIGITAL_SENSF_REQ_RC_NONE 0 | |
51 | #define DIGITAL_SENSF_REQ_RC_SC 1 | |
52 | #define DIGITAL_SENSF_REQ_RC_AP 2 | |
53 | ||
2c66daec TE |
54 | struct digital_sdd_res { |
55 | u8 nfcid1[4]; | |
56 | u8 bcc; | |
57 | } __packed; | |
58 | ||
59 | struct digital_sel_req { | |
60 | u8 sel_cmd; | |
61 | u8 b2; | |
62 | u8 nfcid1[4]; | |
63 | u8 bcc; | |
64 | } __packed; | |
65 | ||
8c0695e4 TE |
66 | struct digital_sensf_req { |
67 | u8 cmd; | |
68 | u8 sc1; | |
69 | u8 sc2; | |
70 | u8 rc; | |
71 | u8 tsn; | |
72 | } __packed; | |
73 | ||
74 | struct digital_sensf_res { | |
75 | u8 cmd; | |
76 | u8 nfcid2[8]; | |
77 | u8 pad0[2]; | |
78 | u8 pad1[3]; | |
79 | u8 mrti_check; | |
80 | u8 mrti_update; | |
81 | u8 pad2; | |
82 | u8 rd[2]; | |
83 | } __packed; | |
84 | ||
2c66daec TE |
85 | static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev, |
86 | struct nfc_target *target); | |
87 | ||
88 | static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg, | |
89 | struct sk_buff *resp) | |
90 | { | |
91 | struct nfc_target *target = arg; | |
92 | int rc; | |
93 | u8 sel_res; | |
94 | u8 nfc_proto; | |
95 | ||
96 | if (IS_ERR(resp)) { | |
97 | rc = PTR_ERR(resp); | |
98 | resp = NULL; | |
99 | goto exit; | |
100 | } | |
101 | ||
102 | if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) { | |
103 | rc = digital_skb_check_crc_a(resp); | |
104 | if (rc) { | |
105 | PROTOCOL_ERR("4.4.1.3"); | |
106 | goto exit; | |
107 | } | |
108 | } | |
109 | ||
110 | if (!resp->len) { | |
111 | rc = -EIO; | |
112 | goto exit; | |
113 | } | |
114 | ||
115 | sel_res = resp->data[0]; | |
116 | ||
117 | if (!DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res)) { | |
118 | rc = digital_in_send_sdd_req(ddev, target); | |
119 | if (rc) | |
120 | goto exit; | |
121 | ||
122 | goto exit_free_skb; | |
123 | } | |
124 | ||
125 | if (DIGITAL_SEL_RES_IS_T2T(sel_res)) { | |
126 | nfc_proto = NFC_PROTO_MIFARE; | |
7d0911c0 TE |
127 | } else if (DIGITAL_SEL_RES_IS_NFC_DEP(sel_res)) { |
128 | nfc_proto = NFC_PROTO_NFC_DEP; | |
2c66daec TE |
129 | } else { |
130 | rc = -EOPNOTSUPP; | |
131 | goto exit; | |
132 | } | |
133 | ||
134 | target->sel_res = sel_res; | |
135 | ||
136 | rc = digital_target_found(ddev, target, nfc_proto); | |
137 | ||
138 | exit: | |
139 | kfree(target); | |
140 | ||
141 | exit_free_skb: | |
142 | dev_kfree_skb(resp); | |
143 | ||
144 | if (rc) | |
145 | digital_poll_next_tech(ddev); | |
146 | } | |
147 | ||
148 | static int digital_in_send_sel_req(struct nfc_digital_dev *ddev, | |
149 | struct nfc_target *target, | |
150 | struct digital_sdd_res *sdd_res) | |
151 | { | |
152 | struct sk_buff *skb; | |
153 | struct digital_sel_req *sel_req; | |
154 | u8 sel_cmd; | |
155 | int rc; | |
156 | ||
157 | skb = digital_skb_alloc(ddev, sizeof(struct digital_sel_req)); | |
158 | if (!skb) | |
159 | return -ENOMEM; | |
160 | ||
161 | skb_put(skb, sizeof(struct digital_sel_req)); | |
162 | sel_req = (struct digital_sel_req *)skb->data; | |
163 | ||
164 | if (target->nfcid1_len <= 4) | |
165 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL1; | |
166 | else if (target->nfcid1_len < 10) | |
167 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL2; | |
168 | else | |
169 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL3; | |
170 | ||
171 | sel_req->sel_cmd = sel_cmd; | |
172 | sel_req->b2 = 0x70; | |
173 | memcpy(sel_req->nfcid1, sdd_res->nfcid1, 4); | |
174 | sel_req->bcc = sdd_res->bcc; | |
175 | ||
176 | if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) { | |
177 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, | |
178 | NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A); | |
179 | if (rc) | |
180 | goto exit; | |
181 | } else { | |
182 | digital_skb_add_crc_a(skb); | |
183 | } | |
184 | ||
185 | rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sel_res, | |
186 | target); | |
187 | exit: | |
188 | if (rc) | |
189 | kfree_skb(skb); | |
190 | ||
191 | return rc; | |
192 | } | |
193 | ||
194 | static void digital_in_recv_sdd_res(struct nfc_digital_dev *ddev, void *arg, | |
195 | struct sk_buff *resp) | |
196 | { | |
197 | struct nfc_target *target = arg; | |
198 | struct digital_sdd_res *sdd_res; | |
199 | int rc; | |
200 | u8 offset, size; | |
201 | u8 i, bcc; | |
202 | ||
203 | if (IS_ERR(resp)) { | |
204 | rc = PTR_ERR(resp); | |
205 | resp = NULL; | |
206 | goto exit; | |
207 | } | |
208 | ||
209 | if (resp->len < DIGITAL_SDD_RES_LEN) { | |
210 | PROTOCOL_ERR("4.7.2.8"); | |
211 | rc = -EINVAL; | |
212 | goto exit; | |
213 | } | |
214 | ||
215 | sdd_res = (struct digital_sdd_res *)resp->data; | |
216 | ||
217 | for (i = 0, bcc = 0; i < 4; i++) | |
218 | bcc ^= sdd_res->nfcid1[i]; | |
219 | ||
220 | if (bcc != sdd_res->bcc) { | |
221 | PROTOCOL_ERR("4.7.2.6"); | |
222 | rc = -EINVAL; | |
223 | goto exit; | |
224 | } | |
225 | ||
226 | if (sdd_res->nfcid1[0] == DIGITAL_SDD_RES_CT) { | |
227 | offset = 1; | |
228 | size = 3; | |
229 | } else { | |
230 | offset = 0; | |
231 | size = 4; | |
232 | } | |
233 | ||
234 | memcpy(target->nfcid1 + target->nfcid1_len, sdd_res->nfcid1 + offset, | |
235 | size); | |
236 | target->nfcid1_len += size; | |
237 | ||
238 | rc = digital_in_send_sel_req(ddev, target, sdd_res); | |
239 | ||
240 | exit: | |
241 | dev_kfree_skb(resp); | |
242 | ||
243 | if (rc) { | |
244 | kfree(target); | |
245 | digital_poll_next_tech(ddev); | |
246 | } | |
247 | } | |
248 | ||
249 | static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev, | |
250 | struct nfc_target *target) | |
251 | { | |
252 | int rc; | |
253 | struct sk_buff *skb; | |
254 | u8 sel_cmd; | |
255 | ||
256 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, | |
257 | NFC_DIGITAL_FRAMING_NFCA_STANDARD); | |
258 | if (rc) | |
259 | return rc; | |
260 | ||
261 | skb = digital_skb_alloc(ddev, 2); | |
262 | if (!skb) { | |
c5da0e4a | 263 | pr_err("alloc_skb failed"); |
2c66daec TE |
264 | return -ENOMEM; |
265 | } | |
266 | ||
267 | if (target->nfcid1_len == 0) | |
268 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL1; | |
269 | else if (target->nfcid1_len == 3) | |
270 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL2; | |
271 | else | |
272 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL3; | |
273 | ||
274 | *skb_put(skb, sizeof(u8)) = sel_cmd; | |
275 | *skb_put(skb, sizeof(u8)) = DIGITAL_SDD_REQ_SEL_PAR; | |
276 | ||
277 | return digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sdd_res, | |
278 | target); | |
279 | } | |
280 | ||
59ee2361 TE |
281 | static void digital_in_recv_sens_res(struct nfc_digital_dev *ddev, void *arg, |
282 | struct sk_buff *resp) | |
283 | { | |
2c66daec TE |
284 | struct nfc_target *target = NULL; |
285 | u16 sens_res; | |
286 | int rc; | |
287 | ||
288 | if (IS_ERR(resp)) { | |
289 | rc = PTR_ERR(resp); | |
290 | resp = NULL; | |
291 | goto exit; | |
292 | } | |
293 | ||
294 | if (resp->len < sizeof(u16)) { | |
295 | rc = -EIO; | |
296 | goto exit; | |
297 | } | |
298 | ||
299 | target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL); | |
300 | if (!target) { | |
301 | rc = -ENOMEM; | |
302 | goto exit; | |
303 | } | |
304 | ||
305 | memcpy(&target->sens_res, resp->data, sizeof(u16)); | |
59ee2361 | 306 | |
2c66daec TE |
307 | sens_res = be16_to_cpu(target->sens_res); |
308 | ||
309 | if (!DIGITAL_SENS_RES_IS_VALID(sens_res)) { | |
310 | PROTOCOL_ERR("4.6.3.3"); | |
311 | rc = -EINVAL; | |
312 | goto exit; | |
313 | } | |
314 | ||
315 | if (DIGITAL_SENS_RES_IS_T1T(sens_res)) | |
316 | rc = digital_target_found(ddev, target, NFC_PROTO_JEWEL); | |
317 | else | |
318 | rc = digital_in_send_sdd_req(ddev, target); | |
319 | ||
320 | exit: | |
321 | dev_kfree_skb(resp); | |
322 | ||
323 | if (rc) { | |
324 | kfree(target); | |
325 | digital_poll_next_tech(ddev); | |
326 | } | |
59ee2361 TE |
327 | } |
328 | ||
329 | int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech) | |
330 | { | |
331 | struct sk_buff *skb; | |
332 | int rc; | |
333 | ||
334 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, | |
335 | NFC_DIGITAL_RF_TECH_106A); | |
336 | if (rc) | |
337 | return rc; | |
338 | ||
339 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, | |
340 | NFC_DIGITAL_FRAMING_NFCA_SHORT); | |
341 | if (rc) | |
342 | return rc; | |
343 | ||
344 | skb = digital_skb_alloc(ddev, 1); | |
345 | if (!skb) | |
346 | return -ENOMEM; | |
347 | ||
348 | *skb_put(skb, sizeof(u8)) = DIGITAL_CMD_SENS_REQ; | |
349 | ||
350 | rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sens_res, NULL); | |
351 | if (rc) | |
352 | kfree_skb(skb); | |
353 | ||
354 | return rc; | |
355 | } | |
2c66daec TE |
356 | |
357 | int digital_in_recv_mifare_res(struct sk_buff *resp) | |
358 | { | |
359 | /* Successful READ command response is 16 data bytes + 2 CRC bytes long. | |
360 | * Since the driver can't differentiate a ACK/NACK response from a valid | |
361 | * READ response, the CRC calculation must be handled at digital level | |
362 | * even if the driver supports it for this technology. | |
363 | */ | |
364 | if (resp->len == DIGITAL_MIFARE_READ_RES_LEN + DIGITAL_CRC_LEN) { | |
365 | if (digital_skb_check_crc_a(resp)) { | |
366 | PROTOCOL_ERR("9.4.1.2"); | |
367 | return -EIO; | |
368 | } | |
369 | ||
370 | return 0; | |
371 | } | |
372 | ||
373 | /* ACK response (i.e. successful WRITE). */ | |
374 | if (resp->len == 1 && resp->data[0] == DIGITAL_MIFARE_ACK_RES) { | |
375 | resp->data[0] = 0; | |
376 | return 0; | |
377 | } | |
378 | ||
379 | /* NACK and any other responses are treated as error. */ | |
380 | return -EIO; | |
381 | } | |
8c0695e4 TE |
382 | |
383 | static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg, | |
384 | struct sk_buff *resp) | |
385 | { | |
386 | int rc; | |
7d0911c0 | 387 | u8 proto; |
8c0695e4 TE |
388 | struct nfc_target target; |
389 | struct digital_sensf_res *sensf_res; | |
390 | ||
391 | if (IS_ERR(resp)) { | |
392 | rc = PTR_ERR(resp); | |
393 | resp = NULL; | |
394 | goto exit; | |
395 | } | |
396 | ||
397 | if (resp->len < DIGITAL_SENSF_RES_MIN_LENGTH) { | |
398 | rc = -EIO; | |
399 | goto exit; | |
400 | } | |
401 | ||
402 | if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) { | |
403 | rc = digital_skb_check_crc_f(resp); | |
404 | if (rc) { | |
405 | PROTOCOL_ERR("6.4.1.8"); | |
406 | goto exit; | |
407 | } | |
408 | } | |
409 | ||
410 | skb_pull(resp, 1); | |
411 | ||
412 | memset(&target, 0, sizeof(struct nfc_target)); | |
413 | ||
414 | sensf_res = (struct digital_sensf_res *)resp->data; | |
415 | ||
416 | memcpy(target.sensf_res, sensf_res, resp->len); | |
417 | target.sensf_res_len = resp->len; | |
418 | ||
419 | memcpy(target.nfcid2, sensf_res->nfcid2, NFC_NFCID2_MAXSIZE); | |
420 | target.nfcid2_len = NFC_NFCID2_MAXSIZE; | |
421 | ||
7d0911c0 TE |
422 | if (target.nfcid2[0] == DIGITAL_SENSF_NFCID2_NFC_DEP_B1 && |
423 | target.nfcid2[1] == DIGITAL_SENSF_NFCID2_NFC_DEP_B2) | |
424 | proto = NFC_PROTO_NFC_DEP; | |
425 | else | |
426 | proto = NFC_PROTO_FELICA; | |
427 | ||
428 | rc = digital_target_found(ddev, &target, proto); | |
8c0695e4 TE |
429 | |
430 | exit: | |
431 | dev_kfree_skb(resp); | |
432 | ||
433 | if (rc) | |
434 | digital_poll_next_tech(ddev); | |
435 | } | |
436 | ||
437 | int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech) | |
438 | { | |
439 | struct digital_sensf_req *sensf_req; | |
440 | struct sk_buff *skb; | |
441 | int rc; | |
442 | u8 size; | |
443 | ||
444 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech); | |
445 | if (rc) | |
446 | return rc; | |
447 | ||
448 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, | |
449 | NFC_DIGITAL_FRAMING_NFCF); | |
450 | if (rc) | |
451 | return rc; | |
452 | ||
453 | size = sizeof(struct digital_sensf_req); | |
454 | ||
455 | skb = digital_skb_alloc(ddev, size); | |
456 | if (!skb) | |
457 | return -ENOMEM; | |
458 | ||
459 | skb_put(skb, size); | |
460 | ||
461 | sensf_req = (struct digital_sensf_req *)skb->data; | |
462 | sensf_req->cmd = DIGITAL_CMD_SENSF_REQ; | |
463 | sensf_req->sc1 = 0xFF; | |
464 | sensf_req->sc2 = 0xFF; | |
465 | sensf_req->rc = 0; | |
466 | sensf_req->tsn = 0; | |
467 | ||
468 | *skb_push(skb, 1) = size + 1; | |
469 | ||
470 | if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) | |
471 | digital_skb_add_crc_f(skb); | |
472 | ||
473 | rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensf_res, | |
474 | NULL); | |
475 | if (rc) | |
476 | kfree_skb(skb); | |
477 | ||
478 | return rc; | |
479 | } | |
1c7a4c24 TE |
480 | |
481 | static int digital_tg_send_sel_res(struct nfc_digital_dev *ddev) | |
482 | { | |
483 | struct sk_buff *skb; | |
484 | int rc; | |
485 | ||
486 | skb = digital_skb_alloc(ddev, 1); | |
487 | if (!skb) | |
488 | return -ENOMEM; | |
489 | ||
490 | *skb_put(skb, 1) = DIGITAL_SEL_RES_NFC_DEP; | |
491 | ||
492 | if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) | |
493 | digital_skb_add_crc_a(skb); | |
494 | ||
495 | rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_atr_req, | |
496 | NULL); | |
497 | if (rc) | |
498 | kfree_skb(skb); | |
499 | ||
500 | return rc; | |
501 | } | |
502 | ||
503 | static void digital_tg_recv_sel_req(struct nfc_digital_dev *ddev, void *arg, | |
504 | struct sk_buff *resp) | |
505 | { | |
506 | int rc; | |
507 | ||
508 | if (IS_ERR(resp)) { | |
509 | rc = PTR_ERR(resp); | |
510 | resp = NULL; | |
511 | goto exit; | |
512 | } | |
513 | ||
514 | if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) { | |
515 | rc = digital_skb_check_crc_a(resp); | |
516 | if (rc) { | |
517 | PROTOCOL_ERR("4.4.1.3"); | |
518 | goto exit; | |
519 | } | |
520 | } | |
521 | ||
522 | /* Silently ignore SEL_REQ content and send a SEL_RES for NFC-DEP */ | |
523 | ||
524 | rc = digital_tg_send_sel_res(ddev); | |
525 | ||
526 | exit: | |
527 | if (rc) | |
528 | digital_poll_next_tech(ddev); | |
529 | ||
530 | dev_kfree_skb(resp); | |
531 | } | |
532 | ||
533 | static int digital_tg_send_sdd_res(struct nfc_digital_dev *ddev) | |
534 | { | |
535 | struct sk_buff *skb; | |
536 | struct digital_sdd_res *sdd_res; | |
537 | int rc, i; | |
538 | ||
539 | skb = digital_skb_alloc(ddev, sizeof(struct digital_sdd_res)); | |
540 | if (!skb) | |
541 | return -ENOMEM; | |
542 | ||
543 | skb_put(skb, sizeof(struct digital_sdd_res)); | |
544 | sdd_res = (struct digital_sdd_res *)skb->data; | |
545 | ||
546 | sdd_res->nfcid1[0] = 0x08; | |
547 | get_random_bytes(sdd_res->nfcid1 + 1, 3); | |
548 | ||
549 | sdd_res->bcc = 0; | |
550 | for (i = 0; i < 4; i++) | |
551 | sdd_res->bcc ^= sdd_res->nfcid1[i]; | |
552 | ||
553 | rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sel_req, | |
554 | NULL); | |
555 | if (rc) | |
556 | kfree_skb(skb); | |
557 | ||
558 | return rc; | |
559 | } | |
560 | ||
561 | static void digital_tg_recv_sdd_req(struct nfc_digital_dev *ddev, void *arg, | |
562 | struct sk_buff *resp) | |
563 | { | |
564 | u8 *sdd_req; | |
565 | int rc; | |
566 | ||
567 | if (IS_ERR(resp)) { | |
568 | rc = PTR_ERR(resp); | |
569 | resp = NULL; | |
570 | goto exit; | |
571 | } | |
572 | ||
573 | sdd_req = resp->data; | |
574 | ||
575 | if (resp->len < 2 || sdd_req[0] != DIGITAL_CMD_SEL_REQ_CL1 || | |
576 | sdd_req[1] != DIGITAL_SDD_REQ_SEL_PAR) { | |
577 | rc = -EINVAL; | |
578 | goto exit; | |
579 | } | |
580 | ||
581 | rc = digital_tg_send_sdd_res(ddev); | |
582 | ||
583 | exit: | |
584 | if (rc) | |
585 | digital_poll_next_tech(ddev); | |
586 | ||
587 | dev_kfree_skb(resp); | |
588 | } | |
589 | ||
590 | static int digital_tg_send_sens_res(struct nfc_digital_dev *ddev) | |
591 | { | |
592 | struct sk_buff *skb; | |
593 | u8 *sens_res; | |
594 | int rc; | |
595 | ||
596 | skb = digital_skb_alloc(ddev, 2); | |
597 | if (!skb) | |
598 | return -ENOMEM; | |
599 | ||
600 | sens_res = skb_put(skb, 2); | |
601 | ||
602 | sens_res[0] = (DIGITAL_SENS_RES_NFC_DEP >> 8) & 0xFF; | |
603 | sens_res[1] = DIGITAL_SENS_RES_NFC_DEP & 0xFF; | |
604 | ||
605 | rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sdd_req, | |
606 | NULL); | |
607 | if (rc) | |
608 | kfree_skb(skb); | |
609 | ||
610 | return rc; | |
611 | } | |
612 | ||
613 | void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg, | |
614 | struct sk_buff *resp) | |
615 | { | |
616 | u8 sens_req; | |
617 | int rc; | |
618 | ||
619 | if (IS_ERR(resp)) { | |
620 | rc = PTR_ERR(resp); | |
621 | resp = NULL; | |
622 | goto exit; | |
623 | } | |
624 | ||
625 | sens_req = resp->data[0]; | |
626 | ||
627 | if (!resp->len || (sens_req != DIGITAL_CMD_SENS_REQ && | |
628 | sens_req != DIGITAL_CMD_ALL_REQ)) { | |
629 | rc = -EINVAL; | |
630 | goto exit; | |
631 | } | |
632 | ||
633 | rc = digital_tg_send_sens_res(ddev); | |
634 | ||
635 | exit: | |
636 | if (rc) | |
637 | digital_poll_next_tech(ddev); | |
638 | ||
639 | dev_kfree_skb(resp); | |
640 | } | |
641 | ||
642 | int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev, | |
643 | struct digital_sensf_req *sensf_req) | |
644 | { | |
645 | struct sk_buff *skb; | |
646 | u8 size; | |
647 | int rc; | |
648 | struct digital_sensf_res *sensf_res; | |
649 | ||
650 | size = sizeof(struct digital_sensf_res); | |
651 | ||
652 | if (sensf_req->rc != DIGITAL_SENSF_REQ_RC_NONE) | |
653 | size -= sizeof(sensf_res->rd); | |
654 | ||
655 | skb = digital_skb_alloc(ddev, size); | |
656 | if (!skb) | |
657 | return -ENOMEM; | |
658 | ||
659 | skb_put(skb, size); | |
660 | ||
661 | sensf_res = (struct digital_sensf_res *)skb->data; | |
662 | ||
663 | memset(sensf_res, 0, size); | |
664 | ||
665 | sensf_res->cmd = DIGITAL_CMD_SENSF_RES; | |
666 | sensf_res->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1; | |
667 | sensf_res->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2; | |
668 | get_random_bytes(&sensf_res->nfcid2[2], 6); | |
669 | ||
670 | switch (sensf_req->rc) { | |
671 | case DIGITAL_SENSF_REQ_RC_SC: | |
672 | sensf_res->rd[0] = sensf_req->sc1; | |
673 | sensf_res->rd[1] = sensf_req->sc2; | |
674 | break; | |
675 | case DIGITAL_SENSF_REQ_RC_AP: | |
676 | sensf_res->rd[0] = DIGITAL_SENSF_RES_RD_AP_B1; | |
677 | sensf_res->rd[1] = DIGITAL_SENSF_RES_RD_AP_B2; | |
678 | break; | |
679 | } | |
680 | ||
681 | *skb_push(skb, sizeof(u8)) = size + 1; | |
682 | ||
683 | if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) | |
684 | digital_skb_add_crc_f(skb); | |
685 | ||
686 | rc = digital_tg_send_cmd(ddev, skb, 300, | |
687 | digital_tg_recv_atr_req, NULL); | |
688 | if (rc) | |
689 | kfree_skb(skb); | |
690 | ||
691 | return rc; | |
692 | } | |
693 | ||
694 | void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg, | |
695 | struct sk_buff *resp) | |
696 | { | |
697 | struct digital_sensf_req *sensf_req; | |
698 | int rc; | |
699 | ||
700 | if (IS_ERR(resp)) { | |
701 | rc = PTR_ERR(resp); | |
702 | resp = NULL; | |
703 | goto exit; | |
704 | } | |
705 | ||
706 | if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) { | |
707 | rc = digital_skb_check_crc_f(resp); | |
708 | if (rc) { | |
709 | PROTOCOL_ERR("6.4.1.8"); | |
710 | goto exit; | |
711 | } | |
712 | } | |
713 | ||
714 | if (resp->len != sizeof(struct digital_sensf_req) + 1) { | |
715 | rc = -EINVAL; | |
716 | goto exit; | |
717 | } | |
718 | ||
719 | skb_pull(resp, 1); | |
720 | sensf_req = (struct digital_sensf_req *)resp->data; | |
721 | ||
722 | if (sensf_req->cmd != DIGITAL_CMD_SENSF_REQ) { | |
723 | rc = -EINVAL; | |
724 | goto exit; | |
725 | } | |
726 | ||
727 | rc = digital_tg_send_sensf_res(ddev, sensf_req); | |
728 | ||
729 | exit: | |
730 | if (rc) | |
731 | digital_poll_next_tech(ddev); | |
732 | ||
733 | dev_kfree_skb(resp); | |
734 | } | |
735 | ||
736 | int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech) | |
737 | { | |
738 | int rc; | |
739 | ||
740 | rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech); | |
741 | if (rc) | |
742 | return rc; | |
743 | ||
744 | rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, | |
745 | NFC_DIGITAL_FRAMING_NFCA_NFC_DEP); | |
746 | if (rc) | |
747 | return rc; | |
748 | ||
749 | return digital_tg_listen(ddev, 300, digital_tg_recv_sens_req, NULL); | |
750 | } | |
751 | ||
752 | int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech) | |
753 | { | |
754 | int rc; | |
755 | u8 *nfcid2; | |
756 | ||
757 | rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech); | |
758 | if (rc) | |
759 | return rc; | |
760 | ||
761 | rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, | |
762 | NFC_DIGITAL_FRAMING_NFCF_NFC_DEP); | |
763 | if (rc) | |
764 | return rc; | |
765 | ||
766 | nfcid2 = kzalloc(NFC_NFCID2_MAXSIZE, GFP_KERNEL); | |
767 | if (!nfcid2) | |
768 | return -ENOMEM; | |
769 | ||
770 | nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1; | |
771 | nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2; | |
772 | get_random_bytes(nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2); | |
773 | ||
774 | return digital_tg_listen(ddev, 300, digital_tg_recv_sensf_req, nfcid2); | |
775 | } |