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>
27 #include <linux/ti_wilink_st.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(void *priv_data, char data)
85 struct hci_st *lhst = (struct hci_st *)priv_data;
88 /* hci_st_open() function needs value of 'data' to know
89 * the registration status(success/fail),So have a back
92 lhst->streg_cbdata = data;
94 /* Got a feedback from ST for BT driver registration
95 * request.Wackup hci_st_open() function to continue
96 * it's open operation.
98 complete(&lhst->wait_for_btdrv_reg_completion);
103 /* Called by Shared Transport layer when receive data is
105 static long hci_st_receive(void *priv_data, struct sk_buff *skb)
109 struct hci_st *lhst = (struct hci_st *)priv_data;
117 BT_DRV_ERR("Invalid SKB received from ST");
118 BTDRV_API_EXIT(-EFAULT);
123 BT_DRV_ERR("Invalid hci_st memory,freeing SKB");
124 BTDRV_API_EXIT(-EFAULT);
127 if (!test_bit(BT_DRV_RUNNING, &lhst->flags)) {
129 BT_DRV_ERR("Device is not running,freeing SKB");
130 BTDRV_API_EXIT(-EINVAL);
135 skb->dev = (struct net_device *)lhst->hdev;
137 /* Forward skb to HCI CORE layer */
138 err = hci_recv_frame(skb);
141 BT_DRV_ERR("Unable to push skb to HCI CORE(%d),freeing SKB",
146 lhst->hdev->stat.byte_rx += len;
152 /* ------- Interfaces to HCI layer ------ */
154 /* Called from HCI core to initialize the device */
155 static int hci_st_open(struct hci_dev *hdev)
157 static struct st_proto_s hci_st_proto;
158 unsigned long timeleft;
165 BT_DRV_DBG("%s %p", hdev->name, hdev);
167 /* Already registered with ST ? */
168 if (test_bit(BT_ST_REGISTERED, &hst->flags)) {
169 BT_DRV_ERR("Registered with ST already,open called again?");
174 /* Populate BT driver info required by ST */
175 memset(&hci_st_proto, 0, sizeof(hci_st_proto));
178 hci_st_proto.type = ST_BT;
180 /* Receive function which called from ST */
181 hci_st_proto.recv = hci_st_receive;
183 /* Packet match function may used in future */
184 hci_st_proto.match_packet = NULL;
186 /* Callback to be called when registration is pending */
187 hci_st_proto.reg_complete_cb = hci_st_registration_completion_cb;
189 /* This is write function pointer of ST. BT driver will make use of this
190 * for sending any packets to chip. ST will assign and give to us, so
192 hci_st_proto.write = NULL;
194 /* send in the hst to be received at registration complete callback
195 * and during st's receive
197 hci_st_proto.priv_data = hst;
199 /* Register with ST layer */
200 err = st_register(&hci_st_proto);
201 if (err == -EINPROGRESS) {
202 /* Prepare wait-for-completion handler data structures.
203 * Needed to syncronize this and st_registration_completion_cb()
206 init_completion(&hst->wait_for_btdrv_reg_completion);
208 /* Reset ST registration callback status flag , this value
209 * will be updated in hci_st_registration_completion_cb()
210 * function whenever it called from ST driver.
212 hst->streg_cbdata = -EINPROGRESS;
214 /* ST is busy with other protocol registration(may be busy with
215 * firmware download).So,Wait till the registration callback
216 * (passed as a argument to st_register() function) getting
219 BT_DRV_DBG(" %s waiting for reg completion signal from ST",
223 wait_for_completion_timeout
224 (&hst->wait_for_btdrv_reg_completion,
225 msecs_to_jiffies(BT_REGISTER_TIMEOUT));
227 BT_DRV_ERR("Timeout(%ld sec),didn't get reg"
228 "completion signal from ST",
229 BT_REGISTER_TIMEOUT / 1000);
230 BTDRV_API_EXIT(-ETIMEDOUT);
234 /* Is ST registration callback called with ERROR value? */
235 if (hst->streg_cbdata != 0) {
236 BT_DRV_ERR("ST reg completion CB called with invalid"
237 "status %d", hst->streg_cbdata);
238 BTDRV_API_EXIT(-EAGAIN);
242 } else if (err == -1) {
243 BT_DRV_ERR("st_register failed %d", err);
244 BTDRV_API_EXIT(-EAGAIN);
248 /* Do we have proper ST write function? */
249 if (hci_st_proto.write != NULL) {
250 /* We need this pointer for sending any Bluetooth pkts */
251 hst->st_write = hci_st_proto.write;
253 BT_DRV_ERR("failed to get ST write func pointer");
255 /* Undo registration with ST */
256 err = st_unregister(ST_BT);
258 BT_DRV_ERR("st_unregister failed %d", err);
260 hst->st_write = NULL;
261 BTDRV_API_EXIT(-EAGAIN);
265 /* Registration with ST layer is completed successfully,
266 * now chip is ready to accept commands from HCI CORE.
267 * Mark HCI Device flag as RUNNING
269 set_bit(HCI_RUNNING, &hdev->flags);
271 /* Registration with ST successful */
272 set_bit(BT_ST_REGISTERED, &hst->flags);
279 static int hci_st_close(struct hci_dev *hdev)
287 /* Unregister from ST layer */
288 if (test_and_clear_bit(BT_ST_REGISTERED, &hst->flags)) {
289 err = st_unregister(ST_BT);
291 BT_DRV_ERR("st_unregister failed %d", err);
292 BTDRV_API_EXIT(-EBUSY);
297 hst->st_write = NULL;
299 /* ST layer would have moved chip to inactive state.
300 * So,clear HCI device RUNNING flag.
302 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) {
311 /* Called from HCI CORE , Sends frames to Shared Transport */
312 static int hci_st_send_frame(struct sk_buff *skb)
314 struct hci_dev *hdev;
321 BT_DRV_ERR("Invalid skb received from HCI CORE");
322 BTDRV_API_EXIT(-ENOMEM);
325 hdev = (struct hci_dev *)skb->dev;
327 BT_DRV_ERR("SKB received for invalid HCI Device (hdev=NULL)");
328 BTDRV_API_EXIT(-ENODEV);
331 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
332 BT_DRV_ERR("Device is not running");
333 BTDRV_API_EXIT(-EBUSY);
337 hst = (struct hci_st *)hdev->driver_data;
339 /* Prepend skb with frame type */
340 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
342 BT_DRV_DBG(" %s: type %d len %d", hdev->name, bt_cb(skb)->pkt_type,
345 /* Insert skb to shared transport layer's transmit queue.
346 * Freeing skb memory is taken care in shared transport layer,
347 * so don't free skb memory here.
349 if (!hst->st_write) {
351 BT_DRV_ERR(" Can't write to ST, st_write null?");
352 BTDRV_API_EXIT(-EAGAIN);
355 len = hst->st_write(skb);
357 /* Something went wrong in st write , free skb memory */
359 BT_DRV_ERR(" ST write failed (%ld)", len);
360 BTDRV_API_EXIT(-EAGAIN);
364 /* ST accepted our skb. So, Go ahead and do rest */
365 hdev->stat.byte_tx += len;
366 hci_st_tx_complete(hst, bt_cb(skb)->pkt_type);
372 static void hci_st_destruct(struct hci_dev *hdev)
377 BT_DRV_ERR("Destruct called with invalid HCI Device"
383 BT_DRV_DBG("%s", hdev->name);
385 /* free hci_st memory */
386 if (hdev->driver_data != NULL)
387 kfree(hdev->driver_data);
393 /* Creates new HCI device */
394 static int hci_st_register_dev(struct hci_st *hst)
396 struct hci_dev *hdev;
400 /* Initialize and register HCI device */
401 hdev = hci_alloc_dev();
403 BT_DRV_ERR("Can't allocate HCI device");
404 BTDRV_API_EXIT(-ENOMEM);
407 BT_DRV_DBG(" HCI device allocated. hdev= %p", hdev);
410 hdev->bus = HCI_UART;
411 hdev->driver_data = hst;
412 hdev->open = hci_st_open;
413 hdev->close = hci_st_close;
415 hdev->send = hci_st_send_frame;
416 hdev->destruct = hci_st_destruct;
417 hdev->owner = THIS_MODULE;
420 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
422 if (hci_register_dev(hdev) < 0) {
423 BT_DRV_ERR("Can't register HCI device");
425 BTDRV_API_EXIT(-ENODEV);
429 BT_DRV_DBG(" HCI device registered. hdev= %p", hdev);
434 /* ------- Module Init interface ------ */
436 static int __init bt_drv_init(void)
444 BT_DRV_DBG(" Bluetooth Driver Version %s", VERSION);
446 /* Allocate local resource memory */
447 hst = kzalloc(sizeof(struct hci_st), GFP_KERNEL);
449 BT_DRV_ERR("Can't allocate control structure");
450 BTDRV_API_EXIT(-ENFILE);
454 /* Expose "hciX" device to user space */
455 err = hci_st_register_dev(hst);
457 /* Release local resource memory */
460 BT_DRV_ERR("Unable to expose hci0 device(%d)", err);
464 set_bit(BT_DRV_RUNNING, &hst->flags);
470 /* ------- Module Exit interface ------ */
472 static void __exit bt_drv_exit(void)
476 /* Deallocate local resource's memory */
478 struct hci_dev *hdev = hst->hdev;
481 BT_DRV_ERR("Invalid hdev memory");
485 if (test_and_clear_bit(BT_DRV_RUNNING, &hst->flags)) {
486 /* Remove HCI device (hciX) created
489 hci_unregister_dev(hdev);
491 /* Free HCI device memory */
499 module_init(bt_drv_init);
500 module_exit(bt_drv_exit);
502 /* ------ Module Info ------ */
504 module_param(reset, bool, 0644);
505 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
507 MODULE_DESCRIPTION("Bluetooth Driver for TI Shared Transport" VERSION);
508 MODULE_VERSION(VERSION);
509 MODULE_LICENSE("GPL");