2 * Marvell Bluetooth driver
4 * Copyright (C) 2009, Marvell International Ltd.
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
15 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
17 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
18 * this warranty disclaimer.
21 #include <net/bluetooth/bluetooth.h>
22 #include <net/bluetooth/hci_core.h>
24 #include "btmrvl_drv.h"
29 * This function is called by interface specific interrupt handler.
30 * It updates Power Save & Host Sleep states, and wakes up the main
33 void btmrvl_interrupt(struct btmrvl_private
*priv
)
37 priv
->adapter
->ps_state
= PS_AWAKE
;
39 priv
->adapter
->wakeup_tries
= 0;
41 priv
->adapter
->int_count
++;
43 wake_up_interruptible(&priv
->main_thread
.wait_q
);
47 EXPORT_SYMBOL_GPL(btmrvl_interrupt
);
49 void btmrvl_check_evtpkt(struct btmrvl_private
*priv
, struct sk_buff
*skb
)
51 struct hci_event_hdr
*hdr
= (void *) skb
->data
;
52 struct hci_ev_cmd_complete
*ec
;
57 if (hdr
->evt
== HCI_EV_CMD_COMPLETE
) {
58 ec
= (void *) (skb
->data
+ HCI_EVENT_HDR_SIZE
);
59 opcode
= __le16_to_cpu(ec
->opcode
);
60 ocf
= hci_opcode_ocf(opcode
);
61 if (ocf
== BT_CMD_MODULE_CFG_REQ
&&
62 priv
->btmrvl_dev
.sendcmdflag
) {
63 priv
->btmrvl_dev
.sendcmdflag
= false;
64 priv
->adapter
->cmd_complete
= true;
65 wake_up_interruptible(&priv
->adapter
->cmd_wait_q
);
71 EXPORT_SYMBOL_GPL(btmrvl_check_evtpkt
);
73 int btmrvl_process_event(struct btmrvl_private
*priv
, struct sk_buff
*skb
)
75 struct btmrvl_adapter
*adapter
= priv
->adapter
;
76 struct btmrvl_event
*event
;
81 event
= (struct btmrvl_event
*) skb
->data
;
82 if (event
->ec
!= 0xff) {
83 BT_DBG("Not Marvell Event=%x", event
->ec
);
88 switch (event
->data
[0]) {
89 case BT_CMD_AUTO_SLEEP_MODE
:
90 if (!event
->data
[2]) {
91 if (event
->data
[1] == BT_PS_ENABLE
)
96 (adapter
->psmode
) ? "Enable" : "Disable");
98 BT_DBG("PS Mode command failed");
102 case BT_CMD_HOST_SLEEP_CONFIG
:
104 BT_DBG("gpio=%x, gap=%x", event
->data
[1],
107 BT_DBG("HSCFG command failed");
110 case BT_CMD_HOST_SLEEP_ENABLE
:
111 if (!event
->data
[1]) {
112 adapter
->hs_state
= HS_ACTIVATED
;
114 adapter
->ps_state
= PS_SLEEP
;
115 wake_up_interruptible(&adapter
->cmd_wait_q
);
116 BT_DBG("HS ACTIVATED!");
118 BT_DBG("HS Enable failed");
122 case BT_CMD_MODULE_CFG_REQ
:
123 if (priv
->btmrvl_dev
.sendcmdflag
&&
124 event
->data
[1] == MODULE_BRINGUP_REQ
) {
125 BT_DBG("EVENT:%s", (event
->data
[2]) ?
126 "Bring-up failed" : "Bring-up succeed");
127 } else if (priv
->btmrvl_dev
.sendcmdflag
&&
128 event
->data
[1] == MODULE_SHUTDOWN_REQ
) {
129 BT_DBG("EVENT:%s", (event
->data
[2]) ?
130 "Shutdown failed" : "Shutdown succeed");
132 BT_DBG("BT_CMD_MODULE_CFG_REQ resp for APP");
137 case BT_EVENT_POWER_STATE
:
138 if (event
->data
[1] == BT_PS_SLEEP
)
139 adapter
->ps_state
= PS_SLEEP
;
141 (adapter
->ps_state
) ? "PS_SLEEP" : "PS_AWAKE");
145 BT_DBG("Unknown Event=%d", event
->data
[0]);
158 EXPORT_SYMBOL_GPL(btmrvl_process_event
);
160 int btmrvl_send_module_cfg_cmd(struct btmrvl_private
*priv
, int subcmd
)
163 struct btmrvl_cmd
*cmd
;
168 skb
= bt_skb_alloc(sizeof(*cmd
), GFP_ATOMIC
);
170 BT_ERR("No free skb");
175 cmd
= (struct btmrvl_cmd
*) skb
->tail
;
176 cmd
->ocf_ogf
= cpu_to_le16((OGF
<< 10) | BT_CMD_MODULE_CFG_REQ
);
178 cmd
->data
[0] = subcmd
;
180 bt_cb(skb
)->pkt_type
= MRVL_VENDOR_PKT
;
182 skb_put(skb
, sizeof(*cmd
));
183 skb
->dev
= (void *) priv
->btmrvl_dev
.hcidev
;
184 skb_queue_head(&priv
->adapter
->tx_queue
, skb
);
186 priv
->btmrvl_dev
.sendcmdflag
= true;
188 priv
->adapter
->cmd_complete
= false;
190 BT_DBG("Queue module cfg Command");
192 wake_up_interruptible(&priv
->main_thread
.wait_q
);
194 if (!wait_event_interruptible_timeout(priv
->adapter
->cmd_wait_q
,
195 priv
->adapter
->cmd_complete
,
196 msecs_to_jiffies(WAIT_UNTIL_CMD_RESP
))) {
198 BT_ERR("module_cfg_cmd(%x): timeout: %d",
199 subcmd
, priv
->btmrvl_dev
.sendcmdflag
);
202 BT_DBG("module cfg Command done");
209 EXPORT_SYMBOL_GPL(btmrvl_send_module_cfg_cmd
);
211 static int btmrvl_enable_hs(struct btmrvl_private
*priv
)
214 struct btmrvl_cmd
*cmd
;
219 skb
= bt_skb_alloc(sizeof(*cmd
), GFP_ATOMIC
);
221 BT_ERR("No free skb");
226 cmd
= (struct btmrvl_cmd
*) skb
->tail
;
227 cmd
->ocf_ogf
= cpu_to_le16((OGF
<< 10) | BT_CMD_HOST_SLEEP_ENABLE
);
230 bt_cb(skb
)->pkt_type
= MRVL_VENDOR_PKT
;
232 skb_put(skb
, sizeof(*cmd
));
233 skb
->dev
= (void *) priv
->btmrvl_dev
.hcidev
;
234 skb_queue_head(&priv
->adapter
->tx_queue
, skb
);
236 BT_DBG("Queue hs enable Command");
238 wake_up_interruptible(&priv
->main_thread
.wait_q
);
240 if (!wait_event_interruptible_timeout(priv
->adapter
->cmd_wait_q
,
241 priv
->adapter
->hs_state
,
242 msecs_to_jiffies(WAIT_UNTIL_HS_STATE_CHANGED
))) {
244 BT_ERR("timeout: %d, %d,%d", priv
->adapter
->hs_state
,
245 priv
->adapter
->ps_state
,
246 priv
->adapter
->wakeup_tries
);
255 int btmrvl_prepare_command(struct btmrvl_private
*priv
)
257 struct sk_buff
*skb
= NULL
;
258 struct btmrvl_cmd
*cmd
;
263 if (priv
->btmrvl_dev
.hscfgcmd
) {
264 priv
->btmrvl_dev
.hscfgcmd
= 0;
266 skb
= bt_skb_alloc(sizeof(*cmd
), GFP_ATOMIC
);
268 BT_ERR("No free skb");
273 cmd
= (struct btmrvl_cmd
*) skb
->tail
;
274 cmd
->ocf_ogf
= cpu_to_le16((OGF
<< 10) |
275 BT_CMD_HOST_SLEEP_CONFIG
);
277 cmd
->data
[0] = (priv
->btmrvl_dev
.gpio_gap
& 0xff00) >> 8;
278 cmd
->data
[1] = (u8
) (priv
->btmrvl_dev
.gpio_gap
& 0x00ff);
280 bt_cb(skb
)->pkt_type
= MRVL_VENDOR_PKT
;
282 skb_put(skb
, sizeof(*cmd
));
283 skb
->dev
= (void *) priv
->btmrvl_dev
.hcidev
;
284 skb_queue_head(&priv
->adapter
->tx_queue
, skb
);
286 BT_DBG("Queue HSCFG Command, gpio=0x%x, gap=0x%x",
287 cmd
->data
[0], cmd
->data
[1]);
290 if (priv
->btmrvl_dev
.pscmd
) {
291 priv
->btmrvl_dev
.pscmd
= 0;
293 skb
= bt_skb_alloc(sizeof(*cmd
), GFP_ATOMIC
);
295 BT_ERR("No free skb");
300 cmd
= (struct btmrvl_cmd
*) skb
->tail
;
301 cmd
->ocf_ogf
= cpu_to_le16((OGF
<< 10) |
302 BT_CMD_AUTO_SLEEP_MODE
);
305 if (priv
->btmrvl_dev
.psmode
)
306 cmd
->data
[0] = BT_PS_ENABLE
;
308 cmd
->data
[0] = BT_PS_DISABLE
;
310 bt_cb(skb
)->pkt_type
= MRVL_VENDOR_PKT
;
312 skb_put(skb
, sizeof(*cmd
));
313 skb
->dev
= (void *) priv
->btmrvl_dev
.hcidev
;
314 skb_queue_head(&priv
->adapter
->tx_queue
, skb
);
316 BT_DBG("Queue PSMODE Command:%d", cmd
->data
[0]);
319 if (priv
->btmrvl_dev
.hscmd
) {
320 priv
->btmrvl_dev
.hscmd
= 0;
322 if (priv
->btmrvl_dev
.hsmode
) {
323 ret
= btmrvl_enable_hs(priv
);
325 ret
= priv
->hw_wakeup_firmware(priv
);
326 priv
->adapter
->hs_state
= HS_DEACTIVATED
;
336 static int btmrvl_tx_pkt(struct btmrvl_private
*priv
, struct sk_buff
*skb
)
342 if (!skb
|| !skb
->data
) {
347 if (!skb
->len
|| ((skb
->len
+ BTM_HEADER_LEN
) > BTM_UPLD_SIZE
)) {
348 BT_ERR("Tx Error: Bad skb length %d : %d",
349 skb
->len
, BTM_UPLD_SIZE
);
354 if (skb_headroom(skb
) < BTM_HEADER_LEN
) {
355 struct sk_buff
*tmp
= skb
;
357 skb
= skb_realloc_headroom(skb
, BTM_HEADER_LEN
);
359 BT_ERR("Tx Error: realloc_headroom failed %d",
369 skb_push(skb
, BTM_HEADER_LEN
);
371 /* header type: byte[3]
372 * HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor
373 * header length: byte[2][1][0]
376 skb
->data
[0] = (skb
->len
& 0x0000ff);
377 skb
->data
[1] = (skb
->len
& 0x00ff00) >> 8;
378 skb
->data
[2] = (skb
->len
& 0xff0000) >> 16;
379 skb
->data
[3] = bt_cb(skb
)->pkt_type
;
381 if (priv
->hw_host_to_card
)
382 ret
= priv
->hw_host_to_card(priv
, skb
->data
, skb
->len
);
389 static void btmrvl_init_adapter(struct btmrvl_private
*priv
)
393 skb_queue_head_init(&priv
->adapter
->tx_queue
);
395 priv
->adapter
->ps_state
= PS_AWAKE
;
397 init_waitqueue_head(&priv
->adapter
->cmd_wait_q
);
402 static void btmrvl_free_adapter(struct btmrvl_private
*priv
)
406 skb_queue_purge(&priv
->adapter
->tx_queue
);
408 kfree(priv
->adapter
);
410 priv
->adapter
= NULL
;
415 static int btmrvl_ioctl(struct hci_dev
*hdev
,
416 unsigned int cmd
, unsigned long arg
)
425 static void btmrvl_destruct(struct hci_dev
*hdev
)
432 static int btmrvl_send_frame(struct sk_buff
*skb
)
434 struct hci_dev
*hdev
= (struct hci_dev
*) skb
->dev
;
435 struct btmrvl_private
*priv
= NULL
;
437 BT_DBG("Enter: type=%d, len=%d", skb
->pkt_type
, skb
->len
);
439 if (!hdev
|| !hdev
->driver_data
) {
440 BT_ERR("Frame for unknown HCI device");
445 priv
= (struct btmrvl_private
*) hdev
->driver_data
;
446 if (!test_bit(HCI_RUNNING
, &hdev
->flags
)) {
447 BT_ERR("Failed testing HCI_RUNING, flags=%lx", hdev
->flags
);
448 print_hex_dump_bytes("data: ", DUMP_PREFIX_OFFSET
,
449 skb
->data
, skb
->len
);
454 switch (bt_cb(skb
)->pkt_type
) {
455 case HCI_COMMAND_PKT
:
459 case HCI_ACLDATA_PKT
:
463 case HCI_SCODATA_PKT
:
468 skb_queue_tail(&priv
->adapter
->tx_queue
, skb
);
470 wake_up_interruptible(&priv
->main_thread
.wait_q
);
477 static int btmrvl_flush(struct hci_dev
*hdev
)
479 struct btmrvl_private
*priv
= hdev
->driver_data
;
483 skb_queue_purge(&priv
->adapter
->tx_queue
);
490 static int btmrvl_close(struct hci_dev
*hdev
)
492 struct btmrvl_private
*priv
= hdev
->driver_data
;
496 if (!test_and_clear_bit(HCI_RUNNING
, &hdev
->flags
)) {
501 skb_queue_purge(&priv
->adapter
->tx_queue
);
508 static int btmrvl_open(struct hci_dev
*hdev
)
512 set_bit(HCI_RUNNING
, &hdev
->flags
);
520 * This function handles the event generated by firmware, rx data
521 * received from firmware, and tx data sent from kernel.
523 static int btmrvl_service_main_thread(void *data
)
525 struct btmrvl_thread
*thread
= data
;
526 struct btmrvl_private
*priv
= thread
->priv
;
527 struct btmrvl_adapter
*adapter
= priv
->adapter
;
534 init_waitqueue_entry(&wait
, current
);
536 current
->flags
|= PF_NOFREEZE
;
539 add_wait_queue(&thread
->wait_q
, &wait
);
541 set_current_state(TASK_INTERRUPTIBLE
);
543 if (adapter
->wakeup_tries
||
544 ((!adapter
->int_count
) &&
545 (!priv
->btmrvl_dev
.tx_dnld_rdy
||
546 skb_queue_empty(&adapter
->tx_queue
)))) {
547 BT_DBG("main_thread is sleeping...");
551 set_current_state(TASK_RUNNING
);
553 remove_wait_queue(&thread
->wait_q
, &wait
);
555 BT_DBG("main_thread woke up");
557 if (kthread_should_stop()) {
558 BT_DBG("main_thread: break from main thread");
562 spin_lock_irqsave(&priv
->driver_lock
, flags
);
563 if (adapter
->int_count
) {
564 adapter
->int_count
= 0;
565 } else if (adapter
->ps_state
== PS_SLEEP
&&
566 !skb_queue_empty(&adapter
->tx_queue
)) {
567 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
568 adapter
->wakeup_tries
++;
569 priv
->hw_wakeup_firmware(priv
);
572 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
574 if (adapter
->ps_state
== PS_SLEEP
)
577 if (!priv
->btmrvl_dev
.tx_dnld_rdy
)
580 skb
= skb_dequeue(&adapter
->tx_queue
);
582 if (btmrvl_tx_pkt(priv
, skb
))
583 priv
->btmrvl_dev
.hcidev
->stat
.err_tx
++;
585 priv
->btmrvl_dev
.hcidev
->stat
.byte_tx
+= skb
->len
;
596 struct btmrvl_private
*btmrvl_add_card(void *card
)
598 struct hci_dev
*hdev
= NULL
;
599 struct btmrvl_private
*priv
;
604 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
606 BT_ERR("Can not allocate priv");
610 priv
->adapter
= kzalloc(sizeof(*priv
->adapter
), GFP_KERNEL
);
611 if (!priv
->adapter
) {
612 BT_ERR("Allocate buffer for btmrvl_adapter failed!");
616 btmrvl_init_adapter(priv
);
618 hdev
= hci_alloc_dev();
620 BT_ERR("Can not allocate HCI device");
624 BT_DBG("Starting kthread...");
625 priv
->main_thread
.priv
= priv
;
626 spin_lock_init(&priv
->driver_lock
);
628 init_waitqueue_head(&priv
->main_thread
.wait_q
);
629 priv
->main_thread
.task
= kthread_run(btmrvl_service_main_thread
,
630 &priv
->main_thread
, "btmrvl_main_service");
632 priv
->btmrvl_dev
.hcidev
= hdev
;
633 priv
->btmrvl_dev
.card
= card
;
635 hdev
->driver_data
= priv
;
637 priv
->btmrvl_dev
.tx_dnld_rdy
= true;
639 hdev
->type
= HCI_SDIO
;
640 hdev
->open
= btmrvl_open
;
641 hdev
->close
= btmrvl_close
;
642 hdev
->flush
= btmrvl_flush
;
643 hdev
->send
= btmrvl_send_frame
;
644 hdev
->destruct
= btmrvl_destruct
;
645 hdev
->ioctl
= btmrvl_ioctl
;
646 hdev
->owner
= THIS_MODULE
;
648 ret
= hci_register_dev(hdev
);
650 BT_ERR("Can not register HCI device");
651 goto err_hci_register_dev
;
654 #ifdef CONFIG_DEBUG_FS
655 btmrvl_debugfs_init(hdev
);
661 err_hci_register_dev
:
662 /* Stop the thread servicing the interrupts */
663 kthread_stop(priv
->main_thread
.task
);
668 btmrvl_free_adapter(priv
);
678 EXPORT_SYMBOL_GPL(btmrvl_add_card
);
680 int btmrvl_remove_card(struct btmrvl_private
*priv
)
682 struct hci_dev
*hdev
;
686 hdev
= priv
->btmrvl_dev
.hcidev
;
688 wake_up_interruptible(&priv
->adapter
->cmd_wait_q
);
690 kthread_stop(priv
->main_thread
.task
);
692 #ifdef CONFIG_DEBUG_FS
693 btmrvl_debugfs_remove(hdev
);
696 hci_unregister_dev(hdev
);
700 priv
->btmrvl_dev
.hcidev
= NULL
;
702 btmrvl_free_adapter(priv
);
710 EXPORT_SYMBOL_GPL(btmrvl_remove_card
);
712 MODULE_AUTHOR("Marvell International Ltd.");
713 MODULE_DESCRIPTION("Marvell Bluetooth Driver ver" VERSION
);
714 MODULE_VERSION(VERSION
);
715 MODULE_LICENSE("GPL v2");