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 Texas Instruments
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <net/bluetooth/bluetooth.h>
25 #include <net/bluetooth/hci_core.h>
30 /* Define this macro to get debug msg */
34 #define BT_DRV_DBG(fmt, arg...) printk(KERN_INFO "(btdrv):"fmt"\n" , ## arg)
35 #define BTDRV_API_START() printk(KERN_INFO "(btdrv): %s Start\n", \
37 #define BTDRV_API_EXIT(errno) printk(KERN_INFO "(btdrv): %s Exit(%d)\n", \
40 #define BT_DRV_DBG(fmt, arg...)
41 #define BTDRV_API_START()
42 #define BTDRV_API_EXIT(errno)
45 #define BT_DRV_ERR(fmt, arg...) printk(KERN_ERR "(btdrv):"fmt"\n" , ## arg)
48 static struct hci_st *hst;
50 /* Increments HCI counters based on pocket ID (cmd,acl,sco) */
51 static inline void hci_st_tx_complete(struct hci_st *hst, int pkt_type)
59 /* Update HCI stat counters */
77 /* ------- Interfaces to Shared Transport ------ */
79 /* Called by ST layer to indicate protocol registration completion
80 * status.hci_st_open() function will wait for signal from this
81 * API when st_register() function returns ST_PENDING.
83 static void hci_st_registration_completion_cb(char data)
87 /* hci_st_open() function needs value of 'data' to know
88 * the registration status(success/fail),So have a back
91 hst->streg_cbdata = data;
93 /* Got a feedback from ST for BT driver registration
94 * request.Wackup hci_st_open() function to continue
95 * it's open operation.
97 complete(&hst->wait_for_btdrv_reg_completion);
102 /* Called by Shared Transport layer when receive data is
104 static long hci_st_receive(struct sk_buff *skb)
115 BT_DRV_ERR("Invalid SKB received from ST");
116 BTDRV_API_EXIT(-EFAULT);
121 BT_DRV_ERR("Invalid hci_st memory,freeing SKB");
122 BTDRV_API_EXIT(-EFAULT);
125 if (!test_bit(BT_DRV_RUNNING, &hst->flags)) {
127 BT_DRV_ERR("Device is not running,freeing SKB");
128 BTDRV_API_EXIT(-EINVAL);
133 skb->dev = (struct net_device *)hst->hdev;
135 /* Forward skb to HCI CORE layer */
136 err = hci_recv_frame(skb);
139 BT_DRV_ERR("Unable to push skb to HCI CORE(%d),freeing SKB",
144 hst->hdev->stat.byte_rx += len;
150 /* ------- Interfaces to HCI layer ------ */
152 /* Called from HCI core to initialize the device */
153 static int hci_st_open(struct hci_dev *hdev)
155 static struct st_proto_s hci_st_proto;
156 unsigned long timeleft;
163 BT_DRV_DBG("%s %p", hdev->name, hdev);
165 /* Already registered with ST ? */
166 if (test_bit(BT_ST_REGISTERED, &hst->flags)) {
167 BT_DRV_ERR("Registered with ST already,open called again?");
172 /* Populate BT driver info required by ST */
173 memset(&hci_st_proto, 0, sizeof(hci_st_proto));
176 hci_st_proto.type = ST_BT;
178 /* Receive function which called from ST */
179 hci_st_proto.recv = hci_st_receive;
181 /* Packet match function may used in future */
182 hci_st_proto.match_packet = NULL;
184 /* Callback to be called when registration is pending */
185 hci_st_proto.reg_complete_cb = hci_st_registration_completion_cb;
187 /* This is write function pointer of ST. BT driver will make use of this
188 * for sending any packets to chip. ST will assign and give to us, so
190 hci_st_proto.write = NULL;
192 /* Register with ST layer */
193 err = st_register(&hci_st_proto);
194 if (err == ST_ERR_PENDING) {
195 /* Prepare wait-for-completion handler data structures.
196 * Needed to syncronize this and st_registration_completion_cb()
199 init_completion(&hst->wait_for_btdrv_reg_completion);
201 /* Reset ST registration callback status flag , this value
202 * will be updated in hci_st_registration_completion_cb()
203 * function whenever it called from ST driver.
205 hst->streg_cbdata = -EINPROGRESS;
207 /* ST is busy with other protocol registration(may be busy with
208 * firmware download).So,Wait till the registration callback
209 * (passed as a argument to st_register() function) getting
212 BT_DRV_DBG(" %s waiting for reg completion signal from ST",
216 wait_for_completion_timeout
217 (&hst->wait_for_btdrv_reg_completion,
218 msecs_to_jiffies(BT_REGISTER_TIMEOUT));
220 BT_DRV_ERR("Timeout(%ld sec),didn't get reg"
221 "completion signal from ST",
222 BT_REGISTER_TIMEOUT / 1000);
223 BTDRV_API_EXIT(-ETIMEDOUT);
227 /* Is ST registration callback called with ERROR value? */
228 if (hst->streg_cbdata != 0) {
229 BT_DRV_ERR("ST reg completion CB called with invalid"
230 "status %d", hst->streg_cbdata);
231 BTDRV_API_EXIT(-EAGAIN);
235 } else if (err == ST_ERR_FAILURE) {
236 BT_DRV_ERR("st_register failed %d", err);
237 BTDRV_API_EXIT(-EAGAIN);
241 /* Do we have proper ST write function? */
242 if (hci_st_proto.write != NULL) {
243 /* We need this pointer for sending any Bluetooth pkts */
244 hst->st_write = hci_st_proto.write;
246 BT_DRV_ERR("failed to get ST write func pointer");
248 /* Undo registration with ST */
249 err = st_unregister(ST_BT);
251 BT_DRV_ERR("st_unregister failed %d", err);
253 hst->st_write = NULL;
254 BTDRV_API_EXIT(-EAGAIN);
258 /* Registration with ST layer is completed successfully,
259 * now chip is ready to accept commands from HCI CORE.
260 * Mark HCI Device flag as RUNNING
262 set_bit(HCI_RUNNING, &hdev->flags);
264 /* Registration with ST successful */
265 set_bit(BT_ST_REGISTERED, &hst->flags);
272 static int hci_st_close(struct hci_dev *hdev)
280 /* Unregister from ST layer */
281 if (test_and_clear_bit(BT_ST_REGISTERED, &hst->flags)) {
282 err = st_unregister(ST_BT);
283 if (err != ST_SUCCESS) {
284 BT_DRV_ERR("st_unregister failed %d", err);
285 BTDRV_API_EXIT(-EBUSY);
290 hst->st_write = NULL;
292 /* ST layer would have moved chip to inactive state.
293 * So,clear HCI device RUNNING flag.
295 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) {
304 /* Called from HCI CORE , Sends frames to Shared Transport */
305 static int hci_st_send_frame(struct sk_buff *skb)
307 struct hci_dev *hdev;
314 BT_DRV_ERR("Invalid skb received from HCI CORE");
315 BTDRV_API_EXIT(-ENOMEM);
318 hdev = (struct hci_dev *)skb->dev;
320 BT_DRV_ERR("SKB received for invalid HCI Device (hdev=NULL)");
321 BTDRV_API_EXIT(-ENODEV);
324 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
325 BT_DRV_ERR("Device is not running");
326 BTDRV_API_EXIT(-EBUSY);
330 hst = (struct hci_st *)hdev->driver_data;
332 /* Prepend skb with frame type */
333 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
335 BT_DRV_DBG(" %s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type,
338 /* Insert skb to shared transport layer's transmit queue.
339 * Freeing skb memory is taken care in shared transport layer,
340 * so don't free skb memory here.
342 if (!hst->st_write) {
344 BT_DRV_ERR(" Can't write to ST, st_write null?");
345 BTDRV_API_EXIT(-EAGAIN);
348 len = hst->st_write(skb);
350 /* Something went wrong in st write , free skb memory */
352 BT_DRV_ERR(" ST write failed (%ld)", len);
353 BTDRV_API_EXIT(-EAGAIN);
357 /* ST accepted our skb. So, Go ahead and do rest */
358 hdev->stat.byte_tx += len;
359 hci_st_tx_complete(hst, bt_cb(skb)->pkt_type);
365 static void hci_st_destruct(struct hci_dev *hdev)
370 BT_DRV_ERR("Destruct called with invalid HCI Device"
376 BT_DRV_DBG("%s", hdev->name);
378 /* free hci_st memory */
379 if (hdev->driver_data != NULL)
380 kfree(hdev->driver_data);
386 /* Creates new HCI device */
387 static int hci_st_register_dev(struct hci_st *hst)
389 struct hci_dev *hdev;
393 /* Initialize and register HCI device */
394 hdev = hci_alloc_dev();
396 BT_DRV_ERR("Can't allocate HCI device");
397 BTDRV_API_EXIT(-ENOMEM);
400 BT_DRV_DBG(" HCI device allocated. hdev= %p", hdev);
403 hdev->bus = HCI_UART;
404 hdev->driver_data = hst;
405 hdev->open = hci_st_open;
406 hdev->close = hci_st_close;
408 hdev->send = hci_st_send_frame;
409 hdev->destruct = hci_st_destruct;
410 hdev->owner = THIS_MODULE;
413 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
415 if (hci_register_dev(hdev) < 0) {
416 BT_DRV_ERR("Can't register HCI device");
418 BTDRV_API_EXIT(-ENODEV);
422 BT_DRV_DBG(" HCI device registered. hdev= %p", hdev);
427 /* ------- Module Init interface ------ */
429 static int __init bt_drv_init(void)
437 BT_DRV_DBG(" Bluetooth Driver Version %s", VERSION);
439 /* Allocate local resource memory */
440 hst = kzalloc(sizeof(struct hci_st), GFP_KERNEL);
442 BT_DRV_ERR("Can't allocate control structure");
443 BTDRV_API_EXIT(-ENFILE);
447 /* Expose "hciX" device to user space */
448 err = hci_st_register_dev(hst);
450 /* Release local resource memory */
453 BT_DRV_ERR("Unable to expose hci0 device(%d)", err);
457 set_bit(BT_DRV_RUNNING, &hst->flags);
463 /* ------- Module Exit interface ------ */
465 static void __exit bt_drv_exit(void)
469 /* Deallocate local resource's memory */
471 struct hci_dev *hdev = hst->hdev;
474 BT_DRV_ERR("Invalid hdev memory");
478 if (test_and_clear_bit(BT_DRV_RUNNING, &hst->flags)) {
479 /* Remove HCI device (hciX) created
482 hci_unregister_dev(hdev);
484 /* Free HCI device memory */
492 module_init(bt_drv_init);
493 module_exit(bt_drv_exit);
495 /* ------ Module Info ------ */
497 module_param(reset, bool, 0644);
498 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
499 MODULE_AUTHOR("Raja Mani <raja_mani@ti.com>");
500 MODULE_DESCRIPTION("Bluetooth Driver for TI Shared Transport" VERSION);
501 MODULE_VERSION(VERSION);
502 MODULE_LICENSE("GPL");