nfcwilink.c 7.7 KB

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