nfcwilink.c 13 KB

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