GNU Linux-libre 4.4.290-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 | 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         case DIGITAL_CMD_TG_LISTEN_MD:
205                 rc = ddev->ops->tg_listen_md(ddev, cmd->timeout,
206                                                digital_send_cmd_complete, cmd);
207                 break;
208
209         default:
210                 pr_err("Unknown cmd type %d\n", cmd->type);
211                 return;
212         }
213
214         if (!rc)
215                 return;
216
217         pr_err("in_send_command returned err %d\n", rc);
218
219         mutex_lock(&ddev->cmd_lock);
220         list_del(&cmd->queue);
221         mutex_unlock(&ddev->cmd_lock);
222
223         kfree_skb(cmd->req);
224         kfree(cmd->mdaa_params);
225         kfree(cmd);
226
227         schedule_work(&ddev->cmd_work);
228 }
229
230 int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
231                      struct sk_buff *skb, struct digital_tg_mdaa_params *params,
232                      u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
233                      void *cb_context)
234 {
235         struct digital_cmd *cmd;
236
237         cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL);
238         if (!cmd)
239                 return -ENOMEM;
240
241         cmd->type = cmd_type;
242         cmd->timeout = timeout;
243         cmd->req = skb;
244         cmd->mdaa_params = params;
245         cmd->cmd_cb = cmd_cb;
246         cmd->cb_context = cb_context;
247         INIT_LIST_HEAD(&cmd->queue);
248
249         mutex_lock(&ddev->cmd_lock);
250         list_add_tail(&cmd->queue, &ddev->cmd_queue);
251         mutex_unlock(&ddev->cmd_lock);
252
253         schedule_work(&ddev->cmd_work);
254
255         return 0;
256 }
257
258 int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
259 {
260         int rc;
261
262         rc = ddev->ops->in_configure_hw(ddev, type, param);
263         if (rc)
264                 pr_err("in_configure_hw failed: %d\n", rc);
265
266         return rc;
267 }
268
269 int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
270 {
271         int rc;
272
273         rc = ddev->ops->tg_configure_hw(ddev, type, param);
274         if (rc)
275                 pr_err("tg_configure_hw failed: %d\n", rc);
276
277         return rc;
278 }
279
280 static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
281 {
282         struct digital_tg_mdaa_params *params;
283         int rc;
284
285         params = kzalloc(sizeof(struct digital_tg_mdaa_params), GFP_KERNEL);
286         if (!params)
287                 return -ENOMEM;
288
289         params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
290         get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
291         params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
292
293         params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
294         params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
295         get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
296         params->sc = DIGITAL_SENSF_FELICA_SC;
297
298         rc = digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
299                               500, digital_tg_recv_atr_req, NULL);
300         if (rc)
301                 kfree(params);
302
303         return rc;
304 }
305
306 static int digital_tg_listen_md(struct nfc_digital_dev *ddev, u8 rf_tech)
307 {
308         return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MD, NULL, NULL, 500,
309                                 digital_tg_recv_md_req, NULL);
310 }
311
312 int digital_target_found(struct nfc_digital_dev *ddev,
313                          struct nfc_target *target, u8 protocol)
314 {
315         int rc;
316         u8 framing;
317         u8 rf_tech;
318         u8 poll_tech_count;
319         int (*check_crc)(struct sk_buff *skb);
320         void (*add_crc)(struct sk_buff *skb);
321
322         rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
323
324         switch (protocol) {
325         case NFC_PROTO_JEWEL:
326                 framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
327                 check_crc = digital_skb_check_crc_b;
328                 add_crc = digital_skb_add_crc_b;
329                 break;
330
331         case NFC_PROTO_MIFARE:
332                 framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
333                 check_crc = digital_skb_check_crc_a;
334                 add_crc = digital_skb_add_crc_a;
335                 break;
336
337         case NFC_PROTO_FELICA:
338                 framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
339                 check_crc = digital_skb_check_crc_f;
340                 add_crc = digital_skb_add_crc_f;
341                 break;
342
343         case NFC_PROTO_NFC_DEP:
344                 if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
345                         framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
346                         check_crc = digital_skb_check_crc_a;
347                         add_crc = digital_skb_add_crc_a;
348                 } else {
349                         framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
350                         check_crc = digital_skb_check_crc_f;
351                         add_crc = digital_skb_add_crc_f;
352                 }
353                 break;
354
355         case NFC_PROTO_ISO15693:
356                 framing = NFC_DIGITAL_FRAMING_ISO15693_T5T;
357                 check_crc = digital_skb_check_crc_b;
358                 add_crc = digital_skb_add_crc_b;
359                 break;
360
361         case NFC_PROTO_ISO14443:
362                 framing = NFC_DIGITAL_FRAMING_NFCA_T4T;
363                 check_crc = digital_skb_check_crc_a;
364                 add_crc = digital_skb_add_crc_a;
365                 break;
366
367         case NFC_PROTO_ISO14443_B:
368                 framing = NFC_DIGITAL_FRAMING_NFCB_T4T;
369                 check_crc = digital_skb_check_crc_b;
370                 add_crc = digital_skb_add_crc_b;
371                 break;
372
373         default:
374                 pr_err("Invalid protocol %d\n", protocol);
375                 return -EINVAL;
376         }
377
378         pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
379
380         ddev->curr_rf_tech = rf_tech;
381
382         if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
383                 ddev->skb_add_crc = digital_skb_add_crc_none;
384                 ddev->skb_check_crc = digital_skb_check_crc_none;
385         } else {
386                 ddev->skb_add_crc = add_crc;
387                 ddev->skb_check_crc = check_crc;
388         }
389
390         rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
391         if (rc)
392                 return rc;
393
394         target->supported_protocols = (1 << protocol);
395
396         poll_tech_count = ddev->poll_tech_count;
397         ddev->poll_tech_count = 0;
398
399         rc = nfc_targets_found(ddev->nfc_dev, target, 1);
400         if (rc) {
401                 ddev->poll_tech_count = poll_tech_count;
402                 return rc;
403         }
404
405         return 0;
406 }
407
408 void digital_poll_next_tech(struct nfc_digital_dev *ddev)
409 {
410         u8 rand_mod;
411
412         digital_switch_rf(ddev, 0);
413
414         mutex_lock(&ddev->poll_lock);
415
416         if (!ddev->poll_tech_count) {
417                 mutex_unlock(&ddev->poll_lock);
418                 return;
419         }
420
421         get_random_bytes(&rand_mod, sizeof(rand_mod));
422         ddev->poll_tech_index = rand_mod % ddev->poll_tech_count;
423
424         mutex_unlock(&ddev->poll_lock);
425
426         schedule_work(&ddev->poll_work);
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,
435                                                     poll_work);
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  */
475 static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
476                               __u32 tm_protocols)
477 {
478         struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
479         u32 matching_im_protocols, matching_tm_protocols;
480
481         pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
482                  tm_protocols, ddev->protocols);
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) {
488                 pr_err("Unknown protocol\n");
489                 return -EINVAL;
490         }
491
492         if (ddev->poll_tech_count) {
493                 pr_err("Already polling\n");
494                 return -EBUSY;
495         }
496
497         if (ddev->curr_protocol) {
498                 pr_err("A target is already active\n");
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
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
513         if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
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
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
525         if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
526                 if (ddev->ops->tg_listen_mdaa) {
527                         digital_add_poll_tech(ddev, 0,
528                                               digital_tg_listen_mdaa);
529                 } else if (ddev->ops->tg_listen_md) {
530                         digital_add_poll_tech(ddev, 0,
531                                               digital_tg_listen_md);
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
544         if (!ddev->poll_tech_count) {
545                 pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n",
546                        matching_im_protocols, matching_tm_protocols);
547                 return -EINVAL;
548         }
549
550         schedule_work(&ddev->poll_work);
551
552         return 0;
553 }
554
555 static void digital_stop_poll(struct nfc_dev *nfc_dev)
556 {
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) {
562                 pr_err("Polling operation was not running\n");
563                 mutex_unlock(&ddev->poll_lock);
564                 return;
565         }
566
567         ddev->poll_tech_count = 0;
568
569         mutex_unlock(&ddev->poll_lock);
570
571         cancel_work_sync(&ddev->poll_work);
572
573         digital_abort_cmd(ddev);
574 }
575
576 static int digital_dev_up(struct nfc_dev *nfc_dev)
577 {
578         struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
579
580         digital_switch_rf(ddev, 1);
581
582         return 0;
583 }
584
585 static int digital_dev_down(struct nfc_dev *nfc_dev)
586 {
587         struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
588
589         digital_switch_rf(ddev, 0);
590
591         return 0;
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 {
598         struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
599         int rc;
600
601         rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
602
603         if (!rc)
604                 ddev->curr_protocol = NFC_PROTO_NFC_DEP;
605
606         return rc;
607 }
608
609 static int digital_dep_link_down(struct nfc_dev *nfc_dev)
610 {
611         struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
612
613         ddev->curr_protocol = 0;
614
615         return 0;
616 }
617
618 static int digital_activate_target(struct nfc_dev *nfc_dev,
619                                    struct nfc_target *target, __u32 protocol)
620 {
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
635         return 0;
636 }
637
638 static void digital_deactivate_target(struct nfc_dev *nfc_dev,
639                                       struct nfc_target *target,
640                                       u8 mode)
641 {
642         struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
643
644         if (!ddev->curr_protocol) {
645                 pr_err("No active target\n");
646                 return;
647         }
648
649         ddev->curr_protocol = 0;
650 }
651
652 static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
653 {
654         struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
655
656         return digital_tg_send_dep_res(ddev, skb);
657 }
658
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);
667                 resp = NULL;
668                 goto done;
669         }
670
671         if (ddev->curr_protocol == NFC_PROTO_MIFARE) {
672                 rc = digital_in_recv_mifare_res(resp);
673                 /* crc check is done in digital_in_recv_mifare_res() */
674                 goto done;
675         }
676
677         if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
678             (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
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);
685
686 done:
687         if (rc) {
688                 kfree_skb(resp);
689                 resp = NULL;
690         }
691
692         data_exch->cb(data_exch->cb_context, resp, rc);
693
694         kfree(data_exch);
695 }
696
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 {
701         struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
702         struct digital_data_exch *data_exch;
703         int rc;
704
705         data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL);
706         if (!data_exch) {
707                 pr_err("Failed to allocate data_exch struct\n");
708                 return -ENOMEM;
709         }
710
711         data_exch->cb = cb;
712         data_exch->cb_context = cb_context;
713
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         }
718
719         if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
720             (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
721                 rc = digital_in_iso_dep_push_sod(ddev, skb);
722                 if (rc)
723                         goto exit;
724         }
725
726         ddev->skb_add_crc(skb);
727
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;
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 ||
760             !ops->switch_rf || (ops->tg_listen_md && !ops->tg_get_rf_tech))
761                 return NULL;
762
763         ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL);
764         if (!ddev)
765                 return NULL;
766
767         ddev->driver_capabilities = driver_capabilities;
768         ddev->ops = ops;
769
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);
777         INIT_WORK(&ddev->poll_work, digital_wq_poll);
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;
783         if (supported_protocols & NFC_PROTO_FELICA_MASK)
784                 ddev->protocols |= NFC_PROTO_FELICA_MASK;
785         if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
786                 ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
787         if (supported_protocols & NFC_PROTO_ISO15693_MASK)
788                 ddev->protocols |= NFC_PROTO_ISO15693_MASK;
789         if (supported_protocols & NFC_PROTO_ISO14443_MASK)
790                 ddev->protocols |= NFC_PROTO_ISO14443_MASK;
791         if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
792                 ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
793
794         ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
795         ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
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) {
801                 pr_err("nfc_allocate_device failed\n");
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);
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 {
831         struct digital_cmd *cmd, *n;
832
833         nfc_unregister_device(ddev->nfc_dev);
834
835         mutex_lock(&ddev->poll_lock);
836         ddev->poll_tech_count = 0;
837         mutex_unlock(&ddev->poll_lock);
838
839         cancel_work_sync(&ddev->poll_work);
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);
845                 kfree(cmd->mdaa_params);
846                 kfree(cmd);
847         }
848 }
849 EXPORT_SYMBOL(nfc_digital_unregister_device);
850
851 MODULE_LICENSE("GPL");