2 * Texas Instrument's NFC Driver For Shared Transport.
4 * NFC Driver acts as interface between NCI core and
5 * TI Shared Transport Layer.
7 * Copyright (C) 2011 Texas Instruments, Inc.
9 * Written by Ilan Elias <ilane@ti.com>
12 * This file is based on btwilink.c, which was written
13 * by Raja Mani and Pavan Savoy.
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.
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.
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
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>
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 */
44 struct nfcwilink_hdr {
51 struct platform_device *pdev;
55 char st_register_cb_status;
56 long (*st_write) (struct sk_buff *);
57 struct completion st_register_completed;
60 /* NFCWILINK driver flags */
65 /* Called by ST when registration is complete */
66 static void nfcwilink_register_complete(void *priv_data, char data)
68 struct nfcwilink *drv = priv_data;
70 nfc_dev_dbg(&drv->pdev->dev, "register_complete entry");
72 /* store ST registration status */
73 drv->st_register_cb_status = data;
75 /* complete the wait in nfc_st_open() */
76 complete(&drv->st_register_completed);
79 /* Called by ST when receive data is available */
80 static long nfcwilink_receive(void *priv_data, struct sk_buff *skb)
82 struct nfcwilink *drv = priv_data;
85 nfc_dev_dbg(&drv->pdev->dev, "receive entry, len %d", skb->len);
95 /* strip the ST header
96 (apart for the chnl byte, which is not received in the hdr) */
97 skb_pull(skb, (NFCWILINK_HDR_LEN-1));
99 skb->dev = (void *) drv->ndev;
101 /* Forward skb to NCI core layer */
102 rc = nci_recv_frame(skb);
104 nfc_dev_err(&drv->pdev->dev, "nci_recv_frame failed %d", rc);
111 /* protocol structure registered with ST */
112 static struct st_proto_s nfcwilink_proto = {
113 .chnl_id = NFCWILINK_CHNL,
114 .max_frame_size = NFCWILINK_MAX_FRAME_SIZE,
115 .hdr_len = (NFCWILINK_HDR_LEN-1), /* not including chnl byte */
116 .offset_len_in_hdr = NFCWILINK_OFFSET_LEN_IN_HDR,
117 .len_size = NFCWILINK_LEN_SIZE,
119 .recv = nfcwilink_receive,
120 .reg_complete_cb = nfcwilink_register_complete,
124 static int nfcwilink_open(struct nci_dev *ndev)
126 struct nfcwilink *drv = nci_get_drvdata(ndev);
127 unsigned long comp_ret;
130 nfc_dev_dbg(&drv->pdev->dev, "open entry");
132 if (test_and_set_bit(NFCWILINK_RUNNING, &drv->flags)) {
137 nfcwilink_proto.priv_data = drv;
139 init_completion(&drv->st_register_completed);
140 drv->st_register_cb_status = -EINPROGRESS;
142 rc = st_register(&nfcwilink_proto);
144 if (rc == -EINPROGRESS) {
145 comp_ret = wait_for_completion_timeout(
146 &drv->st_register_completed,
147 msecs_to_jiffies(NFCWILINK_REGISTER_TIMEOUT));
149 nfc_dev_dbg(&drv->pdev->dev,
150 "wait_for_completion_timeout returned %ld",
157 } else if (drv->st_register_cb_status != 0) {
158 rc = drv->st_register_cb_status;
159 nfc_dev_err(&drv->pdev->dev,
160 "st_register_cb failed %d", rc);
164 nfc_dev_err(&drv->pdev->dev,
165 "st_register failed %d", rc);
170 /* st_register MUST fill the write callback */
171 BUG_ON(nfcwilink_proto.write == NULL);
172 drv->st_write = nfcwilink_proto.write;
177 clear_bit(NFCWILINK_RUNNING, &drv->flags);
183 static int nfcwilink_close(struct nci_dev *ndev)
185 struct nfcwilink *drv = nci_get_drvdata(ndev);
188 nfc_dev_dbg(&drv->pdev->dev, "close entry");
190 if (!test_and_clear_bit(NFCWILINK_RUNNING, &drv->flags))
193 rc = st_unregister(&nfcwilink_proto);
195 nfc_dev_err(&drv->pdev->dev, "st_unregister failed %d", rc);
197 drv->st_write = NULL;
202 static int nfcwilink_send(struct sk_buff *skb)
204 struct nci_dev *ndev = (struct nci_dev *)skb->dev;
205 struct nfcwilink *drv = nci_get_drvdata(ndev);
206 struct nfcwilink_hdr hdr = {NFCWILINK_CHNL, NFCWILINK_OPCODE, 0x0000};
209 nfc_dev_dbg(&drv->pdev->dev, "send entry, len %d", skb->len);
211 if (!test_bit(NFCWILINK_RUNNING, &drv->flags))
214 /* add the ST hdr to the start of the buffer */
216 memcpy(skb_push(skb, NFCWILINK_HDR_LEN), &hdr, NFCWILINK_HDR_LEN);
218 /* Insert skb to shared transport layer's transmit queue.
219 * Freeing skb memory is taken care in shared transport layer,
220 * so don't free skb memory here.
222 len = drv->st_write(skb);
225 nfc_dev_err(&drv->pdev->dev, "st_write failed %ld", len);
232 static struct nci_ops nfcwilink_ops = {
233 .open = nfcwilink_open,
234 .close = nfcwilink_close,
235 .send = nfcwilink_send,
238 static int nfcwilink_probe(struct platform_device *pdev)
240 static struct nfcwilink *drv;
244 nfc_dev_dbg(&pdev->dev, "probe entry");
246 drv = kzalloc(sizeof(struct nfcwilink), GFP_KERNEL);
254 protocols = NFC_PROTO_JEWEL_MASK
255 | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
256 | NFC_PROTO_ISO14443_MASK
257 | NFC_PROTO_NFC_DEP_MASK;
259 drv->ndev = nci_allocate_device(&nfcwilink_ops,
264 nfc_dev_err(&pdev->dev, "nci_allocate_device failed");
269 nci_set_parent_dev(drv->ndev, &pdev->dev);
270 nci_set_drvdata(drv->ndev, drv);
272 rc = nci_register_device(drv->ndev);
274 nfc_dev_err(&pdev->dev, "nci_register_device failed %d", rc);
278 dev_set_drvdata(&pdev->dev, drv);
283 nci_free_device(drv->ndev);
292 static int nfcwilink_remove(struct platform_device *pdev)
294 struct nfcwilink *drv = dev_get_drvdata(&pdev->dev);
295 struct nci_dev *ndev;
297 nfc_dev_dbg(&pdev->dev, "remove entry");
304 nci_unregister_device(ndev);
305 nci_free_device(ndev);
309 dev_set_drvdata(&pdev->dev, NULL);
314 static struct platform_driver nfcwilink_driver = {
315 .probe = nfcwilink_probe,
316 .remove = nfcwilink_remove,
319 .owner = THIS_MODULE,
323 /* ------- Module Init/Exit interfaces ------ */
324 static int __init nfcwilink_init(void)
326 printk(KERN_INFO "NFC Driver for TI WiLink");
328 return platform_driver_register(&nfcwilink_driver);
331 static void __exit nfcwilink_exit(void)
333 platform_driver_unregister(&nfcwilink_driver);
336 module_init(nfcwilink_init);
337 module_exit(nfcwilink_exit);
339 /* ------ Module Info ------ */
341 MODULE_AUTHOR("Ilan Elias <ilane@ti.com>");
342 MODULE_DESCRIPTION("NFC Driver for TI Shared Transport");
343 MODULE_LICENSE("GPL");