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