Linux 5.7.7
[linux/fpc-iii.git] / net / nfc / digital_core.c
blobe3599ed4a7a8765513a3f4c40c7da987cfae0398
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * NFC Digital Protocol stack
4 * Copyright (c) 2013, Intel Corporation.
5 */
7 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
9 #include <linux/module.h>
11 #include "digital.h"
13 #define DIGITAL_PROTO_NFCA_RF_TECH \
14 (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | \
15 NFC_PROTO_NFC_DEP_MASK | NFC_PROTO_ISO14443_MASK)
17 #define DIGITAL_PROTO_NFCB_RF_TECH NFC_PROTO_ISO14443_B_MASK
19 #define DIGITAL_PROTO_NFCF_RF_TECH \
20 (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
22 #define DIGITAL_PROTO_ISO15693_RF_TECH NFC_PROTO_ISO15693_MASK
24 /* Delay between each poll frame (ms) */
25 #define DIGITAL_POLL_INTERVAL 10
27 struct digital_cmd {
28 struct list_head queue;
30 u8 type;
31 u8 pending;
33 u16 timeout;
34 struct sk_buff *req;
35 struct sk_buff *resp;
36 struct digital_tg_mdaa_params *mdaa_params;
38 nfc_digital_cmd_complete_t cmd_cb;
39 void *cb_context;
42 struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
43 unsigned int len)
45 struct sk_buff *skb;
47 skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
48 GFP_KERNEL);
49 if (skb)
50 skb_reserve(skb, ddev->tx_headroom);
52 return skb;
55 void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
56 u8 bitwise_inv, u8 msb_first)
58 u16 crc;
60 crc = crc_func(init, skb->data, skb->len);
62 if (bitwise_inv)
63 crc = ~crc;
65 if (msb_first)
66 crc = __fswab16(crc);
68 skb_put_u8(skb, crc & 0xFF);
69 skb_put_u8(skb, (crc >> 8) & 0xFF);
72 int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
73 u16 crc_init, u8 bitwise_inv, u8 msb_first)
75 int rc;
76 u16 crc;
78 if (skb->len <= 2)
79 return -EIO;
81 crc = crc_func(crc_init, skb->data, skb->len - 2);
83 if (bitwise_inv)
84 crc = ~crc;
86 if (msb_first)
87 crc = __swab16(crc);
89 rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
90 (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
92 if (rc)
93 return -EIO;
95 skb_trim(skb, skb->len - 2);
97 return 0;
100 static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
102 ddev->ops->switch_rf(ddev, on);
105 static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
107 ddev->ops->abort_cmd(ddev);
110 static void digital_wq_cmd_complete(struct work_struct *work)
112 struct digital_cmd *cmd;
113 struct nfc_digital_dev *ddev = container_of(work,
114 struct nfc_digital_dev,
115 cmd_complete_work);
117 mutex_lock(&ddev->cmd_lock);
119 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
120 queue);
121 if (!cmd) {
122 mutex_unlock(&ddev->cmd_lock);
123 return;
126 list_del(&cmd->queue);
128 mutex_unlock(&ddev->cmd_lock);
130 if (!IS_ERR(cmd->resp))
131 print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
132 cmd->resp->data, cmd->resp->len, false);
134 cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
136 kfree(cmd->mdaa_params);
137 kfree(cmd);
139 schedule_work(&ddev->cmd_work);
142 static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
143 void *arg, struct sk_buff *resp)
145 struct digital_cmd *cmd = arg;
147 cmd->resp = resp;
149 schedule_work(&ddev->cmd_complete_work);
152 static void digital_wq_cmd(struct work_struct *work)
154 int rc;
155 struct digital_cmd *cmd;
156 struct digital_tg_mdaa_params *params;
157 struct nfc_digital_dev *ddev = container_of(work,
158 struct nfc_digital_dev,
159 cmd_work);
161 mutex_lock(&ddev->cmd_lock);
163 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
164 queue);
165 if (!cmd || cmd->pending) {
166 mutex_unlock(&ddev->cmd_lock);
167 return;
170 cmd->pending = 1;
172 mutex_unlock(&ddev->cmd_lock);
174 if (cmd->req)
175 print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
176 cmd->req->data, cmd->req->len, false);
178 switch (cmd->type) {
179 case DIGITAL_CMD_IN_SEND:
180 rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
181 digital_send_cmd_complete, cmd);
182 break;
184 case DIGITAL_CMD_TG_SEND:
185 rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout,
186 digital_send_cmd_complete, cmd);
187 break;
189 case DIGITAL_CMD_TG_LISTEN:
190 rc = ddev->ops->tg_listen(ddev, cmd->timeout,
191 digital_send_cmd_complete, cmd);
192 break;
194 case DIGITAL_CMD_TG_LISTEN_MDAA:
195 params = cmd->mdaa_params;
197 rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout,
198 digital_send_cmd_complete, cmd);
199 break;
201 case DIGITAL_CMD_TG_LISTEN_MD:
202 rc = ddev->ops->tg_listen_md(ddev, cmd->timeout,
203 digital_send_cmd_complete, cmd);
204 break;
206 default:
207 pr_err("Unknown cmd type %d\n", cmd->type);
208 return;
211 if (!rc)
212 return;
214 pr_err("in_send_command returned err %d\n", rc);
216 mutex_lock(&ddev->cmd_lock);
217 list_del(&cmd->queue);
218 mutex_unlock(&ddev->cmd_lock);
220 kfree_skb(cmd->req);
221 kfree(cmd->mdaa_params);
222 kfree(cmd);
224 schedule_work(&ddev->cmd_work);
227 int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
228 struct sk_buff *skb, struct digital_tg_mdaa_params *params,
229 u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
230 void *cb_context)
232 struct digital_cmd *cmd;
234 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
235 if (!cmd)
236 return -ENOMEM;
238 cmd->type = cmd_type;
239 cmd->timeout = timeout;
240 cmd->req = skb;
241 cmd->mdaa_params = params;
242 cmd->cmd_cb = cmd_cb;
243 cmd->cb_context = cb_context;
244 INIT_LIST_HEAD(&cmd->queue);
246 mutex_lock(&ddev->cmd_lock);
247 list_add_tail(&cmd->queue, &ddev->cmd_queue);
248 mutex_unlock(&ddev->cmd_lock);
250 schedule_work(&ddev->cmd_work);
252 return 0;
255 int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
257 int rc;
259 rc = ddev->ops->in_configure_hw(ddev, type, param);
260 if (rc)
261 pr_err("in_configure_hw failed: %d\n", rc);
263 return rc;
266 int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
268 int rc;
270 rc = ddev->ops->tg_configure_hw(ddev, type, param);
271 if (rc)
272 pr_err("tg_configure_hw failed: %d\n", rc);
274 return rc;
277 static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
279 struct digital_tg_mdaa_params *params;
281 params = kzalloc(sizeof(*params), GFP_KERNEL);
282 if (!params)
283 return -ENOMEM;
285 params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
286 get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
287 params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
289 params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
290 params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
291 get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
292 params->sc = DIGITAL_SENSF_FELICA_SC;
294 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
295 500, digital_tg_recv_atr_req, NULL);
298 static int digital_tg_listen_md(struct nfc_digital_dev *ddev, u8 rf_tech)
300 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MD, NULL, NULL, 500,
301 digital_tg_recv_md_req, NULL);
304 int digital_target_found(struct nfc_digital_dev *ddev,
305 struct nfc_target *target, u8 protocol)
307 int rc;
308 u8 framing;
309 u8 rf_tech;
310 u8 poll_tech_count;
311 int (*check_crc)(struct sk_buff *skb);
312 void (*add_crc)(struct sk_buff *skb);
314 rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
316 switch (protocol) {
317 case NFC_PROTO_JEWEL:
318 framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
319 check_crc = digital_skb_check_crc_b;
320 add_crc = digital_skb_add_crc_b;
321 break;
323 case NFC_PROTO_MIFARE:
324 framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
325 check_crc = digital_skb_check_crc_a;
326 add_crc = digital_skb_add_crc_a;
327 break;
329 case NFC_PROTO_FELICA:
330 framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
331 check_crc = digital_skb_check_crc_f;
332 add_crc = digital_skb_add_crc_f;
333 break;
335 case NFC_PROTO_NFC_DEP:
336 if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
337 framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
338 check_crc = digital_skb_check_crc_a;
339 add_crc = digital_skb_add_crc_a;
340 } else {
341 framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
342 check_crc = digital_skb_check_crc_f;
343 add_crc = digital_skb_add_crc_f;
345 break;
347 case NFC_PROTO_ISO15693:
348 framing = NFC_DIGITAL_FRAMING_ISO15693_T5T;
349 check_crc = digital_skb_check_crc_b;
350 add_crc = digital_skb_add_crc_b;
351 break;
353 case NFC_PROTO_ISO14443:
354 framing = NFC_DIGITAL_FRAMING_NFCA_T4T;
355 check_crc = digital_skb_check_crc_a;
356 add_crc = digital_skb_add_crc_a;
357 break;
359 case NFC_PROTO_ISO14443_B:
360 framing = NFC_DIGITAL_FRAMING_NFCB_T4T;
361 check_crc = digital_skb_check_crc_b;
362 add_crc = digital_skb_add_crc_b;
363 break;
365 default:
366 pr_err("Invalid protocol %d\n", protocol);
367 return -EINVAL;
370 pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
372 ddev->curr_rf_tech = rf_tech;
374 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
375 ddev->skb_add_crc = digital_skb_add_crc_none;
376 ddev->skb_check_crc = digital_skb_check_crc_none;
377 } else {
378 ddev->skb_add_crc = add_crc;
379 ddev->skb_check_crc = check_crc;
382 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
383 if (rc)
384 return rc;
386 target->supported_protocols = (1 << protocol);
388 poll_tech_count = ddev->poll_tech_count;
389 ddev->poll_tech_count = 0;
391 rc = nfc_targets_found(ddev->nfc_dev, target, 1);
392 if (rc) {
393 ddev->poll_tech_count = poll_tech_count;
394 return rc;
397 return 0;
400 void digital_poll_next_tech(struct nfc_digital_dev *ddev)
402 u8 rand_mod;
404 digital_switch_rf(ddev, 0);
406 mutex_lock(&ddev->poll_lock);
408 if (!ddev->poll_tech_count) {
409 mutex_unlock(&ddev->poll_lock);
410 return;
413 get_random_bytes(&rand_mod, sizeof(rand_mod));
414 ddev->poll_tech_index = rand_mod % ddev->poll_tech_count;
416 mutex_unlock(&ddev->poll_lock);
418 schedule_delayed_work(&ddev->poll_work,
419 msecs_to_jiffies(DIGITAL_POLL_INTERVAL));
422 static void digital_wq_poll(struct work_struct *work)
424 int rc;
425 struct digital_poll_tech *poll_tech;
426 struct nfc_digital_dev *ddev = container_of(work,
427 struct nfc_digital_dev,
428 poll_work.work);
429 mutex_lock(&ddev->poll_lock);
431 if (!ddev->poll_tech_count) {
432 mutex_unlock(&ddev->poll_lock);
433 return;
436 poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
438 mutex_unlock(&ddev->poll_lock);
440 rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
441 if (rc)
442 digital_poll_next_tech(ddev);
445 static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
446 digital_poll_t poll_func)
448 struct digital_poll_tech *poll_tech;
450 if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
451 return;
453 poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
455 poll_tech->rf_tech = rf_tech;
456 poll_tech->poll_func = poll_func;
460 * start_poll operation
462 * For every supported protocol, the corresponding polling function is added
463 * to the table of polling technologies (ddev->poll_techs[]) using
464 * digital_add_poll_tech().
465 * When a polling function fails (by timeout or protocol error) the next one is
466 * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
468 static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
469 __u32 tm_protocols)
471 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
472 u32 matching_im_protocols, matching_tm_protocols;
474 pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
475 tm_protocols, ddev->protocols);
477 matching_im_protocols = ddev->protocols & im_protocols;
478 matching_tm_protocols = ddev->protocols & tm_protocols;
480 if (!matching_im_protocols && !matching_tm_protocols) {
481 pr_err("Unknown protocol\n");
482 return -EINVAL;
485 if (ddev->poll_tech_count) {
486 pr_err("Already polling\n");
487 return -EBUSY;
490 if (ddev->curr_protocol) {
491 pr_err("A target is already active\n");
492 return -EBUSY;
495 ddev->poll_tech_count = 0;
496 ddev->poll_tech_index = 0;
498 if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
499 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
500 digital_in_send_sens_req);
502 if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH)
503 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B,
504 digital_in_send_sensb_req);
506 if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
507 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
508 digital_in_send_sensf_req);
510 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
511 digital_in_send_sensf_req);
514 if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH)
515 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693,
516 digital_in_send_iso15693_inv_req);
518 if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
519 if (ddev->ops->tg_listen_mdaa) {
520 digital_add_poll_tech(ddev, 0,
521 digital_tg_listen_mdaa);
522 } else if (ddev->ops->tg_listen_md) {
523 digital_add_poll_tech(ddev, 0,
524 digital_tg_listen_md);
525 } else {
526 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
527 digital_tg_listen_nfca);
529 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
530 digital_tg_listen_nfcf);
532 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
533 digital_tg_listen_nfcf);
537 if (!ddev->poll_tech_count) {
538 pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n",
539 matching_im_protocols, matching_tm_protocols);
540 return -EINVAL;
543 schedule_delayed_work(&ddev->poll_work, 0);
545 return 0;
548 static void digital_stop_poll(struct nfc_dev *nfc_dev)
550 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
552 mutex_lock(&ddev->poll_lock);
554 if (!ddev->poll_tech_count) {
555 pr_err("Polling operation was not running\n");
556 mutex_unlock(&ddev->poll_lock);
557 return;
560 ddev->poll_tech_count = 0;
562 mutex_unlock(&ddev->poll_lock);
564 cancel_delayed_work_sync(&ddev->poll_work);
566 digital_abort_cmd(ddev);
569 static int digital_dev_up(struct nfc_dev *nfc_dev)
571 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
573 digital_switch_rf(ddev, 1);
575 return 0;
578 static int digital_dev_down(struct nfc_dev *nfc_dev)
580 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
582 digital_switch_rf(ddev, 0);
584 return 0;
587 static int digital_dep_link_up(struct nfc_dev *nfc_dev,
588 struct nfc_target *target,
589 __u8 comm_mode, __u8 *gb, size_t gb_len)
591 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
592 int rc;
594 rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
596 if (!rc)
597 ddev->curr_protocol = NFC_PROTO_NFC_DEP;
599 return rc;
602 static int digital_dep_link_down(struct nfc_dev *nfc_dev)
604 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
606 digital_abort_cmd(ddev);
608 ddev->curr_protocol = 0;
610 return 0;
613 static int digital_activate_target(struct nfc_dev *nfc_dev,
614 struct nfc_target *target, __u32 protocol)
616 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
618 if (ddev->poll_tech_count) {
619 pr_err("Can't activate a target while polling\n");
620 return -EBUSY;
623 if (ddev->curr_protocol) {
624 pr_err("A target is already active\n");
625 return -EBUSY;
628 ddev->curr_protocol = protocol;
630 return 0;
633 static void digital_deactivate_target(struct nfc_dev *nfc_dev,
634 struct nfc_target *target,
635 u8 mode)
637 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
639 if (!ddev->curr_protocol) {
640 pr_err("No active target\n");
641 return;
644 digital_abort_cmd(ddev);
645 ddev->curr_protocol = 0;
648 static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
650 struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
652 return digital_tg_send_dep_res(ddev, skb);
655 static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
656 struct sk_buff *resp)
658 struct digital_data_exch *data_exch = arg;
659 int rc;
661 if (IS_ERR(resp)) {
662 rc = PTR_ERR(resp);
663 resp = NULL;
664 goto done;
667 if (ddev->curr_protocol == NFC_PROTO_MIFARE) {
668 rc = digital_in_recv_mifare_res(resp);
669 /* crc check is done in digital_in_recv_mifare_res() */
670 goto done;
673 if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
674 (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
675 rc = digital_in_iso_dep_pull_sod(ddev, resp);
676 if (rc)
677 goto done;
680 rc = ddev->skb_check_crc(resp);
682 done:
683 if (rc) {
684 kfree_skb(resp);
685 resp = NULL;
688 data_exch->cb(data_exch->cb_context, resp, rc);
690 kfree(data_exch);
693 static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
694 struct sk_buff *skb, data_exchange_cb_t cb,
695 void *cb_context)
697 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
698 struct digital_data_exch *data_exch;
699 int rc;
701 data_exch = kzalloc(sizeof(*data_exch), GFP_KERNEL);
702 if (!data_exch)
703 return -ENOMEM;
705 data_exch->cb = cb;
706 data_exch->cb_context = cb_context;
708 if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) {
709 rc = digital_in_send_dep_req(ddev, target, skb, data_exch);
710 goto exit;
713 if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
714 (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
715 rc = digital_in_iso_dep_push_sod(ddev, skb);
716 if (rc)
717 goto exit;
720 ddev->skb_add_crc(skb);
722 rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
723 data_exch);
725 exit:
726 if (rc)
727 kfree(data_exch);
729 return rc;
732 static struct nfc_ops digital_nfc_ops = {
733 .dev_up = digital_dev_up,
734 .dev_down = digital_dev_down,
735 .start_poll = digital_start_poll,
736 .stop_poll = digital_stop_poll,
737 .dep_link_up = digital_dep_link_up,
738 .dep_link_down = digital_dep_link_down,
739 .activate_target = digital_activate_target,
740 .deactivate_target = digital_deactivate_target,
741 .tm_send = digital_tg_send,
742 .im_transceive = digital_in_send,
745 struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
746 __u32 supported_protocols,
747 __u32 driver_capabilities,
748 int tx_headroom, int tx_tailroom)
750 struct nfc_digital_dev *ddev;
752 if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
753 !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
754 !ops->switch_rf || (ops->tg_listen_md && !ops->tg_get_rf_tech))
755 return NULL;
757 ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
758 if (!ddev)
759 return NULL;
761 ddev->driver_capabilities = driver_capabilities;
762 ddev->ops = ops;
764 mutex_init(&ddev->cmd_lock);
765 INIT_LIST_HEAD(&ddev->cmd_queue);
767 INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
768 INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
770 mutex_init(&ddev->poll_lock);
771 INIT_DELAYED_WORK(&ddev->poll_work, digital_wq_poll);
773 if (supported_protocols & NFC_PROTO_JEWEL_MASK)
774 ddev->protocols |= NFC_PROTO_JEWEL_MASK;
775 if (supported_protocols & NFC_PROTO_MIFARE_MASK)
776 ddev->protocols |= NFC_PROTO_MIFARE_MASK;
777 if (supported_protocols & NFC_PROTO_FELICA_MASK)
778 ddev->protocols |= NFC_PROTO_FELICA_MASK;
779 if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
780 ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
781 if (supported_protocols & NFC_PROTO_ISO15693_MASK)
782 ddev->protocols |= NFC_PROTO_ISO15693_MASK;
783 if (supported_protocols & NFC_PROTO_ISO14443_MASK)
784 ddev->protocols |= NFC_PROTO_ISO14443_MASK;
785 if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
786 ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
788 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
789 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
791 ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
792 ddev->tx_headroom,
793 ddev->tx_tailroom);
794 if (!ddev->nfc_dev) {
795 pr_err("nfc_allocate_device failed\n");
796 goto free_dev;
799 nfc_set_drvdata(ddev->nfc_dev, ddev);
801 return ddev;
803 free_dev:
804 kfree(ddev);
806 return NULL;
808 EXPORT_SYMBOL(nfc_digital_allocate_device);
810 void nfc_digital_free_device(struct nfc_digital_dev *ddev)
812 nfc_free_device(ddev->nfc_dev);
813 kfree(ddev);
815 EXPORT_SYMBOL(nfc_digital_free_device);
817 int nfc_digital_register_device(struct nfc_digital_dev *ddev)
819 return nfc_register_device(ddev->nfc_dev);
821 EXPORT_SYMBOL(nfc_digital_register_device);
823 void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
825 struct digital_cmd *cmd, *n;
827 nfc_unregister_device(ddev->nfc_dev);
829 mutex_lock(&ddev->poll_lock);
830 ddev->poll_tech_count = 0;
831 mutex_unlock(&ddev->poll_lock);
833 cancel_delayed_work_sync(&ddev->poll_work);
834 cancel_work_sync(&ddev->cmd_work);
835 cancel_work_sync(&ddev->cmd_complete_work);
837 list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
838 list_del(&cmd->queue);
840 /* Call the command callback if any and pass it a ENODEV error.
841 * This gives a chance to the command issuer to free any
842 * allocated buffer.
844 if (cmd->cmd_cb)
845 cmd->cmd_cb(ddev, cmd->cb_context, ERR_PTR(-ENODEV));
847 kfree(cmd->mdaa_params);
848 kfree(cmd);
851 EXPORT_SYMBOL(nfc_digital_unregister_device);
853 MODULE_LICENSE("GPL");