Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Generic driver for NXP NCI NFC chips
0004  *
0005  * Copyright (C) 2014  NXP Semiconductors  All rights reserved.
0006  *
0007  * Author: Clément Perrochaud <clement.perrochaud@nxp.com>
0008  *
0009  * Derived from PN544 device driver:
0010  * Copyright (C) 2012  Intel Corporation. All rights reserved.
0011  */
0012 
0013 #include <linux/completion.h>
0014 #include <linux/firmware.h>
0015 #include <linux/nfc.h>
0016 #include <asm/unaligned.h>
0017 
0018 #include "nxp-nci.h"
0019 
0020 /* Crypto operations can take up to 30 seconds */
0021 #define NXP_NCI_FW_ANSWER_TIMEOUT   msecs_to_jiffies(30000)
0022 
0023 #define NXP_NCI_FW_CMD_RESET        0xF0
0024 #define NXP_NCI_FW_CMD_GETVERSION   0xF1
0025 #define NXP_NCI_FW_CMD_CHECKINTEGRITY   0xE0
0026 #define NXP_NCI_FW_CMD_WRITE        0xC0
0027 #define NXP_NCI_FW_CMD_READ     0xA2
0028 #define NXP_NCI_FW_CMD_GETSESSIONSTATE  0xF2
0029 #define NXP_NCI_FW_CMD_LOG      0xA7
0030 #define NXP_NCI_FW_CMD_FORCE        0xD0
0031 #define NXP_NCI_FW_CMD_GET_DIE_ID   0xF4
0032 
0033 #define NXP_NCI_FW_CHUNK_FLAG   0x0400
0034 
0035 #define NXP_NCI_FW_RESULT_OK                0x00
0036 #define NXP_NCI_FW_RESULT_INVALID_ADDR          0x01
0037 #define NXP_NCI_FW_RESULT_GENERIC_ERROR         0x02
0038 #define NXP_NCI_FW_RESULT_UNKNOWN_CMD           0x0B
0039 #define NXP_NCI_FW_RESULT_ABORTED_CMD           0x0C
0040 #define NXP_NCI_FW_RESULT_PLL_ERROR         0x0D
0041 #define NXP_NCI_FW_RESULT_ADDR_RANGE_OFL_ERROR      0x1E
0042 #define NXP_NCI_FW_RESULT_BUFFER_OFL_ERROR      0x1F
0043 #define NXP_NCI_FW_RESULT_MEM_BSY           0x20
0044 #define NXP_NCI_FW_RESULT_SIGNATURE_ERROR       0x21
0045 #define NXP_NCI_FW_RESULT_FIRMWARE_VERSION_ERROR    0x24
0046 #define NXP_NCI_FW_RESULT_PROTOCOL_ERROR        0x28
0047 #define NXP_NCI_FW_RESULT_SFWU_DEGRADED         0x2A
0048 #define NXP_NCI_FW_RESULT_PH_STATUS_FIRST_CHUNK     0x2D
0049 #define NXP_NCI_FW_RESULT_PH_STATUS_NEXT_CHUNK      0x2E
0050 #define NXP_NCI_FW_RESULT_PH_STATUS_INTERNAL_ERROR_5    0xC5
0051 
0052 void nxp_nci_fw_work_complete(struct nxp_nci_info *info, int result)
0053 {
0054     struct nxp_nci_fw_info *fw_info = &info->fw_info;
0055     int r;
0056 
0057     if (info->phy_ops->set_mode) {
0058         r = info->phy_ops->set_mode(info->phy_id, NXP_NCI_MODE_COLD);
0059         if (r < 0 && result == 0)
0060             result = -r;
0061     }
0062 
0063     info->mode = NXP_NCI_MODE_COLD;
0064 
0065     if (fw_info->fw) {
0066         release_firmware(fw_info->fw);
0067         fw_info->fw = NULL;
0068     }
0069 
0070     nfc_fw_download_done(info->ndev->nfc_dev, fw_info->name, (u32) -result);
0071 }
0072 
0073 /* crc_ccitt cannot be used since it is computed MSB first and not LSB first */
0074 static u16 nxp_nci_fw_crc(u8 const *buffer, size_t len)
0075 {
0076     u16 crc = 0xffff;
0077 
0078     while (len--) {
0079         crc = ((crc >> 8) | (crc << 8)) ^ *buffer++;
0080         crc ^= (crc & 0xff) >> 4;
0081         crc ^= (crc & 0xff) << 12;
0082         crc ^= (crc & 0xff) << 5;
0083     }
0084 
0085     return crc;
0086 }
0087 
0088 static int nxp_nci_fw_send_chunk(struct nxp_nci_info *info)
0089 {
0090     struct nxp_nci_fw_info *fw_info = &info->fw_info;
0091     u16 header, crc;
0092     struct sk_buff *skb;
0093     size_t chunk_len;
0094     size_t remaining_len;
0095     int r;
0096 
0097     skb = nci_skb_alloc(info->ndev, info->max_payload, GFP_KERNEL);
0098     if (!skb)
0099         return -ENOMEM;
0100 
0101     chunk_len = info->max_payload - NXP_NCI_FW_HDR_LEN - NXP_NCI_FW_CRC_LEN;
0102     remaining_len = fw_info->frame_size - fw_info->written;
0103 
0104     if (remaining_len > chunk_len) {
0105         header = NXP_NCI_FW_CHUNK_FLAG;
0106     } else {
0107         chunk_len = remaining_len;
0108         header = 0x0000;
0109     }
0110 
0111     header |= chunk_len & NXP_NCI_FW_FRAME_LEN_MASK;
0112     put_unaligned_be16(header, skb_put(skb, NXP_NCI_FW_HDR_LEN));
0113 
0114     skb_put_data(skb, fw_info->data + fw_info->written, chunk_len);
0115 
0116     crc = nxp_nci_fw_crc(skb->data, chunk_len + NXP_NCI_FW_HDR_LEN);
0117     put_unaligned_be16(crc, skb_put(skb, NXP_NCI_FW_CRC_LEN));
0118 
0119     r = info->phy_ops->write(info->phy_id, skb);
0120     if (r >= 0)
0121         r = chunk_len;
0122 
0123     kfree_skb(skb);
0124 
0125     return r;
0126 }
0127 
0128 static int nxp_nci_fw_send(struct nxp_nci_info *info)
0129 {
0130     struct nxp_nci_fw_info *fw_info = &info->fw_info;
0131     long completion_rc;
0132     int r;
0133 
0134     reinit_completion(&fw_info->cmd_completion);
0135 
0136     if (fw_info->written == 0) {
0137         fw_info->frame_size = get_unaligned_be16(fw_info->data) &
0138                       NXP_NCI_FW_FRAME_LEN_MASK;
0139         fw_info->data += NXP_NCI_FW_HDR_LEN;
0140         fw_info->size -= NXP_NCI_FW_HDR_LEN;
0141     }
0142 
0143     if (fw_info->frame_size > fw_info->size)
0144         return -EMSGSIZE;
0145 
0146     r = nxp_nci_fw_send_chunk(info);
0147     if (r < 0)
0148         return r;
0149 
0150     fw_info->written += r;
0151 
0152     if (*fw_info->data == NXP_NCI_FW_CMD_RESET) {
0153         fw_info->cmd_result = 0;
0154         if (fw_info->fw)
0155             schedule_work(&fw_info->work);
0156     } else {
0157         completion_rc = wait_for_completion_interruptible_timeout(
0158             &fw_info->cmd_completion, NXP_NCI_FW_ANSWER_TIMEOUT);
0159         if (completion_rc == 0)
0160             return -ETIMEDOUT;
0161     }
0162 
0163     return 0;
0164 }
0165 
0166 void nxp_nci_fw_work(struct work_struct *work)
0167 {
0168     struct nxp_nci_info *info;
0169     struct nxp_nci_fw_info *fw_info;
0170     int r;
0171 
0172     fw_info = container_of(work, struct nxp_nci_fw_info, work);
0173     info = container_of(fw_info, struct nxp_nci_info, fw_info);
0174 
0175     mutex_lock(&info->info_lock);
0176 
0177     r = fw_info->cmd_result;
0178     if (r < 0)
0179         goto exit_work;
0180 
0181     if (fw_info->written == fw_info->frame_size) {
0182         fw_info->data += fw_info->frame_size;
0183         fw_info->size -= fw_info->frame_size;
0184         fw_info->written = 0;
0185     }
0186 
0187     if (fw_info->size > 0)
0188         r = nxp_nci_fw_send(info);
0189 
0190 exit_work:
0191     if (r < 0 || fw_info->size == 0)
0192         nxp_nci_fw_work_complete(info, r);
0193     mutex_unlock(&info->info_lock);
0194 }
0195 
0196 int nxp_nci_fw_download(struct nci_dev *ndev, const char *firmware_name)
0197 {
0198     struct nxp_nci_info *info = nci_get_drvdata(ndev);
0199     struct nxp_nci_fw_info *fw_info = &info->fw_info;
0200     int r;
0201 
0202     mutex_lock(&info->info_lock);
0203 
0204     if (!info->phy_ops->set_mode || !info->phy_ops->write) {
0205         r = -ENOTSUPP;
0206         goto fw_download_exit;
0207     }
0208 
0209     if (!firmware_name || firmware_name[0] == '\0') {
0210         r = -EINVAL;
0211         goto fw_download_exit;
0212     }
0213 
0214     strcpy(fw_info->name, firmware_name);
0215 
0216     r = request_firmware(&fw_info->fw, firmware_name,
0217                  ndev->nfc_dev->dev.parent);
0218     if (r < 0)
0219         goto fw_download_exit;
0220 
0221     r = info->phy_ops->set_mode(info->phy_id, NXP_NCI_MODE_FW);
0222     if (r < 0) {
0223         release_firmware(fw_info->fw);
0224         goto fw_download_exit;
0225     }
0226 
0227     info->mode = NXP_NCI_MODE_FW;
0228 
0229     fw_info->data = fw_info->fw->data;
0230     fw_info->size = fw_info->fw->size;
0231     fw_info->written = 0;
0232     fw_info->frame_size = 0;
0233     fw_info->cmd_result = 0;
0234 
0235     schedule_work(&fw_info->work);
0236 
0237 fw_download_exit:
0238     mutex_unlock(&info->info_lock);
0239     return r;
0240 }
0241 
0242 static int nxp_nci_fw_read_status(u8 stat)
0243 {
0244     switch (stat) {
0245     case NXP_NCI_FW_RESULT_OK:
0246         return 0;
0247     case NXP_NCI_FW_RESULT_INVALID_ADDR:
0248         return -EINVAL;
0249     case NXP_NCI_FW_RESULT_UNKNOWN_CMD:
0250         return -EINVAL;
0251     case NXP_NCI_FW_RESULT_ABORTED_CMD:
0252         return -EMSGSIZE;
0253     case NXP_NCI_FW_RESULT_ADDR_RANGE_OFL_ERROR:
0254         return -EADDRNOTAVAIL;
0255     case NXP_NCI_FW_RESULT_BUFFER_OFL_ERROR:
0256         return -ENOBUFS;
0257     case NXP_NCI_FW_RESULT_MEM_BSY:
0258         return -ENOKEY;
0259     case NXP_NCI_FW_RESULT_SIGNATURE_ERROR:
0260         return -EKEYREJECTED;
0261     case NXP_NCI_FW_RESULT_FIRMWARE_VERSION_ERROR:
0262         return -EALREADY;
0263     case NXP_NCI_FW_RESULT_PROTOCOL_ERROR:
0264         return -EPROTO;
0265     case NXP_NCI_FW_RESULT_SFWU_DEGRADED:
0266         return -EHWPOISON;
0267     case NXP_NCI_FW_RESULT_PH_STATUS_FIRST_CHUNK:
0268         return 0;
0269     case NXP_NCI_FW_RESULT_PH_STATUS_NEXT_CHUNK:
0270         return 0;
0271     case NXP_NCI_FW_RESULT_PH_STATUS_INTERNAL_ERROR_5:
0272         return -EINVAL;
0273     default:
0274         return -EIO;
0275     }
0276 }
0277 
0278 static u16 nxp_nci_fw_check_crc(struct sk_buff *skb)
0279 {
0280     u16 crc, frame_crc;
0281     size_t len = skb->len - NXP_NCI_FW_CRC_LEN;
0282 
0283     crc = nxp_nci_fw_crc(skb->data, len);
0284     frame_crc = get_unaligned_be16(skb->data + len);
0285 
0286     return (crc ^ frame_crc);
0287 }
0288 
0289 void nxp_nci_fw_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
0290 {
0291     struct nxp_nci_info *info = nci_get_drvdata(ndev);
0292     struct nxp_nci_fw_info *fw_info = &info->fw_info;
0293 
0294     complete(&fw_info->cmd_completion);
0295 
0296     if (skb) {
0297         if (nxp_nci_fw_check_crc(skb) != 0x00)
0298             fw_info->cmd_result = -EBADMSG;
0299         else
0300             fw_info->cmd_result = nxp_nci_fw_read_status(*(u8 *)skb_pull(skb, NXP_NCI_FW_HDR_LEN));
0301         kfree_skb(skb);
0302     } else {
0303         fw_info->cmd_result = -EIO;
0304     }
0305 
0306     if (fw_info->fw)
0307         schedule_work(&fw_info->work);
0308 }
0309 EXPORT_SYMBOL(nxp_nci_fw_recv_frame);