5b0f1ff8036133c50a4427dfa90e08ec9d54cde7
[linux-2.6.git] / drivers / nfc / nfcwilink.c
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
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
43 struct nfcwilink_hdr {
44         u8 chnl;
45         u8 opcode;
46         u16 len;
47 } __packed;
48
49 struct nfcwilink {
50         struct platform_device          *pdev;
51         struct nci_dev                  *ndev;
52         unsigned long                   flags;
53
54         char                            st_register_cb_status;
55         long                            (*st_write) (struct sk_buff *);
56         struct completion               st_register_completed;
57 };
58
59 /* NFCWILINK driver flags */
60 enum {
61         NFCWILINK_RUNNING,
62 };
63
64 /* Called by ST when registration is complete */
65 static void nfcwilink_register_complete(void *priv_data, char data)
66 {
67         struct nfcwilink *drv = priv_data;
68
69         nfc_dev_dbg(&drv->pdev->dev, "register_complete entry");
70
71         /* store ST registration status */
72         drv->st_register_cb_status = data;
73
74         /* complete the wait in nfc_st_open() */
75         complete(&drv->st_register_completed);
76 }
77
78 /* Called by ST when receive data is available */
79 static long nfcwilink_receive(void *priv_data, struct sk_buff *skb)
80 {
81         struct nfcwilink *drv = priv_data;
82         int rc;
83
84         nfc_dev_dbg(&drv->pdev->dev, "receive entry, len %d", skb->len);
85
86         if (!skb)
87                 return -EFAULT;
88
89         if (!drv) {
90                 kfree_skb(skb);
91                 return -EFAULT;
92         }
93
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));
97
98         skb->dev = (void *) drv->ndev;
99
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;
105         }
106
107         return 0;
108 }
109
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,
121 };
122
123 static int nfcwilink_open(struct nci_dev *ndev)
124 {
125         struct nfcwilink *drv = nci_get_drvdata(ndev);
126         unsigned long comp_ret;
127         int rc;
128
129         nfc_dev_dbg(&drv->pdev->dev, "open entry");
130
131         if (test_and_set_bit(NFCWILINK_RUNNING, &drv->flags)) {
132                 rc = -EBUSY;
133                 goto exit;
134         }
135
136         nfcwilink_proto.priv_data = drv;
137
138         init_completion(&drv->st_register_completed);
139         drv->st_register_cb_status = -EINPROGRESS;
140
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));
147
148                         nfc_dev_dbg(&drv->pdev->dev,
149                         "wait_for_completion_timeout returned %ld",
150                         comp_ret);
151
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;
161                         }
162                 } else {
163                         nfc_dev_err(&drv->pdev->dev,
164                                 "st_register failed %d", rc);
165                         goto clear_exit;
166                 }
167         }
168
169         /* st_register MUST fill the write callback */
170         BUG_ON(nfcwilink_proto.write == NULL);
171         drv->st_write = nfcwilink_proto.write;
172
173         goto exit;
174
175 clear_exit:
176         clear_bit(NFCWILINK_RUNNING, &drv->flags);
177
178 exit:
179         return rc;
180 }
181
182 static int nfcwilink_close(struct nci_dev *ndev)
183 {
184         struct nfcwilink *drv = nci_get_drvdata(ndev);
185         int rc;
186
187         nfc_dev_dbg(&drv->pdev->dev, "close entry");
188
189         if (!test_and_clear_bit(NFCWILINK_RUNNING, &drv->flags))
190                 return 0;
191
192         rc = st_unregister(&nfcwilink_proto);
193         if (rc)
194                 nfc_dev_err(&drv->pdev->dev, "st_unregister failed %d", rc);
195
196         drv->st_write = NULL;
197
198         return rc;
199 }
200
201 static int nfcwilink_send(struct sk_buff *skb)
202 {
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;
207
208         nfc_dev_dbg(&drv->pdev->dev, "send entry, len %d", skb->len);
209
210         if (!test_bit(NFCWILINK_RUNNING, &drv->flags))
211                 return -EBUSY;
212
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);
216
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.
220          */
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;
226         }
227
228         return 0;
229 }
230
231 static struct nci_ops nfcwilink_ops = {
232         .open = nfcwilink_open,
233         .close = nfcwilink_close,
234         .send = nfcwilink_send,
235 };
236
237 static int nfcwilink_probe(struct platform_device *pdev)
238 {
239         static struct nfcwilink *drv;
240         int rc;
241         u32 protocols;
242
243         nfc_dev_dbg(&pdev->dev, "probe entry");
244
245         drv = kzalloc(sizeof(struct nfcwilink), GFP_KERNEL);
246         if (!drv) {
247                 rc = -ENOMEM;
248                 goto exit;
249         }
250
251         drv->pdev = pdev;
252
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;
257
258         drv->ndev = nci_allocate_device(&nfcwilink_ops,
259                                         protocols,
260                                         NFCWILINK_HDR_LEN,
261                                         0);
262         if (!drv->ndev) {
263                 nfc_dev_err(&pdev->dev, "nci_allocate_device failed");
264                 rc = -ENOMEM;
265                 goto free_exit;
266         }
267
268         nci_set_parent_dev(drv->ndev, &pdev->dev);
269         nci_set_drvdata(drv->ndev, drv);
270
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;
275         }
276
277         dev_set_drvdata(&pdev->dev, drv);
278
279         goto exit;
280
281 free_dev_exit:
282         nci_free_device(drv->ndev);
283
284 free_exit:
285         kfree(drv);
286
287 exit:
288         return rc;
289 }
290
291 static int nfcwilink_remove(struct platform_device *pdev)
292 {
293         struct nfcwilink *drv = dev_get_drvdata(&pdev->dev);
294         struct nci_dev *ndev;
295
296         nfc_dev_dbg(&pdev->dev, "remove entry");
297
298         if (!drv)
299                 return -EFAULT;
300
301         ndev = drv->ndev;
302
303         nci_unregister_device(ndev);
304         nci_free_device(ndev);
305
306         kfree(drv);
307
308         dev_set_drvdata(&pdev->dev, NULL);
309
310         return 0;
311 }
312
313 static struct platform_driver nfcwilink_driver = {
314         .probe = nfcwilink_probe,
315         .remove = nfcwilink_remove,
316         .driver = {
317                 .name = "nfcwilink",
318                 .owner = THIS_MODULE,
319         },
320 };
321
322 /* ------- Module Init/Exit interfaces ------ */
323 static int __init nfcwilink_init(void)
324 {
325         printk(KERN_INFO "NFC Driver for TI WiLink");
326
327         return platform_driver_register(&nfcwilink_driver);
328 }
329
330 static void __exit nfcwilink_exit(void)
331 {
332         platform_driver_unregister(&nfcwilink_driver);
333 }
334
335 module_init(nfcwilink_init);
336 module_exit(nfcwilink_exit);
337
338 /* ------ Module Info ------ */
339
340 MODULE_AUTHOR("Ilan Elias <ilane@ti.com>");
341 MODULE_DESCRIPTION("NFC Driver for TI Shared Transport");
342 MODULE_LICENSE("GPL");