nfcwilink.c 7.7 KB

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