1 // SPDX-License-Identifier: GPL-2.0-only
3 * Marvell Bluetooth driver
5 * Copyright (C) 2009, Marvell International Ltd.
8 #include <linux/module.h>
10 #include <net/bluetooth/bluetooth.h>
11 #include <net/bluetooth/hci_core.h>
12 #include <linux/mmc/sdio_func.h>
14 #include "btmrvl_drv.h"
15 #include "btmrvl_sdio.h"
20 * This function is called by interface specific interrupt handler.
21 * It updates Power Save & Host Sleep states, and wakes up the main
24 void btmrvl_interrupt(struct btmrvl_private
*priv
)
26 priv
->adapter
->ps_state
= PS_AWAKE
;
28 priv
->adapter
->wakeup_tries
= 0;
30 priv
->adapter
->int_count
++;
32 if (priv
->adapter
->hs_state
== HS_ACTIVATED
) {
33 BT_DBG("BT: HS DEACTIVATED in ISR!");
34 priv
->adapter
->hs_state
= HS_DEACTIVATED
;
37 wake_up_interruptible(&priv
->main_thread
.wait_q
);
39 EXPORT_SYMBOL_GPL(btmrvl_interrupt
);
41 bool btmrvl_check_evtpkt(struct btmrvl_private
*priv
, struct sk_buff
*skb
)
43 struct hci_event_hdr
*hdr
= (void *) skb
->data
;
45 if (hdr
->evt
== HCI_EV_CMD_COMPLETE
) {
46 struct hci_ev_cmd_complete
*ec
;
49 ec
= (void *) (skb
->data
+ HCI_EVENT_HDR_SIZE
);
50 opcode
= __le16_to_cpu(ec
->opcode
);
52 if (priv
->btmrvl_dev
.sendcmdflag
) {
53 priv
->btmrvl_dev
.sendcmdflag
= false;
54 priv
->adapter
->cmd_complete
= true;
55 wake_up_interruptible(&priv
->adapter
->cmd_wait_q
);
57 if (hci_opcode_ogf(opcode
) == 0x3F) {
58 BT_DBG("vendor event skipped: opcode=%#4.4x",
68 EXPORT_SYMBOL_GPL(btmrvl_check_evtpkt
);
70 int btmrvl_process_event(struct btmrvl_private
*priv
, struct sk_buff
*skb
)
72 struct btmrvl_adapter
*adapter
= priv
->adapter
;
73 struct btmrvl_event
*event
;
76 event
= (struct btmrvl_event
*) skb
->data
;
77 if (event
->ec
!= 0xff) {
78 BT_DBG("Not Marvell Event=%x", event
->ec
);
83 switch (event
->data
[0]) {
84 case BT_EVENT_AUTO_SLEEP_MODE
:
85 if (!event
->data
[2]) {
86 if (event
->data
[1] == BT_PS_ENABLE
)
91 (adapter
->psmode
) ? "Enable" : "Disable");
93 BT_DBG("PS Mode command failed");
97 case BT_EVENT_HOST_SLEEP_CONFIG
:
99 BT_DBG("gpio=%x, gap=%x", event
->data
[1],
102 BT_DBG("HSCFG command failed");
105 case BT_EVENT_HOST_SLEEP_ENABLE
:
106 if (!event
->data
[1]) {
107 adapter
->hs_state
= HS_ACTIVATED
;
109 adapter
->ps_state
= PS_SLEEP
;
110 wake_up_interruptible(&adapter
->event_hs_wait_q
);
111 BT_DBG("HS ACTIVATED!");
113 BT_DBG("HS Enable failed");
117 case BT_EVENT_MODULE_CFG_REQ
:
118 if (priv
->btmrvl_dev
.sendcmdflag
&&
119 event
->data
[1] == MODULE_BRINGUP_REQ
) {
121 ((event
->data
[2] == MODULE_BROUGHT_UP
) ||
122 (event
->data
[2] == MODULE_ALREADY_UP
)) ?
123 "Bring-up succeed" : "Bring-up failed");
124 } else if (priv
->btmrvl_dev
.sendcmdflag
&&
125 event
->data
[1] == MODULE_SHUTDOWN_REQ
) {
126 BT_DBG("EVENT:%s", (event
->data
[2]) ?
127 "Shutdown failed" : "Shutdown succeed");
129 BT_DBG("BT_CMD_MODULE_CFG_REQ resp for APP");
134 case BT_EVENT_POWER_STATE
:
135 if (event
->data
[1] == BT_PS_SLEEP
)
136 adapter
->ps_state
= PS_SLEEP
;
138 (adapter
->ps_state
) ? "PS_SLEEP" : "PS_AWAKE");
142 BT_DBG("Unknown Event=%d", event
->data
[0]);
153 EXPORT_SYMBOL_GPL(btmrvl_process_event
);
155 static int btmrvl_send_sync_cmd(struct btmrvl_private
*priv
, u16 opcode
,
156 const void *param
, u8 len
)
159 struct hci_command_hdr
*hdr
;
161 if (priv
->surprise_removed
) {
162 BT_ERR("Card is removed");
166 skb
= bt_skb_alloc(HCI_COMMAND_HDR_SIZE
+ len
, GFP_KERNEL
);
168 BT_ERR("No free skb");
172 hdr
= skb_put(skb
, HCI_COMMAND_HDR_SIZE
);
173 hdr
->opcode
= cpu_to_le16(opcode
);
177 skb_put_data(skb
, param
, len
);
179 hci_skb_pkt_type(skb
) = MRVL_VENDOR_PKT
;
181 skb_queue_head(&priv
->adapter
->tx_queue
, skb
);
183 priv
->btmrvl_dev
.sendcmdflag
= true;
185 priv
->adapter
->cmd_complete
= false;
187 wake_up_interruptible(&priv
->main_thread
.wait_q
);
189 if (!wait_event_interruptible_timeout(priv
->adapter
->cmd_wait_q
,
190 priv
->adapter
->cmd_complete
||
191 priv
->surprise_removed
,
192 WAIT_UNTIL_CMD_RESP
))
195 if (priv
->surprise_removed
)
201 int btmrvl_send_module_cfg_cmd(struct btmrvl_private
*priv
, u8 subcmd
)
205 ret
= btmrvl_send_sync_cmd(priv
, BT_CMD_MODULE_CFG_REQ
, &subcmd
, 1);
207 BT_ERR("module_cfg_cmd(%x) failed", subcmd
);
211 EXPORT_SYMBOL_GPL(btmrvl_send_module_cfg_cmd
);
213 static int btmrvl_enable_sco_routing_to_host(struct btmrvl_private
*priv
)
218 ret
= btmrvl_send_sync_cmd(priv
, BT_CMD_ROUTE_SCO_TO_HOST
, &subcmd
, 1);
220 BT_ERR("BT_CMD_ROUTE_SCO_TO_HOST command failed: %#x", ret
);
225 int btmrvl_pscan_window_reporting(struct btmrvl_private
*priv
, u8 subcmd
)
227 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
230 if (!card
->support_pscan_win_report
)
233 ret
= btmrvl_send_sync_cmd(priv
, BT_CMD_PSCAN_WIN_REPORT_ENABLE
,
236 BT_ERR("PSCAN_WIN_REPORT_ENABLE command failed: %#x", ret
);
240 EXPORT_SYMBOL_GPL(btmrvl_pscan_window_reporting
);
242 int btmrvl_send_hscfg_cmd(struct btmrvl_private
*priv
)
247 param
[0] = (priv
->btmrvl_dev
.gpio_gap
& 0xff00) >> 8;
248 param
[1] = (u8
) (priv
->btmrvl_dev
.gpio_gap
& 0x00ff);
250 BT_DBG("Sending HSCFG Command, gpio=0x%x, gap=0x%x",
253 ret
= btmrvl_send_sync_cmd(priv
, BT_CMD_HOST_SLEEP_CONFIG
, param
, 2);
255 BT_ERR("HSCFG command failed");
259 EXPORT_SYMBOL_GPL(btmrvl_send_hscfg_cmd
);
261 int btmrvl_enable_ps(struct btmrvl_private
*priv
)
266 if (priv
->btmrvl_dev
.psmode
)
267 param
= BT_PS_ENABLE
;
269 param
= BT_PS_DISABLE
;
271 ret
= btmrvl_send_sync_cmd(priv
, BT_CMD_AUTO_SLEEP_MODE
, ¶m
, 1);
273 BT_ERR("PSMODE command failed");
277 EXPORT_SYMBOL_GPL(btmrvl_enable_ps
);
279 int btmrvl_enable_hs(struct btmrvl_private
*priv
)
281 struct btmrvl_adapter
*adapter
= priv
->adapter
;
284 ret
= btmrvl_send_sync_cmd(priv
, BT_CMD_HOST_SLEEP_ENABLE
, NULL
, 0);
286 BT_ERR("Host sleep enable command failed");
290 ret
= wait_event_interruptible_timeout(adapter
->event_hs_wait_q
,
292 priv
->surprise_removed
,
293 WAIT_UNTIL_HS_STATE_CHANGED
);
294 if (ret
< 0 || priv
->surprise_removed
) {
295 BT_ERR("event_hs_wait_q terminated (%d): %d,%d,%d",
296 ret
, adapter
->hs_state
, adapter
->ps_state
,
297 adapter
->wakeup_tries
);
299 BT_ERR("hs_enable timeout: %d,%d,%d", adapter
->hs_state
,
300 adapter
->ps_state
, adapter
->wakeup_tries
);
303 BT_DBG("host sleep enabled: %d,%d,%d", adapter
->hs_state
,
304 adapter
->ps_state
, adapter
->wakeup_tries
);
310 EXPORT_SYMBOL_GPL(btmrvl_enable_hs
);
312 int btmrvl_prepare_command(struct btmrvl_private
*priv
)
316 if (priv
->btmrvl_dev
.hscfgcmd
) {
317 priv
->btmrvl_dev
.hscfgcmd
= 0;
318 btmrvl_send_hscfg_cmd(priv
);
321 if (priv
->btmrvl_dev
.pscmd
) {
322 priv
->btmrvl_dev
.pscmd
= 0;
323 btmrvl_enable_ps(priv
);
326 if (priv
->btmrvl_dev
.hscmd
) {
327 priv
->btmrvl_dev
.hscmd
= 0;
329 if (priv
->btmrvl_dev
.hsmode
) {
330 ret
= btmrvl_enable_hs(priv
);
332 ret
= priv
->hw_wakeup_firmware(priv
);
333 priv
->adapter
->hs_state
= HS_DEACTIVATED
;
334 BT_DBG("BT: HS DEACTIVATED due to host activity!");
341 static int btmrvl_tx_pkt(struct btmrvl_private
*priv
, struct sk_buff
*skb
)
345 if (!skb
|| !skb
->data
)
348 if (!skb
->len
|| ((skb
->len
+ BTM_HEADER_LEN
) > BTM_UPLD_SIZE
)) {
349 BT_ERR("Tx Error: Bad skb length %d : %d",
350 skb
->len
, BTM_UPLD_SIZE
);
354 skb_push(skb
, BTM_HEADER_LEN
);
356 /* header type: byte[3]
357 * HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor
358 * header length: byte[2][1][0]
361 skb
->data
[0] = (skb
->len
& 0x0000ff);
362 skb
->data
[1] = (skb
->len
& 0x00ff00) >> 8;
363 skb
->data
[2] = (skb
->len
& 0xff0000) >> 16;
364 skb
->data
[3] = hci_skb_pkt_type(skb
);
366 if (priv
->hw_host_to_card
)
367 ret
= priv
->hw_host_to_card(priv
, skb
->data
, skb
->len
);
372 static void btmrvl_init_adapter(struct btmrvl_private
*priv
)
376 skb_queue_head_init(&priv
->adapter
->tx_queue
);
378 priv
->adapter
->ps_state
= PS_AWAKE
;
380 buf_size
= ALIGN_SZ(SDIO_BLOCK_SIZE
, BTSDIO_DMA_ALIGN
);
381 priv
->adapter
->hw_regs_buf
= kzalloc(buf_size
, GFP_KERNEL
);
382 if (!priv
->adapter
->hw_regs_buf
) {
383 priv
->adapter
->hw_regs
= NULL
;
384 BT_ERR("Unable to allocate buffer for hw_regs.");
386 priv
->adapter
->hw_regs
=
387 (u8
*)ALIGN_ADDR(priv
->adapter
->hw_regs_buf
,
389 BT_DBG("hw_regs_buf=%p hw_regs=%p",
390 priv
->adapter
->hw_regs_buf
, priv
->adapter
->hw_regs
);
393 init_waitqueue_head(&priv
->adapter
->cmd_wait_q
);
394 init_waitqueue_head(&priv
->adapter
->event_hs_wait_q
);
397 static void btmrvl_free_adapter(struct btmrvl_private
*priv
)
399 skb_queue_purge(&priv
->adapter
->tx_queue
);
401 kfree(priv
->adapter
->hw_regs_buf
);
402 kfree(priv
->adapter
);
404 priv
->adapter
= NULL
;
407 static int btmrvl_send_frame(struct hci_dev
*hdev
, struct sk_buff
*skb
)
409 struct btmrvl_private
*priv
= hci_get_drvdata(hdev
);
411 BT_DBG("type=%d, len=%d", hci_skb_pkt_type(skb
), skb
->len
);
413 if (priv
->adapter
->is_suspending
|| priv
->adapter
->is_suspended
) {
414 BT_ERR("%s: Device is suspending or suspended", __func__
);
418 switch (hci_skb_pkt_type(skb
)) {
419 case HCI_COMMAND_PKT
:
423 case HCI_ACLDATA_PKT
:
427 case HCI_SCODATA_PKT
:
432 skb_queue_tail(&priv
->adapter
->tx_queue
, skb
);
434 if (!priv
->adapter
->is_suspended
)
435 wake_up_interruptible(&priv
->main_thread
.wait_q
);
440 static int btmrvl_flush(struct hci_dev
*hdev
)
442 struct btmrvl_private
*priv
= hci_get_drvdata(hdev
);
444 skb_queue_purge(&priv
->adapter
->tx_queue
);
449 static int btmrvl_close(struct hci_dev
*hdev
)
451 struct btmrvl_private
*priv
= hci_get_drvdata(hdev
);
453 skb_queue_purge(&priv
->adapter
->tx_queue
);
458 static int btmrvl_open(struct hci_dev
*hdev
)
463 static int btmrvl_download_cal_data(struct btmrvl_private
*priv
,
473 print_hex_dump_bytes("Calibration data: ",
474 DUMP_PREFIX_OFFSET
, data
, BT_CAL_HDR_LEN
+ len
);
476 ret
= btmrvl_send_sync_cmd(priv
, BT_CMD_LOAD_CONFIG_DATA
, data
,
477 BT_CAL_HDR_LEN
+ len
);
479 BT_ERR("Failed to download calibration data");
484 static int btmrvl_check_device_tree(struct btmrvl_private
*priv
)
486 struct device_node
*dt_node
;
487 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
488 u8 cal_data
[BT_CAL_HDR_LEN
+ BT_CAL_DATA_SIZE
];
492 if (card
->plt_of_node
) {
493 dt_node
= card
->plt_of_node
;
494 ret
= of_property_read_u16(dt_node
, "marvell,wakeup-pin",
497 gpio
= (priv
->btmrvl_dev
.gpio_gap
& 0xff00) >> 8;
499 ret
= of_property_read_u16(dt_node
, "marvell,wakeup-gap-ms",
502 gap
= (u8
)(priv
->btmrvl_dev
.gpio_gap
& 0x00ff);
504 priv
->btmrvl_dev
.gpio_gap
= (gpio
<< 8) + gap
;
506 ret
= of_property_read_u8_array(dt_node
, "marvell,cal-data",
507 cal_data
+ BT_CAL_HDR_LEN
,
512 BT_DBG("Use cal data from device tree");
513 ret
= btmrvl_download_cal_data(priv
, cal_data
,
516 BT_ERR("Fail to download calibrate data");
522 static int btmrvl_setup(struct hci_dev
*hdev
)
524 struct btmrvl_private
*priv
= hci_get_drvdata(hdev
);
527 ret
= btmrvl_send_module_cfg_cmd(priv
, MODULE_BRINGUP_REQ
);
531 priv
->btmrvl_dev
.gpio_gap
= 0xfffe;
533 btmrvl_check_device_tree(priv
);
535 btmrvl_enable_sco_routing_to_host(priv
);
537 btmrvl_pscan_window_reporting(priv
, 0x01);
539 priv
->btmrvl_dev
.psmode
= 1;
540 btmrvl_enable_ps(priv
);
542 btmrvl_send_hscfg_cmd(priv
);
547 static int btmrvl_set_bdaddr(struct hci_dev
*hdev
, const bdaddr_t
*bdaddr
)
553 buf
[0] = MRVL_VENDOR_PKT
;
554 buf
[1] = sizeof(bdaddr_t
);
555 memcpy(buf
+ 2, bdaddr
, sizeof(bdaddr_t
));
557 skb
= __hci_cmd_sync(hdev
, BT_CMD_SET_BDADDR
, sizeof(buf
), buf
,
561 BT_ERR("%s: changing btmrvl device address failed (%ld)",
570 static bool btmrvl_wakeup(struct hci_dev
*hdev
)
572 struct btmrvl_private
*priv
= hci_get_drvdata(hdev
);
573 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
575 return device_may_wakeup(&card
->func
->dev
);
579 * This function handles the event generated by firmware, rx data
580 * received from firmware, and tx data sent from kernel.
582 static int btmrvl_service_main_thread(void *data
)
584 struct btmrvl_thread
*thread
= data
;
585 struct btmrvl_private
*priv
= thread
->priv
;
586 struct btmrvl_adapter
*adapter
= priv
->adapter
;
587 wait_queue_entry_t wait
;
591 init_waitqueue_entry(&wait
, current
);
594 add_wait_queue(&thread
->wait_q
, &wait
);
596 set_current_state(TASK_INTERRUPTIBLE
);
597 if (kthread_should_stop() || priv
->surprise_removed
) {
598 BT_DBG("main_thread: break from main thread");
602 if (adapter
->wakeup_tries
||
603 ((!adapter
->int_count
) &&
604 (!priv
->btmrvl_dev
.tx_dnld_rdy
||
605 skb_queue_empty(&adapter
->tx_queue
)))) {
606 BT_DBG("main_thread is sleeping...");
610 set_current_state(TASK_RUNNING
);
612 remove_wait_queue(&thread
->wait_q
, &wait
);
614 BT_DBG("main_thread woke up");
616 if (kthread_should_stop() || priv
->surprise_removed
) {
617 BT_DBG("main_thread: break from main thread");
621 spin_lock_irqsave(&priv
->driver_lock
, flags
);
622 if (adapter
->int_count
) {
623 adapter
->int_count
= 0;
624 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
625 priv
->hw_process_int_status(priv
);
626 } else if (adapter
->ps_state
== PS_SLEEP
&&
627 !skb_queue_empty(&adapter
->tx_queue
)) {
628 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
629 adapter
->wakeup_tries
++;
630 priv
->hw_wakeup_firmware(priv
);
633 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
636 if (adapter
->ps_state
== PS_SLEEP
)
639 if (!priv
->btmrvl_dev
.tx_dnld_rdy
||
640 priv
->adapter
->is_suspended
)
643 skb
= skb_dequeue(&adapter
->tx_queue
);
645 if (btmrvl_tx_pkt(priv
, skb
))
646 priv
->btmrvl_dev
.hcidev
->stat
.err_tx
++;
648 priv
->btmrvl_dev
.hcidev
->stat
.byte_tx
+= skb
->len
;
657 int btmrvl_register_hdev(struct btmrvl_private
*priv
)
659 struct hci_dev
*hdev
= NULL
;
660 struct btmrvl_sdio_card
*card
= priv
->btmrvl_dev
.card
;
663 hdev
= hci_alloc_dev();
665 BT_ERR("Can not allocate HCI device");
669 priv
->btmrvl_dev
.hcidev
= hdev
;
670 hci_set_drvdata(hdev
, priv
);
672 hdev
->bus
= HCI_SDIO
;
673 hdev
->open
= btmrvl_open
;
674 hdev
->close
= btmrvl_close
;
675 hdev
->flush
= btmrvl_flush
;
676 hdev
->send
= btmrvl_send_frame
;
677 hdev
->setup
= btmrvl_setup
;
678 hdev
->set_bdaddr
= btmrvl_set_bdaddr
;
679 hdev
->wakeup
= btmrvl_wakeup
;
680 SET_HCIDEV_DEV(hdev
, &card
->func
->dev
);
682 ret
= hci_register_dev(hdev
);
684 BT_ERR("Can not register HCI device");
685 goto err_hci_register_dev
;
688 #ifdef CONFIG_DEBUG_FS
689 btmrvl_debugfs_init(hdev
);
694 err_hci_register_dev
:
698 /* Stop the thread servicing the interrupts */
699 kthread_stop(priv
->main_thread
.task
);
701 btmrvl_free_adapter(priv
);
706 EXPORT_SYMBOL_GPL(btmrvl_register_hdev
);
708 struct btmrvl_private
*btmrvl_add_card(void *card
)
710 struct btmrvl_private
*priv
;
712 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
714 BT_ERR("Can not allocate priv");
718 priv
->adapter
= kzalloc(sizeof(*priv
->adapter
), GFP_KERNEL
);
719 if (!priv
->adapter
) {
720 BT_ERR("Allocate buffer for btmrvl_adapter failed!");
724 btmrvl_init_adapter(priv
);
726 BT_DBG("Starting kthread...");
727 priv
->main_thread
.priv
= priv
;
728 spin_lock_init(&priv
->driver_lock
);
730 init_waitqueue_head(&priv
->main_thread
.wait_q
);
731 priv
->main_thread
.task
= kthread_run(btmrvl_service_main_thread
,
732 &priv
->main_thread
, "btmrvl_main_service");
733 if (IS_ERR(priv
->main_thread
.task
))
736 priv
->btmrvl_dev
.card
= card
;
737 priv
->btmrvl_dev
.tx_dnld_rdy
= true;
742 btmrvl_free_adapter(priv
);
750 EXPORT_SYMBOL_GPL(btmrvl_add_card
);
752 int btmrvl_remove_card(struct btmrvl_private
*priv
)
754 struct hci_dev
*hdev
;
756 hdev
= priv
->btmrvl_dev
.hcidev
;
758 wake_up_interruptible(&priv
->adapter
->cmd_wait_q
);
759 wake_up_interruptible(&priv
->adapter
->event_hs_wait_q
);
761 kthread_stop(priv
->main_thread
.task
);
763 #ifdef CONFIG_DEBUG_FS
764 btmrvl_debugfs_remove(hdev
);
767 hci_unregister_dev(hdev
);
771 priv
->btmrvl_dev
.hcidev
= NULL
;
773 btmrvl_free_adapter(priv
);
779 EXPORT_SYMBOL_GPL(btmrvl_remove_card
);
781 MODULE_AUTHOR("Marvell International Ltd.");
782 MODULE_DESCRIPTION("Marvell Bluetooth driver ver " VERSION
);
783 MODULE_VERSION(VERSION
);
784 MODULE_LICENSE("GPL v2");