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