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/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
{
50 struct platform_device
*pdev
;
54 char st_register_cb_status
;
55 long (*st_write
) (struct sk_buff
*);
56 struct completion st_register_completed
;
59 /* NFCWILINK driver flags */
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
;
84 nfc_dev_dbg(&drv
->pdev
->dev
, "receive entry, len %d", skb
->len
);
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
);
103 nfc_dev_err(&drv
->pdev
->dev
, "nci_recv_frame failed %d", rc
);
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
,
118 .recv
= nfcwilink_receive
,
119 .reg_complete_cb
= nfcwilink_register_complete
,
123 static int nfcwilink_open(struct nci_dev
*ndev
)
125 struct nfcwilink
*drv
= nci_get_drvdata(ndev
);
126 unsigned long comp_ret
;
129 nfc_dev_dbg(&drv
->pdev
->dev
, "open entry");
131 if (test_and_set_bit(NFCWILINK_RUNNING
, &drv
->flags
)) {
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
);
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",
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
);
163 nfc_dev_err(&drv
->pdev
->dev
,
164 "st_register failed %d", rc
);
169 /* st_register MUST fill the write callback */
170 BUG_ON(nfcwilink_proto
.write
== NULL
);
171 drv
->st_write
= nfcwilink_proto
.write
;
176 clear_bit(NFCWILINK_RUNNING
, &drv
->flags
);
182 static int nfcwilink_close(struct nci_dev
*ndev
)
184 struct nfcwilink
*drv
= nci_get_drvdata(ndev
);
187 nfc_dev_dbg(&drv
->pdev
->dev
, "close entry");
189 if (!test_and_clear_bit(NFCWILINK_RUNNING
, &drv
->flags
))
192 rc
= st_unregister(&nfcwilink_proto
);
194 nfc_dev_err(&drv
->pdev
->dev
, "st_unregister failed %d", rc
);
196 drv
->st_write
= NULL
;
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};
208 nfc_dev_dbg(&drv
->pdev
->dev
, "send entry, len %d", skb
->len
);
210 if (!test_bit(NFCWILINK_RUNNING
, &drv
->flags
))
213 /* add the ST hdr to the start of the buffer */
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
);
224 nfc_dev_err(&drv
->pdev
->dev
, "st_write failed %ld", len
);
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
;
243 nfc_dev_dbg(&pdev
->dev
, "probe entry");
245 drv
= kzalloc(sizeof(struct nfcwilink
), GFP_KERNEL
);
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
,
263 nfc_dev_err(&pdev
->dev
, "nci_allocate_device failed");
268 nci_set_parent_dev(drv
->ndev
, &pdev
->dev
);
269 nci_set_drvdata(drv
->ndev
, drv
);
271 rc
= nci_register_device(drv
->ndev
);
273 nfc_dev_err(&pdev
->dev
, "nci_register_device failed %d", rc
);
277 dev_set_drvdata(&pdev
->dev
, drv
);
282 nci_free_device(drv
->ndev
);
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");
303 nci_unregister_device(ndev
);
304 nci_free_device(ndev
);
308 dev_set_drvdata(&pdev
->dev
, NULL
);
313 static struct platform_driver nfcwilink_driver
= {
314 .probe
= nfcwilink_probe
,
315 .remove
= nfcwilink_remove
,
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");