nfcwilink.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 nci_dev *ndev, 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. cmd = (struct nci_vs_nfcc_info_cmd *)
  135. skb_put(skb, sizeof(struct nci_vs_nfcc_info_cmd));
  136. cmd->gid = NCI_VS_NFCC_INFO_CMD_GID;
  137. cmd->oid = NCI_VS_NFCC_INFO_CMD_OID;
  138. cmd->plen = 0;
  139. drv->nfcc_info.plen = 0;
  140. rc = nfcwilink_send(drv->ndev, skb);
  141. if (rc)
  142. return rc;
  143. comp_ret = wait_for_completion_timeout(&drv->completed,
  144. msecs_to_jiffies(NFCWILINK_CMD_TIMEOUT));
  145. nfc_dev_dbg(&drv->pdev->dev, "wait_for_completion_timeout returned %ld",
  146. comp_ret);
  147. if (comp_ret == 0) {
  148. nfc_dev_err(&drv->pdev->dev,
  149. "timeout on wait_for_completion_timeout");
  150. return -ETIMEDOUT;
  151. }
  152. nfc_dev_dbg(&drv->pdev->dev, "nci_vs_nfcc_info_rsp: plen %d, status %d",
  153. drv->nfcc_info.plen,
  154. drv->nfcc_info.status);
  155. if ((drv->nfcc_info.plen != 5) || (drv->nfcc_info.status != 0)) {
  156. nfc_dev_err(&drv->pdev->dev,
  157. "invalid nci_vs_nfcc_info_rsp");
  158. return -EINVAL;
  159. }
  160. snprintf(file_name, BTS_FILE_NAME_MAX_SIZE,
  161. "TINfcInit_%d.%d.%d.%d.bts",
  162. drv->nfcc_info.hw_id,
  163. drv->nfcc_info.sw_ver_x,
  164. drv->nfcc_info.sw_ver_z,
  165. drv->nfcc_info.patch_id);
  166. nfc_dev_info(&drv->pdev->dev, "nfcwilink FW file name: %s", file_name);
  167. return 0;
  168. }
  169. static int nfcwilink_send_bts_cmd(struct nfcwilink *drv, __u8 *data, int len)
  170. {
  171. struct nfcwilink_hdr *hdr = (struct nfcwilink_hdr *)data;
  172. struct sk_buff *skb;
  173. unsigned long comp_ret;
  174. int rc;
  175. nfc_dev_dbg(&drv->pdev->dev, "send_bts_cmd entry");
  176. /* verify valid cmd for the NFC channel */
  177. if ((len <= sizeof(struct nfcwilink_hdr)) ||
  178. (len > BTS_FILE_CMD_MAX_LEN) ||
  179. (hdr->chnl != NFCWILINK_CHNL) ||
  180. (hdr->opcode != NFCWILINK_OPCODE)) {
  181. nfc_dev_err(&drv->pdev->dev,
  182. "ignoring invalid bts cmd, len %d, chnl %d, opcode %d",
  183. len, hdr->chnl, hdr->opcode);
  184. return 0;
  185. }
  186. /* remove the ST header */
  187. len -= sizeof(struct nfcwilink_hdr);
  188. data += sizeof(struct nfcwilink_hdr);
  189. skb = nfcwilink_skb_alloc(len, GFP_KERNEL);
  190. if (!skb) {
  191. nfc_dev_err(&drv->pdev->dev, "no memory for bts cmd");
  192. return -ENOMEM;
  193. }
  194. memcpy(skb_put(skb, len), data, len);
  195. rc = nfcwilink_send(drv->ndev, skb);
  196. if (rc)
  197. return rc;
  198. comp_ret = wait_for_completion_timeout(&drv->completed,
  199. msecs_to_jiffies(NFCWILINK_CMD_TIMEOUT));
  200. nfc_dev_dbg(&drv->pdev->dev, "wait_for_completion_timeout returned %ld",
  201. comp_ret);
  202. if (comp_ret == 0) {
  203. nfc_dev_err(&drv->pdev->dev,
  204. "timeout on wait_for_completion_timeout");
  205. return -ETIMEDOUT;
  206. }
  207. return 0;
  208. }
  209. static int nfcwilink_download_fw(struct nfcwilink *drv)
  210. {
  211. unsigned char file_name[BTS_FILE_NAME_MAX_SIZE];
  212. const struct firmware *fw;
  213. __u16 action_type, action_len;
  214. __u8 *ptr;
  215. int len, rc;
  216. nfc_dev_dbg(&drv->pdev->dev, "download_fw entry");
  217. set_bit(NFCWILINK_FW_DOWNLOAD, &drv->flags);
  218. rc = nfcwilink_get_bts_file_name(drv, file_name);
  219. if (rc)
  220. goto exit;
  221. rc = request_firmware(&fw, file_name, &drv->pdev->dev);
  222. if (rc) {
  223. nfc_dev_err(&drv->pdev->dev, "request_firmware failed %d", rc);
  224. /* if the file is not found, don't exit with failure */
  225. if (rc == -ENOENT)
  226. rc = 0;
  227. goto exit;
  228. }
  229. len = fw->size;
  230. ptr = (__u8 *)fw->data;
  231. if ((len == 0) || (ptr == NULL)) {
  232. nfc_dev_dbg(&drv->pdev->dev,
  233. "request_firmware returned size %d", len);
  234. goto release_fw;
  235. }
  236. if (__le32_to_cpu(((struct bts_file_hdr *)ptr)->magic) !=
  237. BTS_FILE_HDR_MAGIC) {
  238. nfc_dev_err(&drv->pdev->dev, "wrong bts magic number");
  239. rc = -EINVAL;
  240. goto release_fw;
  241. }
  242. /* remove the BTS header */
  243. len -= sizeof(struct bts_file_hdr);
  244. ptr += sizeof(struct bts_file_hdr);
  245. while (len > 0) {
  246. action_type =
  247. __le16_to_cpu(((struct bts_file_action *)ptr)->type);
  248. action_len =
  249. __le16_to_cpu(((struct bts_file_action *)ptr)->len);
  250. nfc_dev_dbg(&drv->pdev->dev, "bts_file_action type %d, len %d",
  251. action_type, action_len);
  252. switch (action_type) {
  253. case BTS_FILE_ACTION_TYPE_SEND_CMD:
  254. rc = nfcwilink_send_bts_cmd(drv,
  255. ((struct bts_file_action *)ptr)->data,
  256. action_len);
  257. if (rc)
  258. goto release_fw;
  259. break;
  260. }
  261. /* advance to the next action */
  262. len -= (sizeof(struct bts_file_action) + action_len);
  263. ptr += (sizeof(struct bts_file_action) + action_len);
  264. }
  265. release_fw:
  266. release_firmware(fw);
  267. exit:
  268. clear_bit(NFCWILINK_FW_DOWNLOAD, &drv->flags);
  269. return rc;
  270. }
  271. /* Called by ST when registration is complete */
  272. static void nfcwilink_register_complete(void *priv_data, char data)
  273. {
  274. struct nfcwilink *drv = priv_data;
  275. nfc_dev_dbg(&drv->pdev->dev, "register_complete entry");
  276. /* store ST registration status */
  277. drv->st_register_cb_status = data;
  278. /* complete the wait in nfc_st_open() */
  279. complete(&drv->completed);
  280. }
  281. /* Called by ST when receive data is available */
  282. static long nfcwilink_receive(void *priv_data, struct sk_buff *skb)
  283. {
  284. struct nfcwilink *drv = priv_data;
  285. int rc;
  286. if (!skb)
  287. return -EFAULT;
  288. if (!drv) {
  289. kfree_skb(skb);
  290. return -EFAULT;
  291. }
  292. nfc_dev_dbg(&drv->pdev->dev, "receive entry, len %d", skb->len);
  293. /* strip the ST header
  294. (apart for the chnl byte, which is not received in the hdr) */
  295. skb_pull(skb, (NFCWILINK_HDR_LEN-1));
  296. if (test_bit(NFCWILINK_FW_DOWNLOAD, &drv->flags)) {
  297. nfcwilink_fw_download_receive(drv, skb);
  298. return 0;
  299. }
  300. /* Forward skb to NCI core layer */
  301. rc = nci_recv_frame(drv->ndev, skb);
  302. if (rc < 0) {
  303. nfc_dev_err(&drv->pdev->dev, "nci_recv_frame failed %d", rc);
  304. return rc;
  305. }
  306. return 0;
  307. }
  308. /* protocol structure registered with ST */
  309. static struct st_proto_s nfcwilink_proto = {
  310. .chnl_id = NFCWILINK_CHNL,
  311. .max_frame_size = NFCWILINK_MAX_FRAME_SIZE,
  312. .hdr_len = (NFCWILINK_HDR_LEN-1), /* not including chnl byte */
  313. .offset_len_in_hdr = NFCWILINK_OFFSET_LEN_IN_HDR,
  314. .len_size = NFCWILINK_LEN_SIZE,
  315. .reserve = 0,
  316. .recv = nfcwilink_receive,
  317. .reg_complete_cb = nfcwilink_register_complete,
  318. .write = NULL,
  319. };
  320. static int nfcwilink_open(struct nci_dev *ndev)
  321. {
  322. struct nfcwilink *drv = nci_get_drvdata(ndev);
  323. unsigned long comp_ret;
  324. int rc;
  325. nfc_dev_dbg(&drv->pdev->dev, "open entry");
  326. if (test_and_set_bit(NFCWILINK_RUNNING, &drv->flags)) {
  327. rc = -EBUSY;
  328. goto exit;
  329. }
  330. nfcwilink_proto.priv_data = drv;
  331. init_completion(&drv->completed);
  332. drv->st_register_cb_status = -EINPROGRESS;
  333. rc = st_register(&nfcwilink_proto);
  334. if (rc < 0) {
  335. if (rc == -EINPROGRESS) {
  336. comp_ret = wait_for_completion_timeout(
  337. &drv->completed,
  338. msecs_to_jiffies(NFCWILINK_REGISTER_TIMEOUT));
  339. nfc_dev_dbg(&drv->pdev->dev,
  340. "wait_for_completion_timeout returned %ld",
  341. comp_ret);
  342. if (comp_ret == 0) {
  343. /* timeout */
  344. rc = -ETIMEDOUT;
  345. goto clear_exit;
  346. } else if (drv->st_register_cb_status != 0) {
  347. rc = drv->st_register_cb_status;
  348. nfc_dev_err(&drv->pdev->dev,
  349. "st_register_cb failed %d", rc);
  350. goto clear_exit;
  351. }
  352. } else {
  353. nfc_dev_err(&drv->pdev->dev,
  354. "st_register failed %d", rc);
  355. goto clear_exit;
  356. }
  357. }
  358. /* st_register MUST fill the write callback */
  359. BUG_ON(nfcwilink_proto.write == NULL);
  360. drv->st_write = nfcwilink_proto.write;
  361. if (nfcwilink_download_fw(drv)) {
  362. nfc_dev_err(&drv->pdev->dev, "nfcwilink_download_fw failed %d",
  363. rc);
  364. /* open should succeed, even if the FW download failed */
  365. }
  366. goto exit;
  367. clear_exit:
  368. clear_bit(NFCWILINK_RUNNING, &drv->flags);
  369. exit:
  370. return rc;
  371. }
  372. static int nfcwilink_close(struct nci_dev *ndev)
  373. {
  374. struct nfcwilink *drv = nci_get_drvdata(ndev);
  375. int rc;
  376. nfc_dev_dbg(&drv->pdev->dev, "close entry");
  377. if (!test_and_clear_bit(NFCWILINK_RUNNING, &drv->flags))
  378. return 0;
  379. rc = st_unregister(&nfcwilink_proto);
  380. if (rc)
  381. nfc_dev_err(&drv->pdev->dev, "st_unregister failed %d", rc);
  382. drv->st_write = NULL;
  383. return rc;
  384. }
  385. static int nfcwilink_send(struct nci_dev *ndev, struct sk_buff *skb)
  386. {
  387. struct nfcwilink *drv = nci_get_drvdata(ndev);
  388. struct nfcwilink_hdr hdr = {NFCWILINK_CHNL, NFCWILINK_OPCODE, 0x0000};
  389. long len;
  390. nfc_dev_dbg(&drv->pdev->dev, "send entry, len %d", skb->len);
  391. if (!test_bit(NFCWILINK_RUNNING, &drv->flags)) {
  392. kfree_skb(skb);
  393. return -EINVAL;
  394. }
  395. /* add the ST hdr to the start of the buffer */
  396. hdr.len = cpu_to_le16(skb->len);
  397. memcpy(skb_push(skb, NFCWILINK_HDR_LEN), &hdr, NFCWILINK_HDR_LEN);
  398. /* Insert skb to shared transport layer's transmit queue.
  399. * Freeing skb memory is taken care in shared transport layer,
  400. * so don't free skb memory here.
  401. */
  402. len = drv->st_write(skb);
  403. if (len < 0) {
  404. kfree_skb(skb);
  405. nfc_dev_err(&drv->pdev->dev, "st_write failed %ld", len);
  406. return -EFAULT;
  407. }
  408. return 0;
  409. }
  410. static struct nci_ops nfcwilink_ops = {
  411. .open = nfcwilink_open,
  412. .close = nfcwilink_close,
  413. .send = nfcwilink_send,
  414. };
  415. static int nfcwilink_probe(struct platform_device *pdev)
  416. {
  417. static struct nfcwilink *drv;
  418. int rc;
  419. __u32 protocols;
  420. nfc_dev_dbg(&pdev->dev, "probe entry");
  421. drv = devm_kzalloc(&pdev->dev, sizeof(struct nfcwilink), GFP_KERNEL);
  422. if (!drv) {
  423. rc = -ENOMEM;
  424. goto exit;
  425. }
  426. drv->pdev = pdev;
  427. protocols = NFC_PROTO_JEWEL_MASK
  428. | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
  429. | NFC_PROTO_ISO14443_MASK
  430. | NFC_PROTO_ISO14443_B_MASK
  431. | NFC_PROTO_NFC_DEP_MASK;
  432. drv->ndev = nci_allocate_device(&nfcwilink_ops,
  433. protocols,
  434. NFCWILINK_HDR_LEN,
  435. 0);
  436. if (!drv->ndev) {
  437. nfc_dev_err(&pdev->dev, "nci_allocate_device failed");
  438. rc = -ENOMEM;
  439. goto exit;
  440. }
  441. nci_set_parent_dev(drv->ndev, &pdev->dev);
  442. nci_set_drvdata(drv->ndev, drv);
  443. rc = nci_register_device(drv->ndev);
  444. if (rc < 0) {
  445. nfc_dev_err(&pdev->dev, "nci_register_device failed %d", rc);
  446. goto free_dev_exit;
  447. }
  448. dev_set_drvdata(&pdev->dev, drv);
  449. goto exit;
  450. free_dev_exit:
  451. nci_free_device(drv->ndev);
  452. exit:
  453. return rc;
  454. }
  455. static int nfcwilink_remove(struct platform_device *pdev)
  456. {
  457. struct nfcwilink *drv = dev_get_drvdata(&pdev->dev);
  458. struct nci_dev *ndev;
  459. nfc_dev_dbg(&pdev->dev, "remove entry");
  460. if (!drv)
  461. return -EFAULT;
  462. ndev = drv->ndev;
  463. nci_unregister_device(ndev);
  464. nci_free_device(ndev);
  465. dev_set_drvdata(&pdev->dev, NULL);
  466. return 0;
  467. }
  468. static struct platform_driver nfcwilink_driver = {
  469. .probe = nfcwilink_probe,
  470. .remove = nfcwilink_remove,
  471. .driver = {
  472. .name = "nfcwilink",
  473. .owner = THIS_MODULE,
  474. },
  475. };
  476. module_platform_driver(nfcwilink_driver);
  477. /* ------ Module Info ------ */
  478. MODULE_AUTHOR("Ilan Elias <ilane@ti.com>");
  479. MODULE_DESCRIPTION("NFC Driver for TI Shared Transport");
  480. MODULE_LICENSE("GPL");