GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / ti-st / bt_drv.c
blob61ae98833b17af5dec882a9e8d18d6b41a232a91
1 /*
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 "st.h"
28 #include "bt_drv.h"
30 /* Define this macro to get debug msg */
31 #undef DEBUG
33 #ifdef DEBUG
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", \
36 __func__)
37 #define BTDRV_API_EXIT(errno) printk(KERN_INFO "(btdrv): %s Exit(%d)\n", \
38 __func__, errno)
39 #else
40 #define BT_DRV_DBG(fmt, arg...)
41 #define BTDRV_API_START()
42 #define BTDRV_API_EXIT(errno)
43 #endif
45 #define BT_DRV_ERR(fmt, arg...) printk(KERN_ERR "(btdrv):"fmt"\n" , ## arg)
47 static int reset;
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)
53 struct hci_dev *hdev;
55 BTDRV_API_START();
57 hdev = hst->hdev;
59 /* Update HCI stat counters */
60 switch (pkt_type) {
61 case HCI_COMMAND_PKT:
62 hdev->stat.cmd_tx++;
63 break;
65 case HCI_ACLDATA_PKT:
66 hdev->stat.acl_tx++;
67 break;
69 case HCI_SCODATA_PKT:
70 hdev->stat.cmd_tx++;
71 break;
74 BTDRV_API_EXIT(0);
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;
86 BTDRV_API_START();
88 /* hci_st_open() function needs value of 'data' to know
89 * the registration status(success/fail),So have a back
90 * up of it.
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);
100 BTDRV_API_EXIT(0);
103 /* Called by Shared Transport layer when receive data is
104 * available */
105 static long hci_st_receive(void *priv_data, struct sk_buff *skb)
107 int err;
108 int len;
109 struct hci_st *lhst = (struct hci_st *)priv_data;
111 BTDRV_API_START();
113 err = 0;
114 len = 0;
116 if (skb == NULL) {
117 BT_DRV_ERR("Invalid SKB received from ST");
118 BTDRV_API_EXIT(-EFAULT);
119 return -EFAULT;
121 if (!lhst) {
122 kfree_skb(skb);
123 BT_DRV_ERR("Invalid hci_st memory,freeing SKB");
124 BTDRV_API_EXIT(-EFAULT);
125 return -EFAULT;
127 if (!test_bit(BT_DRV_RUNNING, &lhst->flags)) {
128 kfree_skb(skb);
129 BT_DRV_ERR("Device is not running,freeing SKB");
130 BTDRV_API_EXIT(-EINVAL);
131 return -EINVAL;
134 len = skb->len;
135 skb->dev = (struct net_device *)lhst->hdev;
137 /* Forward skb to HCI CORE layer */
138 err = hci_recv_frame(skb);
139 if (err) {
140 kfree_skb(skb);
141 BT_DRV_ERR("Unable to push skb to HCI CORE(%d),freeing SKB",
142 err);
143 BTDRV_API_EXIT(err);
144 return err;
146 lhst->hdev->stat.byte_rx += len;
148 BTDRV_API_EXIT(0);
149 return 0;
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;
159 int err;
161 BTDRV_API_START();
163 err = 0;
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?");
170 BTDRV_API_EXIT(0);
171 return 0;
174 /* Populate BT driver info required by ST */
175 memset(&hci_st_proto, 0, sizeof(hci_st_proto));
177 /* BT driver ID */
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
191 * make it as NULL */
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()
204 * functions.
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
217 * called from ST.
219 BT_DRV_DBG(" %s waiting for reg completion signal from ST",
220 __func__);
222 timeleft =
223 wait_for_completion_timeout
224 (&hst->wait_for_btdrv_reg_completion,
225 msecs_to_jiffies(BT_REGISTER_TIMEOUT));
226 if (!timeleft) {
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);
231 return -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);
239 return -EAGAIN;
241 err = 0;
242 } else if (err == -1) {
243 BT_DRV_ERR("st_register failed %d", err);
244 BTDRV_API_EXIT(-EAGAIN);
245 return -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;
252 } else {
253 BT_DRV_ERR("failed to get ST write func pointer");
255 /* Undo registration with ST */
256 err = st_unregister(ST_BT);
257 if (err < 0)
258 BT_DRV_ERR("st_unregister failed %d", err);
260 hst->st_write = NULL;
261 BTDRV_API_EXIT(-EAGAIN);
262 return -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);
274 BTDRV_API_EXIT(err);
275 return err;
278 /* Close device */
279 static int hci_st_close(struct hci_dev *hdev)
281 int err;
283 BTDRV_API_START();
285 err = 0;
287 /* Unregister from ST layer */
288 if (test_and_clear_bit(BT_ST_REGISTERED, &hst->flags)) {
289 err = st_unregister(ST_BT);
290 if (err != 0) {
291 BT_DRV_ERR("st_unregister failed %d", err);
292 BTDRV_API_EXIT(-EBUSY);
293 return -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)) {
303 BTDRV_API_EXIT(0);
304 return 0;
307 BTDRV_API_EXIT(err);
308 return err;
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;
315 struct hci_st *hst;
316 long len;
318 BTDRV_API_START();
320 if (skb == NULL) {
321 BT_DRV_ERR("Invalid skb received from HCI CORE");
322 BTDRV_API_EXIT(-ENOMEM);
323 return -ENOMEM;
325 hdev = (struct hci_dev *)skb->dev;
326 if (!hdev) {
327 BT_DRV_ERR("SKB received for invalid HCI Device (hdev=NULL)");
328 BTDRV_API_EXIT(-ENODEV);
329 return -ENODEV;
331 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
332 BT_DRV_ERR("Device is not running");
333 BTDRV_API_EXIT(-EBUSY);
334 return -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,
343 skb->len);
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) {
350 kfree_skb(skb);
351 BT_DRV_ERR(" Can't write to ST, st_write null?");
352 BTDRV_API_EXIT(-EAGAIN);
353 return -EAGAIN;
355 len = hst->st_write(skb);
356 if (len < 0) {
357 /* Something went wrong in st write , free skb memory */
358 kfree_skb(skb);
359 BT_DRV_ERR(" ST write failed (%ld)", len);
360 BTDRV_API_EXIT(-EAGAIN);
361 return -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);
368 BTDRV_API_EXIT(0);
369 return 0;
372 static void hci_st_destruct(struct hci_dev *hdev)
374 BTDRV_API_START();
376 if (!hdev) {
377 BT_DRV_ERR("Destruct called with invalid HCI Device"
378 "(hdev=NULL)");
379 BTDRV_API_EXIT(0);
380 return;
383 BT_DRV_DBG("%s", hdev->name);
385 /* free hci_st memory */
386 if (hdev->driver_data != NULL)
387 kfree(hdev->driver_data);
389 BTDRV_API_EXIT(0);
390 return;
393 /* Creates new HCI device */
394 static int hci_st_register_dev(struct hci_st *hst)
396 struct hci_dev *hdev;
398 BTDRV_API_START();
400 /* Initialize and register HCI device */
401 hdev = hci_alloc_dev();
402 if (!hdev) {
403 BT_DRV_ERR("Can't allocate HCI device");
404 BTDRV_API_EXIT(-ENOMEM);
405 return -ENOMEM;
407 BT_DRV_DBG(" HCI device allocated. hdev= %p", hdev);
409 hst->hdev = hdev;
410 hdev->bus = HCI_UART;
411 hdev->driver_data = hst;
412 hdev->open = hci_st_open;
413 hdev->close = hci_st_close;
414 hdev->flush = NULL;
415 hdev->send = hci_st_send_frame;
416 hdev->destruct = hci_st_destruct;
417 hdev->owner = THIS_MODULE;
419 if (reset)
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");
424 hci_free_dev(hdev);
425 BTDRV_API_EXIT(-ENODEV);
426 return -ENODEV;
429 BT_DRV_DBG(" HCI device registered. hdev= %p", hdev);
430 BTDRV_API_EXIT(0);
431 return 0;
434 /* ------- Module Init interface ------ */
436 static int __init bt_drv_init(void)
438 int err;
440 BTDRV_API_START();
442 err = 0;
444 BT_DRV_DBG(" Bluetooth Driver Version %s", VERSION);
446 /* Allocate local resource memory */
447 hst = kzalloc(sizeof(struct hci_st), GFP_KERNEL);
448 if (!hst) {
449 BT_DRV_ERR("Can't allocate control structure");
450 BTDRV_API_EXIT(-ENFILE);
451 return -ENFILE;
454 /* Expose "hciX" device to user space */
455 err = hci_st_register_dev(hst);
456 if (err) {
457 /* Release local resource memory */
458 kfree(hst);
460 BT_DRV_ERR("Unable to expose hci0 device(%d)", err);
461 BTDRV_API_EXIT(err);
462 return err;
464 set_bit(BT_DRV_RUNNING, &hst->flags);
466 BTDRV_API_EXIT(err);
467 return err;
470 /* ------- Module Exit interface ------ */
472 static void __exit bt_drv_exit(void)
474 BTDRV_API_START();
476 /* Deallocate local resource's memory */
477 if (hst) {
478 struct hci_dev *hdev = hst->hdev;
480 if (hdev == NULL) {
481 BT_DRV_ERR("Invalid hdev memory");
482 kfree(hst);
483 } else {
484 hci_st_close(hdev);
485 if (test_and_clear_bit(BT_DRV_RUNNING, &hst->flags)) {
486 /* Remove HCI device (hciX) created
487 * in module init.
489 hci_unregister_dev(hdev);
491 /* Free HCI device memory */
492 hci_free_dev(hdev);
496 BTDRV_API_EXIT(0);
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");
506 MODULE_AUTHOR("Raja Mani <raja_mani@ti.com>");
507 MODULE_DESCRIPTION("Bluetooth Driver for TI Shared Transport" VERSION);
508 MODULE_VERSION(VERSION);
509 MODULE_LICENSE("GPL");