Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * NFC Digital Protocol stack
0004  * Copyright (c) 2013, Intel Corporation.
0005  */
0006 
0007 #define pr_fmt(fmt) "digital: %s: " fmt, __func__
0008 
0009 #include <linux/module.h>
0010 
0011 #include "digital.h"
0012 
0013 #define DIGITAL_PROTO_NFCA_RF_TECH \
0014     (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | \
0015     NFC_PROTO_NFC_DEP_MASK | NFC_PROTO_ISO14443_MASK)
0016 
0017 #define DIGITAL_PROTO_NFCB_RF_TECH  NFC_PROTO_ISO14443_B_MASK
0018 
0019 #define DIGITAL_PROTO_NFCF_RF_TECH \
0020     (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
0021 
0022 #define DIGITAL_PROTO_ISO15693_RF_TECH  NFC_PROTO_ISO15693_MASK
0023 
0024 /* Delay between each poll frame (ms) */
0025 #define DIGITAL_POLL_INTERVAL 10
0026 
0027 struct digital_cmd {
0028     struct list_head queue;
0029 
0030     u8 type;
0031     u8 pending;
0032 
0033     u16 timeout;
0034     struct sk_buff *req;
0035     struct sk_buff *resp;
0036     struct digital_tg_mdaa_params *mdaa_params;
0037 
0038     nfc_digital_cmd_complete_t cmd_cb;
0039     void *cb_context;
0040 };
0041 
0042 struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
0043                   unsigned int len)
0044 {
0045     struct sk_buff *skb;
0046 
0047     skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
0048             GFP_KERNEL);
0049     if (skb)
0050         skb_reserve(skb, ddev->tx_headroom);
0051 
0052     return skb;
0053 }
0054 
0055 void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
0056              u8 bitwise_inv, u8 msb_first)
0057 {
0058     u16 crc;
0059 
0060     crc = crc_func(init, skb->data, skb->len);
0061 
0062     if (bitwise_inv)
0063         crc = ~crc;
0064 
0065     if (msb_first)
0066         crc = __fswab16(crc);
0067 
0068     skb_put_u8(skb, crc & 0xFF);
0069     skb_put_u8(skb, (crc >> 8) & 0xFF);
0070 }
0071 
0072 int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
0073               u16 crc_init, u8 bitwise_inv, u8 msb_first)
0074 {
0075     int rc;
0076     u16 crc;
0077 
0078     if (skb->len <= 2)
0079         return -EIO;
0080 
0081     crc = crc_func(crc_init, skb->data, skb->len - 2);
0082 
0083     if (bitwise_inv)
0084         crc = ~crc;
0085 
0086     if (msb_first)
0087         crc = __swab16(crc);
0088 
0089     rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
0090          (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
0091 
0092     if (rc)
0093         return -EIO;
0094 
0095     skb_trim(skb, skb->len - 2);
0096 
0097     return 0;
0098 }
0099 
0100 static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
0101 {
0102     ddev->ops->switch_rf(ddev, on);
0103 }
0104 
0105 static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
0106 {
0107     ddev->ops->abort_cmd(ddev);
0108 }
0109 
0110 static void digital_wq_cmd_complete(struct work_struct *work)
0111 {
0112     struct digital_cmd *cmd;
0113     struct nfc_digital_dev *ddev = container_of(work,
0114                             struct nfc_digital_dev,
0115                             cmd_complete_work);
0116 
0117     mutex_lock(&ddev->cmd_lock);
0118 
0119     cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
0120                        queue);
0121     if (!cmd) {
0122         mutex_unlock(&ddev->cmd_lock);
0123         return;
0124     }
0125 
0126     list_del(&cmd->queue);
0127 
0128     mutex_unlock(&ddev->cmd_lock);
0129 
0130     if (!IS_ERR(cmd->resp))
0131         print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
0132                      cmd->resp->data, cmd->resp->len, false);
0133 
0134     cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
0135 
0136     kfree(cmd->mdaa_params);
0137     kfree(cmd);
0138 
0139     schedule_work(&ddev->cmd_work);
0140 }
0141 
0142 static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
0143                       void *arg, struct sk_buff *resp)
0144 {
0145     struct digital_cmd *cmd = arg;
0146 
0147     cmd->resp = resp;
0148 
0149     schedule_work(&ddev->cmd_complete_work);
0150 }
0151 
0152 static void digital_wq_cmd(struct work_struct *work)
0153 {
0154     int rc;
0155     struct digital_cmd *cmd;
0156     struct digital_tg_mdaa_params *params;
0157     struct nfc_digital_dev *ddev = container_of(work,
0158                             struct nfc_digital_dev,
0159                             cmd_work);
0160 
0161     mutex_lock(&ddev->cmd_lock);
0162 
0163     cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
0164                        queue);
0165     if (!cmd || cmd->pending) {
0166         mutex_unlock(&ddev->cmd_lock);
0167         return;
0168     }
0169 
0170     cmd->pending = 1;
0171 
0172     mutex_unlock(&ddev->cmd_lock);
0173 
0174     if (cmd->req)
0175         print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
0176                      cmd->req->data, cmd->req->len, false);
0177 
0178     switch (cmd->type) {
0179     case DIGITAL_CMD_IN_SEND:
0180         rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
0181                         digital_send_cmd_complete, cmd);
0182         break;
0183 
0184     case DIGITAL_CMD_TG_SEND:
0185         rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout,
0186                         digital_send_cmd_complete, cmd);
0187         break;
0188 
0189     case DIGITAL_CMD_TG_LISTEN:
0190         rc = ddev->ops->tg_listen(ddev, cmd->timeout,
0191                       digital_send_cmd_complete, cmd);
0192         break;
0193 
0194     case DIGITAL_CMD_TG_LISTEN_MDAA:
0195         params = cmd->mdaa_params;
0196 
0197         rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout,
0198                            digital_send_cmd_complete, cmd);
0199         break;
0200 
0201     case DIGITAL_CMD_TG_LISTEN_MD:
0202         rc = ddev->ops->tg_listen_md(ddev, cmd->timeout,
0203                            digital_send_cmd_complete, cmd);
0204         break;
0205 
0206     default:
0207         pr_err("Unknown cmd type %d\n", cmd->type);
0208         return;
0209     }
0210 
0211     if (!rc)
0212         return;
0213 
0214     pr_err("in_send_command returned err %d\n", rc);
0215 
0216     mutex_lock(&ddev->cmd_lock);
0217     list_del(&cmd->queue);
0218     mutex_unlock(&ddev->cmd_lock);
0219 
0220     kfree_skb(cmd->req);
0221     kfree(cmd->mdaa_params);
0222     kfree(cmd);
0223 
0224     schedule_work(&ddev->cmd_work);
0225 }
0226 
0227 int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
0228              struct sk_buff *skb, struct digital_tg_mdaa_params *params,
0229              u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
0230              void *cb_context)
0231 {
0232     struct digital_cmd *cmd;
0233 
0234     cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
0235     if (!cmd)
0236         return -ENOMEM;
0237 
0238     cmd->type = cmd_type;
0239     cmd->timeout = timeout;
0240     cmd->req = skb;
0241     cmd->mdaa_params = params;
0242     cmd->cmd_cb = cmd_cb;
0243     cmd->cb_context = cb_context;
0244     INIT_LIST_HEAD(&cmd->queue);
0245 
0246     mutex_lock(&ddev->cmd_lock);
0247     list_add_tail(&cmd->queue, &ddev->cmd_queue);
0248     mutex_unlock(&ddev->cmd_lock);
0249 
0250     schedule_work(&ddev->cmd_work);
0251 
0252     return 0;
0253 }
0254 
0255 int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
0256 {
0257     int rc;
0258 
0259     rc = ddev->ops->in_configure_hw(ddev, type, param);
0260     if (rc)
0261         pr_err("in_configure_hw failed: %d\n", rc);
0262 
0263     return rc;
0264 }
0265 
0266 int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
0267 {
0268     int rc;
0269 
0270     rc = ddev->ops->tg_configure_hw(ddev, type, param);
0271     if (rc)
0272         pr_err("tg_configure_hw failed: %d\n", rc);
0273 
0274     return rc;
0275 }
0276 
0277 static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
0278 {
0279     struct digital_tg_mdaa_params *params;
0280     int rc;
0281 
0282     params = kzalloc(sizeof(*params), GFP_KERNEL);
0283     if (!params)
0284         return -ENOMEM;
0285 
0286     params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
0287     get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
0288     params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
0289 
0290     params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
0291     params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
0292     get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
0293     params->sc = DIGITAL_SENSF_FELICA_SC;
0294 
0295     rc = digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
0296                   500, digital_tg_recv_atr_req, NULL);
0297     if (rc)
0298         kfree(params);
0299 
0300     return rc;
0301 }
0302 
0303 static int digital_tg_listen_md(struct nfc_digital_dev *ddev, u8 rf_tech)
0304 {
0305     return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MD, NULL, NULL, 500,
0306                 digital_tg_recv_md_req, NULL);
0307 }
0308 
0309 int digital_target_found(struct nfc_digital_dev *ddev,
0310              struct nfc_target *target, u8 protocol)
0311 {
0312     int rc;
0313     u8 framing;
0314     u8 rf_tech;
0315     u8 poll_tech_count;
0316     int (*check_crc)(struct sk_buff *skb);
0317     void (*add_crc)(struct sk_buff *skb);
0318 
0319     rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
0320 
0321     switch (protocol) {
0322     case NFC_PROTO_JEWEL:
0323         framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
0324         check_crc = digital_skb_check_crc_b;
0325         add_crc = digital_skb_add_crc_b;
0326         break;
0327 
0328     case NFC_PROTO_MIFARE:
0329         framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
0330         check_crc = digital_skb_check_crc_a;
0331         add_crc = digital_skb_add_crc_a;
0332         break;
0333 
0334     case NFC_PROTO_FELICA:
0335         framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
0336         check_crc = digital_skb_check_crc_f;
0337         add_crc = digital_skb_add_crc_f;
0338         break;
0339 
0340     case NFC_PROTO_NFC_DEP:
0341         if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
0342             framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
0343             check_crc = digital_skb_check_crc_a;
0344             add_crc = digital_skb_add_crc_a;
0345         } else {
0346             framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
0347             check_crc = digital_skb_check_crc_f;
0348             add_crc = digital_skb_add_crc_f;
0349         }
0350         break;
0351 
0352     case NFC_PROTO_ISO15693:
0353         framing = NFC_DIGITAL_FRAMING_ISO15693_T5T;
0354         check_crc = digital_skb_check_crc_b;
0355         add_crc = digital_skb_add_crc_b;
0356         break;
0357 
0358     case NFC_PROTO_ISO14443:
0359         framing = NFC_DIGITAL_FRAMING_NFCA_T4T;
0360         check_crc = digital_skb_check_crc_a;
0361         add_crc = digital_skb_add_crc_a;
0362         break;
0363 
0364     case NFC_PROTO_ISO14443_B:
0365         framing = NFC_DIGITAL_FRAMING_NFCB_T4T;
0366         check_crc = digital_skb_check_crc_b;
0367         add_crc = digital_skb_add_crc_b;
0368         break;
0369 
0370     default:
0371         pr_err("Invalid protocol %d\n", protocol);
0372         return -EINVAL;
0373     }
0374 
0375     pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
0376 
0377     ddev->curr_rf_tech = rf_tech;
0378 
0379     if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
0380         ddev->skb_add_crc = digital_skb_add_crc_none;
0381         ddev->skb_check_crc = digital_skb_check_crc_none;
0382     } else {
0383         ddev->skb_add_crc = add_crc;
0384         ddev->skb_check_crc = check_crc;
0385     }
0386 
0387     rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
0388     if (rc)
0389         return rc;
0390 
0391     target->supported_protocols = (1 << protocol);
0392 
0393     poll_tech_count = ddev->poll_tech_count;
0394     ddev->poll_tech_count = 0;
0395 
0396     rc = nfc_targets_found(ddev->nfc_dev, target, 1);
0397     if (rc) {
0398         ddev->poll_tech_count = poll_tech_count;
0399         return rc;
0400     }
0401 
0402     return 0;
0403 }
0404 
0405 void digital_poll_next_tech(struct nfc_digital_dev *ddev)
0406 {
0407     u8 rand_mod;
0408 
0409     digital_switch_rf(ddev, 0);
0410 
0411     mutex_lock(&ddev->poll_lock);
0412 
0413     if (!ddev->poll_tech_count) {
0414         mutex_unlock(&ddev->poll_lock);
0415         return;
0416     }
0417 
0418     get_random_bytes(&rand_mod, sizeof(rand_mod));
0419     ddev->poll_tech_index = rand_mod % ddev->poll_tech_count;
0420 
0421     mutex_unlock(&ddev->poll_lock);
0422 
0423     schedule_delayed_work(&ddev->poll_work,
0424                   msecs_to_jiffies(DIGITAL_POLL_INTERVAL));
0425 }
0426 
0427 static void digital_wq_poll(struct work_struct *work)
0428 {
0429     int rc;
0430     struct digital_poll_tech *poll_tech;
0431     struct nfc_digital_dev *ddev = container_of(work,
0432                             struct nfc_digital_dev,
0433                             poll_work.work);
0434     mutex_lock(&ddev->poll_lock);
0435 
0436     if (!ddev->poll_tech_count) {
0437         mutex_unlock(&ddev->poll_lock);
0438         return;
0439     }
0440 
0441     poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
0442 
0443     mutex_unlock(&ddev->poll_lock);
0444 
0445     rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
0446     if (rc)
0447         digital_poll_next_tech(ddev);
0448 }
0449 
0450 static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
0451                   digital_poll_t poll_func)
0452 {
0453     struct digital_poll_tech *poll_tech;
0454 
0455     if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
0456         return;
0457 
0458     poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
0459 
0460     poll_tech->rf_tech = rf_tech;
0461     poll_tech->poll_func = poll_func;
0462 }
0463 
0464 /**
0465  * digital_start_poll - start_poll operation
0466  * @nfc_dev: device to be polled
0467  * @im_protocols: bitset of nfc initiator protocols to be used for polling
0468  * @tm_protocols: bitset of nfc transport protocols to be used for polling
0469  *
0470  * For every supported protocol, the corresponding polling function is added
0471  * to the table of polling technologies (ddev->poll_techs[]) using
0472  * digital_add_poll_tech().
0473  * When a polling function fails (by timeout or protocol error) the next one is
0474  * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
0475  */
0476 static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
0477                   __u32 tm_protocols)
0478 {
0479     struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
0480     u32 matching_im_protocols, matching_tm_protocols;
0481 
0482     pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
0483          tm_protocols, ddev->protocols);
0484 
0485     matching_im_protocols = ddev->protocols & im_protocols;
0486     matching_tm_protocols = ddev->protocols & tm_protocols;
0487 
0488     if (!matching_im_protocols && !matching_tm_protocols) {
0489         pr_err("Unknown protocol\n");
0490         return -EINVAL;
0491     }
0492 
0493     if (ddev->poll_tech_count) {
0494         pr_err("Already polling\n");
0495         return -EBUSY;
0496     }
0497 
0498     if (ddev->curr_protocol) {
0499         pr_err("A target is already active\n");
0500         return -EBUSY;
0501     }
0502 
0503     ddev->poll_tech_count = 0;
0504     ddev->poll_tech_index = 0;
0505 
0506     if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
0507         digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
0508                       digital_in_send_sens_req);
0509 
0510     if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH)
0511         digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B,
0512                       digital_in_send_sensb_req);
0513 
0514     if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
0515         digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
0516                       digital_in_send_sensf_req);
0517 
0518         digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
0519                       digital_in_send_sensf_req);
0520     }
0521 
0522     if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH)
0523         digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693,
0524                       digital_in_send_iso15693_inv_req);
0525 
0526     if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
0527         if (ddev->ops->tg_listen_mdaa) {
0528             digital_add_poll_tech(ddev, 0,
0529                           digital_tg_listen_mdaa);
0530         } else if (ddev->ops->tg_listen_md) {
0531             digital_add_poll_tech(ddev, 0,
0532                           digital_tg_listen_md);
0533         } else {
0534             digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
0535                           digital_tg_listen_nfca);
0536 
0537             digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
0538                           digital_tg_listen_nfcf);
0539 
0540             digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
0541                           digital_tg_listen_nfcf);
0542         }
0543     }
0544 
0545     if (!ddev->poll_tech_count) {
0546         pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n",
0547                matching_im_protocols, matching_tm_protocols);
0548         return -EINVAL;
0549     }
0550 
0551     schedule_delayed_work(&ddev->poll_work, 0);
0552 
0553     return 0;
0554 }
0555 
0556 static void digital_stop_poll(struct nfc_dev *nfc_dev)
0557 {
0558     struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
0559 
0560     mutex_lock(&ddev->poll_lock);
0561 
0562     if (!ddev->poll_tech_count) {
0563         pr_err("Polling operation was not running\n");
0564         mutex_unlock(&ddev->poll_lock);
0565         return;
0566     }
0567 
0568     ddev->poll_tech_count = 0;
0569 
0570     mutex_unlock(&ddev->poll_lock);
0571 
0572     cancel_delayed_work_sync(&ddev->poll_work);
0573 
0574     digital_abort_cmd(ddev);
0575 }
0576 
0577 static int digital_dev_up(struct nfc_dev *nfc_dev)
0578 {
0579     struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
0580 
0581     digital_switch_rf(ddev, 1);
0582 
0583     return 0;
0584 }
0585 
0586 static int digital_dev_down(struct nfc_dev *nfc_dev)
0587 {
0588     struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
0589 
0590     digital_switch_rf(ddev, 0);
0591 
0592     return 0;
0593 }
0594 
0595 static int digital_dep_link_up(struct nfc_dev *nfc_dev,
0596                    struct nfc_target *target,
0597                    __u8 comm_mode, __u8 *gb, size_t gb_len)
0598 {
0599     struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
0600     int rc;
0601 
0602     rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
0603 
0604     if (!rc)
0605         ddev->curr_protocol = NFC_PROTO_NFC_DEP;
0606 
0607     return rc;
0608 }
0609 
0610 static int digital_dep_link_down(struct nfc_dev *nfc_dev)
0611 {
0612     struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
0613 
0614     digital_abort_cmd(ddev);
0615 
0616     ddev->curr_protocol = 0;
0617 
0618     return 0;
0619 }
0620 
0621 static int digital_activate_target(struct nfc_dev *nfc_dev,
0622                    struct nfc_target *target, __u32 protocol)
0623 {
0624     struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
0625 
0626     if (ddev->poll_tech_count) {
0627         pr_err("Can't activate a target while polling\n");
0628         return -EBUSY;
0629     }
0630 
0631     if (ddev->curr_protocol) {
0632         pr_err("A target is already active\n");
0633         return -EBUSY;
0634     }
0635 
0636     ddev->curr_protocol = protocol;
0637 
0638     return 0;
0639 }
0640 
0641 static void digital_deactivate_target(struct nfc_dev *nfc_dev,
0642                       struct nfc_target *target,
0643                       u8 mode)
0644 {
0645     struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
0646 
0647     if (!ddev->curr_protocol) {
0648         pr_err("No active target\n");
0649         return;
0650     }
0651 
0652     digital_abort_cmd(ddev);
0653     ddev->curr_protocol = 0;
0654 }
0655 
0656 static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
0657 {
0658     struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
0659 
0660     return digital_tg_send_dep_res(ddev, skb);
0661 }
0662 
0663 static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
0664                      struct sk_buff *resp)
0665 {
0666     struct digital_data_exch *data_exch = arg;
0667     int rc;
0668 
0669     if (IS_ERR(resp)) {
0670         rc = PTR_ERR(resp);
0671         resp = NULL;
0672         goto done;
0673     }
0674 
0675     if (ddev->curr_protocol == NFC_PROTO_MIFARE) {
0676         rc = digital_in_recv_mifare_res(resp);
0677         /* crc check is done in digital_in_recv_mifare_res() */
0678         goto done;
0679     }
0680 
0681     if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
0682         (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
0683         rc = digital_in_iso_dep_pull_sod(ddev, resp);
0684         if (rc)
0685             goto done;
0686     }
0687 
0688     rc = ddev->skb_check_crc(resp);
0689 
0690 done:
0691     if (rc) {
0692         kfree_skb(resp);
0693         resp = NULL;
0694     }
0695 
0696     data_exch->cb(data_exch->cb_context, resp, rc);
0697 
0698     kfree(data_exch);
0699 }
0700 
0701 static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
0702                struct sk_buff *skb, data_exchange_cb_t cb,
0703                void *cb_context)
0704 {
0705     struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
0706     struct digital_data_exch *data_exch;
0707     int rc;
0708 
0709     data_exch = kzalloc(sizeof(*data_exch), GFP_KERNEL);
0710     if (!data_exch)
0711         return -ENOMEM;
0712 
0713     data_exch->cb = cb;
0714     data_exch->cb_context = cb_context;
0715 
0716     if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) {
0717         rc = digital_in_send_dep_req(ddev, target, skb, data_exch);
0718         goto exit;
0719     }
0720 
0721     if ((ddev->curr_protocol == NFC_PROTO_ISO14443) ||
0722         (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) {
0723         rc = digital_in_iso_dep_push_sod(ddev, skb);
0724         if (rc)
0725             goto exit;
0726     }
0727 
0728     ddev->skb_add_crc(skb);
0729 
0730     rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
0731                  data_exch);
0732 
0733 exit:
0734     if (rc)
0735         kfree(data_exch);
0736 
0737     return rc;
0738 }
0739 
0740 static const struct nfc_ops digital_nfc_ops = {
0741     .dev_up = digital_dev_up,
0742     .dev_down = digital_dev_down,
0743     .start_poll = digital_start_poll,
0744     .stop_poll = digital_stop_poll,
0745     .dep_link_up = digital_dep_link_up,
0746     .dep_link_down = digital_dep_link_down,
0747     .activate_target = digital_activate_target,
0748     .deactivate_target = digital_deactivate_target,
0749     .tm_send = digital_tg_send,
0750     .im_transceive = digital_in_send,
0751 };
0752 
0753 struct nfc_digital_dev *nfc_digital_allocate_device(const struct nfc_digital_ops *ops,
0754                         __u32 supported_protocols,
0755                         __u32 driver_capabilities,
0756                         int tx_headroom, int tx_tailroom)
0757 {
0758     struct nfc_digital_dev *ddev;
0759 
0760     if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
0761         !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
0762         !ops->switch_rf || (ops->tg_listen_md && !ops->tg_get_rf_tech))
0763         return NULL;
0764 
0765     ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
0766     if (!ddev)
0767         return NULL;
0768 
0769     ddev->driver_capabilities = driver_capabilities;
0770     ddev->ops = ops;
0771 
0772     mutex_init(&ddev->cmd_lock);
0773     INIT_LIST_HEAD(&ddev->cmd_queue);
0774 
0775     INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
0776     INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
0777 
0778     mutex_init(&ddev->poll_lock);
0779     INIT_DELAYED_WORK(&ddev->poll_work, digital_wq_poll);
0780 
0781     if (supported_protocols & NFC_PROTO_JEWEL_MASK)
0782         ddev->protocols |= NFC_PROTO_JEWEL_MASK;
0783     if (supported_protocols & NFC_PROTO_MIFARE_MASK)
0784         ddev->protocols |= NFC_PROTO_MIFARE_MASK;
0785     if (supported_protocols & NFC_PROTO_FELICA_MASK)
0786         ddev->protocols |= NFC_PROTO_FELICA_MASK;
0787     if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
0788         ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
0789     if (supported_protocols & NFC_PROTO_ISO15693_MASK)
0790         ddev->protocols |= NFC_PROTO_ISO15693_MASK;
0791     if (supported_protocols & NFC_PROTO_ISO14443_MASK)
0792         ddev->protocols |= NFC_PROTO_ISO14443_MASK;
0793     if (supported_protocols & NFC_PROTO_ISO14443_B_MASK)
0794         ddev->protocols |= NFC_PROTO_ISO14443_B_MASK;
0795 
0796     ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
0797     ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
0798 
0799     ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
0800                         ddev->tx_headroom,
0801                         ddev->tx_tailroom);
0802     if (!ddev->nfc_dev) {
0803         pr_err("nfc_allocate_device failed\n");
0804         goto free_dev;
0805     }
0806 
0807     nfc_set_drvdata(ddev->nfc_dev, ddev);
0808 
0809     return ddev;
0810 
0811 free_dev:
0812     kfree(ddev);
0813 
0814     return NULL;
0815 }
0816 EXPORT_SYMBOL(nfc_digital_allocate_device);
0817 
0818 void nfc_digital_free_device(struct nfc_digital_dev *ddev)
0819 {
0820     nfc_free_device(ddev->nfc_dev);
0821     kfree(ddev);
0822 }
0823 EXPORT_SYMBOL(nfc_digital_free_device);
0824 
0825 int nfc_digital_register_device(struct nfc_digital_dev *ddev)
0826 {
0827     return nfc_register_device(ddev->nfc_dev);
0828 }
0829 EXPORT_SYMBOL(nfc_digital_register_device);
0830 
0831 void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
0832 {
0833     struct digital_cmd *cmd, *n;
0834 
0835     nfc_unregister_device(ddev->nfc_dev);
0836 
0837     mutex_lock(&ddev->poll_lock);
0838     ddev->poll_tech_count = 0;
0839     mutex_unlock(&ddev->poll_lock);
0840 
0841     cancel_delayed_work_sync(&ddev->poll_work);
0842     cancel_work_sync(&ddev->cmd_work);
0843     cancel_work_sync(&ddev->cmd_complete_work);
0844 
0845     list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
0846         list_del(&cmd->queue);
0847 
0848         /* Call the command callback if any and pass it a ENODEV error.
0849          * This gives a chance to the command issuer to free any
0850          * allocated buffer.
0851          */
0852         if (cmd->cmd_cb)
0853             cmd->cmd_cb(ddev, cmd->cb_context, ERR_PTR(-ENODEV));
0854 
0855         kfree(cmd->mdaa_params);
0856         kfree(cmd);
0857     }
0858 }
0859 EXPORT_SYMBOL(nfc_digital_unregister_device);
0860 
0861 MODULE_LICENSE("GPL");