2 * Texas Instrument's Bluetooth Driver For Shared Transport.
4 * Bluetooth Driver acts as interface between HCI core and
5 * TI Shared Transport Layer.
7 * Copyright (C) 2009-2010 Texas Instruments
8 * Author: Raja Mani <raja_mani@ti.com>
9 * Pavan Savoy <pavan_savoy@ti.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/platform_device.h>
27 #include <net/bluetooth/bluetooth.h>
28 #include <net/bluetooth/hci_core.h>
30 #include <linux/ti_wilink_st.h>
32 /* Bluetooth Driver Version */
35 /* Number of seconds to wait for registration completion
36 * when ST returns PENDING status.
38 #define BT_REGISTER_TIMEOUT 6000 /* 6 sec */
41 * struct ti_st - driver operation structure
42 * @hdev: hci device pointer which binds to bt driver
43 * @reg_status: ST registration callback status
44 * @st_write: write function provided by the ST driver
45 * to be used by the driver during send_frame.
46 * @wait_reg_completion - completion sync between ti_st_open
47 * and ti_st_registration_completion_cb.
52 long (*st_write
) (struct sk_buff
*);
53 struct completion wait_reg_completion
;
56 /* Increments HCI counters based on pocket ID (cmd,acl,sco) */
57 static inline void ti_st_tx_complete(struct ti_st
*hst
, int pkt_type
)
59 struct hci_dev
*hdev
= hst
->hdev
;
61 /* Update HCI stat counters */
77 /* ------- Interfaces to Shared Transport ------ */
79 /* Called by ST layer to indicate protocol registration completion
80 * status.ti_st_open() function will wait for signal from this
81 * API when st_register() function returns ST_PENDING.
83 static void st_registration_completion_cb(void *priv_data
, char data
)
85 struct ti_st
*lhst
= priv_data
;
87 /* Save registration status for use in ti_st_open() */
88 lhst
->reg_status
= data
;
89 /* complete the wait in ti_st_open() */
90 complete(&lhst
->wait_reg_completion
);
93 /* Called by Shared Transport layer when receive data is
95 static long st_receive(void *priv_data
, struct sk_buff
*skb
)
97 struct ti_st
*lhst
= priv_data
;
108 skb
->dev
= (void *) lhst
->hdev
;
110 /* Forward skb to HCI core layer */
111 err
= hci_recv_frame(skb
);
113 BT_ERR("Unable to push skb to HCI core(%d)", err
);
117 lhst
->hdev
->stat
.byte_rx
+= skb
->len
;
122 /* ------- Interfaces to HCI layer ------ */
123 /* protocol structure registered with shared transport */
124 static struct st_proto_s ti_st_proto
= {
127 .reg_complete_cb
= st_registration_completion_cb
,
130 /* Called from HCI core to initialize the device */
131 static int ti_st_open(struct hci_dev
*hdev
)
133 unsigned long timeleft
;
137 BT_DBG("%s %p", hdev
->name
, hdev
);
138 if (test_and_set_bit(HCI_RUNNING
, &hdev
->flags
)) {
139 BT_ERR("btwilink already opened");
143 /* provide contexts for callbacks from ST */
144 hst
= hdev
->driver_data
;
145 ti_st_proto
.priv_data
= hst
;
147 err
= st_register(&ti_st_proto
);
148 if (err
== -EINPROGRESS
) {
149 /* ST is busy with either protocol registration or firmware
152 /* Prepare wait-for-completion handler data structures.
154 init_completion(&hst
->wait_reg_completion
);
156 /* Reset ST registration callback status flag , this value
157 * will be updated in ti_st_registration_completion_cb()
158 * function whenever it called from ST driver.
160 hst
->reg_status
= -EINPROGRESS
;
162 BT_DBG("waiting for registration completion signal from ST");
163 timeleft
= wait_for_completion_timeout
164 (&hst
->wait_reg_completion
,
165 msecs_to_jiffies(BT_REGISTER_TIMEOUT
));
167 clear_bit(HCI_RUNNING
, &hdev
->flags
);
168 BT_ERR("Timeout(%d sec),didn't get reg "
169 "completion signal from ST",
170 BT_REGISTER_TIMEOUT
/ 1000);
174 /* Is ST registration callback called with ERROR status? */
175 if (hst
->reg_status
!= 0) {
176 clear_bit(HCI_RUNNING
, &hdev
->flags
);
177 BT_ERR("ST registration completed with invalid "
178 "status %d", hst
->reg_status
);
182 } else if (err
!= 0) {
183 clear_bit(HCI_RUNNING
, &hdev
->flags
);
184 BT_ERR("st_register failed %d", err
);
188 /* ti_st_proto.write is filled up by the underlying shared
189 * transport driver upon registration
191 hst
->st_write
= ti_st_proto
.write
;
192 if (!hst
->st_write
) {
193 BT_ERR("undefined ST write function");
194 clear_bit(HCI_RUNNING
, &hdev
->flags
);
196 /* Undo registration with ST */
197 err
= st_unregister(ST_BT
);
199 BT_ERR("st_unregister() failed with error %d", err
);
201 hst
->st_write
= NULL
;
209 static int ti_st_close(struct hci_dev
*hdev
)
212 struct ti_st
*hst
= hdev
->driver_data
;
214 if (!test_and_clear_bit(HCI_RUNNING
, &hdev
->flags
))
217 /* continue to unregister from transport */
218 err
= st_unregister(ST_BT
);
220 BT_ERR("st_unregister() failed with error %d", err
);
222 hst
->st_write
= NULL
;
227 static int ti_st_send_frame(struct sk_buff
*skb
)
229 struct hci_dev
*hdev
;
233 hdev
= (struct hci_dev
*)skb
->dev
;
235 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
238 hst
= hdev
->driver_data
;
240 /* Prepend skb with frame type */
241 memcpy(skb_push(skb
, 1), &bt_cb(skb
)->pkt_type
, 1);
243 BT_DBG("%s: type %d len %d", hdev
->name
, bt_cb(skb
)->pkt_type
,
246 /* Insert skb to shared transport layer's transmit queue.
247 * Freeing skb memory is taken care in shared transport layer,
248 * so don't free skb memory here.
250 len
= hst
->st_write(skb
);
253 BT_ERR("ST write failed (%ld)", len
);
254 /* Try Again, would only fail if UART has gone bad */
258 /* ST accepted our skb. So, Go ahead and do rest */
259 hdev
->stat
.byte_tx
+= len
;
260 ti_st_tx_complete(hst
, bt_cb(skb
)->pkt_type
);
265 static void ti_st_destruct(struct hci_dev
*hdev
)
267 BT_DBG("%s", hdev
->name
);
268 kfree(hdev
->driver_data
);
271 static int bt_ti_probe(struct platform_device
*pdev
)
273 static struct ti_st
*hst
;
274 struct hci_dev
*hdev
;
277 hst
= kzalloc(sizeof(struct ti_st
), GFP_KERNEL
);
281 /* Expose "hciX" device to user space */
282 hdev
= hci_alloc_dev();
288 BT_DBG("hdev %p", hdev
);
291 hdev
->bus
= HCI_UART
;
292 hdev
->driver_data
= hst
;
293 hdev
->open
= ti_st_open
;
294 hdev
->close
= ti_st_close
;
296 hdev
->send
= ti_st_send_frame
;
297 hdev
->destruct
= ti_st_destruct
;
298 hdev
->owner
= THIS_MODULE
;
300 err
= hci_register_dev(hdev
);
302 BT_ERR("Can't register HCI device error %d", err
);
308 BT_DBG("HCI device registered (hdev %p)", hdev
);
310 dev_set_drvdata(&pdev
->dev
, hst
);
314 static int bt_ti_remove(struct platform_device
*pdev
)
316 struct hci_dev
*hdev
;
317 struct ti_st
*hst
= dev_get_drvdata(&pdev
->dev
);
324 hci_unregister_dev(hdev
);
329 dev_set_drvdata(&pdev
->dev
, NULL
);
333 static struct platform_driver btwilink_driver
= {
334 .probe
= bt_ti_probe
,
335 .remove
= bt_ti_remove
,
338 .owner
= THIS_MODULE
,
342 /* ------- Module Init/Exit interfaces ------ */
343 static int __init
btwilink_init(void)
345 BT_INFO("Bluetooth Driver for TI WiLink - Version %s", VERSION
);
347 return platform_driver_register(&btwilink_driver
);
350 static void __exit
btwilink_exit(void)
352 platform_driver_unregister(&btwilink_driver
);
355 module_init(btwilink_init
);
356 module_exit(btwilink_exit
);
358 /* ------ Module Info ------ */
360 MODULE_AUTHOR("Raja Mani <raja_mani@ti.com>");
361 MODULE_DESCRIPTION("Bluetooth Driver for TI Shared Transport" VERSION
);
362 MODULE_VERSION(VERSION
);
363 MODULE_LICENSE("GPL");