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