Commit | Line | Data |
---|---|---|
4b10884e 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 | ||
4b10884e TE |
18 | #include <linux/module.h> |
19 | ||
20 | #include "digital.h" | |
21 | ||
59ee2361 | 22 | #define DIGITAL_PROTO_NFCA_RF_TECH \ |
ce2e56cd SS |
23 | (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | \ |
24 | NFC_PROTO_NFC_DEP_MASK | NFC_PROTO_ISO14443_MASK) | |
59ee2361 | 25 | |
24734607 MG |
26 | #define DIGITAL_PROTO_NFCB_RF_TECH NFC_PROTO_ISO14443_B_MASK |
27 | ||
7d0911c0 TE |
28 | #define DIGITAL_PROTO_NFCF_RF_TECH \ |
29 | (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK) | |
8c0695e4 | 30 | |
a381d482 MG |
31 | #define DIGITAL_PROTO_ISO15693_RF_TECH NFC_PROTO_ISO15693_MASK |
32 | ||
7854a445 TE |
33 | /* Delay between each poll frame (ms) */ |
34 | #define DIGITAL_POLL_INTERVAL 10 | |
35 | ||
59ee2361 TE |
36 | struct digital_cmd { |
37 | struct list_head queue; | |
38 | ||
39 | u8 type; | |
40 | u8 pending; | |
41 | ||
42 | u16 timeout; | |
43 | struct sk_buff *req; | |
44 | struct sk_buff *resp; | |
1c7a4c24 | 45 | struct digital_tg_mdaa_params *mdaa_params; |
59ee2361 TE |
46 | |
47 | nfc_digital_cmd_complete_t cmd_cb; | |
48 | void *cb_context; | |
49 | }; | |
50 | ||
51 | struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev, | |
52 | unsigned int len) | |
53 | { | |
54 | struct sk_buff *skb; | |
55 | ||
56 | skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom, | |
57 | GFP_KERNEL); | |
58 | if (skb) | |
59 | skb_reserve(skb, ddev->tx_headroom); | |
60 | ||
61 | return skb; | |
62 | } | |
63 | ||
2c66daec TE |
64 | void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init, |
65 | u8 bitwise_inv, u8 msb_first) | |
66 | { | |
67 | u16 crc; | |
68 | ||
69 | crc = crc_func(init, skb->data, skb->len); | |
70 | ||
71 | if (bitwise_inv) | |
72 | crc = ~crc; | |
73 | ||
74 | if (msb_first) | |
75 | crc = __fswab16(crc); | |
76 | ||
77 | *skb_put(skb, 1) = crc & 0xFF; | |
78 | *skb_put(skb, 1) = (crc >> 8) & 0xFF; | |
79 | } | |
80 | ||
81 | int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func, | |
82 | u16 crc_init, u8 bitwise_inv, u8 msb_first) | |
83 | { | |
84 | int rc; | |
85 | u16 crc; | |
86 | ||
87 | if (skb->len <= 2) | |
88 | return -EIO; | |
89 | ||
90 | crc = crc_func(crc_init, skb->data, skb->len - 2); | |
91 | ||
92 | if (bitwise_inv) | |
93 | crc = ~crc; | |
94 | ||
95 | if (msb_first) | |
96 | crc = __swab16(crc); | |
97 | ||
98 | rc = (skb->data[skb->len - 2] - (crc & 0xFF)) + | |
99 | (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF)); | |
100 | ||
101 | if (rc) | |
102 | return -EIO; | |
103 | ||
104 | skb_trim(skb, skb->len - 2); | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
59ee2361 TE |
109 | static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on) |
110 | { | |
111 | ddev->ops->switch_rf(ddev, on); | |
112 | } | |
113 | ||
114 | static inline void digital_abort_cmd(struct nfc_digital_dev *ddev) | |
115 | { | |
116 | ddev->ops->abort_cmd(ddev); | |
117 | } | |
118 | ||
119 | static void digital_wq_cmd_complete(struct work_struct *work) | |
120 | { | |
121 | struct digital_cmd *cmd; | |
122 | struct nfc_digital_dev *ddev = container_of(work, | |
123 | struct nfc_digital_dev, | |
124 | cmd_complete_work); | |
125 | ||
126 | mutex_lock(&ddev->cmd_lock); | |
127 | ||
128 | cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd, | |
129 | queue); | |
130 | if (!cmd) { | |
131 | mutex_unlock(&ddev->cmd_lock); | |
132 | return; | |
133 | } | |
134 | ||
135 | list_del(&cmd->queue); | |
136 | ||
137 | mutex_unlock(&ddev->cmd_lock); | |
138 | ||
139 | if (!IS_ERR(cmd->resp)) | |
140 | print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1, | |
141 | cmd->resp->data, cmd->resp->len, false); | |
142 | ||
143 | cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp); | |
144 | ||
1c7a4c24 | 145 | kfree(cmd->mdaa_params); |
59ee2361 TE |
146 | kfree(cmd); |
147 | ||
148 | schedule_work(&ddev->cmd_work); | |
149 | } | |
150 | ||
151 | static void digital_send_cmd_complete(struct nfc_digital_dev *ddev, | |
152 | void *arg, struct sk_buff *resp) | |
153 | { | |
154 | struct digital_cmd *cmd = arg; | |
155 | ||
156 | cmd->resp = resp; | |
157 | ||
158 | schedule_work(&ddev->cmd_complete_work); | |
159 | } | |
160 | ||
161 | static void digital_wq_cmd(struct work_struct *work) | |
162 | { | |
163 | int rc; | |
164 | struct digital_cmd *cmd; | |
1c7a4c24 | 165 | struct digital_tg_mdaa_params *params; |
59ee2361 TE |
166 | struct nfc_digital_dev *ddev = container_of(work, |
167 | struct nfc_digital_dev, | |
168 | cmd_work); | |
169 | ||
170 | mutex_lock(&ddev->cmd_lock); | |
171 | ||
172 | cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd, | |
173 | queue); | |
174 | if (!cmd || cmd->pending) { | |
175 | mutex_unlock(&ddev->cmd_lock); | |
176 | return; | |
177 | } | |
178 | ||
179 | mutex_unlock(&ddev->cmd_lock); | |
180 | ||
181 | if (cmd->req) | |
182 | print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1, | |
183 | cmd->req->data, cmd->req->len, false); | |
184 | ||
185 | switch (cmd->type) { | |
186 | case DIGITAL_CMD_IN_SEND: | |
187 | rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout, | |
188 | digital_send_cmd_complete, cmd); | |
189 | break; | |
1c7a4c24 TE |
190 | |
191 | case DIGITAL_CMD_TG_SEND: | |
192 | rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout, | |
193 | digital_send_cmd_complete, cmd); | |
194 | break; | |
195 | ||
196 | case DIGITAL_CMD_TG_LISTEN: | |
197 | rc = ddev->ops->tg_listen(ddev, cmd->timeout, | |
198 | digital_send_cmd_complete, cmd); | |
199 | break; | |
200 | ||
201 | case DIGITAL_CMD_TG_LISTEN_MDAA: | |
202 | params = cmd->mdaa_params; | |
203 | ||
204 | rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout, | |
205 | digital_send_cmd_complete, cmd); | |
206 | break; | |
207 | ||
bf30a67c MG |
208 | case DIGITAL_CMD_TG_LISTEN_MD: |
209 | rc = ddev->ops->tg_listen_md(ddev, cmd->timeout, | |
210 | digital_send_cmd_complete, cmd); | |
211 | break; | |
212 | ||
59ee2361 | 213 | default: |
26042530 | 214 | pr_err("Unknown cmd type %d\n", cmd->type); |
59ee2361 TE |
215 | return; |
216 | } | |
217 | ||
218 | if (!rc) | |
219 | return; | |
220 | ||
26042530 | 221 | pr_err("in_send_command returned err %d\n", rc); |
59ee2361 TE |
222 | |
223 | mutex_lock(&ddev->cmd_lock); | |
224 | list_del(&cmd->queue); | |
225 | mutex_unlock(&ddev->cmd_lock); | |
226 | ||
227 | kfree_skb(cmd->req); | |
1c7a4c24 | 228 | kfree(cmd->mdaa_params); |
59ee2361 TE |
229 | kfree(cmd); |
230 | ||
231 | schedule_work(&ddev->cmd_work); | |
232 | } | |
233 | ||
234 | int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type, | |
1c7a4c24 TE |
235 | struct sk_buff *skb, struct digital_tg_mdaa_params *params, |
236 | u16 timeout, nfc_digital_cmd_complete_t cmd_cb, | |
237 | void *cb_context) | |
59ee2361 TE |
238 | { |
239 | struct digital_cmd *cmd; | |
240 | ||
241 | cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL); | |
242 | if (!cmd) | |
243 | return -ENOMEM; | |
244 | ||
245 | cmd->type = cmd_type; | |
246 | cmd->timeout = timeout; | |
247 | cmd->req = skb; | |
1c7a4c24 | 248 | cmd->mdaa_params = params; |
59ee2361 TE |
249 | cmd->cmd_cb = cmd_cb; |
250 | cmd->cb_context = cb_context; | |
251 | INIT_LIST_HEAD(&cmd->queue); | |
252 | ||
253 | mutex_lock(&ddev->cmd_lock); | |
254 | list_add_tail(&cmd->queue, &ddev->cmd_queue); | |
255 | mutex_unlock(&ddev->cmd_lock); | |
256 | ||
257 | schedule_work(&ddev->cmd_work); | |
258 | ||
259 | return 0; | |
260 | } | |
261 | ||
262 | int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param) | |
263 | { | |
264 | int rc; | |
265 | ||
266 | rc = ddev->ops->in_configure_hw(ddev, type, param); | |
267 | if (rc) | |
26042530 | 268 | pr_err("in_configure_hw failed: %d\n", rc); |
59ee2361 TE |
269 | |
270 | return rc; | |
271 | } | |
272 | ||
1c7a4c24 TE |
273 | int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param) |
274 | { | |
275 | int rc; | |
276 | ||
277 | rc = ddev->ops->tg_configure_hw(ddev, type, param); | |
278 | if (rc) | |
26042530 | 279 | pr_err("tg_configure_hw failed: %d\n", rc); |
1c7a4c24 TE |
280 | |
281 | return rc; | |
282 | } | |
283 | ||
284 | static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech) | |
285 | { | |
286 | struct digital_tg_mdaa_params *params; | |
287 | ||
288 | params = kzalloc(sizeof(struct digital_tg_mdaa_params), GFP_KERNEL); | |
289 | if (!params) | |
290 | return -ENOMEM; | |
291 | ||
292 | params->sens_res = DIGITAL_SENS_RES_NFC_DEP; | |
293 | get_random_bytes(params->nfcid1, sizeof(params->nfcid1)); | |
294 | params->sel_res = DIGITAL_SEL_RES_NFC_DEP; | |
295 | ||
296 | params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1; | |
297 | params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2; | |
298 | get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2); | |
299 | params->sc = DIGITAL_SENSF_FELICA_SC; | |
300 | ||
301 | return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params, | |
302 | 500, digital_tg_recv_atr_req, NULL); | |
303 | } | |
304 | ||
bf30a67c MG |
305 | static int digital_tg_listen_md(struct nfc_digital_dev *ddev, u8 rf_tech) |
306 | { | |
307 | return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MD, NULL, NULL, 500, | |
308 | digital_tg_recv_md_req, NULL); | |
309 | } | |
310 | ||
2c66daec TE |
311 | int digital_target_found(struct nfc_digital_dev *ddev, |
312 | struct nfc_target *target, u8 protocol) | |
313 | { | |
314 | int rc; | |
315 | u8 framing; | |
316 | u8 rf_tech; | |
0529a7ad | 317 | u8 poll_tech_count; |
2c66daec TE |
318 | int (*check_crc)(struct sk_buff *skb); |
319 | void (*add_crc)(struct sk_buff *skb); | |
320 | ||
321 | rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech; | |
322 | ||
323 | switch (protocol) { | |
324 | case NFC_PROTO_JEWEL: | |
325 | framing = NFC_DIGITAL_FRAMING_NFCA_T1T; | |
326 | check_crc = digital_skb_check_crc_b; | |
327 | add_crc = digital_skb_add_crc_b; | |
328 | break; | |
329 | ||
330 | case NFC_PROTO_MIFARE: | |
331 | framing = NFC_DIGITAL_FRAMING_NFCA_T2T; | |
332 | check_crc = digital_skb_check_crc_a; | |
333 | add_crc = digital_skb_add_crc_a; | |
334 | break; | |
335 | ||
8c0695e4 TE |
336 | case NFC_PROTO_FELICA: |
337 | framing = NFC_DIGITAL_FRAMING_NFCF_T3T; | |
338 | check_crc = digital_skb_check_crc_f; | |
339 | add_crc = digital_skb_add_crc_f; | |
340 | break; | |
341 | ||
7d0911c0 TE |
342 | case NFC_PROTO_NFC_DEP: |
343 | if (rf_tech == NFC_DIGITAL_RF_TECH_106A) { | |
344 | framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP; | |
345 | check_crc = digital_skb_check_crc_a; | |
346 | add_crc = digital_skb_add_crc_a; | |
347 | } else { | |
348 | framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP; | |
349 | check_crc = digital_skb_check_crc_f; | |
350 | add_crc = digital_skb_add_crc_f; | |
351 | } | |
352 | break; | |
353 | ||
a381d482 | 354 | case NFC_PROTO_ISO15693: |
ceeee42d | 355 | framing = NFC_DIGITAL_FRAMING_ISO15693_T5T; |
a381d482 MG |
356 | check_crc = digital_skb_check_crc_b; |
357 | add_crc = digital_skb_add_crc_b; | |
564af14e | 358 | break; |
12e3d241 TE |
359 | |
360 | case NFC_PROTO_ISO14443: | |
361 | framing = NFC_DIGITAL_FRAMING_NFCA_T4T; | |
362 | check_crc = digital_skb_check_crc_a; | |
363 | add_crc = digital_skb_add_crc_a; | |
a381d482 MG |
364 | break; |
365 | ||
24734607 MG |
366 | case NFC_PROTO_ISO14443_B: |
367 | framing = NFC_DIGITAL_FRAMING_NFCB_T4T; | |
368 | check_crc = digital_skb_check_crc_b; | |
369 | add_crc = digital_skb_add_crc_b; | |
370 | break; | |
371 | ||
2c66daec | 372 | default: |
26042530 | 373 | pr_err("Invalid protocol %d\n", protocol); |
2c66daec TE |
374 | return -EINVAL; |
375 | } | |
376 | ||
26042530 | 377 | pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol); |
2c66daec TE |
378 | |
379 | ddev->curr_rf_tech = rf_tech; | |
2c66daec TE |
380 | |
381 | if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) { | |
382 | ddev->skb_add_crc = digital_skb_add_crc_none; | |
383 | ddev->skb_check_crc = digital_skb_check_crc_none; | |
384 | } else { | |
385 | ddev->skb_add_crc = add_crc; | |
386 | ddev->skb_check_crc = check_crc; | |
387 | } | |
388 | ||
389 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing); | |
390 | if (rc) | |
391 | return rc; | |
392 | ||
393 | target->supported_protocols = (1 << protocol); | |
2c66daec | 394 | |
0529a7ad | 395 | poll_tech_count = ddev->poll_tech_count; |
2c66daec TE |
396 | ddev->poll_tech_count = 0; |
397 | ||
0529a7ad MG |
398 | rc = nfc_targets_found(ddev->nfc_dev, target, 1); |
399 | if (rc) { | |
400 | ddev->poll_tech_count = poll_tech_count; | |
401 | return rc; | |
402 | } | |
403 | ||
2c66daec TE |
404 | return 0; |
405 | } | |
406 | ||
59ee2361 TE |
407 | void digital_poll_next_tech(struct nfc_digital_dev *ddev) |
408 | { | |
9dc33705 TE |
409 | u8 rand_mod; |
410 | ||
59ee2361 TE |
411 | digital_switch_rf(ddev, 0); |
412 | ||
413 | mutex_lock(&ddev->poll_lock); | |
414 | ||
415 | if (!ddev->poll_tech_count) { | |
416 | mutex_unlock(&ddev->poll_lock); | |
417 | return; | |
418 | } | |
419 | ||
9dc33705 TE |
420 | get_random_bytes(&rand_mod, sizeof(rand_mod)); |
421 | ddev->poll_tech_index = rand_mod % ddev->poll_tech_count; | |
59ee2361 TE |
422 | |
423 | mutex_unlock(&ddev->poll_lock); | |
424 | ||
7854a445 TE |
425 | schedule_delayed_work(&ddev->poll_work, |
426 | msecs_to_jiffies(DIGITAL_POLL_INTERVAL)); | |
59ee2361 TE |
427 | } |
428 | ||
429 | static void digital_wq_poll(struct work_struct *work) | |
430 | { | |
431 | int rc; | |
432 | struct digital_poll_tech *poll_tech; | |
433 | struct nfc_digital_dev *ddev = container_of(work, | |
434 | struct nfc_digital_dev, | |
7854a445 | 435 | poll_work.work); |
59ee2361 TE |
436 | mutex_lock(&ddev->poll_lock); |
437 | ||
438 | if (!ddev->poll_tech_count) { | |
439 | mutex_unlock(&ddev->poll_lock); | |
440 | return; | |
441 | } | |
442 | ||
443 | poll_tech = &ddev->poll_techs[ddev->poll_tech_index]; | |
444 | ||
445 | mutex_unlock(&ddev->poll_lock); | |
446 | ||
447 | rc = poll_tech->poll_func(ddev, poll_tech->rf_tech); | |
448 | if (rc) | |
449 | digital_poll_next_tech(ddev); | |
450 | } | |
451 | ||
452 | static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech, | |
453 | digital_poll_t poll_func) | |
454 | { | |
455 | struct digital_poll_tech *poll_tech; | |
456 | ||
457 | if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX) | |
458 | return; | |
459 | ||
460 | poll_tech = &ddev->poll_techs[ddev->poll_tech_count++]; | |
461 | ||
462 | poll_tech->rf_tech = rf_tech; | |
463 | poll_tech->poll_func = poll_func; | |
464 | } | |
465 | ||
466 | /** | |
467 | * start_poll operation | |
468 | * | |
469 | * For every supported protocol, the corresponding polling function is added | |
470 | * to the table of polling technologies (ddev->poll_techs[]) using | |
471 | * digital_add_poll_tech(). | |
472 | * When a polling function fails (by timeout or protocol error) the next one is | |
473 | * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work). | |
474 | */ | |
4b10884e TE |
475 | static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols, |
476 | __u32 tm_protocols) | |
477 | { | |
59ee2361 TE |
478 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
479 | u32 matching_im_protocols, matching_tm_protocols; | |
480 | ||
26042530 | 481 | pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols, |
c5da0e4a | 482 | tm_protocols, ddev->protocols); |
59ee2361 TE |
483 | |
484 | matching_im_protocols = ddev->protocols & im_protocols; | |
485 | matching_tm_protocols = ddev->protocols & tm_protocols; | |
486 | ||
487 | if (!matching_im_protocols && !matching_tm_protocols) { | |
26042530 | 488 | pr_err("Unknown protocol\n"); |
59ee2361 TE |
489 | return -EINVAL; |
490 | } | |
491 | ||
492 | if (ddev->poll_tech_count) { | |
26042530 | 493 | pr_err("Already polling\n"); |
59ee2361 TE |
494 | return -EBUSY; |
495 | } | |
496 | ||
497 | if (ddev->curr_protocol) { | |
26042530 | 498 | pr_err("A target is already active\n"); |
59ee2361 TE |
499 | return -EBUSY; |
500 | } | |
501 | ||
502 | ddev->poll_tech_count = 0; | |
503 | ddev->poll_tech_index = 0; | |
504 | ||
505 | if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH) | |
506 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A, | |
507 | digital_in_send_sens_req); | |
508 | ||
24734607 MG |
509 | if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH) |
510 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B, | |
511 | digital_in_send_sensb_req); | |
512 | ||
4f913d46 | 513 | if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) { |
8c0695e4 TE |
514 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F, |
515 | digital_in_send_sensf_req); | |
516 | ||
517 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F, | |
518 | digital_in_send_sensf_req); | |
519 | } | |
520 | ||
a381d482 MG |
521 | if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH) |
522 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693, | |
523 | digital_in_send_iso15693_inv_req); | |
524 | ||
4f913d46 | 525 | if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) { |
1c7a4c24 TE |
526 | if (ddev->ops->tg_listen_mdaa) { |
527 | digital_add_poll_tech(ddev, 0, | |
528 | digital_tg_listen_mdaa); | |
bf30a67c MG |
529 | } else if (ddev->ops->tg_listen_md) { |
530 | digital_add_poll_tech(ddev, 0, | |
531 | digital_tg_listen_md); | |
1c7a4c24 TE |
532 | } else { |
533 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A, | |
534 | digital_tg_listen_nfca); | |
535 | ||
536 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F, | |
537 | digital_tg_listen_nfcf); | |
538 | ||
539 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F, | |
540 | digital_tg_listen_nfcf); | |
541 | } | |
542 | } | |
543 | ||
59ee2361 | 544 | if (!ddev->poll_tech_count) { |
26042530 | 545 | pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n", |
59ee2361 TE |
546 | matching_im_protocols, matching_tm_protocols); |
547 | return -EINVAL; | |
548 | } | |
549 | ||
7854a445 | 550 | schedule_delayed_work(&ddev->poll_work, 0); |
59ee2361 TE |
551 | |
552 | return 0; | |
4b10884e TE |
553 | } |
554 | ||
555 | static void digital_stop_poll(struct nfc_dev *nfc_dev) | |
556 | { | |
59ee2361 TE |
557 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
558 | ||
559 | mutex_lock(&ddev->poll_lock); | |
560 | ||
561 | if (!ddev->poll_tech_count) { | |
26042530 | 562 | pr_err("Polling operation was not running\n"); |
59ee2361 TE |
563 | mutex_unlock(&ddev->poll_lock); |
564 | return; | |
565 | } | |
566 | ||
567 | ddev->poll_tech_count = 0; | |
568 | ||
569 | mutex_unlock(&ddev->poll_lock); | |
570 | ||
7854a445 | 571 | cancel_delayed_work_sync(&ddev->poll_work); |
59ee2361 TE |
572 | |
573 | digital_abort_cmd(ddev); | |
4b10884e TE |
574 | } |
575 | ||
576 | static int digital_dev_up(struct nfc_dev *nfc_dev) | |
577 | { | |
59ee2361 TE |
578 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
579 | ||
580 | digital_switch_rf(ddev, 1); | |
581 | ||
582 | return 0; | |
4b10884e TE |
583 | } |
584 | ||
585 | static int digital_dev_down(struct nfc_dev *nfc_dev) | |
586 | { | |
59ee2361 TE |
587 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
588 | ||
589 | digital_switch_rf(ddev, 0); | |
590 | ||
591 | return 0; | |
4b10884e TE |
592 | } |
593 | ||
594 | static int digital_dep_link_up(struct nfc_dev *nfc_dev, | |
595 | struct nfc_target *target, | |
596 | __u8 comm_mode, __u8 *gb, size_t gb_len) | |
597 | { | |
7d0911c0 | 598 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
48e10445 TE |
599 | int rc; |
600 | ||
601 | rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len); | |
7d0911c0 | 602 | |
48e10445 TE |
603 | if (!rc) |
604 | ddev->curr_protocol = NFC_PROTO_NFC_DEP; | |
605 | ||
606 | return rc; | |
4b10884e TE |
607 | } |
608 | ||
609 | static int digital_dep_link_down(struct nfc_dev *nfc_dev) | |
610 | { | |
7d0911c0 TE |
611 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
612 | ||
613 | ddev->curr_protocol = 0; | |
614 | ||
615 | return 0; | |
4b10884e TE |
616 | } |
617 | ||
618 | static int digital_activate_target(struct nfc_dev *nfc_dev, | |
619 | struct nfc_target *target, __u32 protocol) | |
620 | { | |
48e10445 TE |
621 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
622 | ||
623 | if (ddev->poll_tech_count) { | |
624 | pr_err("Can't activate a target while polling\n"); | |
625 | return -EBUSY; | |
626 | } | |
627 | ||
628 | if (ddev->curr_protocol) { | |
629 | pr_err("A target is already active\n"); | |
630 | return -EBUSY; | |
631 | } | |
632 | ||
633 | ddev->curr_protocol = protocol; | |
634 | ||
59ee2361 | 635 | return 0; |
4b10884e TE |
636 | } |
637 | ||
638 | static void digital_deactivate_target(struct nfc_dev *nfc_dev, | |
96d4581f CR |
639 | struct nfc_target *target, |
640 | u8 mode) | |
4b10884e | 641 | { |
59ee2361 TE |
642 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
643 | ||
48e10445 TE |
644 | if (!ddev->curr_protocol) { |
645 | pr_err("No active target\n"); | |
646 | return; | |
647 | } | |
648 | ||
59ee2361 | 649 | ddev->curr_protocol = 0; |
4b10884e TE |
650 | } |
651 | ||
652 | static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb) | |
653 | { | |
1c7a4c24 TE |
654 | struct nfc_digital_dev *ddev = nfc_get_drvdata(dev); |
655 | ||
656 | return digital_tg_send_dep_res(ddev, skb); | |
4b10884e TE |
657 | } |
658 | ||
2c66daec TE |
659 | static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg, |
660 | struct sk_buff *resp) | |
661 | { | |
662 | struct digital_data_exch *data_exch = arg; | |
663 | int rc; | |
664 | ||
665 | if (IS_ERR(resp)) { | |
666 | rc = PTR_ERR(resp); | |
c813007f | 667 | resp = NULL; |
2c66daec TE |
668 | goto done; |
669 | } | |
670 | ||
c813007f | 671 | if (ddev->curr_protocol == NFC_PROTO_MIFARE) { |
2c66daec | 672 | rc = digital_in_recv_mifare_res(resp); |
c813007f TE |
673 | /* crc check is done in digital_in_recv_mifare_res() */ |
674 | goto done; | |
675 | } | |
676 | ||
24734607 MG |
677 | if ((ddev->curr_protocol == NFC_PROTO_ISO14443) || |
678 | (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) { | |
c813007f TE |
679 | rc = digital_in_iso_dep_pull_sod(ddev, resp); |
680 | if (rc) | |
681 | goto done; | |
682 | } | |
683 | ||
684 | rc = ddev->skb_check_crc(resp); | |
2c66daec | 685 | |
c813007f | 686 | done: |
2c66daec TE |
687 | if (rc) { |
688 | kfree_skb(resp); | |
689 | resp = NULL; | |
690 | } | |
691 | ||
2c66daec TE |
692 | data_exch->cb(data_exch->cb_context, resp, rc); |
693 | ||
694 | kfree(data_exch); | |
695 | } | |
696 | ||
4b10884e TE |
697 | static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target, |
698 | struct sk_buff *skb, data_exchange_cb_t cb, | |
699 | void *cb_context) | |
700 | { | |
2c66daec TE |
701 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
702 | struct digital_data_exch *data_exch; | |
c813007f | 703 | int rc; |
2c66daec TE |
704 | |
705 | data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL); | |
706 | if (!data_exch) { | |
26042530 | 707 | pr_err("Failed to allocate data_exch struct\n"); |
2c66daec TE |
708 | return -ENOMEM; |
709 | } | |
710 | ||
711 | data_exch->cb = cb; | |
712 | data_exch->cb_context = cb_context; | |
713 | ||
6ea7398d TE |
714 | if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) { |
715 | rc = digital_in_send_dep_req(ddev, target, skb, data_exch); | |
716 | goto exit; | |
717 | } | |
7d0911c0 | 718 | |
24734607 MG |
719 | if ((ddev->curr_protocol == NFC_PROTO_ISO14443) || |
720 | (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) { | |
c813007f TE |
721 | rc = digital_in_iso_dep_push_sod(ddev, skb); |
722 | if (rc) | |
6ea7398d | 723 | goto exit; |
c813007f TE |
724 | } |
725 | ||
2c66daec TE |
726 | ddev->skb_add_crc(skb); |
727 | ||
6ea7398d TE |
728 | rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete, |
729 | data_exch); | |
730 | ||
731 | exit: | |
732 | if (rc) | |
733 | kfree(data_exch); | |
734 | ||
735 | return rc; | |
4b10884e TE |
736 | } |
737 | ||
738 | static struct nfc_ops digital_nfc_ops = { | |
739 | .dev_up = digital_dev_up, | |
740 | .dev_down = digital_dev_down, | |
741 | .start_poll = digital_start_poll, | |
742 | .stop_poll = digital_stop_poll, | |
743 | .dep_link_up = digital_dep_link_up, | |
744 | .dep_link_down = digital_dep_link_down, | |
745 | .activate_target = digital_activate_target, | |
746 | .deactivate_target = digital_deactivate_target, | |
747 | .tm_send = digital_tg_send, | |
748 | .im_transceive = digital_in_send, | |
749 | }; | |
750 | ||
751 | struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops, | |
752 | __u32 supported_protocols, | |
753 | __u32 driver_capabilities, | |
754 | int tx_headroom, int tx_tailroom) | |
755 | { | |
756 | struct nfc_digital_dev *ddev; | |
757 | ||
758 | if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen || | |
759 | !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd || | |
bf30a67c | 760 | !ops->switch_rf || (ops->tg_listen_md && !ops->tg_get_rf_tech)) |
4b10884e TE |
761 | return NULL; |
762 | ||
763 | ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL); | |
26042530 | 764 | if (!ddev) |
4b10884e | 765 | return NULL; |
4b10884e TE |
766 | |
767 | ddev->driver_capabilities = driver_capabilities; | |
768 | ddev->ops = ops; | |
769 | ||
59ee2361 TE |
770 | mutex_init(&ddev->cmd_lock); |
771 | INIT_LIST_HEAD(&ddev->cmd_queue); | |
772 | ||
773 | INIT_WORK(&ddev->cmd_work, digital_wq_cmd); | |
774 | INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete); | |
775 | ||
776 | mutex_init(&ddev->poll_lock); | |
7854a445 | 777 | INIT_DELAYED_WORK(&ddev->poll_work, digital_wq_poll); |
59ee2361 TE |
778 | |
779 | if (supported_protocols & NFC_PROTO_JEWEL_MASK) | |
780 | ddev->protocols |= NFC_PROTO_JEWEL_MASK; | |
781 | if (supported_protocols & NFC_PROTO_MIFARE_MASK) | |
782 | ddev->protocols |= NFC_PROTO_MIFARE_MASK; | |
8c0695e4 TE |
783 | if (supported_protocols & NFC_PROTO_FELICA_MASK) |
784 | ddev->protocols |= NFC_PROTO_FELICA_MASK; | |
7d0911c0 TE |
785 | if (supported_protocols & NFC_PROTO_NFC_DEP_MASK) |
786 | ddev->protocols |= NFC_PROTO_NFC_DEP_MASK; | |
a381d482 MG |
787 | if (supported_protocols & NFC_PROTO_ISO15693_MASK) |
788 | ddev->protocols |= NFC_PROTO_ISO15693_MASK; | |
12e3d241 TE |
789 | if (supported_protocols & NFC_PROTO_ISO14443_MASK) |
790 | ddev->protocols |= NFC_PROTO_ISO14443_MASK; | |
24734607 MG |
791 | if (supported_protocols & NFC_PROTO_ISO14443_B_MASK) |
792 | ddev->protocols |= NFC_PROTO_ISO14443_B_MASK; | |
59ee2361 TE |
793 | |
794 | ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN; | |
795 | ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN; | |
4b10884e TE |
796 | |
797 | ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols, | |
798 | ddev->tx_headroom, | |
799 | ddev->tx_tailroom); | |
800 | if (!ddev->nfc_dev) { | |
26042530 | 801 | pr_err("nfc_allocate_device failed\n"); |
4b10884e TE |
802 | goto free_dev; |
803 | } | |
804 | ||
805 | nfc_set_drvdata(ddev->nfc_dev, ddev); | |
806 | ||
807 | return ddev; | |
808 | ||
809 | free_dev: | |
810 | kfree(ddev); | |
811 | ||
812 | return NULL; | |
813 | } | |
814 | EXPORT_SYMBOL(nfc_digital_allocate_device); | |
815 | ||
816 | void nfc_digital_free_device(struct nfc_digital_dev *ddev) | |
817 | { | |
818 | nfc_free_device(ddev->nfc_dev); | |
4b10884e TE |
819 | kfree(ddev); |
820 | } | |
821 | EXPORT_SYMBOL(nfc_digital_free_device); | |
822 | ||
823 | int nfc_digital_register_device(struct nfc_digital_dev *ddev) | |
824 | { | |
825 | return nfc_register_device(ddev->nfc_dev); | |
826 | } | |
827 | EXPORT_SYMBOL(nfc_digital_register_device); | |
828 | ||
829 | void nfc_digital_unregister_device(struct nfc_digital_dev *ddev) | |
830 | { | |
59ee2361 TE |
831 | struct digital_cmd *cmd, *n; |
832 | ||
4b10884e | 833 | nfc_unregister_device(ddev->nfc_dev); |
59ee2361 TE |
834 | |
835 | mutex_lock(&ddev->poll_lock); | |
836 | ddev->poll_tech_count = 0; | |
837 | mutex_unlock(&ddev->poll_lock); | |
838 | ||
7854a445 | 839 | cancel_delayed_work_sync(&ddev->poll_work); |
59ee2361 TE |
840 | cancel_work_sync(&ddev->cmd_work); |
841 | cancel_work_sync(&ddev->cmd_complete_work); | |
842 | ||
843 | list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) { | |
844 | list_del(&cmd->queue); | |
82e57952 TE |
845 | |
846 | /* Call the command callback if any and pass it a ENODEV error. | |
847 | * This gives a chance to the command issuer to free any | |
848 | * allocated buffer. | |
849 | */ | |
850 | if (cmd->cmd_cb) | |
851 | cmd->cmd_cb(ddev, cmd->cb_context, ERR_PTR(-ENODEV)); | |
852 | ||
1c7a4c24 | 853 | kfree(cmd->mdaa_params); |
59ee2361 TE |
854 | kfree(cmd); |
855 | } | |
4b10884e TE |
856 | } |
857 | EXPORT_SYMBOL(nfc_digital_unregister_device); | |
858 | ||
859 | MODULE_LICENSE("GPL"); |