nfcwilink.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/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. struct nfcwilink_hdr {
  44. __u8 chnl;
  45. __u8 opcode;
  46. __le16 len;
  47. } __packed;
  48. struct nfcwilink {
  49. struct platform_device *pdev;
  50. struct nci_dev *ndev;
  51. unsigned long flags;
  52. char st_register_cb_status;
  53. long (*st_write) (struct sk_buff *);
  54. struct completion st_register_completed;
  55. };
  56. /* NFCWILINK driver flags */
  57. enum {
  58. NFCWILINK_RUNNING,
  59. };
  60. /* Called by ST when registration is complete */
  61. static void nfcwilink_register_complete(void *priv_data, char data)
  62. {
  63. struct nfcwilink *drv = priv_data;
  64. nfc_dev_dbg(&drv->pdev->dev, "register_complete entry");
  65. /* store ST registration status */
  66. drv->st_register_cb_status = data;
  67. /* complete the wait in nfc_st_open() */
  68. complete(&drv->st_register_completed);
  69. }
  70. /* Called by ST when receive data is available */
  71. static long nfcwilink_receive(void *priv_data, struct sk_buff *skb)
  72. {
  73. struct nfcwilink *drv = priv_data;
  74. int rc;
  75. nfc_dev_dbg(&drv->pdev->dev, "receive entry, len %d", skb->len);
  76. if (!skb)
  77. return -EFAULT;
  78. if (!drv) {
  79. kfree_skb(skb);
  80. return -EFAULT;
  81. }
  82. /* strip the ST header
  83. (apart for the chnl byte, which is not received in the hdr) */
  84. skb_pull(skb, (NFCWILINK_HDR_LEN-1));
  85. skb->dev = (void *) drv->ndev;
  86. /* Forward skb to NCI core layer */
  87. rc = nci_recv_frame(skb);
  88. if (rc < 0) {
  89. nfc_dev_err(&drv->pdev->dev, "nci_recv_frame failed %d", rc);
  90. return rc;
  91. }
  92. return 0;
  93. }
  94. /* protocol structure registered with ST */
  95. static struct st_proto_s nfcwilink_proto = {
  96. .chnl_id = NFCWILINK_CHNL,
  97. .max_frame_size = NFCWILINK_MAX_FRAME_SIZE,
  98. .hdr_len = (NFCWILINK_HDR_LEN-1), /* not including chnl byte */
  99. .offset_len_in_hdr = NFCWILINK_OFFSET_LEN_IN_HDR,
  100. .len_size = NFCWILINK_LEN_SIZE,
  101. .reserve = 0,
  102. .recv = nfcwilink_receive,
  103. .reg_complete_cb = nfcwilink_register_complete,
  104. .write = NULL,
  105. };
  106. static int nfcwilink_open(struct nci_dev *ndev)
  107. {
  108. struct nfcwilink *drv = nci_get_drvdata(ndev);
  109. unsigned long comp_ret;
  110. int rc;
  111. nfc_dev_dbg(&drv->pdev->dev, "open entry");
  112. if (test_and_set_bit(NFCWILINK_RUNNING, &drv->flags)) {
  113. rc = -EBUSY;
  114. goto exit;
  115. }
  116. nfcwilink_proto.priv_data = drv;
  117. init_completion(&drv->st_register_completed);
  118. drv->st_register_cb_status = -EINPROGRESS;
  119. rc = st_register(&nfcwilink_proto);
  120. if (rc < 0) {
  121. if (rc == -EINPROGRESS) {
  122. comp_ret = wait_for_completion_timeout(
  123. &drv->st_register_completed,
  124. msecs_to_jiffies(NFCWILINK_REGISTER_TIMEOUT));
  125. nfc_dev_dbg(&drv->pdev->dev,
  126. "wait_for_completion_timeout returned %ld",
  127. comp_ret);
  128. if (comp_ret == 0) {
  129. /* timeout */
  130. rc = -ETIMEDOUT;
  131. goto clear_exit;
  132. } else if (drv->st_register_cb_status != 0) {
  133. rc = drv->st_register_cb_status;
  134. nfc_dev_err(&drv->pdev->dev,
  135. "st_register_cb failed %d", rc);
  136. goto clear_exit;
  137. }
  138. } else {
  139. nfc_dev_err(&drv->pdev->dev,
  140. "st_register failed %d", rc);
  141. goto clear_exit;
  142. }
  143. }
  144. /* st_register MUST fill the write callback */
  145. BUG_ON(nfcwilink_proto.write == NULL);
  146. drv->st_write = nfcwilink_proto.write;
  147. goto exit;
  148. clear_exit:
  149. clear_bit(NFCWILINK_RUNNING, &drv->flags);
  150. exit:
  151. return rc;
  152. }
  153. static int nfcwilink_close(struct nci_dev *ndev)
  154. {
  155. struct nfcwilink *drv = nci_get_drvdata(ndev);
  156. int rc;
  157. nfc_dev_dbg(&drv->pdev->dev, "close entry");
  158. if (!test_and_clear_bit(NFCWILINK_RUNNING, &drv->flags))
  159. return 0;
  160. rc = st_unregister(&nfcwilink_proto);
  161. if (rc)
  162. nfc_dev_err(&drv->pdev->dev, "st_unregister failed %d", rc);
  163. drv->st_write = NULL;
  164. return rc;
  165. }
  166. static int nfcwilink_send(struct sk_buff *skb)
  167. {
  168. struct nci_dev *ndev = (struct nci_dev *)skb->dev;
  169. struct nfcwilink *drv = nci_get_drvdata(ndev);
  170. struct nfcwilink_hdr hdr = {NFCWILINK_CHNL, NFCWILINK_OPCODE, 0x0000};
  171. long len;
  172. nfc_dev_dbg(&drv->pdev->dev, "send entry, len %d", skb->len);
  173. if (!test_bit(NFCWILINK_RUNNING, &drv->flags))
  174. return -EBUSY;
  175. /* add the ST hdr to the start of the buffer */
  176. hdr.len = cpu_to_le16(skb->len);
  177. memcpy(skb_push(skb, NFCWILINK_HDR_LEN), &hdr, NFCWILINK_HDR_LEN);
  178. /* Insert skb to shared transport layer's transmit queue.
  179. * Freeing skb memory is taken care in shared transport layer,
  180. * so don't free skb memory here.
  181. */
  182. len = drv->st_write(skb);
  183. if (len < 0) {
  184. kfree_skb(skb);
  185. nfc_dev_err(&drv->pdev->dev, "st_write failed %ld", len);
  186. return -EFAULT;
  187. }
  188. return 0;
  189. }
  190. static struct nci_ops nfcwilink_ops = {
  191. .open = nfcwilink_open,
  192. .close = nfcwilink_close,
  193. .send = nfcwilink_send,
  194. };
  195. static int nfcwilink_probe(struct platform_device *pdev)
  196. {
  197. static struct nfcwilink *drv;
  198. int rc;
  199. __u32 protocols;
  200. nfc_dev_dbg(&pdev->dev, "probe entry");
  201. drv = kzalloc(sizeof(struct nfcwilink), GFP_KERNEL);
  202. if (!drv) {
  203. rc = -ENOMEM;
  204. goto exit;
  205. }
  206. drv->pdev = pdev;
  207. protocols = NFC_PROTO_JEWEL_MASK
  208. | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
  209. | NFC_PROTO_ISO14443_MASK
  210. | NFC_PROTO_NFC_DEP_MASK;
  211. drv->ndev = nci_allocate_device(&nfcwilink_ops,
  212. protocols,
  213. NFCWILINK_HDR_LEN,
  214. 0);
  215. if (!drv->ndev) {
  216. nfc_dev_err(&pdev->dev, "nci_allocate_device failed");
  217. rc = -ENOMEM;
  218. goto free_exit;
  219. }
  220. nci_set_parent_dev(drv->ndev, &pdev->dev);
  221. nci_set_drvdata(drv->ndev, drv);
  222. rc = nci_register_device(drv->ndev);
  223. if (rc < 0) {
  224. nfc_dev_err(&pdev->dev, "nci_register_device failed %d", rc);
  225. goto free_dev_exit;
  226. }
  227. dev_set_drvdata(&pdev->dev, drv);
  228. goto exit;
  229. free_dev_exit:
  230. nci_free_device(drv->ndev);
  231. free_exit:
  232. kfree(drv);
  233. exit:
  234. return rc;
  235. }
  236. static int nfcwilink_remove(struct platform_device *pdev)
  237. {
  238. struct nfcwilink *drv = dev_get_drvdata(&pdev->dev);
  239. struct nci_dev *ndev;
  240. nfc_dev_dbg(&pdev->dev, "remove entry");
  241. if (!drv)
  242. return -EFAULT;
  243. ndev = drv->ndev;
  244. nci_unregister_device(ndev);
  245. nci_free_device(ndev);
  246. kfree(drv);
  247. dev_set_drvdata(&pdev->dev, NULL);
  248. return 0;
  249. }
  250. static struct platform_driver nfcwilink_driver = {
  251. .probe = nfcwilink_probe,
  252. .remove = nfcwilink_remove,
  253. .driver = {
  254. .name = "nfcwilink",
  255. .owner = THIS_MODULE,
  256. },
  257. };
  258. /* ------- Module Init/Exit interfaces ------ */
  259. static int __init nfcwilink_init(void)
  260. {
  261. printk(KERN_INFO "NFC Driver for TI WiLink");
  262. return platform_driver_register(&nfcwilink_driver);
  263. }
  264. static void __exit nfcwilink_exit(void)
  265. {
  266. platform_driver_unregister(&nfcwilink_driver);
  267. }
  268. module_init(nfcwilink_init);
  269. module_exit(nfcwilink_exit);
  270. /* ------ Module Info ------ */
  271. MODULE_AUTHOR("Ilan Elias <ilane@ti.com>");
  272. MODULE_DESCRIPTION("NFC Driver for TI Shared Transport");
  273. MODULE_LICENSE("GPL");