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)) | |
12e3d241 | 33 | #define DIGITAL_SEL_RES_IS_T4T(sel_res) ((sel_res) & 0x20) |
7d0911c0 | 34 | #define DIGITAL_SEL_RES_IS_NFC_DEP(sel_res) ((sel_res) & 0x40) |
2c66daec | 35 | |
13292c9a | 36 | #define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x0C00) == 0x0C00) |
2c66daec | 37 | #define DIGITAL_SENS_RES_IS_VALID(sens_res) \ |
13292c9a TE |
38 | ((!((sens_res) & 0x001F) && (((sens_res) & 0x0C00) == 0x0C00)) || \ |
39 | (((sens_res) & 0x001F) && ((sens_res) & 0x0C00) != 0x0C00)) | |
2c66daec TE |
40 | |
41 | #define DIGITAL_MIFARE_READ_RES_LEN 16 | |
42 | #define DIGITAL_MIFARE_ACK_RES 0x0A | |
43 | ||
8c0695e4 TE |
44 | #define DIGITAL_CMD_SENSF_REQ 0x00 |
45 | #define DIGITAL_CMD_SENSF_RES 0x01 | |
46 | ||
47 | #define DIGITAL_SENSF_RES_MIN_LENGTH 17 | |
48 | #define DIGITAL_SENSF_RES_RD_AP_B1 0x00 | |
49 | #define DIGITAL_SENSF_RES_RD_AP_B2 0x8F | |
50 | ||
51 | #define DIGITAL_SENSF_REQ_RC_NONE 0 | |
52 | #define DIGITAL_SENSF_REQ_RC_SC 1 | |
53 | #define DIGITAL_SENSF_REQ_RC_AP 2 | |
54 | ||
a381d482 MG |
55 | #define DIGITAL_CMD_ISO15693_INVENTORY_REQ 0x01 |
56 | ||
57 | #define DIGITAL_ISO15693_REQ_FLAG_DATA_RATE BIT(1) | |
58 | #define DIGITAL_ISO15693_REQ_FLAG_INVENTORY BIT(2) | |
59 | #define DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS BIT(5) | |
60 | #define DIGITAL_ISO15693_RES_FLAG_ERROR BIT(0) | |
61 | #define DIGITAL_ISO15693_RES_IS_VALID(flags) \ | |
62 | (!((flags) & DIGITAL_ISO15693_RES_FLAG_ERROR)) | |
63 | ||
12e3d241 TE |
64 | static const u8 digital_ats_fsc[] = { |
65 | 16, 24, 32, 40, 48, 64, 96, 128, | |
66 | }; | |
67 | ||
68 | #define DIGITAL_ATS_FSCI(t0) ((t0) & 0x0F) | |
69 | #define DIGITAL_ATS_MAX_FSC 256 | |
70 | ||
71 | #define DIGITAL_RATS_BYTE1 0xE0 | |
72 | #define DIGITAL_RATS_PARAM 0x80 | |
73 | ||
2c66daec TE |
74 | struct digital_sdd_res { |
75 | u8 nfcid1[4]; | |
76 | u8 bcc; | |
77 | } __packed; | |
78 | ||
79 | struct digital_sel_req { | |
80 | u8 sel_cmd; | |
81 | u8 b2; | |
82 | u8 nfcid1[4]; | |
83 | u8 bcc; | |
84 | } __packed; | |
85 | ||
8c0695e4 TE |
86 | struct digital_sensf_req { |
87 | u8 cmd; | |
88 | u8 sc1; | |
89 | u8 sc2; | |
90 | u8 rc; | |
91 | u8 tsn; | |
92 | } __packed; | |
93 | ||
94 | struct digital_sensf_res { | |
95 | u8 cmd; | |
96 | u8 nfcid2[8]; | |
97 | u8 pad0[2]; | |
98 | u8 pad1[3]; | |
99 | u8 mrti_check; | |
100 | u8 mrti_update; | |
101 | u8 pad2; | |
102 | u8 rd[2]; | |
103 | } __packed; | |
104 | ||
a381d482 MG |
105 | struct digital_iso15693_inv_req { |
106 | u8 flags; | |
107 | u8 cmd; | |
108 | u8 mask_len; | |
109 | u64 mask; | |
110 | } __packed; | |
111 | ||
112 | struct digital_iso15693_inv_res { | |
113 | u8 flags; | |
114 | u8 dsfid; | |
115 | u64 uid; | |
116 | } __packed; | |
117 | ||
2c66daec TE |
118 | static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev, |
119 | struct nfc_target *target); | |
120 | ||
12e3d241 TE |
121 | static void digital_in_recv_ats(struct nfc_digital_dev *ddev, void *arg, |
122 | struct sk_buff *resp) | |
123 | { | |
124 | struct nfc_target *target = arg; | |
125 | u8 fsdi; | |
126 | int rc; | |
127 | ||
128 | if (IS_ERR(resp)) { | |
129 | rc = PTR_ERR(resp); | |
130 | resp = NULL; | |
131 | goto exit; | |
132 | } | |
133 | ||
134 | if (resp->len < 2) { | |
135 | rc = -EIO; | |
136 | goto exit; | |
137 | } | |
138 | ||
139 | fsdi = DIGITAL_ATS_FSCI(resp->data[1]); | |
140 | if (fsdi >= 8) | |
141 | ddev->target_fsc = DIGITAL_ATS_MAX_FSC; | |
142 | else | |
143 | ddev->target_fsc = digital_ats_fsc[fsdi]; | |
144 | ||
145 | ddev->curr_nfc_dep_pni = 0; | |
146 | ||
147 | rc = digital_target_found(ddev, target, NFC_PROTO_ISO14443); | |
148 | ||
149 | exit: | |
150 | dev_kfree_skb(resp); | |
151 | kfree(target); | |
152 | ||
153 | if (rc) | |
154 | digital_poll_next_tech(ddev); | |
155 | } | |
156 | ||
157 | static int digital_in_send_rats(struct nfc_digital_dev *ddev, | |
158 | struct nfc_target *target) | |
159 | { | |
160 | int rc; | |
161 | struct sk_buff *skb; | |
162 | ||
163 | skb = digital_skb_alloc(ddev, 2); | |
164 | if (!skb) | |
165 | return -ENOMEM; | |
166 | ||
167 | *skb_put(skb, 1) = DIGITAL_RATS_BYTE1; | |
168 | *skb_put(skb, 1) = DIGITAL_RATS_PARAM; | |
169 | ||
170 | rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_ats, | |
171 | target); | |
172 | if (rc) | |
173 | kfree_skb(skb); | |
174 | ||
175 | return rc; | |
176 | } | |
177 | ||
2c66daec TE |
178 | static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg, |
179 | struct sk_buff *resp) | |
180 | { | |
181 | struct nfc_target *target = arg; | |
182 | int rc; | |
183 | u8 sel_res; | |
184 | u8 nfc_proto; | |
185 | ||
186 | if (IS_ERR(resp)) { | |
187 | rc = PTR_ERR(resp); | |
188 | resp = NULL; | |
189 | goto exit; | |
190 | } | |
191 | ||
192 | if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) { | |
193 | rc = digital_skb_check_crc_a(resp); | |
194 | if (rc) { | |
195 | PROTOCOL_ERR("4.4.1.3"); | |
196 | goto exit; | |
197 | } | |
198 | } | |
199 | ||
200 | if (!resp->len) { | |
201 | rc = -EIO; | |
202 | goto exit; | |
203 | } | |
204 | ||
205 | sel_res = resp->data[0]; | |
206 | ||
207 | if (!DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res)) { | |
208 | rc = digital_in_send_sdd_req(ddev, target); | |
209 | if (rc) | |
210 | goto exit; | |
211 | ||
212 | goto exit_free_skb; | |
213 | } | |
214 | ||
12e3d241 TE |
215 | target->sel_res = sel_res; |
216 | ||
2c66daec TE |
217 | if (DIGITAL_SEL_RES_IS_T2T(sel_res)) { |
218 | nfc_proto = NFC_PROTO_MIFARE; | |
12e3d241 TE |
219 | } else if (DIGITAL_SEL_RES_IS_T4T(sel_res)) { |
220 | rc = digital_in_send_rats(ddev, target); | |
221 | if (rc) | |
222 | goto exit; | |
223 | /* | |
224 | * Skip target_found and don't free it for now. This will be | |
225 | * done when receiving the ATS | |
226 | */ | |
227 | goto exit_free_skb; | |
7d0911c0 TE |
228 | } else if (DIGITAL_SEL_RES_IS_NFC_DEP(sel_res)) { |
229 | nfc_proto = NFC_PROTO_NFC_DEP; | |
2c66daec TE |
230 | } else { |
231 | rc = -EOPNOTSUPP; | |
232 | goto exit; | |
233 | } | |
234 | ||
2c66daec TE |
235 | rc = digital_target_found(ddev, target, nfc_proto); |
236 | ||
237 | exit: | |
238 | kfree(target); | |
239 | ||
240 | exit_free_skb: | |
241 | dev_kfree_skb(resp); | |
242 | ||
243 | if (rc) | |
244 | digital_poll_next_tech(ddev); | |
245 | } | |
246 | ||
247 | static int digital_in_send_sel_req(struct nfc_digital_dev *ddev, | |
248 | struct nfc_target *target, | |
249 | struct digital_sdd_res *sdd_res) | |
250 | { | |
251 | struct sk_buff *skb; | |
252 | struct digital_sel_req *sel_req; | |
253 | u8 sel_cmd; | |
254 | int rc; | |
255 | ||
256 | skb = digital_skb_alloc(ddev, sizeof(struct digital_sel_req)); | |
257 | if (!skb) | |
258 | return -ENOMEM; | |
259 | ||
260 | skb_put(skb, sizeof(struct digital_sel_req)); | |
261 | sel_req = (struct digital_sel_req *)skb->data; | |
262 | ||
263 | if (target->nfcid1_len <= 4) | |
264 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL1; | |
265 | else if (target->nfcid1_len < 10) | |
266 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL2; | |
267 | else | |
268 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL3; | |
269 | ||
270 | sel_req->sel_cmd = sel_cmd; | |
271 | sel_req->b2 = 0x70; | |
272 | memcpy(sel_req->nfcid1, sdd_res->nfcid1, 4); | |
273 | sel_req->bcc = sdd_res->bcc; | |
274 | ||
275 | if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) { | |
276 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, | |
277 | NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A); | |
278 | if (rc) | |
279 | goto exit; | |
280 | } else { | |
281 | digital_skb_add_crc_a(skb); | |
282 | } | |
283 | ||
284 | rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sel_res, | |
285 | target); | |
286 | exit: | |
287 | if (rc) | |
288 | kfree_skb(skb); | |
289 | ||
290 | return rc; | |
291 | } | |
292 | ||
293 | static void digital_in_recv_sdd_res(struct nfc_digital_dev *ddev, void *arg, | |
294 | struct sk_buff *resp) | |
295 | { | |
296 | struct nfc_target *target = arg; | |
297 | struct digital_sdd_res *sdd_res; | |
298 | int rc; | |
299 | u8 offset, size; | |
300 | u8 i, bcc; | |
301 | ||
302 | if (IS_ERR(resp)) { | |
303 | rc = PTR_ERR(resp); | |
304 | resp = NULL; | |
305 | goto exit; | |
306 | } | |
307 | ||
308 | if (resp->len < DIGITAL_SDD_RES_LEN) { | |
309 | PROTOCOL_ERR("4.7.2.8"); | |
310 | rc = -EINVAL; | |
311 | goto exit; | |
312 | } | |
313 | ||
314 | sdd_res = (struct digital_sdd_res *)resp->data; | |
315 | ||
316 | for (i = 0, bcc = 0; i < 4; i++) | |
317 | bcc ^= sdd_res->nfcid1[i]; | |
318 | ||
319 | if (bcc != sdd_res->bcc) { | |
320 | PROTOCOL_ERR("4.7.2.6"); | |
321 | rc = -EINVAL; | |
322 | goto exit; | |
323 | } | |
324 | ||
325 | if (sdd_res->nfcid1[0] == DIGITAL_SDD_RES_CT) { | |
326 | offset = 1; | |
327 | size = 3; | |
328 | } else { | |
329 | offset = 0; | |
330 | size = 4; | |
331 | } | |
332 | ||
333 | memcpy(target->nfcid1 + target->nfcid1_len, sdd_res->nfcid1 + offset, | |
334 | size); | |
335 | target->nfcid1_len += size; | |
336 | ||
337 | rc = digital_in_send_sel_req(ddev, target, sdd_res); | |
338 | ||
339 | exit: | |
340 | dev_kfree_skb(resp); | |
341 | ||
342 | if (rc) { | |
343 | kfree(target); | |
344 | digital_poll_next_tech(ddev); | |
345 | } | |
346 | } | |
347 | ||
348 | static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev, | |
349 | struct nfc_target *target) | |
350 | { | |
351 | int rc; | |
352 | struct sk_buff *skb; | |
353 | u8 sel_cmd; | |
354 | ||
355 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, | |
356 | NFC_DIGITAL_FRAMING_NFCA_STANDARD); | |
357 | if (rc) | |
358 | return rc; | |
359 | ||
360 | skb = digital_skb_alloc(ddev, 2); | |
26042530 | 361 | if (!skb) |
2c66daec | 362 | return -ENOMEM; |
2c66daec TE |
363 | |
364 | if (target->nfcid1_len == 0) | |
365 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL1; | |
366 | else if (target->nfcid1_len == 3) | |
367 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL2; | |
368 | else | |
369 | sel_cmd = DIGITAL_CMD_SEL_REQ_CL3; | |
370 | ||
371 | *skb_put(skb, sizeof(u8)) = sel_cmd; | |
372 | *skb_put(skb, sizeof(u8)) = DIGITAL_SDD_REQ_SEL_PAR; | |
373 | ||
374 | return digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sdd_res, | |
375 | target); | |
376 | } | |
377 | ||
59ee2361 TE |
378 | static void digital_in_recv_sens_res(struct nfc_digital_dev *ddev, void *arg, |
379 | struct sk_buff *resp) | |
380 | { | |
2c66daec | 381 | struct nfc_target *target = NULL; |
2c66daec TE |
382 | int rc; |
383 | ||
384 | if (IS_ERR(resp)) { | |
385 | rc = PTR_ERR(resp); | |
386 | resp = NULL; | |
387 | goto exit; | |
388 | } | |
389 | ||
390 | if (resp->len < sizeof(u16)) { | |
391 | rc = -EIO; | |
392 | goto exit; | |
393 | } | |
394 | ||
395 | target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL); | |
396 | if (!target) { | |
397 | rc = -ENOMEM; | |
398 | goto exit; | |
399 | } | |
400 | ||
13292c9a | 401 | target->sens_res = __le16_to_cpu(*(__le16 *)resp->data); |
59ee2361 | 402 | |
13292c9a | 403 | if (!DIGITAL_SENS_RES_IS_VALID(target->sens_res)) { |
2c66daec TE |
404 | PROTOCOL_ERR("4.6.3.3"); |
405 | rc = -EINVAL; | |
406 | goto exit; | |
407 | } | |
408 | ||
13292c9a | 409 | if (DIGITAL_SENS_RES_IS_T1T(target->sens_res)) |
2c66daec TE |
410 | rc = digital_target_found(ddev, target, NFC_PROTO_JEWEL); |
411 | else | |
412 | rc = digital_in_send_sdd_req(ddev, target); | |
413 | ||
414 | exit: | |
415 | dev_kfree_skb(resp); | |
416 | ||
417 | if (rc) { | |
418 | kfree(target); | |
419 | digital_poll_next_tech(ddev); | |
420 | } | |
59ee2361 TE |
421 | } |
422 | ||
423 | int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech) | |
424 | { | |
425 | struct sk_buff *skb; | |
426 | int rc; | |
427 | ||
428 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, | |
429 | NFC_DIGITAL_RF_TECH_106A); | |
430 | if (rc) | |
431 | return rc; | |
432 | ||
433 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, | |
434 | NFC_DIGITAL_FRAMING_NFCA_SHORT); | |
435 | if (rc) | |
436 | return rc; | |
437 | ||
438 | skb = digital_skb_alloc(ddev, 1); | |
439 | if (!skb) | |
440 | return -ENOMEM; | |
441 | ||
442 | *skb_put(skb, sizeof(u8)) = DIGITAL_CMD_SENS_REQ; | |
443 | ||
444 | rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sens_res, NULL); | |
445 | if (rc) | |
446 | kfree_skb(skb); | |
447 | ||
448 | return rc; | |
449 | } | |
2c66daec TE |
450 | |
451 | int digital_in_recv_mifare_res(struct sk_buff *resp) | |
452 | { | |
453 | /* Successful READ command response is 16 data bytes + 2 CRC bytes long. | |
454 | * Since the driver can't differentiate a ACK/NACK response from a valid | |
455 | * READ response, the CRC calculation must be handled at digital level | |
456 | * even if the driver supports it for this technology. | |
457 | */ | |
458 | if (resp->len == DIGITAL_MIFARE_READ_RES_LEN + DIGITAL_CRC_LEN) { | |
459 | if (digital_skb_check_crc_a(resp)) { | |
460 | PROTOCOL_ERR("9.4.1.2"); | |
461 | return -EIO; | |
462 | } | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
467 | /* ACK response (i.e. successful WRITE). */ | |
468 | if (resp->len == 1 && resp->data[0] == DIGITAL_MIFARE_ACK_RES) { | |
469 | resp->data[0] = 0; | |
470 | return 0; | |
471 | } | |
472 | ||
473 | /* NACK and any other responses are treated as error. */ | |
474 | return -EIO; | |
475 | } | |
8c0695e4 TE |
476 | |
477 | static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg, | |
478 | struct sk_buff *resp) | |
479 | { | |
480 | int rc; | |
7d0911c0 | 481 | u8 proto; |
8c0695e4 TE |
482 | struct nfc_target target; |
483 | struct digital_sensf_res *sensf_res; | |
484 | ||
485 | if (IS_ERR(resp)) { | |
486 | rc = PTR_ERR(resp); | |
487 | resp = NULL; | |
488 | goto exit; | |
489 | } | |
490 | ||
491 | if (resp->len < DIGITAL_SENSF_RES_MIN_LENGTH) { | |
492 | rc = -EIO; | |
493 | goto exit; | |
494 | } | |
495 | ||
496 | if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) { | |
497 | rc = digital_skb_check_crc_f(resp); | |
498 | if (rc) { | |
499 | PROTOCOL_ERR("6.4.1.8"); | |
500 | goto exit; | |
501 | } | |
502 | } | |
503 | ||
504 | skb_pull(resp, 1); | |
505 | ||
506 | memset(&target, 0, sizeof(struct nfc_target)); | |
507 | ||
508 | sensf_res = (struct digital_sensf_res *)resp->data; | |
509 | ||
510 | memcpy(target.sensf_res, sensf_res, resp->len); | |
511 | target.sensf_res_len = resp->len; | |
512 | ||
513 | memcpy(target.nfcid2, sensf_res->nfcid2, NFC_NFCID2_MAXSIZE); | |
514 | target.nfcid2_len = NFC_NFCID2_MAXSIZE; | |
515 | ||
7d0911c0 TE |
516 | if (target.nfcid2[0] == DIGITAL_SENSF_NFCID2_NFC_DEP_B1 && |
517 | target.nfcid2[1] == DIGITAL_SENSF_NFCID2_NFC_DEP_B2) | |
518 | proto = NFC_PROTO_NFC_DEP; | |
519 | else | |
520 | proto = NFC_PROTO_FELICA; | |
521 | ||
522 | rc = digital_target_found(ddev, &target, proto); | |
8c0695e4 TE |
523 | |
524 | exit: | |
525 | dev_kfree_skb(resp); | |
526 | ||
527 | if (rc) | |
528 | digital_poll_next_tech(ddev); | |
529 | } | |
530 | ||
531 | int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech) | |
532 | { | |
533 | struct digital_sensf_req *sensf_req; | |
534 | struct sk_buff *skb; | |
535 | int rc; | |
536 | u8 size; | |
537 | ||
538 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech); | |
539 | if (rc) | |
540 | return rc; | |
541 | ||
542 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, | |
543 | NFC_DIGITAL_FRAMING_NFCF); | |
544 | if (rc) | |
545 | return rc; | |
546 | ||
547 | size = sizeof(struct digital_sensf_req); | |
548 | ||
549 | skb = digital_skb_alloc(ddev, size); | |
550 | if (!skb) | |
551 | return -ENOMEM; | |
552 | ||
553 | skb_put(skb, size); | |
554 | ||
555 | sensf_req = (struct digital_sensf_req *)skb->data; | |
556 | sensf_req->cmd = DIGITAL_CMD_SENSF_REQ; | |
557 | sensf_req->sc1 = 0xFF; | |
558 | sensf_req->sc2 = 0xFF; | |
559 | sensf_req->rc = 0; | |
560 | sensf_req->tsn = 0; | |
561 | ||
562 | *skb_push(skb, 1) = size + 1; | |
563 | ||
564 | if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) | |
565 | digital_skb_add_crc_f(skb); | |
566 | ||
567 | rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensf_res, | |
568 | NULL); | |
569 | if (rc) | |
570 | kfree_skb(skb); | |
571 | ||
572 | return rc; | |
573 | } | |
1c7a4c24 | 574 | |
a381d482 MG |
575 | static void digital_in_recv_iso15693_inv_res(struct nfc_digital_dev *ddev, |
576 | void *arg, struct sk_buff *resp) | |
577 | { | |
578 | struct digital_iso15693_inv_res *res; | |
579 | struct nfc_target *target = NULL; | |
580 | int rc; | |
581 | ||
582 | if (IS_ERR(resp)) { | |
583 | rc = PTR_ERR(resp); | |
584 | resp = NULL; | |
585 | goto out_free_skb; | |
586 | } | |
587 | ||
588 | if (resp->len != sizeof(*res)) { | |
589 | rc = -EIO; | |
590 | goto out_free_skb; | |
591 | } | |
592 | ||
593 | res = (struct digital_iso15693_inv_res *)resp->data; | |
594 | ||
595 | if (!DIGITAL_ISO15693_RES_IS_VALID(res->flags)) { | |
596 | PROTOCOL_ERR("ISO15693 - 10.3.1"); | |
597 | rc = -EINVAL; | |
598 | goto out_free_skb; | |
599 | } | |
600 | ||
601 | target = kzalloc(sizeof(*target), GFP_KERNEL); | |
602 | if (!target) { | |
603 | rc = -ENOMEM; | |
604 | goto out_free_skb; | |
605 | } | |
606 | ||
607 | target->is_iso15693 = 1; | |
608 | target->iso15693_dsfid = res->dsfid; | |
609 | memcpy(target->iso15693_uid, &res->uid, sizeof(target->iso15693_uid)); | |
610 | ||
611 | rc = digital_target_found(ddev, target, NFC_PROTO_ISO15693); | |
612 | ||
613 | kfree(target); | |
614 | ||
615 | out_free_skb: | |
616 | dev_kfree_skb(resp); | |
617 | ||
618 | if (rc) | |
619 | digital_poll_next_tech(ddev); | |
620 | } | |
621 | ||
622 | int digital_in_send_iso15693_inv_req(struct nfc_digital_dev *ddev, u8 rf_tech) | |
623 | { | |
624 | struct digital_iso15693_inv_req *req; | |
625 | struct sk_buff *skb; | |
626 | int rc; | |
627 | ||
628 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, | |
629 | NFC_DIGITAL_RF_TECH_ISO15693); | |
630 | if (rc) | |
631 | return rc; | |
632 | ||
633 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, | |
634 | NFC_DIGITAL_FRAMING_ISO15693_INVENTORY); | |
635 | if (rc) | |
636 | return rc; | |
637 | ||
638 | skb = digital_skb_alloc(ddev, sizeof(*req)); | |
639 | if (!skb) | |
640 | return -ENOMEM; | |
641 | ||
642 | skb_put(skb, sizeof(*req) - sizeof(req->mask)); /* No mask */ | |
643 | req = (struct digital_iso15693_inv_req *)skb->data; | |
644 | ||
645 | /* Single sub-carrier, high data rate, no AFI, single slot | |
646 | * Inventory command | |
647 | */ | |
648 | req->flags = DIGITAL_ISO15693_REQ_FLAG_DATA_RATE | | |
649 | DIGITAL_ISO15693_REQ_FLAG_INVENTORY | | |
650 | DIGITAL_ISO15693_REQ_FLAG_NB_SLOTS; | |
651 | req->cmd = DIGITAL_CMD_ISO15693_INVENTORY_REQ; | |
652 | req->mask_len = 0; | |
653 | ||
654 | rc = digital_in_send_cmd(ddev, skb, 30, | |
655 | digital_in_recv_iso15693_inv_res, NULL); | |
656 | if (rc) | |
657 | kfree_skb(skb); | |
658 | ||
659 | return rc; | |
660 | } | |
661 | ||
1c7a4c24 TE |
662 | static int digital_tg_send_sel_res(struct nfc_digital_dev *ddev) |
663 | { | |
664 | struct sk_buff *skb; | |
665 | int rc; | |
666 | ||
667 | skb = digital_skb_alloc(ddev, 1); | |
668 | if (!skb) | |
669 | return -ENOMEM; | |
670 | ||
671 | *skb_put(skb, 1) = DIGITAL_SEL_RES_NFC_DEP; | |
672 | ||
673 | if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) | |
674 | digital_skb_add_crc_a(skb); | |
675 | ||
676 | rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_atr_req, | |
677 | NULL); | |
678 | if (rc) | |
679 | kfree_skb(skb); | |
680 | ||
681 | return rc; | |
682 | } | |
683 | ||
684 | static void digital_tg_recv_sel_req(struct nfc_digital_dev *ddev, void *arg, | |
685 | struct sk_buff *resp) | |
686 | { | |
687 | int rc; | |
688 | ||
689 | if (IS_ERR(resp)) { | |
690 | rc = PTR_ERR(resp); | |
691 | resp = NULL; | |
692 | goto exit; | |
693 | } | |
694 | ||
695 | if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) { | |
696 | rc = digital_skb_check_crc_a(resp); | |
697 | if (rc) { | |
698 | PROTOCOL_ERR("4.4.1.3"); | |
699 | goto exit; | |
700 | } | |
701 | } | |
702 | ||
703 | /* Silently ignore SEL_REQ content and send a SEL_RES for NFC-DEP */ | |
704 | ||
705 | rc = digital_tg_send_sel_res(ddev); | |
706 | ||
707 | exit: | |
708 | if (rc) | |
709 | digital_poll_next_tech(ddev); | |
710 | ||
711 | dev_kfree_skb(resp); | |
712 | } | |
713 | ||
714 | static int digital_tg_send_sdd_res(struct nfc_digital_dev *ddev) | |
715 | { | |
716 | struct sk_buff *skb; | |
717 | struct digital_sdd_res *sdd_res; | |
718 | int rc, i; | |
719 | ||
720 | skb = digital_skb_alloc(ddev, sizeof(struct digital_sdd_res)); | |
721 | if (!skb) | |
722 | return -ENOMEM; | |
723 | ||
724 | skb_put(skb, sizeof(struct digital_sdd_res)); | |
725 | sdd_res = (struct digital_sdd_res *)skb->data; | |
726 | ||
727 | sdd_res->nfcid1[0] = 0x08; | |
728 | get_random_bytes(sdd_res->nfcid1 + 1, 3); | |
729 | ||
730 | sdd_res->bcc = 0; | |
731 | for (i = 0; i < 4; i++) | |
732 | sdd_res->bcc ^= sdd_res->nfcid1[i]; | |
733 | ||
734 | rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sel_req, | |
735 | NULL); | |
736 | if (rc) | |
737 | kfree_skb(skb); | |
738 | ||
739 | return rc; | |
740 | } | |
741 | ||
742 | static void digital_tg_recv_sdd_req(struct nfc_digital_dev *ddev, void *arg, | |
743 | struct sk_buff *resp) | |
744 | { | |
745 | u8 *sdd_req; | |
746 | int rc; | |
747 | ||
748 | if (IS_ERR(resp)) { | |
749 | rc = PTR_ERR(resp); | |
750 | resp = NULL; | |
751 | goto exit; | |
752 | } | |
753 | ||
754 | sdd_req = resp->data; | |
755 | ||
756 | if (resp->len < 2 || sdd_req[0] != DIGITAL_CMD_SEL_REQ_CL1 || | |
757 | sdd_req[1] != DIGITAL_SDD_REQ_SEL_PAR) { | |
758 | rc = -EINVAL; | |
759 | goto exit; | |
760 | } | |
761 | ||
762 | rc = digital_tg_send_sdd_res(ddev); | |
763 | ||
764 | exit: | |
765 | if (rc) | |
766 | digital_poll_next_tech(ddev); | |
767 | ||
768 | dev_kfree_skb(resp); | |
769 | } | |
770 | ||
771 | static int digital_tg_send_sens_res(struct nfc_digital_dev *ddev) | |
772 | { | |
773 | struct sk_buff *skb; | |
774 | u8 *sens_res; | |
775 | int rc; | |
776 | ||
777 | skb = digital_skb_alloc(ddev, 2); | |
778 | if (!skb) | |
779 | return -ENOMEM; | |
780 | ||
781 | sens_res = skb_put(skb, 2); | |
782 | ||
783 | sens_res[0] = (DIGITAL_SENS_RES_NFC_DEP >> 8) & 0xFF; | |
784 | sens_res[1] = DIGITAL_SENS_RES_NFC_DEP & 0xFF; | |
785 | ||
786 | rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sdd_req, | |
787 | NULL); | |
788 | if (rc) | |
789 | kfree_skb(skb); | |
790 | ||
791 | return rc; | |
792 | } | |
793 | ||
794 | void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg, | |
795 | struct sk_buff *resp) | |
796 | { | |
797 | u8 sens_req; | |
798 | int rc; | |
799 | ||
800 | if (IS_ERR(resp)) { | |
801 | rc = PTR_ERR(resp); | |
802 | resp = NULL; | |
803 | goto exit; | |
804 | } | |
805 | ||
806 | sens_req = resp->data[0]; | |
807 | ||
808 | if (!resp->len || (sens_req != DIGITAL_CMD_SENS_REQ && | |
809 | sens_req != DIGITAL_CMD_ALL_REQ)) { | |
810 | rc = -EINVAL; | |
811 | goto exit; | |
812 | } | |
813 | ||
814 | rc = digital_tg_send_sens_res(ddev); | |
815 | ||
816 | exit: | |
817 | if (rc) | |
818 | digital_poll_next_tech(ddev); | |
819 | ||
820 | dev_kfree_skb(resp); | |
821 | } | |
822 | ||
180106bd | 823 | static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev, |
1c7a4c24 TE |
824 | struct digital_sensf_req *sensf_req) |
825 | { | |
826 | struct sk_buff *skb; | |
827 | u8 size; | |
828 | int rc; | |
829 | struct digital_sensf_res *sensf_res; | |
830 | ||
831 | size = sizeof(struct digital_sensf_res); | |
832 | ||
833 | if (sensf_req->rc != DIGITAL_SENSF_REQ_RC_NONE) | |
834 | size -= sizeof(sensf_res->rd); | |
835 | ||
836 | skb = digital_skb_alloc(ddev, size); | |
837 | if (!skb) | |
838 | return -ENOMEM; | |
839 | ||
840 | skb_put(skb, size); | |
841 | ||
842 | sensf_res = (struct digital_sensf_res *)skb->data; | |
843 | ||
844 | memset(sensf_res, 0, size); | |
845 | ||
846 | sensf_res->cmd = DIGITAL_CMD_SENSF_RES; | |
847 | sensf_res->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1; | |
848 | sensf_res->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2; | |
849 | get_random_bytes(&sensf_res->nfcid2[2], 6); | |
850 | ||
851 | switch (sensf_req->rc) { | |
852 | case DIGITAL_SENSF_REQ_RC_SC: | |
853 | sensf_res->rd[0] = sensf_req->sc1; | |
854 | sensf_res->rd[1] = sensf_req->sc2; | |
855 | break; | |
856 | case DIGITAL_SENSF_REQ_RC_AP: | |
857 | sensf_res->rd[0] = DIGITAL_SENSF_RES_RD_AP_B1; | |
858 | sensf_res->rd[1] = DIGITAL_SENSF_RES_RD_AP_B2; | |
859 | break; | |
860 | } | |
861 | ||
862 | *skb_push(skb, sizeof(u8)) = size + 1; | |
863 | ||
864 | if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) | |
865 | digital_skb_add_crc_f(skb); | |
866 | ||
867 | rc = digital_tg_send_cmd(ddev, skb, 300, | |
868 | digital_tg_recv_atr_req, NULL); | |
869 | if (rc) | |
870 | kfree_skb(skb); | |
871 | ||
872 | return rc; | |
873 | } | |
874 | ||
875 | void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg, | |
876 | struct sk_buff *resp) | |
877 | { | |
878 | struct digital_sensf_req *sensf_req; | |
879 | int rc; | |
880 | ||
881 | if (IS_ERR(resp)) { | |
882 | rc = PTR_ERR(resp); | |
883 | resp = NULL; | |
884 | goto exit; | |
885 | } | |
886 | ||
887 | if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) { | |
888 | rc = digital_skb_check_crc_f(resp); | |
889 | if (rc) { | |
890 | PROTOCOL_ERR("6.4.1.8"); | |
891 | goto exit; | |
892 | } | |
893 | } | |
894 | ||
895 | if (resp->len != sizeof(struct digital_sensf_req) + 1) { | |
896 | rc = -EINVAL; | |
897 | goto exit; | |
898 | } | |
899 | ||
900 | skb_pull(resp, 1); | |
901 | sensf_req = (struct digital_sensf_req *)resp->data; | |
902 | ||
903 | if (sensf_req->cmd != DIGITAL_CMD_SENSF_REQ) { | |
904 | rc = -EINVAL; | |
905 | goto exit; | |
906 | } | |
907 | ||
908 | rc = digital_tg_send_sensf_res(ddev, sensf_req); | |
909 | ||
910 | exit: | |
911 | if (rc) | |
912 | digital_poll_next_tech(ddev); | |
913 | ||
914 | dev_kfree_skb(resp); | |
915 | } | |
916 | ||
917 | int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech) | |
918 | { | |
919 | int rc; | |
920 | ||
921 | rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech); | |
922 | if (rc) | |
923 | return rc; | |
924 | ||
925 | rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, | |
926 | NFC_DIGITAL_FRAMING_NFCA_NFC_DEP); | |
927 | if (rc) | |
928 | return rc; | |
929 | ||
930 | return digital_tg_listen(ddev, 300, digital_tg_recv_sens_req, NULL); | |
931 | } | |
932 | ||
933 | int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech) | |
934 | { | |
935 | int rc; | |
936 | u8 *nfcid2; | |
937 | ||
938 | rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech); | |
939 | if (rc) | |
940 | return rc; | |
941 | ||
942 | rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, | |
943 | NFC_DIGITAL_FRAMING_NFCF_NFC_DEP); | |
944 | if (rc) | |
945 | return rc; | |
946 | ||
947 | nfcid2 = kzalloc(NFC_NFCID2_MAXSIZE, GFP_KERNEL); | |
948 | if (!nfcid2) | |
949 | return -ENOMEM; | |
950 | ||
951 | nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1; | |
952 | nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2; | |
953 | get_random_bytes(nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2); | |
954 | ||
955 | return digital_tg_listen(ddev, 300, digital_tg_recv_sensf_req, nfcid2); | |
956 | } |