firmware.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 <asm/unaligned.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. skb_put_data(skb, fw_info->data + fw_info->written, chunk_len);
  108. crc = nxp_nci_fw_crc(skb->data, chunk_len + NXP_NCI_FW_HDR_LEN);
  109. put_unaligned_be16(crc, skb_put(skb, NXP_NCI_FW_CRC_LEN));
  110. r = info->phy_ops->write(info->phy_id, skb);
  111. if (r >= 0)
  112. r = chunk_len;
  113. kfree_skb(skb);
  114. chunk_exit:
  115. return r;
  116. }
  117. static int nxp_nci_fw_send(struct nxp_nci_info *info)
  118. {
  119. struct nxp_nci_fw_info *fw_info = &info->fw_info;
  120. long completion_rc;
  121. int r;
  122. reinit_completion(&fw_info->cmd_completion);
  123. if (fw_info->written == 0) {
  124. fw_info->frame_size = get_unaligned_be16(fw_info->data) &
  125. NXP_NCI_FW_FRAME_LEN_MASK;
  126. fw_info->data += NXP_NCI_FW_HDR_LEN;
  127. fw_info->size -= NXP_NCI_FW_HDR_LEN;
  128. }
  129. if (fw_info->frame_size > fw_info->size)
  130. return -EMSGSIZE;
  131. r = nxp_nci_fw_send_chunk(info);
  132. if (r < 0)
  133. return r;
  134. fw_info->written += r;
  135. if (*fw_info->data == NXP_NCI_FW_CMD_RESET) {
  136. fw_info->cmd_result = 0;
  137. if (fw_info->fw)
  138. schedule_work(&fw_info->work);
  139. } else {
  140. completion_rc = wait_for_completion_interruptible_timeout(
  141. &fw_info->cmd_completion, NXP_NCI_FW_ANSWER_TIMEOUT);
  142. if (completion_rc == 0)
  143. return -ETIMEDOUT;
  144. }
  145. return 0;
  146. }
  147. void nxp_nci_fw_work(struct work_struct *work)
  148. {
  149. struct nxp_nci_info *info;
  150. struct nxp_nci_fw_info *fw_info;
  151. int r;
  152. fw_info = container_of(work, struct nxp_nci_fw_info, work);
  153. info = container_of(fw_info, struct nxp_nci_info, fw_info);
  154. mutex_lock(&info->info_lock);
  155. r = fw_info->cmd_result;
  156. if (r < 0)
  157. goto exit_work;
  158. if (fw_info->written == fw_info->frame_size) {
  159. fw_info->data += fw_info->frame_size;
  160. fw_info->size -= fw_info->frame_size;
  161. fw_info->written = 0;
  162. }
  163. if (fw_info->size > 0)
  164. r = nxp_nci_fw_send(info);
  165. exit_work:
  166. if (r < 0 || fw_info->size == 0)
  167. nxp_nci_fw_work_complete(info, r);
  168. mutex_unlock(&info->info_lock);
  169. }
  170. int nxp_nci_fw_download(struct nci_dev *ndev, const char *firmware_name)
  171. {
  172. struct nxp_nci_info *info = nci_get_drvdata(ndev);
  173. struct nxp_nci_fw_info *fw_info = &info->fw_info;
  174. int r;
  175. mutex_lock(&info->info_lock);
  176. if (!info->phy_ops->set_mode || !info->phy_ops->write) {
  177. r = -ENOTSUPP;
  178. goto fw_download_exit;
  179. }
  180. if (!firmware_name || firmware_name[0] == '\0') {
  181. r = -EINVAL;
  182. goto fw_download_exit;
  183. }
  184. strcpy(fw_info->name, firmware_name);
  185. r = request_firmware(&fw_info->fw, firmware_name,
  186. ndev->nfc_dev->dev.parent);
  187. if (r < 0)
  188. goto fw_download_exit;
  189. r = info->phy_ops->set_mode(info->phy_id, NXP_NCI_MODE_FW);
  190. if (r < 0) {
  191. release_firmware(fw_info->fw);
  192. goto fw_download_exit;
  193. }
  194. info->mode = NXP_NCI_MODE_FW;
  195. fw_info->data = fw_info->fw->data;
  196. fw_info->size = fw_info->fw->size;
  197. fw_info->written = 0;
  198. fw_info->frame_size = 0;
  199. fw_info->cmd_result = 0;
  200. schedule_work(&fw_info->work);
  201. fw_download_exit:
  202. mutex_unlock(&info->info_lock);
  203. return r;
  204. }
  205. static int nxp_nci_fw_read_status(u8 stat)
  206. {
  207. switch (stat) {
  208. case NXP_NCI_FW_RESULT_OK:
  209. return 0;
  210. case NXP_NCI_FW_RESULT_INVALID_ADDR:
  211. return -EINVAL;
  212. case NXP_NCI_FW_RESULT_UNKNOWN_CMD:
  213. return -EINVAL;
  214. case NXP_NCI_FW_RESULT_ABORTED_CMD:
  215. return -EMSGSIZE;
  216. case NXP_NCI_FW_RESULT_ADDR_RANGE_OFL_ERROR:
  217. return -EADDRNOTAVAIL;
  218. case NXP_NCI_FW_RESULT_BUFFER_OFL_ERROR:
  219. return -ENOBUFS;
  220. case NXP_NCI_FW_RESULT_MEM_BSY:
  221. return -ENOKEY;
  222. case NXP_NCI_FW_RESULT_SIGNATURE_ERROR:
  223. return -EKEYREJECTED;
  224. case NXP_NCI_FW_RESULT_FIRMWARE_VERSION_ERROR:
  225. return -EALREADY;
  226. case NXP_NCI_FW_RESULT_PROTOCOL_ERROR:
  227. return -EPROTO;
  228. case NXP_NCI_FW_RESULT_SFWU_DEGRADED:
  229. return -EHWPOISON;
  230. case NXP_NCI_FW_RESULT_PH_STATUS_FIRST_CHUNK:
  231. return 0;
  232. case NXP_NCI_FW_RESULT_PH_STATUS_NEXT_CHUNK:
  233. return 0;
  234. case NXP_NCI_FW_RESULT_PH_STATUS_INTERNAL_ERROR_5:
  235. return -EINVAL;
  236. default:
  237. return -EIO;
  238. }
  239. }
  240. static u16 nxp_nci_fw_check_crc(struct sk_buff *skb)
  241. {
  242. u16 crc, frame_crc;
  243. size_t len = skb->len - NXP_NCI_FW_CRC_LEN;
  244. crc = nxp_nci_fw_crc(skb->data, len);
  245. frame_crc = get_unaligned_be16(skb->data + len);
  246. return (crc ^ frame_crc);
  247. }
  248. void nxp_nci_fw_recv_frame(struct nci_dev *ndev, struct sk_buff *skb)
  249. {
  250. struct nxp_nci_info *info = nci_get_drvdata(ndev);
  251. struct nxp_nci_fw_info *fw_info = &info->fw_info;
  252. complete(&fw_info->cmd_completion);
  253. if (skb) {
  254. if (nxp_nci_fw_check_crc(skb) != 0x00)
  255. fw_info->cmd_result = -EBADMSG;
  256. else
  257. fw_info->cmd_result = nxp_nci_fw_read_status(*(u8 *)skb_pull(skb, NXP_NCI_FW_HDR_LEN));
  258. kfree_skb(skb);
  259. } else {
  260. fw_info->cmd_result = -EIO;
  261. }
  262. if (fw_info->fw)
  263. schedule_work(&fw_info->work);
  264. }
  265. EXPORT_SYMBOL(nxp_nci_fw_recv_frame);