Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  The NFC Controller Interface is the communication protocol between an
0004  *  NFC Controller (NFCC) and a Device Host (DH).
0005  *
0006  *  Copyright (C) 2011 Texas Instruments, Inc.
0007  *  Copyright (C) 2014 Marvell International Ltd.
0008  *
0009  *  Written by Ilan Elias <ilane@ti.com>
0010  *
0011  *  Acknowledgements:
0012  *  This file is based on hci_core.c, which was written
0013  *  by Maxim Krasnyansky.
0014  */
0015 
0016 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
0017 
0018 #include <linux/module.h>
0019 #include <linux/kernel.h>
0020 #include <linux/types.h>
0021 #include <linux/workqueue.h>
0022 #include <linux/completion.h>
0023 #include <linux/export.h>
0024 #include <linux/sched.h>
0025 #include <linux/bitops.h>
0026 #include <linux/skbuff.h>
0027 
0028 #include "../nfc.h"
0029 #include <net/nfc/nci.h>
0030 #include <net/nfc/nci_core.h>
0031 #include <linux/nfc.h>
0032 
0033 struct core_conn_create_data {
0034     int length;
0035     struct nci_core_conn_create_cmd *cmd;
0036 };
0037 
0038 static void nci_cmd_work(struct work_struct *work);
0039 static void nci_rx_work(struct work_struct *work);
0040 static void nci_tx_work(struct work_struct *work);
0041 
0042 struct nci_conn_info *nci_get_conn_info_by_conn_id(struct nci_dev *ndev,
0043                            int conn_id)
0044 {
0045     struct nci_conn_info *conn_info;
0046 
0047     list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
0048         if (conn_info->conn_id == conn_id)
0049             return conn_info;
0050     }
0051 
0052     return NULL;
0053 }
0054 
0055 int nci_get_conn_info_by_dest_type_params(struct nci_dev *ndev, u8 dest_type,
0056                       const struct dest_spec_params *params)
0057 {
0058     const struct nci_conn_info *conn_info;
0059 
0060     list_for_each_entry(conn_info, &ndev->conn_info_list, list) {
0061         if (conn_info->dest_type == dest_type) {
0062             if (!params)
0063                 return conn_info->conn_id;
0064 
0065             if (params->id == conn_info->dest_params->id &&
0066                 params->protocol == conn_info->dest_params->protocol)
0067                 return conn_info->conn_id;
0068         }
0069     }
0070 
0071     return -EINVAL;
0072 }
0073 EXPORT_SYMBOL(nci_get_conn_info_by_dest_type_params);
0074 
0075 /* ---- NCI requests ---- */
0076 
0077 void nci_req_complete(struct nci_dev *ndev, int result)
0078 {
0079     if (ndev->req_status == NCI_REQ_PEND) {
0080         ndev->req_result = result;
0081         ndev->req_status = NCI_REQ_DONE;
0082         complete(&ndev->req_completion);
0083     }
0084 }
0085 EXPORT_SYMBOL(nci_req_complete);
0086 
0087 static void nci_req_cancel(struct nci_dev *ndev, int err)
0088 {
0089     if (ndev->req_status == NCI_REQ_PEND) {
0090         ndev->req_result = err;
0091         ndev->req_status = NCI_REQ_CANCELED;
0092         complete(&ndev->req_completion);
0093     }
0094 }
0095 
0096 /* Execute request and wait for completion. */
0097 static int __nci_request(struct nci_dev *ndev,
0098              void (*req)(struct nci_dev *ndev, const void *opt),
0099              const void *opt, __u32 timeout)
0100 {
0101     int rc = 0;
0102     long completion_rc;
0103 
0104     ndev->req_status = NCI_REQ_PEND;
0105 
0106     reinit_completion(&ndev->req_completion);
0107     req(ndev, opt);
0108     completion_rc =
0109         wait_for_completion_interruptible_timeout(&ndev->req_completion,
0110                               timeout);
0111 
0112     pr_debug("wait_for_completion return %ld\n", completion_rc);
0113 
0114     if (completion_rc > 0) {
0115         switch (ndev->req_status) {
0116         case NCI_REQ_DONE:
0117             rc = nci_to_errno(ndev->req_result);
0118             break;
0119 
0120         case NCI_REQ_CANCELED:
0121             rc = -ndev->req_result;
0122             break;
0123 
0124         default:
0125             rc = -ETIMEDOUT;
0126             break;
0127         }
0128     } else {
0129         pr_err("wait_for_completion_interruptible_timeout failed %ld\n",
0130                completion_rc);
0131 
0132         rc = ((completion_rc == 0) ? (-ETIMEDOUT) : (completion_rc));
0133     }
0134 
0135     ndev->req_status = ndev->req_result = 0;
0136 
0137     return rc;
0138 }
0139 
0140 inline int nci_request(struct nci_dev *ndev,
0141                void (*req)(struct nci_dev *ndev,
0142                    const void *opt),
0143                const void *opt, __u32 timeout)
0144 {
0145     int rc;
0146 
0147     /* Serialize all requests */
0148     mutex_lock(&ndev->req_lock);
0149     /* check the state after obtaing the lock against any races
0150      * from nci_close_device when the device gets removed.
0151      */
0152     if (test_bit(NCI_UP, &ndev->flags))
0153         rc = __nci_request(ndev, req, opt, timeout);
0154     else
0155         rc = -ENETDOWN;
0156     mutex_unlock(&ndev->req_lock);
0157 
0158     return rc;
0159 }
0160 
0161 static void nci_reset_req(struct nci_dev *ndev, const void *opt)
0162 {
0163     struct nci_core_reset_cmd cmd;
0164 
0165     cmd.reset_type = NCI_RESET_TYPE_RESET_CONFIG;
0166     nci_send_cmd(ndev, NCI_OP_CORE_RESET_CMD, 1, &cmd);
0167 }
0168 
0169 static void nci_init_req(struct nci_dev *ndev, const void *opt)
0170 {
0171     u8 plen = 0;
0172 
0173     if (opt)
0174         plen = sizeof(struct nci_core_init_v2_cmd);
0175 
0176     nci_send_cmd(ndev, NCI_OP_CORE_INIT_CMD, plen, opt);
0177 }
0178 
0179 static void nci_init_complete_req(struct nci_dev *ndev, const void *opt)
0180 {
0181     struct nci_rf_disc_map_cmd cmd;
0182     struct disc_map_config *cfg = cmd.mapping_configs;
0183     __u8 *num = &cmd.num_mapping_configs;
0184     int i;
0185 
0186     /* set rf mapping configurations */
0187     *num = 0;
0188 
0189     /* by default mapping is set to NCI_RF_INTERFACE_FRAME */
0190     for (i = 0; i < ndev->num_supported_rf_interfaces; i++) {
0191         if (ndev->supported_rf_interfaces[i] ==
0192             NCI_RF_INTERFACE_ISO_DEP) {
0193             cfg[*num].rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
0194             cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
0195                 NCI_DISC_MAP_MODE_LISTEN;
0196             cfg[*num].rf_interface = NCI_RF_INTERFACE_ISO_DEP;
0197             (*num)++;
0198         } else if (ndev->supported_rf_interfaces[i] ==
0199                NCI_RF_INTERFACE_NFC_DEP) {
0200             cfg[*num].rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
0201             cfg[*num].mode = NCI_DISC_MAP_MODE_POLL |
0202                 NCI_DISC_MAP_MODE_LISTEN;
0203             cfg[*num].rf_interface = NCI_RF_INTERFACE_NFC_DEP;
0204             (*num)++;
0205         }
0206 
0207         if (*num == NCI_MAX_NUM_MAPPING_CONFIGS)
0208             break;
0209     }
0210 
0211     nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_MAP_CMD,
0212              (1 + ((*num) * sizeof(struct disc_map_config))), &cmd);
0213 }
0214 
0215 struct nci_set_config_param {
0216     __u8        id;
0217     size_t      len;
0218     const __u8  *val;
0219 };
0220 
0221 static void nci_set_config_req(struct nci_dev *ndev, const void *opt)
0222 {
0223     const struct nci_set_config_param *param = opt;
0224     struct nci_core_set_config_cmd cmd;
0225 
0226     BUG_ON(param->len > NCI_MAX_PARAM_LEN);
0227 
0228     cmd.num_params = 1;
0229     cmd.param.id = param->id;
0230     cmd.param.len = param->len;
0231     memcpy(cmd.param.val, param->val, param->len);
0232 
0233     nci_send_cmd(ndev, NCI_OP_CORE_SET_CONFIG_CMD, (3 + param->len), &cmd);
0234 }
0235 
0236 struct nci_rf_discover_param {
0237     __u32   im_protocols;
0238     __u32   tm_protocols;
0239 };
0240 
0241 static void nci_rf_discover_req(struct nci_dev *ndev, const void *opt)
0242 {
0243     const struct nci_rf_discover_param *param = opt;
0244     struct nci_rf_disc_cmd cmd;
0245 
0246     cmd.num_disc_configs = 0;
0247 
0248     if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
0249         (param->im_protocols & NFC_PROTO_JEWEL_MASK ||
0250          param->im_protocols & NFC_PROTO_MIFARE_MASK ||
0251          param->im_protocols & NFC_PROTO_ISO14443_MASK ||
0252          param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
0253         cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
0254             NCI_NFC_A_PASSIVE_POLL_MODE;
0255         cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
0256         cmd.num_disc_configs++;
0257     }
0258 
0259     if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
0260         (param->im_protocols & NFC_PROTO_ISO14443_B_MASK)) {
0261         cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
0262             NCI_NFC_B_PASSIVE_POLL_MODE;
0263         cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
0264         cmd.num_disc_configs++;
0265     }
0266 
0267     if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
0268         (param->im_protocols & NFC_PROTO_FELICA_MASK ||
0269          param->im_protocols & NFC_PROTO_NFC_DEP_MASK)) {
0270         cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
0271             NCI_NFC_F_PASSIVE_POLL_MODE;
0272         cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
0273         cmd.num_disc_configs++;
0274     }
0275 
0276     if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS) &&
0277         (param->im_protocols & NFC_PROTO_ISO15693_MASK)) {
0278         cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
0279             NCI_NFC_V_PASSIVE_POLL_MODE;
0280         cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
0281         cmd.num_disc_configs++;
0282     }
0283 
0284     if ((cmd.num_disc_configs < NCI_MAX_NUM_RF_CONFIGS - 1) &&
0285         (param->tm_protocols & NFC_PROTO_NFC_DEP_MASK)) {
0286         cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
0287             NCI_NFC_A_PASSIVE_LISTEN_MODE;
0288         cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
0289         cmd.num_disc_configs++;
0290         cmd.disc_configs[cmd.num_disc_configs].rf_tech_and_mode =
0291             NCI_NFC_F_PASSIVE_LISTEN_MODE;
0292         cmd.disc_configs[cmd.num_disc_configs].frequency = 1;
0293         cmd.num_disc_configs++;
0294     }
0295 
0296     nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_CMD,
0297              (1 + (cmd.num_disc_configs * sizeof(struct disc_config))),
0298              &cmd);
0299 }
0300 
0301 struct nci_rf_discover_select_param {
0302     __u8    rf_discovery_id;
0303     __u8    rf_protocol;
0304 };
0305 
0306 static void nci_rf_discover_select_req(struct nci_dev *ndev, const void *opt)
0307 {
0308     const struct nci_rf_discover_select_param *param = opt;
0309     struct nci_rf_discover_select_cmd cmd;
0310 
0311     cmd.rf_discovery_id = param->rf_discovery_id;
0312     cmd.rf_protocol = param->rf_protocol;
0313 
0314     switch (cmd.rf_protocol) {
0315     case NCI_RF_PROTOCOL_ISO_DEP:
0316         cmd.rf_interface = NCI_RF_INTERFACE_ISO_DEP;
0317         break;
0318 
0319     case NCI_RF_PROTOCOL_NFC_DEP:
0320         cmd.rf_interface = NCI_RF_INTERFACE_NFC_DEP;
0321         break;
0322 
0323     default:
0324         cmd.rf_interface = NCI_RF_INTERFACE_FRAME;
0325         break;
0326     }
0327 
0328     nci_send_cmd(ndev, NCI_OP_RF_DISCOVER_SELECT_CMD,
0329              sizeof(struct nci_rf_discover_select_cmd), &cmd);
0330 }
0331 
0332 static void nci_rf_deactivate_req(struct nci_dev *ndev, const void *opt)
0333 {
0334     struct nci_rf_deactivate_cmd cmd;
0335 
0336     cmd.type = (unsigned long)opt;
0337 
0338     nci_send_cmd(ndev, NCI_OP_RF_DEACTIVATE_CMD,
0339              sizeof(struct nci_rf_deactivate_cmd), &cmd);
0340 }
0341 
0342 struct nci_cmd_param {
0343     __u16 opcode;
0344     size_t len;
0345     const __u8 *payload;
0346 };
0347 
0348 static void nci_generic_req(struct nci_dev *ndev, const void *opt)
0349 {
0350     const struct nci_cmd_param *param = opt;
0351 
0352     nci_send_cmd(ndev, param->opcode, param->len, param->payload);
0353 }
0354 
0355 int nci_prop_cmd(struct nci_dev *ndev, __u8 oid, size_t len, const __u8 *payload)
0356 {
0357     struct nci_cmd_param param;
0358 
0359     param.opcode = nci_opcode_pack(NCI_GID_PROPRIETARY, oid);
0360     param.len = len;
0361     param.payload = payload;
0362 
0363     return __nci_request(ndev, nci_generic_req, &param,
0364                  msecs_to_jiffies(NCI_CMD_TIMEOUT));
0365 }
0366 EXPORT_SYMBOL(nci_prop_cmd);
0367 
0368 int nci_core_cmd(struct nci_dev *ndev, __u16 opcode, size_t len,
0369          const __u8 *payload)
0370 {
0371     struct nci_cmd_param param;
0372 
0373     param.opcode = opcode;
0374     param.len = len;
0375     param.payload = payload;
0376 
0377     return __nci_request(ndev, nci_generic_req, &param,
0378                  msecs_to_jiffies(NCI_CMD_TIMEOUT));
0379 }
0380 EXPORT_SYMBOL(nci_core_cmd);
0381 
0382 int nci_core_reset(struct nci_dev *ndev)
0383 {
0384     return __nci_request(ndev, nci_reset_req, (void *)0,
0385                  msecs_to_jiffies(NCI_RESET_TIMEOUT));
0386 }
0387 EXPORT_SYMBOL(nci_core_reset);
0388 
0389 int nci_core_init(struct nci_dev *ndev)
0390 {
0391     return __nci_request(ndev, nci_init_req, (void *)0,
0392                  msecs_to_jiffies(NCI_INIT_TIMEOUT));
0393 }
0394 EXPORT_SYMBOL(nci_core_init);
0395 
0396 struct nci_loopback_data {
0397     u8 conn_id;
0398     struct sk_buff *data;
0399 };
0400 
0401 static void nci_send_data_req(struct nci_dev *ndev, const void *opt)
0402 {
0403     const struct nci_loopback_data *data = opt;
0404 
0405     nci_send_data(ndev, data->conn_id, data->data);
0406 }
0407 
0408 static void nci_nfcc_loopback_cb(void *context, struct sk_buff *skb, int err)
0409 {
0410     struct nci_dev *ndev = (struct nci_dev *)context;
0411     struct nci_conn_info *conn_info;
0412 
0413     conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
0414     if (!conn_info) {
0415         nci_req_complete(ndev, NCI_STATUS_REJECTED);
0416         return;
0417     }
0418 
0419     conn_info->rx_skb = skb;
0420 
0421     nci_req_complete(ndev, NCI_STATUS_OK);
0422 }
0423 
0424 int nci_nfcc_loopback(struct nci_dev *ndev, const void *data, size_t data_len,
0425               struct sk_buff **resp)
0426 {
0427     int r;
0428     struct nci_loopback_data loopback_data;
0429     struct nci_conn_info *conn_info;
0430     struct sk_buff *skb;
0431     int conn_id = nci_get_conn_info_by_dest_type_params(ndev,
0432                     NCI_DESTINATION_NFCC_LOOPBACK, NULL);
0433 
0434     if (conn_id < 0) {
0435         r = nci_core_conn_create(ndev, NCI_DESTINATION_NFCC_LOOPBACK,
0436                      0, 0, NULL);
0437         if (r != NCI_STATUS_OK)
0438             return r;
0439 
0440         conn_id = nci_get_conn_info_by_dest_type_params(ndev,
0441                     NCI_DESTINATION_NFCC_LOOPBACK,
0442                     NULL);
0443     }
0444 
0445     conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
0446     if (!conn_info)
0447         return -EPROTO;
0448 
0449     /* store cb and context to be used on receiving data */
0450     conn_info->data_exchange_cb = nci_nfcc_loopback_cb;
0451     conn_info->data_exchange_cb_context = ndev;
0452 
0453     skb = nci_skb_alloc(ndev, NCI_DATA_HDR_SIZE + data_len, GFP_KERNEL);
0454     if (!skb)
0455         return -ENOMEM;
0456 
0457     skb_reserve(skb, NCI_DATA_HDR_SIZE);
0458     skb_put_data(skb, data, data_len);
0459 
0460     loopback_data.conn_id = conn_id;
0461     loopback_data.data = skb;
0462 
0463     ndev->cur_conn_id = conn_id;
0464     r = nci_request(ndev, nci_send_data_req, &loopback_data,
0465             msecs_to_jiffies(NCI_DATA_TIMEOUT));
0466     if (r == NCI_STATUS_OK && resp)
0467         *resp = conn_info->rx_skb;
0468 
0469     return r;
0470 }
0471 EXPORT_SYMBOL(nci_nfcc_loopback);
0472 
0473 static int nci_open_device(struct nci_dev *ndev)
0474 {
0475     int rc = 0;
0476 
0477     mutex_lock(&ndev->req_lock);
0478 
0479     if (test_bit(NCI_UNREG, &ndev->flags)) {
0480         rc = -ENODEV;
0481         goto done;
0482     }
0483 
0484     if (test_bit(NCI_UP, &ndev->flags)) {
0485         rc = -EALREADY;
0486         goto done;
0487     }
0488 
0489     if (ndev->ops->open(ndev)) {
0490         rc = -EIO;
0491         goto done;
0492     }
0493 
0494     atomic_set(&ndev->cmd_cnt, 1);
0495 
0496     set_bit(NCI_INIT, &ndev->flags);
0497 
0498     if (ndev->ops->init)
0499         rc = ndev->ops->init(ndev);
0500 
0501     if (!rc) {
0502         rc = __nci_request(ndev, nci_reset_req, (void *)0,
0503                    msecs_to_jiffies(NCI_RESET_TIMEOUT));
0504     }
0505 
0506     if (!rc && ndev->ops->setup) {
0507         rc = ndev->ops->setup(ndev);
0508     }
0509 
0510     if (!rc) {
0511         struct nci_core_init_v2_cmd nci_init_v2_cmd = {
0512             .feature1 = NCI_FEATURE_DISABLE,
0513             .feature2 = NCI_FEATURE_DISABLE
0514         };
0515         const void *opt = NULL;
0516 
0517         if (ndev->nci_ver & NCI_VER_2_MASK)
0518             opt = &nci_init_v2_cmd;
0519 
0520         rc = __nci_request(ndev, nci_init_req, opt,
0521                    msecs_to_jiffies(NCI_INIT_TIMEOUT));
0522     }
0523 
0524     if (!rc && ndev->ops->post_setup)
0525         rc = ndev->ops->post_setup(ndev);
0526 
0527     if (!rc) {
0528         rc = __nci_request(ndev, nci_init_complete_req, (void *)0,
0529                    msecs_to_jiffies(NCI_INIT_TIMEOUT));
0530     }
0531 
0532     clear_bit(NCI_INIT, &ndev->flags);
0533 
0534     if (!rc) {
0535         set_bit(NCI_UP, &ndev->flags);
0536         nci_clear_target_list(ndev);
0537         atomic_set(&ndev->state, NCI_IDLE);
0538     } else {
0539         /* Init failed, cleanup */
0540         skb_queue_purge(&ndev->cmd_q);
0541         skb_queue_purge(&ndev->rx_q);
0542         skb_queue_purge(&ndev->tx_q);
0543 
0544         ndev->ops->close(ndev);
0545         ndev->flags = 0;
0546     }
0547 
0548 done:
0549     mutex_unlock(&ndev->req_lock);
0550     return rc;
0551 }
0552 
0553 static int nci_close_device(struct nci_dev *ndev)
0554 {
0555     nci_req_cancel(ndev, ENODEV);
0556 
0557     /* This mutex needs to be held as a barrier for
0558      * caller nci_unregister_device
0559      */
0560     mutex_lock(&ndev->req_lock);
0561 
0562     if (!test_and_clear_bit(NCI_UP, &ndev->flags)) {
0563         /* Need to flush the cmd wq in case
0564          * there is a queued/running cmd_work
0565          */
0566         flush_workqueue(ndev->cmd_wq);
0567         del_timer_sync(&ndev->cmd_timer);
0568         del_timer_sync(&ndev->data_timer);
0569         mutex_unlock(&ndev->req_lock);
0570         return 0;
0571     }
0572 
0573     /* Drop RX and TX queues */
0574     skb_queue_purge(&ndev->rx_q);
0575     skb_queue_purge(&ndev->tx_q);
0576 
0577     /* Flush RX and TX wq */
0578     flush_workqueue(ndev->rx_wq);
0579     flush_workqueue(ndev->tx_wq);
0580 
0581     /* Reset device */
0582     skb_queue_purge(&ndev->cmd_q);
0583     atomic_set(&ndev->cmd_cnt, 1);
0584 
0585     set_bit(NCI_INIT, &ndev->flags);
0586     __nci_request(ndev, nci_reset_req, (void *)0,
0587               msecs_to_jiffies(NCI_RESET_TIMEOUT));
0588 
0589     /* After this point our queues are empty
0590      * and no works are scheduled.
0591      */
0592     ndev->ops->close(ndev);
0593 
0594     clear_bit(NCI_INIT, &ndev->flags);
0595 
0596     /* Flush cmd wq */
0597     flush_workqueue(ndev->cmd_wq);
0598 
0599     del_timer_sync(&ndev->cmd_timer);
0600 
0601     /* Clear flags except NCI_UNREG */
0602     ndev->flags &= BIT(NCI_UNREG);
0603 
0604     mutex_unlock(&ndev->req_lock);
0605 
0606     return 0;
0607 }
0608 
0609 /* NCI command timer function */
0610 static void nci_cmd_timer(struct timer_list *t)
0611 {
0612     struct nci_dev *ndev = from_timer(ndev, t, cmd_timer);
0613 
0614     atomic_set(&ndev->cmd_cnt, 1);
0615     queue_work(ndev->cmd_wq, &ndev->cmd_work);
0616 }
0617 
0618 /* NCI data exchange timer function */
0619 static void nci_data_timer(struct timer_list *t)
0620 {
0621     struct nci_dev *ndev = from_timer(ndev, t, data_timer);
0622 
0623     set_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
0624     queue_work(ndev->rx_wq, &ndev->rx_work);
0625 }
0626 
0627 static int nci_dev_up(struct nfc_dev *nfc_dev)
0628 {
0629     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
0630 
0631     return nci_open_device(ndev);
0632 }
0633 
0634 static int nci_dev_down(struct nfc_dev *nfc_dev)
0635 {
0636     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
0637 
0638     return nci_close_device(ndev);
0639 }
0640 
0641 int nci_set_config(struct nci_dev *ndev, __u8 id, size_t len, const __u8 *val)
0642 {
0643     struct nci_set_config_param param;
0644 
0645     if (!val || !len)
0646         return 0;
0647 
0648     param.id = id;
0649     param.len = len;
0650     param.val = val;
0651 
0652     return __nci_request(ndev, nci_set_config_req, &param,
0653                  msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
0654 }
0655 EXPORT_SYMBOL(nci_set_config);
0656 
0657 static void nci_nfcee_discover_req(struct nci_dev *ndev, const void *opt)
0658 {
0659     struct nci_nfcee_discover_cmd cmd;
0660     __u8 action = (unsigned long)opt;
0661 
0662     cmd.discovery_action = action;
0663 
0664     nci_send_cmd(ndev, NCI_OP_NFCEE_DISCOVER_CMD, 1, &cmd);
0665 }
0666 
0667 int nci_nfcee_discover(struct nci_dev *ndev, u8 action)
0668 {
0669     unsigned long opt = action;
0670 
0671     return __nci_request(ndev, nci_nfcee_discover_req, (void *)opt,
0672                 msecs_to_jiffies(NCI_CMD_TIMEOUT));
0673 }
0674 EXPORT_SYMBOL(nci_nfcee_discover);
0675 
0676 static void nci_nfcee_mode_set_req(struct nci_dev *ndev, const void *opt)
0677 {
0678     const struct nci_nfcee_mode_set_cmd *cmd = opt;
0679 
0680     nci_send_cmd(ndev, NCI_OP_NFCEE_MODE_SET_CMD,
0681              sizeof(struct nci_nfcee_mode_set_cmd), cmd);
0682 }
0683 
0684 int nci_nfcee_mode_set(struct nci_dev *ndev, u8 nfcee_id, u8 nfcee_mode)
0685 {
0686     struct nci_nfcee_mode_set_cmd cmd;
0687 
0688     cmd.nfcee_id = nfcee_id;
0689     cmd.nfcee_mode = nfcee_mode;
0690 
0691     return __nci_request(ndev, nci_nfcee_mode_set_req, &cmd,
0692                  msecs_to_jiffies(NCI_CMD_TIMEOUT));
0693 }
0694 EXPORT_SYMBOL(nci_nfcee_mode_set);
0695 
0696 static void nci_core_conn_create_req(struct nci_dev *ndev, const void *opt)
0697 {
0698     const struct core_conn_create_data *data = opt;
0699 
0700     nci_send_cmd(ndev, NCI_OP_CORE_CONN_CREATE_CMD, data->length, data->cmd);
0701 }
0702 
0703 int nci_core_conn_create(struct nci_dev *ndev, u8 destination_type,
0704              u8 number_destination_params,
0705              size_t params_len,
0706              const struct core_conn_create_dest_spec_params *params)
0707 {
0708     int r;
0709     struct nci_core_conn_create_cmd *cmd;
0710     struct core_conn_create_data data;
0711 
0712     data.length = params_len + sizeof(struct nci_core_conn_create_cmd);
0713     cmd = kzalloc(data.length, GFP_KERNEL);
0714     if (!cmd)
0715         return -ENOMEM;
0716 
0717     cmd->destination_type = destination_type;
0718     cmd->number_destination_params = number_destination_params;
0719 
0720     data.cmd = cmd;
0721 
0722     if (params) {
0723         memcpy(cmd->params, params, params_len);
0724         if (params->length > 0)
0725             memcpy(&ndev->cur_params,
0726                    &params->value[DEST_SPEC_PARAMS_ID_INDEX],
0727                    sizeof(struct dest_spec_params));
0728         else
0729             ndev->cur_params.id = 0;
0730     } else {
0731         ndev->cur_params.id = 0;
0732     }
0733     ndev->cur_dest_type = destination_type;
0734 
0735     r = __nci_request(ndev, nci_core_conn_create_req, &data,
0736               msecs_to_jiffies(NCI_CMD_TIMEOUT));
0737     kfree(cmd);
0738     return r;
0739 }
0740 EXPORT_SYMBOL(nci_core_conn_create);
0741 
0742 static void nci_core_conn_close_req(struct nci_dev *ndev, const void *opt)
0743 {
0744     __u8 conn_id = (unsigned long)opt;
0745 
0746     nci_send_cmd(ndev, NCI_OP_CORE_CONN_CLOSE_CMD, 1, &conn_id);
0747 }
0748 
0749 int nci_core_conn_close(struct nci_dev *ndev, u8 conn_id)
0750 {
0751     unsigned long opt = conn_id;
0752 
0753     ndev->cur_conn_id = conn_id;
0754     return __nci_request(ndev, nci_core_conn_close_req, (void *)opt,
0755                  msecs_to_jiffies(NCI_CMD_TIMEOUT));
0756 }
0757 EXPORT_SYMBOL(nci_core_conn_close);
0758 
0759 static int nci_set_local_general_bytes(struct nfc_dev *nfc_dev)
0760 {
0761     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
0762     struct nci_set_config_param param;
0763     int rc;
0764 
0765     param.val = nfc_get_local_general_bytes(nfc_dev, &param.len);
0766     if ((param.val == NULL) || (param.len == 0))
0767         return 0;
0768 
0769     if (param.len > NFC_MAX_GT_LEN)
0770         return -EINVAL;
0771 
0772     param.id = NCI_PN_ATR_REQ_GEN_BYTES;
0773 
0774     rc = nci_request(ndev, nci_set_config_req, &param,
0775              msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
0776     if (rc)
0777         return rc;
0778 
0779     param.id = NCI_LN_ATR_RES_GEN_BYTES;
0780 
0781     return nci_request(ndev, nci_set_config_req, &param,
0782                msecs_to_jiffies(NCI_SET_CONFIG_TIMEOUT));
0783 }
0784 
0785 static int nci_set_listen_parameters(struct nfc_dev *nfc_dev)
0786 {
0787     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
0788     int rc;
0789     __u8 val;
0790 
0791     val = NCI_LA_SEL_INFO_NFC_DEP_MASK;
0792 
0793     rc = nci_set_config(ndev, NCI_LA_SEL_INFO, 1, &val);
0794     if (rc)
0795         return rc;
0796 
0797     val = NCI_LF_PROTOCOL_TYPE_NFC_DEP_MASK;
0798 
0799     rc = nci_set_config(ndev, NCI_LF_PROTOCOL_TYPE, 1, &val);
0800     if (rc)
0801         return rc;
0802 
0803     val = NCI_LF_CON_BITR_F_212 | NCI_LF_CON_BITR_F_424;
0804 
0805     return nci_set_config(ndev, NCI_LF_CON_BITR_F, 1, &val);
0806 }
0807 
0808 static int nci_start_poll(struct nfc_dev *nfc_dev,
0809               __u32 im_protocols, __u32 tm_protocols)
0810 {
0811     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
0812     struct nci_rf_discover_param param;
0813     int rc;
0814 
0815     if ((atomic_read(&ndev->state) == NCI_DISCOVERY) ||
0816         (atomic_read(&ndev->state) == NCI_W4_ALL_DISCOVERIES)) {
0817         pr_err("unable to start poll, since poll is already active\n");
0818         return -EBUSY;
0819     }
0820 
0821     if (ndev->target_active_prot) {
0822         pr_err("there is an active target\n");
0823         return -EBUSY;
0824     }
0825 
0826     if ((atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) ||
0827         (atomic_read(&ndev->state) == NCI_POLL_ACTIVE)) {
0828         pr_debug("target active or w4 select, implicitly deactivate\n");
0829 
0830         rc = nci_request(ndev, nci_rf_deactivate_req,
0831                  (void *)NCI_DEACTIVATE_TYPE_IDLE_MODE,
0832                  msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
0833         if (rc)
0834             return -EBUSY;
0835     }
0836 
0837     if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
0838         rc = nci_set_local_general_bytes(nfc_dev);
0839         if (rc) {
0840             pr_err("failed to set local general bytes\n");
0841             return rc;
0842         }
0843     }
0844 
0845     if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
0846         rc = nci_set_listen_parameters(nfc_dev);
0847         if (rc)
0848             pr_err("failed to set listen parameters\n");
0849     }
0850 
0851     param.im_protocols = im_protocols;
0852     param.tm_protocols = tm_protocols;
0853     rc = nci_request(ndev, nci_rf_discover_req, &param,
0854              msecs_to_jiffies(NCI_RF_DISC_TIMEOUT));
0855 
0856     if (!rc)
0857         ndev->poll_prots = im_protocols;
0858 
0859     return rc;
0860 }
0861 
0862 static void nci_stop_poll(struct nfc_dev *nfc_dev)
0863 {
0864     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
0865 
0866     if ((atomic_read(&ndev->state) != NCI_DISCOVERY) &&
0867         (atomic_read(&ndev->state) != NCI_W4_ALL_DISCOVERIES)) {
0868         pr_err("unable to stop poll, since poll is not active\n");
0869         return;
0870     }
0871 
0872     nci_request(ndev, nci_rf_deactivate_req,
0873             (void *)NCI_DEACTIVATE_TYPE_IDLE_MODE,
0874             msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
0875 }
0876 
0877 static int nci_activate_target(struct nfc_dev *nfc_dev,
0878                    struct nfc_target *target, __u32 protocol)
0879 {
0880     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
0881     struct nci_rf_discover_select_param param;
0882     const struct nfc_target *nci_target = NULL;
0883     int i;
0884     int rc = 0;
0885 
0886     pr_debug("target_idx %d, protocol 0x%x\n", target->idx, protocol);
0887 
0888     if ((atomic_read(&ndev->state) != NCI_W4_HOST_SELECT) &&
0889         (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
0890         pr_err("there is no available target to activate\n");
0891         return -EINVAL;
0892     }
0893 
0894     if (ndev->target_active_prot) {
0895         pr_err("there is already an active target\n");
0896         return -EBUSY;
0897     }
0898 
0899     for (i = 0; i < ndev->n_targets; i++) {
0900         if (ndev->targets[i].idx == target->idx) {
0901             nci_target = &ndev->targets[i];
0902             break;
0903         }
0904     }
0905 
0906     if (!nci_target) {
0907         pr_err("unable to find the selected target\n");
0908         return -EINVAL;
0909     }
0910 
0911     if (!(nci_target->supported_protocols & (1 << protocol))) {
0912         pr_err("target does not support the requested protocol 0x%x\n",
0913                protocol);
0914         return -EINVAL;
0915     }
0916 
0917     if (atomic_read(&ndev->state) == NCI_W4_HOST_SELECT) {
0918         param.rf_discovery_id = nci_target->logical_idx;
0919 
0920         if (protocol == NFC_PROTO_JEWEL)
0921             param.rf_protocol = NCI_RF_PROTOCOL_T1T;
0922         else if (protocol == NFC_PROTO_MIFARE)
0923             param.rf_protocol = NCI_RF_PROTOCOL_T2T;
0924         else if (protocol == NFC_PROTO_FELICA)
0925             param.rf_protocol = NCI_RF_PROTOCOL_T3T;
0926         else if (protocol == NFC_PROTO_ISO14443 ||
0927              protocol == NFC_PROTO_ISO14443_B)
0928             param.rf_protocol = NCI_RF_PROTOCOL_ISO_DEP;
0929         else
0930             param.rf_protocol = NCI_RF_PROTOCOL_NFC_DEP;
0931 
0932         rc = nci_request(ndev, nci_rf_discover_select_req, &param,
0933                  msecs_to_jiffies(NCI_RF_DISC_SELECT_TIMEOUT));
0934     }
0935 
0936     if (!rc)
0937         ndev->target_active_prot = protocol;
0938 
0939     return rc;
0940 }
0941 
0942 static void nci_deactivate_target(struct nfc_dev *nfc_dev,
0943                   struct nfc_target *target,
0944                   __u8 mode)
0945 {
0946     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
0947     unsigned long nci_mode = NCI_DEACTIVATE_TYPE_IDLE_MODE;
0948 
0949     if (!ndev->target_active_prot) {
0950         pr_err("unable to deactivate target, no active target\n");
0951         return;
0952     }
0953 
0954     ndev->target_active_prot = 0;
0955 
0956     switch (mode) {
0957     case NFC_TARGET_MODE_SLEEP:
0958         nci_mode = NCI_DEACTIVATE_TYPE_SLEEP_MODE;
0959         break;
0960     }
0961 
0962     if (atomic_read(&ndev->state) == NCI_POLL_ACTIVE) {
0963         nci_request(ndev, nci_rf_deactivate_req, (void *)nci_mode,
0964                 msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
0965     }
0966 }
0967 
0968 static int nci_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
0969                __u8 comm_mode, __u8 *gb, size_t gb_len)
0970 {
0971     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
0972     int rc;
0973 
0974     pr_debug("target_idx %d, comm_mode %d\n", target->idx, comm_mode);
0975 
0976     rc = nci_activate_target(nfc_dev, target, NFC_PROTO_NFC_DEP);
0977     if (rc)
0978         return rc;
0979 
0980     rc = nfc_set_remote_general_bytes(nfc_dev, ndev->remote_gb,
0981                       ndev->remote_gb_len);
0982     if (!rc)
0983         rc = nfc_dep_link_is_up(nfc_dev, target->idx, NFC_COMM_PASSIVE,
0984                     NFC_RF_INITIATOR);
0985 
0986     return rc;
0987 }
0988 
0989 static int nci_dep_link_down(struct nfc_dev *nfc_dev)
0990 {
0991     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
0992     int rc;
0993 
0994     if (nfc_dev->rf_mode == NFC_RF_INITIATOR) {
0995         nci_deactivate_target(nfc_dev, NULL, NCI_DEACTIVATE_TYPE_IDLE_MODE);
0996     } else {
0997         if (atomic_read(&ndev->state) == NCI_LISTEN_ACTIVE ||
0998             atomic_read(&ndev->state) == NCI_DISCOVERY) {
0999             nci_request(ndev, nci_rf_deactivate_req, (void *)0,
1000                     msecs_to_jiffies(NCI_RF_DEACTIVATE_TIMEOUT));
1001         }
1002 
1003         rc = nfc_tm_deactivated(nfc_dev);
1004         if (rc)
1005             pr_err("error when signaling tm deactivation\n");
1006     }
1007 
1008     return 0;
1009 }
1010 
1011 
1012 static int nci_transceive(struct nfc_dev *nfc_dev, struct nfc_target *target,
1013               struct sk_buff *skb,
1014               data_exchange_cb_t cb, void *cb_context)
1015 {
1016     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1017     int rc;
1018     struct nci_conn_info *conn_info;
1019 
1020     conn_info = ndev->rf_conn_info;
1021     if (!conn_info)
1022         return -EPROTO;
1023 
1024     pr_debug("target_idx %d, len %d\n", target->idx, skb->len);
1025 
1026     if (!ndev->target_active_prot) {
1027         pr_err("unable to exchange data, no active target\n");
1028         return -EINVAL;
1029     }
1030 
1031     if (test_and_set_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1032         return -EBUSY;
1033 
1034     /* store cb and context to be used on receiving data */
1035     conn_info->data_exchange_cb = cb;
1036     conn_info->data_exchange_cb_context = cb_context;
1037 
1038     rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1039     if (rc)
1040         clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
1041 
1042     return rc;
1043 }
1044 
1045 static int nci_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
1046 {
1047     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1048     int rc;
1049 
1050     rc = nci_send_data(ndev, NCI_STATIC_RF_CONN_ID, skb);
1051     if (rc)
1052         pr_err("unable to send data\n");
1053 
1054     return rc;
1055 }
1056 
1057 static int nci_enable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1058 {
1059     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1060 
1061     if (ndev->ops->enable_se)
1062         return ndev->ops->enable_se(ndev, se_idx);
1063 
1064     return 0;
1065 }
1066 
1067 static int nci_disable_se(struct nfc_dev *nfc_dev, u32 se_idx)
1068 {
1069     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1070 
1071     if (ndev->ops->disable_se)
1072         return ndev->ops->disable_se(ndev, se_idx);
1073 
1074     return 0;
1075 }
1076 
1077 static int nci_discover_se(struct nfc_dev *nfc_dev)
1078 {
1079     int r;
1080     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1081 
1082     if (ndev->ops->discover_se) {
1083         r = nci_nfcee_discover(ndev, NCI_NFCEE_DISCOVERY_ACTION_ENABLE);
1084         if (r != NCI_STATUS_OK)
1085             return -EPROTO;
1086 
1087         return ndev->ops->discover_se(ndev);
1088     }
1089 
1090     return 0;
1091 }
1092 
1093 static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
1094              u8 *apdu, size_t apdu_length,
1095              se_io_cb_t cb, void *cb_context)
1096 {
1097     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1098 
1099     if (ndev->ops->se_io)
1100         return ndev->ops->se_io(ndev, se_idx, apdu,
1101                 apdu_length, cb, cb_context);
1102 
1103     return 0;
1104 }
1105 
1106 static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
1107 {
1108     struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
1109 
1110     if (!ndev->ops->fw_download)
1111         return -ENOTSUPP;
1112 
1113     return ndev->ops->fw_download(ndev, firmware_name);
1114 }
1115 
1116 static const struct nfc_ops nci_nfc_ops = {
1117     .dev_up = nci_dev_up,
1118     .dev_down = nci_dev_down,
1119     .start_poll = nci_start_poll,
1120     .stop_poll = nci_stop_poll,
1121     .dep_link_up = nci_dep_link_up,
1122     .dep_link_down = nci_dep_link_down,
1123     .activate_target = nci_activate_target,
1124     .deactivate_target = nci_deactivate_target,
1125     .im_transceive = nci_transceive,
1126     .tm_send = nci_tm_send,
1127     .enable_se = nci_enable_se,
1128     .disable_se = nci_disable_se,
1129     .discover_se = nci_discover_se,
1130     .se_io = nci_se_io,
1131     .fw_download = nci_fw_download,
1132 };
1133 
1134 /* ---- Interface to NCI drivers ---- */
1135 /**
1136  * nci_allocate_device - allocate a new nci device
1137  *
1138  * @ops: device operations
1139  * @supported_protocols: NFC protocols supported by the device
1140  * @tx_headroom: Reserved space at beginning of skb
1141  * @tx_tailroom: Reserved space at end of skb
1142  */
1143 struct nci_dev *nci_allocate_device(const struct nci_ops *ops,
1144                     __u32 supported_protocols,
1145                     int tx_headroom, int tx_tailroom)
1146 {
1147     struct nci_dev *ndev;
1148 
1149     pr_debug("supported_protocols 0x%x\n", supported_protocols);
1150 
1151     if (!ops->open || !ops->close || !ops->send)
1152         return NULL;
1153 
1154     if (!supported_protocols)
1155         return NULL;
1156 
1157     ndev = kzalloc(sizeof(struct nci_dev), GFP_KERNEL);
1158     if (!ndev)
1159         return NULL;
1160 
1161     ndev->ops = ops;
1162 
1163     if (ops->n_prop_ops > NCI_MAX_PROPRIETARY_CMD) {
1164         pr_err("Too many proprietary commands: %zd\n",
1165                ops->n_prop_ops);
1166         goto free_nci;
1167     }
1168 
1169     ndev->tx_headroom = tx_headroom;
1170     ndev->tx_tailroom = tx_tailroom;
1171     init_completion(&ndev->req_completion);
1172 
1173     ndev->nfc_dev = nfc_allocate_device(&nci_nfc_ops,
1174                         supported_protocols,
1175                         tx_headroom + NCI_DATA_HDR_SIZE,
1176                         tx_tailroom);
1177     if (!ndev->nfc_dev)
1178         goto free_nci;
1179 
1180     ndev->hci_dev = nci_hci_allocate(ndev);
1181     if (!ndev->hci_dev)
1182         goto free_nfc;
1183 
1184     nfc_set_drvdata(ndev->nfc_dev, ndev);
1185 
1186     return ndev;
1187 
1188 free_nfc:
1189     nfc_free_device(ndev->nfc_dev);
1190 free_nci:
1191     kfree(ndev);
1192     return NULL;
1193 }
1194 EXPORT_SYMBOL(nci_allocate_device);
1195 
1196 /**
1197  * nci_free_device - deallocate nci device
1198  *
1199  * @ndev: The nci device to deallocate
1200  */
1201 void nci_free_device(struct nci_dev *ndev)
1202 {
1203     nfc_free_device(ndev->nfc_dev);
1204     nci_hci_deallocate(ndev);
1205     kfree(ndev);
1206 }
1207 EXPORT_SYMBOL(nci_free_device);
1208 
1209 /**
1210  * nci_register_device - register a nci device in the nfc subsystem
1211  *
1212  * @ndev: The nci device to register
1213  */
1214 int nci_register_device(struct nci_dev *ndev)
1215 {
1216     int rc;
1217     struct device *dev = &ndev->nfc_dev->dev;
1218     char name[32];
1219 
1220     ndev->flags = 0;
1221 
1222     INIT_WORK(&ndev->cmd_work, nci_cmd_work);
1223     snprintf(name, sizeof(name), "%s_nci_cmd_wq", dev_name(dev));
1224     ndev->cmd_wq = create_singlethread_workqueue(name);
1225     if (!ndev->cmd_wq) {
1226         rc = -ENOMEM;
1227         goto exit;
1228     }
1229 
1230     INIT_WORK(&ndev->rx_work, nci_rx_work);
1231     snprintf(name, sizeof(name), "%s_nci_rx_wq", dev_name(dev));
1232     ndev->rx_wq = create_singlethread_workqueue(name);
1233     if (!ndev->rx_wq) {
1234         rc = -ENOMEM;
1235         goto destroy_cmd_wq_exit;
1236     }
1237 
1238     INIT_WORK(&ndev->tx_work, nci_tx_work);
1239     snprintf(name, sizeof(name), "%s_nci_tx_wq", dev_name(dev));
1240     ndev->tx_wq = create_singlethread_workqueue(name);
1241     if (!ndev->tx_wq) {
1242         rc = -ENOMEM;
1243         goto destroy_rx_wq_exit;
1244     }
1245 
1246     skb_queue_head_init(&ndev->cmd_q);
1247     skb_queue_head_init(&ndev->rx_q);
1248     skb_queue_head_init(&ndev->tx_q);
1249 
1250     timer_setup(&ndev->cmd_timer, nci_cmd_timer, 0);
1251     timer_setup(&ndev->data_timer, nci_data_timer, 0);
1252 
1253     mutex_init(&ndev->req_lock);
1254     INIT_LIST_HEAD(&ndev->conn_info_list);
1255 
1256     rc = nfc_register_device(ndev->nfc_dev);
1257     if (rc)
1258         goto destroy_tx_wq_exit;
1259 
1260     goto exit;
1261 
1262 destroy_tx_wq_exit:
1263     destroy_workqueue(ndev->tx_wq);
1264 
1265 destroy_rx_wq_exit:
1266     destroy_workqueue(ndev->rx_wq);
1267 
1268 destroy_cmd_wq_exit:
1269     destroy_workqueue(ndev->cmd_wq);
1270 
1271 exit:
1272     return rc;
1273 }
1274 EXPORT_SYMBOL(nci_register_device);
1275 
1276 /**
1277  * nci_unregister_device - unregister a nci device in the nfc subsystem
1278  *
1279  * @ndev: The nci device to unregister
1280  */
1281 void nci_unregister_device(struct nci_dev *ndev)
1282 {
1283     struct nci_conn_info *conn_info, *n;
1284 
1285     /* This set_bit is not protected with specialized barrier,
1286      * However, it is fine because the mutex_lock(&ndev->req_lock);
1287      * in nci_close_device() will help to emit one.
1288      */
1289     set_bit(NCI_UNREG, &ndev->flags);
1290 
1291     nci_close_device(ndev);
1292 
1293     destroy_workqueue(ndev->cmd_wq);
1294     destroy_workqueue(ndev->rx_wq);
1295     destroy_workqueue(ndev->tx_wq);
1296 
1297     list_for_each_entry_safe(conn_info, n, &ndev->conn_info_list, list) {
1298         list_del(&conn_info->list);
1299         /* conn_info is allocated with devm_kzalloc */
1300     }
1301 
1302     nfc_unregister_device(ndev->nfc_dev);
1303 }
1304 EXPORT_SYMBOL(nci_unregister_device);
1305 
1306 /**
1307  * nci_recv_frame - receive frame from NCI drivers
1308  *
1309  * @ndev: The nci device
1310  * @skb: The sk_buff to receive
1311  */
1312 int nci_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
1313 {
1314     pr_debug("len %d\n", skb->len);
1315 
1316     if (!ndev || (!test_bit(NCI_UP, &ndev->flags) &&
1317         !test_bit(NCI_INIT, &ndev->flags))) {
1318         kfree_skb(skb);
1319         return -ENXIO;
1320     }
1321 
1322     /* Queue frame for rx worker thread */
1323     skb_queue_tail(&ndev->rx_q, skb);
1324     queue_work(ndev->rx_wq, &ndev->rx_work);
1325 
1326     return 0;
1327 }
1328 EXPORT_SYMBOL(nci_recv_frame);
1329 
1330 int nci_send_frame(struct nci_dev *ndev, struct sk_buff *skb)
1331 {
1332     pr_debug("len %d\n", skb->len);
1333 
1334     if (!ndev) {
1335         kfree_skb(skb);
1336         return -ENODEV;
1337     }
1338 
1339     /* Get rid of skb owner, prior to sending to the driver. */
1340     skb_orphan(skb);
1341 
1342     /* Send copy to sniffer */
1343     nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1344                  RAW_PAYLOAD_NCI, NFC_DIRECTION_TX);
1345 
1346     return ndev->ops->send(ndev, skb);
1347 }
1348 EXPORT_SYMBOL(nci_send_frame);
1349 
1350 /* Send NCI command */
1351 int nci_send_cmd(struct nci_dev *ndev, __u16 opcode, __u8 plen, const void *payload)
1352 {
1353     struct nci_ctrl_hdr *hdr;
1354     struct sk_buff *skb;
1355 
1356     pr_debug("opcode 0x%x, plen %d\n", opcode, plen);
1357 
1358     skb = nci_skb_alloc(ndev, (NCI_CTRL_HDR_SIZE + plen), GFP_KERNEL);
1359     if (!skb) {
1360         pr_err("no memory for command\n");
1361         return -ENOMEM;
1362     }
1363 
1364     hdr = skb_put(skb, NCI_CTRL_HDR_SIZE);
1365     hdr->gid = nci_opcode_gid(opcode);
1366     hdr->oid = nci_opcode_oid(opcode);
1367     hdr->plen = plen;
1368 
1369     nci_mt_set((__u8 *)hdr, NCI_MT_CMD_PKT);
1370     nci_pbf_set((__u8 *)hdr, NCI_PBF_LAST);
1371 
1372     if (plen)
1373         skb_put_data(skb, payload, plen);
1374 
1375     skb_queue_tail(&ndev->cmd_q, skb);
1376     queue_work(ndev->cmd_wq, &ndev->cmd_work);
1377 
1378     return 0;
1379 }
1380 EXPORT_SYMBOL(nci_send_cmd);
1381 
1382 /* Proprietary commands API */
1383 static const struct nci_driver_ops *ops_cmd_lookup(const struct nci_driver_ops *ops,
1384                            size_t n_ops,
1385                            __u16 opcode)
1386 {
1387     size_t i;
1388     const struct nci_driver_ops *op;
1389 
1390     if (!ops || !n_ops)
1391         return NULL;
1392 
1393     for (i = 0; i < n_ops; i++) {
1394         op = &ops[i];
1395         if (op->opcode == opcode)
1396             return op;
1397     }
1398 
1399     return NULL;
1400 }
1401 
1402 static int nci_op_rsp_packet(struct nci_dev *ndev, __u16 rsp_opcode,
1403                  struct sk_buff *skb, const struct nci_driver_ops *ops,
1404                  size_t n_ops)
1405 {
1406     const struct nci_driver_ops *op;
1407 
1408     op = ops_cmd_lookup(ops, n_ops, rsp_opcode);
1409     if (!op || !op->rsp)
1410         return -ENOTSUPP;
1411 
1412     return op->rsp(ndev, skb);
1413 }
1414 
1415 static int nci_op_ntf_packet(struct nci_dev *ndev, __u16 ntf_opcode,
1416                  struct sk_buff *skb, const struct nci_driver_ops *ops,
1417                  size_t n_ops)
1418 {
1419     const struct nci_driver_ops *op;
1420 
1421     op = ops_cmd_lookup(ops, n_ops, ntf_opcode);
1422     if (!op || !op->ntf)
1423         return -ENOTSUPP;
1424 
1425     return op->ntf(ndev, skb);
1426 }
1427 
1428 int nci_prop_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1429             struct sk_buff *skb)
1430 {
1431     return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1432                  ndev->ops->n_prop_ops);
1433 }
1434 
1435 int nci_prop_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1436             struct sk_buff *skb)
1437 {
1438     return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->prop_ops,
1439                  ndev->ops->n_prop_ops);
1440 }
1441 
1442 int nci_core_rsp_packet(struct nci_dev *ndev, __u16 opcode,
1443             struct sk_buff *skb)
1444 {
1445     return nci_op_rsp_packet(ndev, opcode, skb, ndev->ops->core_ops,
1446                   ndev->ops->n_core_ops);
1447 }
1448 
1449 int nci_core_ntf_packet(struct nci_dev *ndev, __u16 opcode,
1450             struct sk_buff *skb)
1451 {
1452     return nci_op_ntf_packet(ndev, opcode, skb, ndev->ops->core_ops,
1453                  ndev->ops->n_core_ops);
1454 }
1455 
1456 /* ---- NCI TX Data worker thread ---- */
1457 
1458 static void nci_tx_work(struct work_struct *work)
1459 {
1460     struct nci_dev *ndev = container_of(work, struct nci_dev, tx_work);
1461     struct nci_conn_info *conn_info;
1462     struct sk_buff *skb;
1463 
1464     conn_info = nci_get_conn_info_by_conn_id(ndev, ndev->cur_conn_id);
1465     if (!conn_info)
1466         return;
1467 
1468     pr_debug("credits_cnt %d\n", atomic_read(&conn_info->credits_cnt));
1469 
1470     /* Send queued tx data */
1471     while (atomic_read(&conn_info->credits_cnt)) {
1472         skb = skb_dequeue(&ndev->tx_q);
1473         if (!skb)
1474             return;
1475 
1476         /* Check if data flow control is used */
1477         if (atomic_read(&conn_info->credits_cnt) !=
1478             NCI_DATA_FLOW_CONTROL_NOT_USED)
1479             atomic_dec(&conn_info->credits_cnt);
1480 
1481         pr_debug("NCI TX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
1482              nci_pbf(skb->data),
1483              nci_conn_id(skb->data),
1484              nci_plen(skb->data));
1485 
1486         nci_send_frame(ndev, skb);
1487 
1488         mod_timer(&ndev->data_timer,
1489               jiffies + msecs_to_jiffies(NCI_DATA_TIMEOUT));
1490     }
1491 }
1492 
1493 /* ----- NCI RX worker thread (data & control) ----- */
1494 
1495 static void nci_rx_work(struct work_struct *work)
1496 {
1497     struct nci_dev *ndev = container_of(work, struct nci_dev, rx_work);
1498     struct sk_buff *skb;
1499 
1500     while ((skb = skb_dequeue(&ndev->rx_q))) {
1501 
1502         /* Send copy to sniffer */
1503         nfc_send_to_raw_sock(ndev->nfc_dev, skb,
1504                      RAW_PAYLOAD_NCI, NFC_DIRECTION_RX);
1505 
1506         /* Process frame */
1507         switch (nci_mt(skb->data)) {
1508         case NCI_MT_RSP_PKT:
1509             nci_rsp_packet(ndev, skb);
1510             break;
1511 
1512         case NCI_MT_NTF_PKT:
1513             nci_ntf_packet(ndev, skb);
1514             break;
1515 
1516         case NCI_MT_DATA_PKT:
1517             nci_rx_data_packet(ndev, skb);
1518             break;
1519 
1520         default:
1521             pr_err("unknown MT 0x%x\n", nci_mt(skb->data));
1522             kfree_skb(skb);
1523             break;
1524         }
1525     }
1526 
1527     /* check if a data exchange timeout has occurred */
1528     if (test_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags)) {
1529         /* complete the data exchange transaction, if exists */
1530         if (test_bit(NCI_DATA_EXCHANGE, &ndev->flags))
1531             nci_data_exchange_complete(ndev, NULL,
1532                            ndev->cur_conn_id,
1533                            -ETIMEDOUT);
1534 
1535         clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
1536     }
1537 }
1538 
1539 /* ----- NCI TX CMD worker thread ----- */
1540 
1541 static void nci_cmd_work(struct work_struct *work)
1542 {
1543     struct nci_dev *ndev = container_of(work, struct nci_dev, cmd_work);
1544     struct sk_buff *skb;
1545 
1546     pr_debug("cmd_cnt %d\n", atomic_read(&ndev->cmd_cnt));
1547 
1548     /* Send queued command */
1549     if (atomic_read(&ndev->cmd_cnt)) {
1550         skb = skb_dequeue(&ndev->cmd_q);
1551         if (!skb)
1552             return;
1553 
1554         atomic_dec(&ndev->cmd_cnt);
1555 
1556         pr_debug("NCI TX: MT=cmd, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
1557              nci_pbf(skb->data),
1558              nci_opcode_gid(nci_opcode(skb->data)),
1559              nci_opcode_oid(nci_opcode(skb->data)),
1560              nci_plen(skb->data));
1561 
1562         nci_send_frame(ndev, skb);
1563 
1564         mod_timer(&ndev->cmd_timer,
1565               jiffies + msecs_to_jiffies(NCI_CMD_TIMEOUT));
1566     }
1567 }
1568 
1569 MODULE_LICENSE("GPL");