nfcwilink.c 13 KB

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