nfcwilink.c 14 KB

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