firmware.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Generic driver for NXP NCI NFC chips
  3. *
  4. * Copyright (C) 2014 NXP Semiconductors All rights reserved.
  5. *
  6. * Author: Clément Perrochaud <clement.perrochaud@nxp.com>
  7. *
  8. * Derived from PN544 device driver:
  9. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms and conditions of the GNU General Public License,
  13. * version 2, as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <linux/completion.h>
  24. #include <linux/firmware.h>
  25. #include <linux/nfc.h>
  26. #include <linux/unaligned/access_ok.h>
  27. #include "nxp-nci.h"
  28. /* Crypto operations can take up to 30 seconds */
  29. #define NXP_NCI_FW_ANSWER_TIMEOUT msecs_to_jiffies(30000)
  30. #define NXP_NCI_FW_CMD_RESET 0xF0
  31. #define NXP_NCI_FW_CMD_GETVERSION 0xF1
  32. #define NXP_NCI_FW_CMD_CHECKINTEGRITY 0xE0
  33. #define NXP_NCI_FW_CMD_WRITE 0xC0
  34. #define NXP_NCI_FW_CMD_READ 0xA2
  35. #define NXP_NCI_FW_CMD_GETSESSIONSTATE 0xF2
  36. #define NXP_NCI_FW_CMD_LOG 0xA7
  37. #define NXP_NCI_FW_CMD_FORCE 0xD0
  38. #define NXP_NCI_FW_CMD_GET_DIE_ID 0xF4
  39. #define NXP_NCI_FW_CHUNK_FLAG 0x0400
  40. #define NXP_NCI_FW_RESULT_OK 0x00
  41. #define NXP_NCI_FW_RESULT_INVALID_ADDR 0x01
  42. #define NXP_NCI_FW_RESULT_GENERIC_ERROR 0x02
  43. #define NXP_NCI_FW_RESULT_UNKNOWN_CMD 0x0B
  44. #define NXP_NCI_FW_RESULT_ABORTED_CMD 0x0C
  45. #define NXP_NCI_FW_RESULT_PLL_ERROR 0x0D
  46. #define NXP_NCI_FW_RESULT_ADDR_RANGE_OFL_ERROR 0x1E
  47. #define NXP_NCI_FW_RESULT_BUFFER_OFL_ERROR 0x1F
  48. #define NXP_NCI_FW_RESULT_MEM_BSY 0x20
  49. #define NXP_NCI_FW_RESULT_SIGNATURE_ERROR 0x21
  50. #define NXP_NCI_FW_RESULT_FIRMWARE_VERSION_ERROR 0x24
  51. #define NXP_NCI_FW_RESULT_PROTOCOL_ERROR 0x28
  52. #define NXP_NCI_FW_RESULT_SFWU_DEGRADED 0x2A
  53. #define NXP_NCI_FW_RESULT_PH_STATUS_FIRST_CHUNK 0x2D
  54. #define NXP_NCI_FW_RESULT_PH_STATUS_NEXT_CHUNK 0x2E
  55. #define NXP_NCI_FW_RESULT_PH_STATUS_INTERNAL_ERROR_5 0xC5
  56. void nxp_nci_fw_work_complete(struct nxp_nci_info *info, int result)
  57. {
  58. struct nxp_nci_fw_info *fw_info = &info->fw_info;
  59. int r;
  60. if (info->phy_ops->set_mode) {
  61. r = info->phy_ops->set_mode(info->phy_id, NXP_NCI_MODE_COLD);
  62. if (r < 0 && result == 0)
  63. result = -r;
  64. }
  65. info->mode = NXP_NCI_MODE_COLD;
  66. if (fw_info->fw) {
  67. release_firmware(fw_info->fw);
  68. fw_info->fw = NULL;
  69. }
  70. nfc_fw_download_done(info->ndev->nfc_dev, fw_info->name, (u32) -result);
  71. }
  72. /* crc_ccitt cannot be used since it is computed MSB first and not LSB first */
  73. static u16 nxp_nci_fw_crc(u8 const *buffer, size_t len)
  74. {
  75. u16 crc = 0xffff;
  76. while (len--) {
  77. crc = ((crc >> 8) | (crc << 8)) ^ *buffer++;
  78. crc ^= (crc & 0xff) >> 4;
  79. crc ^= (crc & 0xff) << 12;
  80. crc ^= (crc & 0xff) << 5;
  81. }
  82. return crc;
  83. }
  84. static int nxp_nci_fw_send_chunk(struct nxp_nci_info *info)
  85. {
  86. struct nxp_nci_fw_info *fw_info = &info->fw_info;
  87. u16 header, crc;
  88. struct sk_buff *skb;
  89. size_t chunk_len;
  90. size_t remaining_len;
  91. int r;
  92. skb = nci_skb_alloc(info->ndev, info->max_payload, GFP_KERNEL);
  93. if (!skb) {
  94. r = -ENOMEM;
  95. goto chunk_exit;
  96. }
  97. chunk_len = info->max_payload - NXP_NCI_FW_HDR_LEN - NXP_NCI_FW_CRC_LEN;
  98. remaining_len = fw_info->frame_size - fw_info->written;
  99. if (remaining_len > chunk_len) {
  100. header = NXP_NCI_FW_CHUNK_FLAG;
  101. } else {
  102. chunk_len = remaining_len;
  103. header = 0x0000;
  104. }
  105. header |= chunk_len & NXP_NCI_FW_FRAME_LEN_MASK;
  106. put_unaligned_be16(header, skb_put(skb, NXP_NCI_FW_HDR_LEN));
  107. memcpy(skb_put(skb, chunk_len), fw_info->data + fw_info->written,
  108. chunk_len);
  109. crc = nxp_nci_fw_crc(skb->data, chunk_len + NXP_NCI_FW_HDR_LEN);
  110. put_unaligned_be16(crc, skb_put(skb, NXP_NCI_FW_CRC_LEN));
  111. r = info->phy_ops->write(info->phy_id, skb);
  112. if (r >= 0)
  113. r = chunk_len;
  114. kfree_skb(skb);
  115. chunk_exit:
  116. return r;
  117. }
  118. static int nxp_nci_fw_send(struct nxp_nci_info *info)
  119. {
  120. struct nxp_nci_fw_info *fw_info = &info->fw_info;
  121. long completion_rc;
  122. int r;
  123. reinit_completion(&fw_info->cmd_completion);
  124. if (fw_info->written == 0) {
  125. fw_info->frame_size = get_unaligned_be16(fw_info->data) &
  126. NXP_NCI_FW_FRAME_LEN_MASK;
  127. fw_info->data += NXP_NCI_FW_HDR_LEN;
  128. fw_info->size -= NXP_NCI_FW_HDR_LEN;
  129. }
  130. if (fw_info->frame_size > fw_info->size)
  131. return -EMSGSIZE;
  132. r = nxp_nci_fw_send_chunk(info);
  133. if (r < 0)
  134. return r;
  135. fw_info->written += r;
  136. if (*fw_info->data == NXP_NCI_FW_CMD_RESET) {
  137. fw_info->cmd_result = 0;
  138. if (fw_info->fw)
  139. schedule_work(&fw_info->work);
  140. } else {
  141. completion_rc = wait_for_completion_interruptible_timeout(
  142. &fw_info->cmd_completion, NXP_NCI_FW_ANSWER_TIMEOUT);
  143. if (completion_rc == 0)
  144. return -ETIMEDOUT;
  145. }
  146. return 0;
  147. }
  148. void nxp_nci_fw_work(struct work_struct *work)
  149. {
  150. struct nxp_nci_info *info;
  151. struct nxp_nci_fw_info *fw_info;
  152. int r;
  153. fw_info = container_of(work, struct nxp_nci_fw_info, work);
  154. info = container_of(fw_info, struct nxp_nci_info, fw_info);
  155. mutex_lock(&info->info_lock);
  156. r = fw_info->cmd_result;
  157. if (r < 0)
  158. goto exit_work;
  159. if (fw_info->written == fw_info->frame_size) {
  160. fw_info->data += fw_info->frame_size;
  161. fw_info->size -= fw_info->frame_size;
  162. fw_info->written = 0;
  163. }
  164. if (fw_info->size > 0)
  165. r = nxp_nci_fw_send(info);
  166. exit_work:
  167. if (r < 0 || fw_info->size == 0)
  168. nxp_nci_fw_work_complete(info, r);
  169. mutex_unlock(&info->info_lock);
  170. }
  171. int nxp_nci_fw_download(struct nci_dev *ndev, const char *firmware_name)
  172. {
  173. struct nxp_nci_info *info = nci_get_drvdata(ndev);
  174. struct nxp_nci_fw_info *fw_info = &info->fw_info;
  175. int r;
  176. mutex_lock(&info->info_lock);
  177. if (!info->phy_ops->set_mode || !info->phy_ops->write) {
  178. r = -ENOTSUPP;
  179. goto fw_download_exit;
  180. }
  181. if (!firmware_name || firmware_name[0] == '\0') {
  182. r = -EINVAL;
  183. goto fw_download_exit;
  184. }
  185. strcpy(fw_info->name, firmware_name);
  186. r = reject_firmware(&fw_info->fw, firmware_name,
  187. ndev->nfc_dev->dev.parent);
  188. if (r < 0)
  189. goto fw_download_exit;
  190. r = info->phy_ops->set_mode(info->phy_id, NXP_NCI_MODE_FW);
  191. if (r < 0) {
  192. release_firmware(fw_info->fw);
  193. goto fw_download_exit;
  194. }
  195. info->mode = NXP_NCI_MODE_FW;
  196. fw_info->data = fw_info->fw->data;
  197. fw_info->size = fw_info->fw->size;
  198. fw_info->written = 0;
  199. fw_info->frame_size = 0;
  200. fw_info->cmd_result = 0;
  201. schedule_work(&fw_info->work);
  202. fw_download_exit:
  203. mutex_unlock(&info->info_lock);
  204. return r;
  205. }
  206. static int nxp_nci_fw_read_status(u8 stat)
  207. {
  208. switch (stat) {
  209. case NXP_NCI_FW_RESULT_OK:
  210. return 0;
  211. case NXP_NCI_FW_RESULT_INVALID_ADDR:
  212. return -EINVAL;
  213. case NXP_NCI_FW_RESULT_UNKNOWN_CMD:
  214. return -EINVAL;
  215. case NXP_NCI_FW_RESULT_ABORTED_CMD:
  216. return -EMSGSIZE;
  217. case NXP_NCI_FW_RESULT_ADDR_RANGE_OFL_ERROR:
  218. return -EADDRNOTAVAIL;
  219. case NXP_NCI_FW_RESULT_BUFFER_OFL_ERROR:
  220. return -ENOBUFS;
  221. case NXP_NCI_FW_RESULT_MEM_BSY:
  222. return -ENOKEY;
  223. case NXP_NCI_FW_RESULT_SIGNATURE_ERROR:
  224. return -EKEYREJECTED;
  225. case NXP_NCI_FW_RESULT_FIRMWARE_VERSION_ERROR:
  226. return -EALREADY;
  227. case NXP_NCI_FW_RESULT_PROTOCOL_ERROR:
  228. return -EPROTO;
  229. case NXP_NCI_FW_RESULT_SFWU_DEGRADED:
  230. return -EHWPOISON;
  231. case NXP_NCI_FW_RESULT_PH_STATUS_FIRST_CHUNK:
  232. return 0;
  233. case NXP_NCI_FW_RESULT_PH_STATUS_NEXT_CHUNK:
  234. return 0;
  235. case NXP_NCI_FW_RESULT_PH_STATUS_INTERNAL_ERROR_5:
  236. return -EINVAL;
  237. default:
  238. return -EIO;
  239. }
  240. }
  241. static u16 nxp_nci_fw_check_crc(struct sk_buff *skb)
  242. {
  243. u16 crc, frame_crc;
  244. size_t len = skb->len - NXP_NCI_FW_CRC_LEN;
  245. crc = nxp_nci_fw_crc(skb->data, len);
  246. frame_crc = get_unaligned_be16(skb->data + len);
  247. return (crc ^ frame_crc);
  248. }
  249. void nxp_nci_fw_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
  250. {
  251. struct nxp_nci_info *info = nci_get_drvdata(ndev);
  252. struct nxp_nci_fw_info *fw_info = &info->fw_info;
  253. complete(&fw_info->cmd_completion);
  254. if (skb) {
  255. if (nxp_nci_fw_check_crc(skb) != 0x00)
  256. fw_info->cmd_result = -EBADMSG;
  257. else
  258. fw_info->cmd_result = nxp_nci_fw_read_status(
  259. *skb_pull(skb, NXP_NCI_FW_HDR_LEN));
  260. kfree_skb(skb);
  261. } else {
  262. fw_info->cmd_result = -EIO;
  263. }
  264. if (fw_info->fw)
  265. schedule_work(&fw_info->work);
  266. }
  267. EXPORT_SYMBOL(nxp_nci_fw_recv_frame);