Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / nfc / nfcwilink.c
blob5b0f1ff8036133c50a4427dfa90e08ec9d54cde7
1 /*
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>
11 * Acknowledgements:
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/nfc.h>
31 #include <net/nfc/nci.h>
32 #include <net/nfc/nci_core.h>
33 #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 */
43 struct nfcwilink_hdr {
44 u8 chnl;
45 u8 opcode;
46 u16 len;
47 } __packed;
49 struct nfcwilink {
50 struct platform_device *pdev;
51 struct nci_dev *ndev;
52 unsigned long flags;
54 char st_register_cb_status;
55 long (*st_write) (struct sk_buff *);
56 struct completion st_register_completed;
59 /* NFCWILINK driver flags */
60 enum {
61 NFCWILINK_RUNNING,
64 /* Called by ST when registration is complete */
65 static void nfcwilink_register_complete(void *priv_data, char data)
67 struct nfcwilink *drv = priv_data;
69 nfc_dev_dbg(&drv->pdev->dev, "register_complete entry");
71 /* store ST registration status */
72 drv->st_register_cb_status = data;
74 /* complete the wait in nfc_st_open() */
75 complete(&drv->st_register_completed);
78 /* Called by ST when receive data is available */
79 static long nfcwilink_receive(void *priv_data, struct sk_buff *skb)
81 struct nfcwilink *drv = priv_data;
82 int rc;
84 nfc_dev_dbg(&drv->pdev->dev, "receive entry, len %d", skb->len);
86 if (!skb)
87 return -EFAULT;
89 if (!drv) {
90 kfree_skb(skb);
91 return -EFAULT;
94 /* strip the ST header
95 (apart for the chnl byte, which is not received in the hdr) */
96 skb_pull(skb, (NFCWILINK_HDR_LEN-1));
98 skb->dev = (void *) drv->ndev;
100 /* Forward skb to NCI core layer */
101 rc = nci_recv_frame(skb);
102 if (rc < 0) {
103 nfc_dev_err(&drv->pdev->dev, "nci_recv_frame failed %d", rc);
104 return rc;
107 return 0;
110 /* protocol structure registered with ST */
111 static struct st_proto_s nfcwilink_proto = {
112 .chnl_id = NFCWILINK_CHNL,
113 .max_frame_size = NFCWILINK_MAX_FRAME_SIZE,
114 .hdr_len = (NFCWILINK_HDR_LEN-1), /* not including chnl byte */
115 .offset_len_in_hdr = NFCWILINK_OFFSET_LEN_IN_HDR,
116 .len_size = NFCWILINK_LEN_SIZE,
117 .reserve = 0,
118 .recv = nfcwilink_receive,
119 .reg_complete_cb = nfcwilink_register_complete,
120 .write = NULL,
123 static int nfcwilink_open(struct nci_dev *ndev)
125 struct nfcwilink *drv = nci_get_drvdata(ndev);
126 unsigned long comp_ret;
127 int rc;
129 nfc_dev_dbg(&drv->pdev->dev, "open entry");
131 if (test_and_set_bit(NFCWILINK_RUNNING, &drv->flags)) {
132 rc = -EBUSY;
133 goto exit;
136 nfcwilink_proto.priv_data = drv;
138 init_completion(&drv->st_register_completed);
139 drv->st_register_cb_status = -EINPROGRESS;
141 rc = st_register(&nfcwilink_proto);
142 if (rc < 0) {
143 if (rc == -EINPROGRESS) {
144 comp_ret = wait_for_completion_timeout(
145 &drv->st_register_completed,
146 msecs_to_jiffies(NFCWILINK_REGISTER_TIMEOUT));
148 nfc_dev_dbg(&drv->pdev->dev,
149 "wait_for_completion_timeout returned %ld",
150 comp_ret);
152 if (comp_ret == 0) {
153 /* timeout */
154 rc = -ETIMEDOUT;
155 goto clear_exit;
156 } else if (drv->st_register_cb_status != 0) {
157 rc = drv->st_register_cb_status;
158 nfc_dev_err(&drv->pdev->dev,
159 "st_register_cb failed %d", rc);
160 goto clear_exit;
162 } else {
163 nfc_dev_err(&drv->pdev->dev,
164 "st_register failed %d", rc);
165 goto clear_exit;
169 /* st_register MUST fill the write callback */
170 BUG_ON(nfcwilink_proto.write == NULL);
171 drv->st_write = nfcwilink_proto.write;
173 goto exit;
175 clear_exit:
176 clear_bit(NFCWILINK_RUNNING, &drv->flags);
178 exit:
179 return rc;
182 static int nfcwilink_close(struct nci_dev *ndev)
184 struct nfcwilink *drv = nci_get_drvdata(ndev);
185 int rc;
187 nfc_dev_dbg(&drv->pdev->dev, "close entry");
189 if (!test_and_clear_bit(NFCWILINK_RUNNING, &drv->flags))
190 return 0;
192 rc = st_unregister(&nfcwilink_proto);
193 if (rc)
194 nfc_dev_err(&drv->pdev->dev, "st_unregister failed %d", rc);
196 drv->st_write = NULL;
198 return rc;
201 static int nfcwilink_send(struct sk_buff *skb)
203 struct nci_dev *ndev = (struct nci_dev *)skb->dev;
204 struct nfcwilink *drv = nci_get_drvdata(ndev);
205 struct nfcwilink_hdr hdr = {NFCWILINK_CHNL, NFCWILINK_OPCODE, 0x0000};
206 long len;
208 nfc_dev_dbg(&drv->pdev->dev, "send entry, len %d", skb->len);
210 if (!test_bit(NFCWILINK_RUNNING, &drv->flags))
211 return -EBUSY;
213 /* add the ST hdr to the start of the buffer */
214 hdr.len = skb->len;
215 memcpy(skb_push(skb, NFCWILINK_HDR_LEN), &hdr, NFCWILINK_HDR_LEN);
217 /* Insert skb to shared transport layer's transmit queue.
218 * Freeing skb memory is taken care in shared transport layer,
219 * so don't free skb memory here.
221 len = drv->st_write(skb);
222 if (len < 0) {
223 kfree_skb(skb);
224 nfc_dev_err(&drv->pdev->dev, "st_write failed %ld", len);
225 return -EFAULT;
228 return 0;
231 static struct nci_ops nfcwilink_ops = {
232 .open = nfcwilink_open,
233 .close = nfcwilink_close,
234 .send = nfcwilink_send,
237 static int nfcwilink_probe(struct platform_device *pdev)
239 static struct nfcwilink *drv;
240 int rc;
241 u32 protocols;
243 nfc_dev_dbg(&pdev->dev, "probe entry");
245 drv = kzalloc(sizeof(struct nfcwilink), GFP_KERNEL);
246 if (!drv) {
247 rc = -ENOMEM;
248 goto exit;
251 drv->pdev = pdev;
253 protocols = NFC_PROTO_JEWEL_MASK
254 | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
255 | NFC_PROTO_ISO14443_MASK
256 | NFC_PROTO_NFC_DEP_MASK;
258 drv->ndev = nci_allocate_device(&nfcwilink_ops,
259 protocols,
260 NFCWILINK_HDR_LEN,
262 if (!drv->ndev) {
263 nfc_dev_err(&pdev->dev, "nci_allocate_device failed");
264 rc = -ENOMEM;
265 goto free_exit;
268 nci_set_parent_dev(drv->ndev, &pdev->dev);
269 nci_set_drvdata(drv->ndev, drv);
271 rc = nci_register_device(drv->ndev);
272 if (rc < 0) {
273 nfc_dev_err(&pdev->dev, "nci_register_device failed %d", rc);
274 goto free_dev_exit;
277 dev_set_drvdata(&pdev->dev, drv);
279 goto exit;
281 free_dev_exit:
282 nci_free_device(drv->ndev);
284 free_exit:
285 kfree(drv);
287 exit:
288 return rc;
291 static int nfcwilink_remove(struct platform_device *pdev)
293 struct nfcwilink *drv = dev_get_drvdata(&pdev->dev);
294 struct nci_dev *ndev;
296 nfc_dev_dbg(&pdev->dev, "remove entry");
298 if (!drv)
299 return -EFAULT;
301 ndev = drv->ndev;
303 nci_unregister_device(ndev);
304 nci_free_device(ndev);
306 kfree(drv);
308 dev_set_drvdata(&pdev->dev, NULL);
310 return 0;
313 static struct platform_driver nfcwilink_driver = {
314 .probe = nfcwilink_probe,
315 .remove = nfcwilink_remove,
316 .driver = {
317 .name = "nfcwilink",
318 .owner = THIS_MODULE,
322 /* ------- Module Init/Exit interfaces ------ */
323 static int __init nfcwilink_init(void)
325 printk(KERN_INFO "NFC Driver for TI WiLink");
327 return platform_driver_register(&nfcwilink_driver);
330 static void __exit nfcwilink_exit(void)
332 platform_driver_unregister(&nfcwilink_driver);
335 module_init(nfcwilink_init);
336 module_exit(nfcwilink_exit);
338 /* ------ Module Info ------ */
340 MODULE_AUTHOR("Ilan Elias <ilane@ti.com>");
341 MODULE_DESCRIPTION("NFC Driver for TI Shared Transport");
342 MODULE_LICENSE("GPL");