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 <linux/module.h>
23 #include <net/bluetooth/bluetooth.h>
24 #include <net/bluetooth/hci_core.h>
26 #include "btmrvl_drv.h"
31 * This function is called by interface specific interrupt handler.
32 * It updates Power Save & Host Sleep states, and wakes up the main
35 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
);
45 EXPORT_SYMBOL_GPL(btmrvl_interrupt
);
47 bool btmrvl_check_evtpkt(struct btmrvl_private
*priv
, struct sk_buff
*skb
)
49 struct hci_event_hdr
*hdr
= (void *) skb
->data
;
51 if (hdr
->evt
== HCI_EV_CMD_COMPLETE
) {
52 struct hci_ev_cmd_complete
*ec
;
55 ec
= (void *) (skb
->data
+ HCI_EVENT_HDR_SIZE
);
56 opcode
= __le16_to_cpu(ec
->opcode
);
57 ocf
= hci_opcode_ocf(opcode
);
58 ogf
= hci_opcode_ogf(opcode
);
60 if (ocf
== BT_CMD_MODULE_CFG_REQ
&&
61 priv
->btmrvl_dev
.sendcmdflag
) {
62 priv
->btmrvl_dev
.sendcmdflag
= false;
63 priv
->adapter
->cmd_complete
= true;
64 wake_up_interruptible(&priv
->adapter
->cmd_wait_q
);
68 BT_DBG("vendor event skipped: ogf 0x%4.4x ocf 0x%4.4x",
77 EXPORT_SYMBOL_GPL(btmrvl_check_evtpkt
);
79 int btmrvl_process_event(struct btmrvl_private
*priv
, struct sk_buff
*skb
)
81 struct btmrvl_adapter
*adapter
= priv
->adapter
;
82 struct btmrvl_event
*event
;
85 event
= (struct btmrvl_event
*) skb
->data
;
86 if (event
->ec
!= 0xff) {
87 BT_DBG("Not Marvell Event=%x", event
->ec
);
92 switch (event
->data
[0]) {
93 case BT_CMD_AUTO_SLEEP_MODE
:
94 if (!event
->data
[2]) {
95 if (event
->data
[1] == BT_PS_ENABLE
)
100 (adapter
->psmode
) ? "Enable" : "Disable");
102 BT_DBG("PS Mode command failed");
106 case BT_CMD_HOST_SLEEP_CONFIG
:
108 BT_DBG("gpio=%x, gap=%x", event
->data
[1],
111 BT_DBG("HSCFG command failed");
114 case BT_CMD_HOST_SLEEP_ENABLE
:
115 if (!event
->data
[1]) {
116 adapter
->hs_state
= HS_ACTIVATED
;
118 adapter
->ps_state
= PS_SLEEP
;
119 wake_up_interruptible(&adapter
->cmd_wait_q
);
120 BT_DBG("HS ACTIVATED!");
122 BT_DBG("HS Enable failed");
126 case BT_CMD_MODULE_CFG_REQ
:
127 if (priv
->btmrvl_dev
.sendcmdflag
&&
128 event
->data
[1] == MODULE_BRINGUP_REQ
) {
130 ((event
->data
[2] == MODULE_BROUGHT_UP
) ||
131 (event
->data
[2] == MODULE_ALREADY_UP
)) ?
132 "Bring-up succeed" : "Bring-up failed");
134 if (event
->length
> 3 && event
->data
[3])
135 priv
->btmrvl_dev
.dev_type
= HCI_AMP
;
137 priv
->btmrvl_dev
.dev_type
= HCI_BREDR
;
139 BT_DBG("dev_type: %d", priv
->btmrvl_dev
.dev_type
);
140 } else if (priv
->btmrvl_dev
.sendcmdflag
&&
141 event
->data
[1] == MODULE_SHUTDOWN_REQ
) {
142 BT_DBG("EVENT:%s", (event
->data
[2]) ?
143 "Shutdown failed" : "Shutdown succeed");
145 BT_DBG("BT_CMD_MODULE_CFG_REQ resp for APP");
150 case BT_EVENT_POWER_STATE
:
151 if (event
->data
[1] == BT_PS_SLEEP
)
152 adapter
->ps_state
= PS_SLEEP
;
154 (adapter
->ps_state
) ? "PS_SLEEP" : "PS_AWAKE");
158 BT_DBG("Unknown Event=%d", event
->data
[0]);
169 EXPORT_SYMBOL_GPL(btmrvl_process_event
);
171 int btmrvl_send_module_cfg_cmd(struct btmrvl_private
*priv
, int subcmd
)
174 struct btmrvl_cmd
*cmd
;
177 skb
= bt_skb_alloc(sizeof(*cmd
), GFP_ATOMIC
);
179 BT_ERR("No free skb");
183 cmd
= (struct btmrvl_cmd
*) skb_put(skb
, sizeof(*cmd
));
184 cmd
->ocf_ogf
= cpu_to_le16(hci_opcode_pack(OGF
, BT_CMD_MODULE_CFG_REQ
));
186 cmd
->data
[0] = subcmd
;
188 bt_cb(skb
)->pkt_type
= MRVL_VENDOR_PKT
;
190 skb
->dev
= (void *) priv
->btmrvl_dev
.hcidev
;
191 skb_queue_head(&priv
->adapter
->tx_queue
, skb
);
193 priv
->btmrvl_dev
.sendcmdflag
= true;
195 priv
->adapter
->cmd_complete
= false;
197 BT_DBG("Queue module cfg Command");
199 wake_up_interruptible(&priv
->main_thread
.wait_q
);
201 if (!wait_event_interruptible_timeout(priv
->adapter
->cmd_wait_q
,
202 priv
->adapter
->cmd_complete
,
203 msecs_to_jiffies(WAIT_UNTIL_CMD_RESP
))) {
205 BT_ERR("module_cfg_cmd(%x): timeout: %d",
206 subcmd
, priv
->btmrvl_dev
.sendcmdflag
);
209 BT_DBG("module cfg Command done");
213 EXPORT_SYMBOL_GPL(btmrvl_send_module_cfg_cmd
);
215 int btmrvl_send_hscfg_cmd(struct btmrvl_private
*priv
)
218 struct btmrvl_cmd
*cmd
;
220 skb
= bt_skb_alloc(sizeof(*cmd
), GFP_ATOMIC
);
222 BT_ERR("No free skb");
226 cmd
= (struct btmrvl_cmd
*) skb_put(skb
, sizeof(*cmd
));
227 cmd
->ocf_ogf
= cpu_to_le16(hci_opcode_pack(OGF
,
228 BT_CMD_HOST_SLEEP_CONFIG
));
230 cmd
->data
[0] = (priv
->btmrvl_dev
.gpio_gap
& 0xff00) >> 8;
231 cmd
->data
[1] = (u8
) (priv
->btmrvl_dev
.gpio_gap
& 0x00ff);
233 bt_cb(skb
)->pkt_type
= MRVL_VENDOR_PKT
;
235 skb
->dev
= (void *) priv
->btmrvl_dev
.hcidev
;
236 skb_queue_head(&priv
->adapter
->tx_queue
, skb
);
238 BT_DBG("Queue HSCFG Command, gpio=0x%x, gap=0x%x", cmd
->data
[0],
243 EXPORT_SYMBOL_GPL(btmrvl_send_hscfg_cmd
);
245 int btmrvl_enable_ps(struct btmrvl_private
*priv
)
248 struct btmrvl_cmd
*cmd
;
250 skb
= bt_skb_alloc(sizeof(*cmd
), GFP_ATOMIC
);
252 BT_ERR("No free skb");
256 cmd
= (struct btmrvl_cmd
*) skb_put(skb
, sizeof(*cmd
));
257 cmd
->ocf_ogf
= cpu_to_le16(hci_opcode_pack(OGF
,
258 BT_CMD_AUTO_SLEEP_MODE
));
261 if (priv
->btmrvl_dev
.psmode
)
262 cmd
->data
[0] = BT_PS_ENABLE
;
264 cmd
->data
[0] = BT_PS_DISABLE
;
266 bt_cb(skb
)->pkt_type
= MRVL_VENDOR_PKT
;
268 skb
->dev
= (void *) priv
->btmrvl_dev
.hcidev
;
269 skb_queue_head(&priv
->adapter
->tx_queue
, skb
);
271 BT_DBG("Queue PSMODE Command:%d", cmd
->data
[0]);
275 EXPORT_SYMBOL_GPL(btmrvl_enable_ps
);
277 int btmrvl_enable_hs(struct btmrvl_private
*priv
)
280 struct btmrvl_cmd
*cmd
;
283 skb
= bt_skb_alloc(sizeof(*cmd
), GFP_ATOMIC
);
285 BT_ERR("No free skb");
289 cmd
= (struct btmrvl_cmd
*) skb_put(skb
, sizeof(*cmd
));
290 cmd
->ocf_ogf
= cpu_to_le16(hci_opcode_pack(OGF
, BT_CMD_HOST_SLEEP_ENABLE
));
293 bt_cb(skb
)->pkt_type
= MRVL_VENDOR_PKT
;
295 skb
->dev
= (void *) priv
->btmrvl_dev
.hcidev
;
296 skb_queue_head(&priv
->adapter
->tx_queue
, skb
);
298 BT_DBG("Queue hs enable Command");
300 wake_up_interruptible(&priv
->main_thread
.wait_q
);
302 if (!wait_event_interruptible_timeout(priv
->adapter
->cmd_wait_q
,
303 priv
->adapter
->hs_state
,
304 msecs_to_jiffies(WAIT_UNTIL_HS_STATE_CHANGED
))) {
306 BT_ERR("timeout: %d, %d,%d", priv
->adapter
->hs_state
,
307 priv
->adapter
->ps_state
,
308 priv
->adapter
->wakeup_tries
);
313 EXPORT_SYMBOL_GPL(btmrvl_enable_hs
);
315 int btmrvl_prepare_command(struct btmrvl_private
*priv
)
319 if (priv
->btmrvl_dev
.hscfgcmd
) {
320 priv
->btmrvl_dev
.hscfgcmd
= 0;
321 btmrvl_send_hscfg_cmd(priv
);
324 if (priv
->btmrvl_dev
.pscmd
) {
325 priv
->btmrvl_dev
.pscmd
= 0;
326 btmrvl_enable_ps(priv
);
329 if (priv
->btmrvl_dev
.hscmd
) {
330 priv
->btmrvl_dev
.hscmd
= 0;
332 if (priv
->btmrvl_dev
.hsmode
) {
333 ret
= btmrvl_enable_hs(priv
);
335 ret
= priv
->hw_wakeup_firmware(priv
);
336 priv
->adapter
->hs_state
= HS_DEACTIVATED
;
343 static int btmrvl_tx_pkt(struct btmrvl_private
*priv
, struct sk_buff
*skb
)
347 if (!skb
|| !skb
->data
)
350 if (!skb
->len
|| ((skb
->len
+ BTM_HEADER_LEN
) > BTM_UPLD_SIZE
)) {
351 BT_ERR("Tx Error: Bad skb length %d : %d",
352 skb
->len
, BTM_UPLD_SIZE
);
356 if (skb_headroom(skb
) < BTM_HEADER_LEN
) {
357 struct sk_buff
*tmp
= skb
;
359 skb
= skb_realloc_headroom(skb
, BTM_HEADER_LEN
);
361 BT_ERR("Tx Error: realloc_headroom failed %d",
370 skb_push(skb
, BTM_HEADER_LEN
);
372 /* header type: byte[3]
373 * HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor
374 * header length: byte[2][1][0]
377 skb
->data
[0] = (skb
->len
& 0x0000ff);
378 skb
->data
[1] = (skb
->len
& 0x00ff00) >> 8;
379 skb
->data
[2] = (skb
->len
& 0xff0000) >> 16;
380 skb
->data
[3] = bt_cb(skb
)->pkt_type
;
382 if (priv
->hw_host_to_card
)
383 ret
= priv
->hw_host_to_card(priv
, skb
->data
, skb
->len
);
388 static void btmrvl_init_adapter(struct btmrvl_private
*priv
)
390 skb_queue_head_init(&priv
->adapter
->tx_queue
);
392 priv
->adapter
->ps_state
= PS_AWAKE
;
394 init_waitqueue_head(&priv
->adapter
->cmd_wait_q
);
397 static void btmrvl_free_adapter(struct btmrvl_private
*priv
)
399 skb_queue_purge(&priv
->adapter
->tx_queue
);
401 kfree(priv
->adapter
);
403 priv
->adapter
= NULL
;
406 static int btmrvl_ioctl(struct hci_dev
*hdev
,
407 unsigned int cmd
, unsigned long arg
)
412 static int btmrvl_send_frame(struct sk_buff
*skb
)
414 struct hci_dev
*hdev
= (struct hci_dev
*) skb
->dev
;
415 struct btmrvl_private
*priv
= NULL
;
417 BT_DBG("type=%d, len=%d", skb
->pkt_type
, skb
->len
);
420 BT_ERR("Frame for unknown HCI device");
424 priv
= hci_get_drvdata(hdev
);
426 if (!test_bit(HCI_RUNNING
, &hdev
->flags
)) {
427 BT_ERR("Failed testing HCI_RUNING, flags=%lx", hdev
->flags
);
428 print_hex_dump_bytes("data: ", DUMP_PREFIX_OFFSET
,
429 skb
->data
, skb
->len
);
433 switch (bt_cb(skb
)->pkt_type
) {
434 case HCI_COMMAND_PKT
:
438 case HCI_ACLDATA_PKT
:
442 case HCI_SCODATA_PKT
:
447 skb_queue_tail(&priv
->adapter
->tx_queue
, skb
);
449 wake_up_interruptible(&priv
->main_thread
.wait_q
);
454 static int btmrvl_flush(struct hci_dev
*hdev
)
456 struct btmrvl_private
*priv
= hci_get_drvdata(hdev
);
458 skb_queue_purge(&priv
->adapter
->tx_queue
);
463 static int btmrvl_close(struct hci_dev
*hdev
)
465 struct btmrvl_private
*priv
= hci_get_drvdata(hdev
);
467 if (!test_and_clear_bit(HCI_RUNNING
, &hdev
->flags
))
470 skb_queue_purge(&priv
->adapter
->tx_queue
);
475 static int btmrvl_open(struct hci_dev
*hdev
)
477 set_bit(HCI_RUNNING
, &hdev
->flags
);
483 * This function handles the event generated by firmware, rx data
484 * received from firmware, and tx data sent from kernel.
486 static int btmrvl_service_main_thread(void *data
)
488 struct btmrvl_thread
*thread
= data
;
489 struct btmrvl_private
*priv
= thread
->priv
;
490 struct btmrvl_adapter
*adapter
= priv
->adapter
;
495 init_waitqueue_entry(&wait
, current
);
498 add_wait_queue(&thread
->wait_q
, &wait
);
500 set_current_state(TASK_INTERRUPTIBLE
);
501 if (kthread_should_stop()) {
502 BT_DBG("main_thread: break from main thread");
506 if (adapter
->wakeup_tries
||
507 ((!adapter
->int_count
) &&
508 (!priv
->btmrvl_dev
.tx_dnld_rdy
||
509 skb_queue_empty(&adapter
->tx_queue
)))) {
510 BT_DBG("main_thread is sleeping...");
514 set_current_state(TASK_RUNNING
);
516 remove_wait_queue(&thread
->wait_q
, &wait
);
518 BT_DBG("main_thread woke up");
520 spin_lock_irqsave(&priv
->driver_lock
, flags
);
521 if (adapter
->int_count
) {
522 adapter
->int_count
= 0;
523 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
524 priv
->hw_process_int_status(priv
);
525 } else if (adapter
->ps_state
== PS_SLEEP
&&
526 !skb_queue_empty(&adapter
->tx_queue
)) {
527 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
528 adapter
->wakeup_tries
++;
529 priv
->hw_wakeup_firmware(priv
);
532 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
535 if (adapter
->ps_state
== PS_SLEEP
)
538 if (!priv
->btmrvl_dev
.tx_dnld_rdy
)
541 skb
= skb_dequeue(&adapter
->tx_queue
);
543 if (btmrvl_tx_pkt(priv
, skb
))
544 priv
->btmrvl_dev
.hcidev
->stat
.err_tx
++;
546 priv
->btmrvl_dev
.hcidev
->stat
.byte_tx
+= skb
->len
;
555 int btmrvl_register_hdev(struct btmrvl_private
*priv
)
557 struct hci_dev
*hdev
= NULL
;
560 hdev
= hci_alloc_dev();
562 BT_ERR("Can not allocate HCI device");
566 priv
->btmrvl_dev
.hcidev
= hdev
;
567 hci_set_drvdata(hdev
, priv
);
569 hdev
->bus
= HCI_SDIO
;
570 hdev
->open
= btmrvl_open
;
571 hdev
->close
= btmrvl_close
;
572 hdev
->flush
= btmrvl_flush
;
573 hdev
->send
= btmrvl_send_frame
;
574 hdev
->ioctl
= btmrvl_ioctl
;
576 btmrvl_send_module_cfg_cmd(priv
, MODULE_BRINGUP_REQ
);
578 hdev
->dev_type
= priv
->btmrvl_dev
.dev_type
;
580 ret
= hci_register_dev(hdev
);
582 BT_ERR("Can not register HCI device");
583 goto err_hci_register_dev
;
586 #ifdef CONFIG_DEBUG_FS
587 btmrvl_debugfs_init(hdev
);
592 err_hci_register_dev
:
596 /* Stop the thread servicing the interrupts */
597 kthread_stop(priv
->main_thread
.task
);
599 btmrvl_free_adapter(priv
);
604 EXPORT_SYMBOL_GPL(btmrvl_register_hdev
);
606 struct btmrvl_private
*btmrvl_add_card(void *card
)
608 struct btmrvl_private
*priv
;
610 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
612 BT_ERR("Can not allocate priv");
616 priv
->adapter
= kzalloc(sizeof(*priv
->adapter
), GFP_KERNEL
);
617 if (!priv
->adapter
) {
618 BT_ERR("Allocate buffer for btmrvl_adapter failed!");
622 btmrvl_init_adapter(priv
);
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
.card
= card
;
633 priv
->btmrvl_dev
.tx_dnld_rdy
= true;
643 EXPORT_SYMBOL_GPL(btmrvl_add_card
);
645 int btmrvl_remove_card(struct btmrvl_private
*priv
)
647 struct hci_dev
*hdev
;
649 hdev
= priv
->btmrvl_dev
.hcidev
;
651 wake_up_interruptible(&priv
->adapter
->cmd_wait_q
);
653 kthread_stop(priv
->main_thread
.task
);
655 #ifdef CONFIG_DEBUG_FS
656 btmrvl_debugfs_remove(hdev
);
659 hci_unregister_dev(hdev
);
663 priv
->btmrvl_dev
.hcidev
= NULL
;
665 btmrvl_free_adapter(priv
);
671 EXPORT_SYMBOL_GPL(btmrvl_remove_card
);
673 MODULE_AUTHOR("Marvell International Ltd.");
674 MODULE_DESCRIPTION("Marvell Bluetooth driver ver " VERSION
);
675 MODULE_VERSION(VERSION
);
676 MODULE_LICENSE("GPL v2");