nfcwilink.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * Texas Instrument's NFC Driver For Shared Transport.
  3. *
  4. * NFC Driver acts as interface between NCI core and
  5. * TI Shared Transport Layer.
  6. *
  7. * Copyright (C) 2011 Texas Instruments, Inc.
  8. *
  9. * Written by Ilan Elias <ilane@ti.com>
  10. *
  11. * Acknowledgements:
  12. * This file is based on btwilink.c, which was written
  13. * by Raja Mani and Pavan Savoy.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License version 2 as
  17. * published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. #include <linux/platform_device.h>
  29. #include <linux/module.h>
  30. #include <linux/types.h>
  31. #include <linux/firmware.h>
  32. #include <linux/nfc.h>
  33. #include <net/nfc/nci.h>
  34. #include <net/nfc/nci_core.h>
  35. #include <linux/ti_wilink_st.h>
  36. #define NFCWILINK_CHNL 12
  37. #define NFCWILINK_OPCODE 7
  38. #define NFCWILINK_MAX_FRAME_SIZE 300
  39. #define NFCWILINK_HDR_LEN 4
  40. #define NFCWILINK_OFFSET_LEN_IN_HDR 1
  41. #define NFCWILINK_LEN_SIZE 2
  42. #define NFCWILINK_REGISTER_TIMEOUT 8000 /* 8 sec */
  43. #define NFCWILINK_CMD_TIMEOUT 5000 /* 5 sec */
  44. #define BTS_FILE_NAME_MAX_SIZE 40
  45. #define BTS_FILE_HDR_MAGIC 0x42535442
  46. #define BTS_FILE_CMD_MAX_LEN 0xff
  47. #define BTS_FILE_ACTION_TYPE_SEND_CMD 1
  48. #define NCI_VS_NFCC_INFO_CMD_GID 0x2f
  49. #define NCI_VS_NFCC_INFO_CMD_OID 0x12
  50. #define NCI_VS_NFCC_INFO_RSP_GID 0x4f
  51. #define NCI_VS_NFCC_INFO_RSP_OID 0x12
  52. struct nfcwilink_hdr {
  53. __u8 chnl;
  54. __u8 opcode;
  55. __le16 len;
  56. } __packed;
  57. struct nci_vs_nfcc_info_cmd {
  58. __u8 gid;
  59. __u8 oid;
  60. __u8 plen;
  61. } __packed;
  62. struct nci_vs_nfcc_info_rsp {
  63. __u8 gid;
  64. __u8 oid;
  65. __u8 plen;
  66. __u8 status;
  67. __u8 hw_id;
  68. __u8 sw_ver_x;
  69. __u8 sw_ver_z;
  70. __u8 patch_id;
  71. } __packed;
  72. struct bts_file_hdr {
  73. __le32 magic;
  74. __le32 ver;
  75. __u8 rfu[24];
  76. __u8 actions[0];
  77. } __packed;
  78. struct bts_file_action {
  79. __le16 type;
  80. __le16 len;
  81. __u8 data[0];
  82. } __packed;
  83. struct nfcwilink {
  84. struct platform_device *pdev;
  85. struct nci_dev *ndev;
  86. unsigned long flags;
  87. char st_register_cb_status;
  88. long (*st_write) (struct sk_buff *);
  89. struct completion completed;
  90. struct nci_vs_nfcc_info_rsp nfcc_info;
  91. };
  92. /* NFCWILINK driver flags */
  93. enum {
  94. NFCWILINK_RUNNING,
  95. NFCWILINK_FW_DOWNLOAD,
  96. };
  97. static int nfcwilink_send(struct nci_dev *ndev, struct sk_buff *skb);
  98. static inline struct sk_buff *nfcwilink_skb_alloc(unsigned int len, gfp_t how)
  99. {
  100. struct sk_buff *skb;
  101. skb = alloc_skb(len + NFCWILINK_HDR_LEN, how);
  102. if (skb)
  103. skb_reserve(skb, NFCWILINK_HDR_LEN);
  104. return skb;
  105. }
  106. static void nfcwilink_fw_download_receive(struct nfcwilink *drv,
  107. struct sk_buff *skb)
  108. {
  109. struct nci_vs_nfcc_info_rsp *rsp = (void *)skb->data;
  110. /* Detect NCI_VS_NFCC_INFO_RSP and store the result */
  111. if ((skb->len > 3) && (rsp->gid == NCI_VS_NFCC_INFO_RSP_GID) &&
  112. (rsp->oid == NCI_VS_NFCC_INFO_RSP_OID)) {
  113. memcpy(&drv->nfcc_info, rsp,
  114. sizeof(struct nci_vs_nfcc_info_rsp));
  115. }
  116. kfree_skb(skb);
  117. complete(&drv->completed);
  118. }
  119. static int nfcwilink_get_bts_file_name(struct nfcwilink *drv, char *file_name)
  120. {
  121. struct nci_vs_nfcc_info_cmd *cmd;
  122. struct sk_buff *skb;
  123. unsigned long comp_ret;
  124. int rc;
  125. skb = nfcwilink_skb_alloc(sizeof(struct nci_vs_nfcc_info_cmd),
  126. GFP_KERNEL);
  127. if (!skb) {
  128. nfc_err(&drv->pdev->dev,
  129. "no memory for nci_vs_nfcc_info_cmd\n");
  130. return -ENOMEM;
  131. }
  132. cmd = (struct nci_vs_nfcc_info_cmd *)
  133. skb_put(skb, sizeof(struct nci_vs_nfcc_info_cmd));
  134. cmd->gid = NCI_VS_NFCC_INFO_CMD_GID;
  135. cmd->oid = NCI_VS_NFCC_INFO_CMD_OID;
  136. cmd->plen = 0;
  137. drv->nfcc_info.plen = 0;
  138. rc = nfcwilink_send(drv->ndev, skb);
  139. if (rc)
  140. return rc;
  141. comp_ret = wait_for_completion_timeout(&drv->completed,
  142. msecs_to_jiffies(NFCWILINK_CMD_TIMEOUT));
  143. dev_dbg(&drv->pdev->dev, "wait_for_completion_timeout returned %ld\n",
  144. comp_ret);
  145. if (comp_ret == 0) {
  146. nfc_err(&drv->pdev->dev,
  147. "timeout on wait_for_completion_timeout\n");
  148. return -ETIMEDOUT;
  149. }
  150. dev_dbg(&drv->pdev->dev, "nci_vs_nfcc_info_rsp: plen %d, status %d\n",
  151. drv->nfcc_info.plen, drv->nfcc_info.status);
  152. if ((drv->nfcc_info.plen != 5) || (drv->nfcc_info.status != 0)) {
  153. nfc_err(&drv->pdev->dev, "invalid nci_vs_nfcc_info_rsp\n");
  154. return -EINVAL;
  155. }
  156. snprintf(file_name, BTS_FILE_NAME_MAX_SIZE,
  157. "TINfcInit_%d.%d.%d.%d.bts",
  158. drv->nfcc_info.hw_id,
  159. drv->nfcc_info.sw_ver_x,
  160. drv->nfcc_info.sw_ver_z,
  161. drv->nfcc_info.patch_id);
  162. nfc_info(&drv->pdev->dev, "nfcwilink FW file name: %s\n", file_name);
  163. return 0;
  164. }
  165. static int nfcwilink_send_bts_cmd(struct nfcwilink *drv, __u8 *data, int len)
  166. {
  167. struct nfcwilink_hdr *hdr = (struct nfcwilink_hdr *)data;
  168. struct sk_buff *skb;
  169. unsigned long comp_ret;
  170. int rc;
  171. /* verify valid cmd for the NFC channel */
  172. if ((len <= sizeof(struct nfcwilink_hdr)) ||
  173. (len > BTS_FILE_CMD_MAX_LEN) ||
  174. (hdr->chnl != NFCWILINK_CHNL) ||
  175. (hdr->opcode != NFCWILINK_OPCODE)) {
  176. nfc_err(&drv->pdev->dev,
  177. "ignoring invalid bts cmd, len %d, chnl %d, opcode %d\n",
  178. len, hdr->chnl, hdr->opcode);
  179. return 0;
  180. }
  181. /* remove the ST header */
  182. len -= sizeof(struct nfcwilink_hdr);
  183. data += sizeof(struct nfcwilink_hdr);
  184. skb = nfcwilink_skb_alloc(len, GFP_KERNEL);
  185. if (!skb) {
  186. nfc_err(&drv->pdev->dev, "no memory for bts cmd\n");
  187. return -ENOMEM;
  188. }
  189. memcpy(skb_put(skb, len), data, len);
  190. rc = nfcwilink_send(drv->ndev, skb);
  191. if (rc)
  192. return rc;
  193. comp_ret = wait_for_completion_timeout(&drv->completed,
  194. msecs_to_jiffies(NFCWILINK_CMD_TIMEOUT));
  195. dev_dbg(&drv->pdev->dev, "wait_for_completion_timeout returned %ld\n",
  196. comp_ret);
  197. if (comp_ret == 0) {
  198. nfc_err(&drv->pdev->dev,
  199. "timeout on wait_for_completion_timeout\n");
  200. return -ETIMEDOUT;
  201. }
  202. return 0;
  203. }
  204. static int nfcwilink_download_fw(struct nfcwilink *drv)
  205. {
  206. unsigned char file_name[BTS_FILE_NAME_MAX_SIZE];
  207. const struct firmware *fw;
  208. __u16 action_type, action_len;
  209. __u8 *ptr;
  210. int len, rc;
  211. set_bit(NFCWILINK_FW_DOWNLOAD, &drv->flags);
  212. rc = nfcwilink_get_bts_file_name(drv, file_name);
  213. if (rc)
  214. goto exit;
  215. rc = request_firmware(&fw, file_name, &drv->pdev->dev);
  216. if (rc) {
  217. nfc_err(&drv->pdev->dev, "request_firmware failed %d\n", rc);
  218. /* if the file is not found, don't exit with failure */
  219. if (rc == -ENOENT)
  220. rc = 0;
  221. goto exit;
  222. }
  223. len = fw->size;
  224. ptr = (__u8 *)fw->data;
  225. if ((len == 0) || (ptr == NULL)) {
  226. dev_dbg(&drv->pdev->dev,
  227. "request_firmware returned size %d\n", len);
  228. goto release_fw;
  229. }
  230. if (__le32_to_cpu(((struct bts_file_hdr *)ptr)->magic) !=
  231. BTS_FILE_HDR_MAGIC) {
  232. nfc_err(&drv->pdev->dev, "wrong bts magic number\n");
  233. rc = -EINVAL;
  234. goto release_fw;
  235. }
  236. /* remove the BTS header */
  237. len -= sizeof(struct bts_file_hdr);
  238. ptr += sizeof(struct bts_file_hdr);
  239. while (len > 0) {
  240. action_type =
  241. __le16_to_cpu(((struct bts_file_action *)ptr)->type);
  242. action_len =
  243. __le16_to_cpu(((struct bts_file_action *)ptr)->len);
  244. dev_dbg(&drv->pdev->dev, "bts_file_action type %d, len %d\n",
  245. action_type, action_len);
  246. switch (action_type) {
  247. case BTS_FILE_ACTION_TYPE_SEND_CMD:
  248. rc = nfcwilink_send_bts_cmd(drv,
  249. ((struct bts_file_action *)ptr)->data,
  250. action_len);
  251. if (rc)
  252. goto release_fw;
  253. break;
  254. }
  255. /* advance to the next action */
  256. len -= (sizeof(struct bts_file_action) + action_len);
  257. ptr += (sizeof(struct bts_file_action) + action_len);
  258. }
  259. release_fw:
  260. release_firmware(fw);
  261. exit:
  262. clear_bit(NFCWILINK_FW_DOWNLOAD, &drv->flags);
  263. return rc;
  264. }
  265. /* Called by ST when registration is complete */
  266. static void nfcwilink_register_complete(void *priv_data, char data)
  267. {
  268. struct nfcwilink *drv = priv_data;
  269. /* store ST registration status */
  270. drv->st_register_cb_status = data;
  271. /* complete the wait in nfc_st_open() */
  272. complete(&drv->completed);
  273. }
  274. /* Called by ST when receive data is available */
  275. static long nfcwilink_receive(void *priv_data, struct sk_buff *skb)
  276. {
  277. struct nfcwilink *drv = priv_data;
  278. int rc;
  279. if (!skb)
  280. return -EFAULT;
  281. if (!drv) {
  282. kfree_skb(skb);
  283. return -EFAULT;
  284. }
  285. dev_dbg(&drv->pdev->dev, "receive entry, len %d\n", skb->len);
  286. /* strip the ST header
  287. (apart for the chnl byte, which is not received in the hdr) */
  288. skb_pull(skb, (NFCWILINK_HDR_LEN-1));
  289. if (test_bit(NFCWILINK_FW_DOWNLOAD, &drv->flags)) {
  290. nfcwilink_fw_download_receive(drv, skb);
  291. return 0;
  292. }
  293. /* Forward skb to NCI core layer */
  294. rc = nci_recv_frame(drv->ndev, skb);
  295. if (rc < 0) {
  296. nfc_err(&drv->pdev->dev, "nci_recv_frame failed %d\n", rc);
  297. return rc;
  298. }
  299. return 0;
  300. }
  301. /* protocol structure registered with ST */
  302. static struct st_proto_s nfcwilink_proto = {
  303. .chnl_id = NFCWILINK_CHNL,
  304. .max_frame_size = NFCWILINK_MAX_FRAME_SIZE,
  305. .hdr_len = (NFCWILINK_HDR_LEN-1), /* not including chnl byte */
  306. .offset_len_in_hdr = NFCWILINK_OFFSET_LEN_IN_HDR,
  307. .len_size = NFCWILINK_LEN_SIZE,
  308. .reserve = 0,
  309. .recv = nfcwilink_receive,
  310. .reg_complete_cb = nfcwilink_register_complete,
  311. .write = NULL,
  312. };
  313. static int nfcwilink_open(struct nci_dev *ndev)
  314. {
  315. struct nfcwilink *drv = nci_get_drvdata(ndev);
  316. unsigned long comp_ret;
  317. int rc;
  318. if (test_and_set_bit(NFCWILINK_RUNNING, &drv->flags)) {
  319. rc = -EBUSY;
  320. goto exit;
  321. }
  322. nfcwilink_proto.priv_data = drv;
  323. init_completion(&drv->completed);
  324. drv->st_register_cb_status = -EINPROGRESS;
  325. rc = st_register(&nfcwilink_proto);
  326. if (rc < 0) {
  327. if (rc == -EINPROGRESS) {
  328. comp_ret = wait_for_completion_timeout(
  329. &drv->completed,
  330. msecs_to_jiffies(NFCWILINK_REGISTER_TIMEOUT));
  331. dev_dbg(&drv->pdev->dev,
  332. "wait_for_completion_timeout returned %ld\n",
  333. comp_ret);
  334. if (comp_ret == 0) {
  335. /* timeout */
  336. rc = -ETIMEDOUT;
  337. goto clear_exit;
  338. } else if (drv->st_register_cb_status != 0) {
  339. rc = drv->st_register_cb_status;
  340. nfc_err(&drv->pdev->dev,
  341. "st_register_cb failed %d\n", rc);
  342. goto clear_exit;
  343. }
  344. } else {
  345. nfc_err(&drv->pdev->dev, "st_register failed %d\n", rc);
  346. goto clear_exit;
  347. }
  348. }
  349. /* st_register MUST fill the write callback */
  350. BUG_ON(nfcwilink_proto.write == NULL);
  351. drv->st_write = nfcwilink_proto.write;
  352. if (nfcwilink_download_fw(drv)) {
  353. nfc_err(&drv->pdev->dev, "nfcwilink_download_fw failed %d\n",
  354. rc);
  355. /* open should succeed, even if the FW download failed */
  356. }
  357. goto exit;
  358. clear_exit:
  359. clear_bit(NFCWILINK_RUNNING, &drv->flags);
  360. exit:
  361. return rc;
  362. }
  363. static int nfcwilink_close(struct nci_dev *ndev)
  364. {
  365. struct nfcwilink *drv = nci_get_drvdata(ndev);
  366. int rc;
  367. if (!test_and_clear_bit(NFCWILINK_RUNNING, &drv->flags))
  368. return 0;
  369. rc = st_unregister(&nfcwilink_proto);
  370. if (rc)
  371. nfc_err(&drv->pdev->dev, "st_unregister failed %d\n", rc);
  372. drv->st_write = NULL;
  373. return rc;
  374. }
  375. static int nfcwilink_send(struct nci_dev *ndev, struct sk_buff *skb)
  376. {
  377. struct nfcwilink *drv = nci_get_drvdata(ndev);
  378. struct nfcwilink_hdr hdr = {NFCWILINK_CHNL, NFCWILINK_OPCODE, 0x0000};
  379. long len;
  380. dev_dbg(&drv->pdev->dev, "send entry, len %d\n", skb->len);
  381. if (!test_bit(NFCWILINK_RUNNING, &drv->flags)) {
  382. kfree_skb(skb);
  383. return -EINVAL;
  384. }
  385. /* add the ST hdr to the start of the buffer */
  386. hdr.len = cpu_to_le16(skb->len);
  387. memcpy(skb_push(skb, NFCWILINK_HDR_LEN), &hdr, NFCWILINK_HDR_LEN);
  388. /* Insert skb to shared transport layer's transmit queue.
  389. * Freeing skb memory is taken care in shared transport layer,
  390. * so don't free skb memory here.
  391. */
  392. len = drv->st_write(skb);
  393. if (len < 0) {
  394. kfree_skb(skb);
  395. nfc_err(&drv->pdev->dev, "st_write failed %ld\n", len);
  396. return -EFAULT;
  397. }
  398. return 0;
  399. }
  400. static struct nci_ops nfcwilink_ops = {
  401. .open = nfcwilink_open,
  402. .close = nfcwilink_close,
  403. .send = nfcwilink_send,
  404. };
  405. static int nfcwilink_probe(struct platform_device *pdev)
  406. {
  407. struct nfcwilink *drv;
  408. int rc;
  409. __u32 protocols;
  410. drv = devm_kzalloc(&pdev->dev, sizeof(struct nfcwilink), GFP_KERNEL);
  411. if (!drv) {
  412. rc = -ENOMEM;
  413. goto exit;
  414. }
  415. drv->pdev = pdev;
  416. protocols = NFC_PROTO_JEWEL_MASK
  417. | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
  418. | NFC_PROTO_ISO14443_MASK
  419. | NFC_PROTO_ISO14443_B_MASK
  420. | NFC_PROTO_NFC_DEP_MASK;
  421. drv->ndev = nci_allocate_device(&nfcwilink_ops,
  422. protocols,
  423. NFCWILINK_HDR_LEN,
  424. 0);
  425. if (!drv->ndev) {
  426. nfc_err(&pdev->dev, "nci_allocate_device failed\n");
  427. rc = -ENOMEM;
  428. goto exit;
  429. }
  430. nci_set_parent_dev(drv->ndev, &pdev->dev);
  431. nci_set_drvdata(drv->ndev, drv);
  432. rc = nci_register_device(drv->ndev);
  433. if (rc < 0) {
  434. nfc_err(&pdev->dev, "nci_register_device failed %d\n", rc);
  435. goto free_dev_exit;
  436. }
  437. dev_set_drvdata(&pdev->dev, drv);
  438. goto exit;
  439. free_dev_exit:
  440. nci_free_device(drv->ndev);
  441. exit:
  442. return rc;
  443. }
  444. static int nfcwilink_remove(struct platform_device *pdev)
  445. {
  446. struct nfcwilink *drv = dev_get_drvdata(&pdev->dev);
  447. struct nci_dev *ndev;
  448. if (!drv)
  449. return -EFAULT;
  450. ndev = drv->ndev;
  451. nci_unregister_device(ndev);
  452. nci_free_device(ndev);
  453. return 0;
  454. }
  455. static struct platform_driver nfcwilink_driver = {
  456. .probe = nfcwilink_probe,
  457. .remove = nfcwilink_remove,
  458. .driver = {
  459. .name = "nfcwilink",
  460. },
  461. };
  462. module_platform_driver(nfcwilink_driver);
  463. /* ------ Module Info ------ */
  464. MODULE_AUTHOR("Ilan Elias <ilane@ti.com>");
  465. MODULE_DESCRIPTION("NFC Driver for TI Shared Transport");
  466. MODULE_LICENSE("GPL");