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");
125 if (event->length > 3 && event->data[3])
126 priv->btmrvl_dev.dev_type = HCI_AMP;
128 priv->btmrvl_dev.dev_type = HCI_PRIMARY;
130 BT_DBG("dev_type: %d", priv->btmrvl_dev.dev_type);
131 } else if (priv->btmrvl_dev.sendcmdflag &&
132 event->data[1] == MODULE_SHUTDOWN_REQ) {
133 BT_DBG("EVENT:%s", (event->data[2]) ?
134 "Shutdown failed" : "Shutdown succeed");
136 BT_DBG("BT_CMD_MODULE_CFG_REQ resp for APP");
141 case BT_EVENT_POWER_STATE:
142 if (event->data[1] == BT_PS_SLEEP)
143 adapter->ps_state = PS_SLEEP;
145 (adapter->ps_state) ? "PS_SLEEP" : "PS_AWAKE");
149 BT_DBG("Unknown Event=%d", event->data[0]);
160 EXPORT_SYMBOL_GPL(btmrvl_process_event);
162 static int btmrvl_send_sync_cmd(struct btmrvl_private *priv, u16 opcode,
163 const void *param, u8 len)
166 struct hci_command_hdr *hdr;
168 if (priv->surprise_removed) {
169 BT_ERR("Card is removed");
173 skb = bt_skb_alloc(HCI_COMMAND_HDR_SIZE + len, GFP_KERNEL);
175 BT_ERR("No free skb");
179 hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
180 hdr->opcode = cpu_to_le16(opcode);
184 skb_put_data(skb, param, len);
186 hci_skb_pkt_type(skb) = MRVL_VENDOR_PKT;
188 skb_queue_head(&priv->adapter->tx_queue, skb);
190 priv->btmrvl_dev.sendcmdflag = true;
192 priv->adapter->cmd_complete = false;
194 wake_up_interruptible(&priv->main_thread.wait_q);
196 if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q,
197 priv->adapter->cmd_complete ||
198 priv->surprise_removed,
199 WAIT_UNTIL_CMD_RESP))
202 if (priv->surprise_removed)
208 int btmrvl_send_module_cfg_cmd(struct btmrvl_private *priv, u8 subcmd)
212 ret = btmrvl_send_sync_cmd(priv, BT_CMD_MODULE_CFG_REQ, &subcmd, 1);
214 BT_ERR("module_cfg_cmd(%x) failed", subcmd);
218 EXPORT_SYMBOL_GPL(btmrvl_send_module_cfg_cmd);
220 static int btmrvl_enable_sco_routing_to_host(struct btmrvl_private *priv)
225 ret = btmrvl_send_sync_cmd(priv, BT_CMD_ROUTE_SCO_TO_HOST, &subcmd, 1);
227 BT_ERR("BT_CMD_ROUTE_SCO_TO_HOST command failed: %#x", ret);
232 int btmrvl_pscan_window_reporting(struct btmrvl_private *priv, u8 subcmd)
234 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
237 if (!card->support_pscan_win_report)
240 ret = btmrvl_send_sync_cmd(priv, BT_CMD_PSCAN_WIN_REPORT_ENABLE,
243 BT_ERR("PSCAN_WIN_REPORT_ENABLE command failed: %#x", ret);
247 EXPORT_SYMBOL_GPL(btmrvl_pscan_window_reporting);
249 int btmrvl_send_hscfg_cmd(struct btmrvl_private *priv)
254 param[0] = (priv->btmrvl_dev.gpio_gap & 0xff00) >> 8;
255 param[1] = (u8) (priv->btmrvl_dev.gpio_gap & 0x00ff);
257 BT_DBG("Sending HSCFG Command, gpio=0x%x, gap=0x%x",
260 ret = btmrvl_send_sync_cmd(priv, BT_CMD_HOST_SLEEP_CONFIG, param, 2);
262 BT_ERR("HSCFG command failed");
266 EXPORT_SYMBOL_GPL(btmrvl_send_hscfg_cmd);
268 int btmrvl_enable_ps(struct btmrvl_private *priv)
273 if (priv->btmrvl_dev.psmode)
274 param = BT_PS_ENABLE;
276 param = BT_PS_DISABLE;
278 ret = btmrvl_send_sync_cmd(priv, BT_CMD_AUTO_SLEEP_MODE, ¶m, 1);
280 BT_ERR("PSMODE command failed");
284 EXPORT_SYMBOL_GPL(btmrvl_enable_ps);
286 int btmrvl_enable_hs(struct btmrvl_private *priv)
288 struct btmrvl_adapter *adapter = priv->adapter;
291 ret = btmrvl_send_sync_cmd(priv, BT_CMD_HOST_SLEEP_ENABLE, NULL, 0);
293 BT_ERR("Host sleep enable command failed");
297 ret = wait_event_interruptible_timeout(adapter->event_hs_wait_q,
299 priv->surprise_removed,
300 WAIT_UNTIL_HS_STATE_CHANGED);
301 if (ret < 0 || priv->surprise_removed) {
302 BT_ERR("event_hs_wait_q terminated (%d): %d,%d,%d",
303 ret, adapter->hs_state, adapter->ps_state,
304 adapter->wakeup_tries);
306 BT_ERR("hs_enable timeout: %d,%d,%d", adapter->hs_state,
307 adapter->ps_state, adapter->wakeup_tries);
310 BT_DBG("host sleep enabled: %d,%d,%d", adapter->hs_state,
311 adapter->ps_state, adapter->wakeup_tries);
317 EXPORT_SYMBOL_GPL(btmrvl_enable_hs);
319 int btmrvl_prepare_command(struct btmrvl_private *priv)
323 if (priv->btmrvl_dev.hscfgcmd) {
324 priv->btmrvl_dev.hscfgcmd = 0;
325 btmrvl_send_hscfg_cmd(priv);
328 if (priv->btmrvl_dev.pscmd) {
329 priv->btmrvl_dev.pscmd = 0;
330 btmrvl_enable_ps(priv);
333 if (priv->btmrvl_dev.hscmd) {
334 priv->btmrvl_dev.hscmd = 0;
336 if (priv->btmrvl_dev.hsmode) {
337 ret = btmrvl_enable_hs(priv);
339 ret = priv->hw_wakeup_firmware(priv);
340 priv->adapter->hs_state = HS_DEACTIVATED;
341 BT_DBG("BT: HS DEACTIVATED due to host activity!");
348 static int btmrvl_tx_pkt(struct btmrvl_private *priv, struct sk_buff *skb)
352 if (!skb || !skb->data)
355 if (!skb->len || ((skb->len + BTM_HEADER_LEN) > BTM_UPLD_SIZE)) {
356 BT_ERR("Tx Error: Bad skb length %d : %d",
357 skb->len, BTM_UPLD_SIZE);
361 skb_push(skb, BTM_HEADER_LEN);
363 /* header type: byte[3]
364 * HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor
365 * header length: byte[2][1][0]
368 skb->data[0] = (skb->len & 0x0000ff);
369 skb->data[1] = (skb->len & 0x00ff00) >> 8;
370 skb->data[2] = (skb->len & 0xff0000) >> 16;
371 skb->data[3] = hci_skb_pkt_type(skb);
373 if (priv->hw_host_to_card)
374 ret = priv->hw_host_to_card(priv, skb->data, skb->len);
379 static void btmrvl_init_adapter(struct btmrvl_private *priv)
383 skb_queue_head_init(&priv->adapter->tx_queue);
385 priv->adapter->ps_state = PS_AWAKE;
387 buf_size = ALIGN_SZ(SDIO_BLOCK_SIZE, BTSDIO_DMA_ALIGN);
388 priv->adapter->hw_regs_buf = kzalloc(buf_size, GFP_KERNEL);
389 if (!priv->adapter->hw_regs_buf) {
390 priv->adapter->hw_regs = NULL;
391 BT_ERR("Unable to allocate buffer for hw_regs.");
393 priv->adapter->hw_regs =
394 (u8 *)ALIGN_ADDR(priv->adapter->hw_regs_buf,
396 BT_DBG("hw_regs_buf=%p hw_regs=%p",
397 priv->adapter->hw_regs_buf, priv->adapter->hw_regs);
400 init_waitqueue_head(&priv->adapter->cmd_wait_q);
401 init_waitqueue_head(&priv->adapter->event_hs_wait_q);
404 static void btmrvl_free_adapter(struct btmrvl_private *priv)
406 skb_queue_purge(&priv->adapter->tx_queue);
408 kfree(priv->adapter->hw_regs_buf);
409 kfree(priv->adapter);
411 priv->adapter = NULL;
414 static int btmrvl_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
416 struct btmrvl_private *priv = hci_get_drvdata(hdev);
418 BT_DBG("type=%d, len=%d", hci_skb_pkt_type(skb), skb->len);
420 if (priv->adapter->is_suspending || priv->adapter->is_suspended) {
421 BT_ERR("%s: Device is suspending or suspended", __func__);
425 switch (hci_skb_pkt_type(skb)) {
426 case HCI_COMMAND_PKT:
430 case HCI_ACLDATA_PKT:
434 case HCI_SCODATA_PKT:
439 skb_queue_tail(&priv->adapter->tx_queue, skb);
441 if (!priv->adapter->is_suspended)
442 wake_up_interruptible(&priv->main_thread.wait_q);
447 static int btmrvl_flush(struct hci_dev *hdev)
449 struct btmrvl_private *priv = hci_get_drvdata(hdev);
451 skb_queue_purge(&priv->adapter->tx_queue);
456 static int btmrvl_close(struct hci_dev *hdev)
458 struct btmrvl_private *priv = hci_get_drvdata(hdev);
460 skb_queue_purge(&priv->adapter->tx_queue);
465 static int btmrvl_open(struct hci_dev *hdev)
470 static int btmrvl_download_cal_data(struct btmrvl_private *priv,
480 print_hex_dump_bytes("Calibration data: ",
481 DUMP_PREFIX_OFFSET, data, BT_CAL_HDR_LEN + len);
483 ret = btmrvl_send_sync_cmd(priv, BT_CMD_LOAD_CONFIG_DATA, data,
484 BT_CAL_HDR_LEN + len);
486 BT_ERR("Failed to download calibration data");
491 static int btmrvl_check_device_tree(struct btmrvl_private *priv)
493 struct device_node *dt_node;
494 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
495 u8 cal_data[BT_CAL_HDR_LEN + BT_CAL_DATA_SIZE];
499 if (card->plt_of_node) {
500 dt_node = card->plt_of_node;
501 ret = of_property_read_u16(dt_node, "marvell,wakeup-pin",
504 gpio = (priv->btmrvl_dev.gpio_gap & 0xff00) >> 8;
506 ret = of_property_read_u16(dt_node, "marvell,wakeup-gap-ms",
509 gap = (u8)(priv->btmrvl_dev.gpio_gap & 0x00ff);
511 priv->btmrvl_dev.gpio_gap = (gpio << 8) + gap;
513 ret = of_property_read_u8_array(dt_node, "marvell,cal-data",
514 cal_data + BT_CAL_HDR_LEN,
519 BT_DBG("Use cal data from device tree");
520 ret = btmrvl_download_cal_data(priv, cal_data,
523 BT_ERR("Fail to download calibrate data");
529 static int btmrvl_setup(struct hci_dev *hdev)
531 struct btmrvl_private *priv = hci_get_drvdata(hdev);
534 ret = btmrvl_send_module_cfg_cmd(priv, MODULE_BRINGUP_REQ);
538 priv->btmrvl_dev.gpio_gap = 0xfffe;
540 btmrvl_check_device_tree(priv);
542 btmrvl_enable_sco_routing_to_host(priv);
544 btmrvl_pscan_window_reporting(priv, 0x01);
546 priv->btmrvl_dev.psmode = 1;
547 btmrvl_enable_ps(priv);
549 btmrvl_send_hscfg_cmd(priv);
554 static int btmrvl_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
560 buf[0] = MRVL_VENDOR_PKT;
561 buf[1] = sizeof(bdaddr_t);
562 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
564 skb = __hci_cmd_sync(hdev, BT_CMD_SET_BDADDR, sizeof(buf), buf,
568 BT_ERR("%s: changing btmrvl device address failed (%ld)",
577 static bool btmrvl_wakeup(struct hci_dev *hdev)
579 struct btmrvl_private *priv = hci_get_drvdata(hdev);
580 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
582 return device_may_wakeup(&card->func->dev);
586 * This function handles the event generated by firmware, rx data
587 * received from firmware, and tx data sent from kernel.
589 static int btmrvl_service_main_thread(void *data)
591 struct btmrvl_thread *thread = data;
592 struct btmrvl_private *priv = thread->priv;
593 struct btmrvl_adapter *adapter = priv->adapter;
594 wait_queue_entry_t wait;
598 init_waitqueue_entry(&wait, current);
601 add_wait_queue(&thread->wait_q, &wait);
603 set_current_state(TASK_INTERRUPTIBLE);
604 if (kthread_should_stop() || priv->surprise_removed) {
605 BT_DBG("main_thread: break from main thread");
609 if (adapter->wakeup_tries ||
610 ((!adapter->int_count) &&
611 (!priv->btmrvl_dev.tx_dnld_rdy ||
612 skb_queue_empty(&adapter->tx_queue)))) {
613 BT_DBG("main_thread is sleeping...");
617 set_current_state(TASK_RUNNING);
619 remove_wait_queue(&thread->wait_q, &wait);
621 BT_DBG("main_thread woke up");
623 if (kthread_should_stop() || priv->surprise_removed) {
624 BT_DBG("main_thread: break from main thread");
628 spin_lock_irqsave(&priv->driver_lock, flags);
629 if (adapter->int_count) {
630 adapter->int_count = 0;
631 spin_unlock_irqrestore(&priv->driver_lock, flags);
632 priv->hw_process_int_status(priv);
633 } else if (adapter->ps_state == PS_SLEEP &&
634 !skb_queue_empty(&adapter->tx_queue)) {
635 spin_unlock_irqrestore(&priv->driver_lock, flags);
636 adapter->wakeup_tries++;
637 priv->hw_wakeup_firmware(priv);
640 spin_unlock_irqrestore(&priv->driver_lock, flags);
643 if (adapter->ps_state == PS_SLEEP)
646 if (!priv->btmrvl_dev.tx_dnld_rdy ||
647 priv->adapter->is_suspended)
650 skb = skb_dequeue(&adapter->tx_queue);
652 if (btmrvl_tx_pkt(priv, skb))
653 priv->btmrvl_dev.hcidev->stat.err_tx++;
655 priv->btmrvl_dev.hcidev->stat.byte_tx += skb->len;
664 int btmrvl_register_hdev(struct btmrvl_private *priv)
666 struct hci_dev *hdev = NULL;
667 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
670 hdev = hci_alloc_dev();
672 BT_ERR("Can not allocate HCI device");
676 priv->btmrvl_dev.hcidev = hdev;
677 hci_set_drvdata(hdev, priv);
679 hdev->bus = HCI_SDIO;
680 hdev->open = btmrvl_open;
681 hdev->close = btmrvl_close;
682 hdev->flush = btmrvl_flush;
683 hdev->send = btmrvl_send_frame;
684 hdev->setup = btmrvl_setup;
685 hdev->set_bdaddr = btmrvl_set_bdaddr;
686 hdev->wakeup = btmrvl_wakeup;
687 SET_HCIDEV_DEV(hdev, &card->func->dev);
689 hdev->dev_type = priv->btmrvl_dev.dev_type;
691 ret = hci_register_dev(hdev);
693 BT_ERR("Can not register HCI device");
694 goto err_hci_register_dev;
697 #ifdef CONFIG_DEBUG_FS
698 btmrvl_debugfs_init(hdev);
703 err_hci_register_dev:
707 /* Stop the thread servicing the interrupts */
708 kthread_stop(priv->main_thread.task);
710 btmrvl_free_adapter(priv);
715 EXPORT_SYMBOL_GPL(btmrvl_register_hdev);
717 struct btmrvl_private *btmrvl_add_card(void *card)
719 struct btmrvl_private *priv;
721 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
723 BT_ERR("Can not allocate priv");
727 priv->adapter = kzalloc(sizeof(*priv->adapter), GFP_KERNEL);
728 if (!priv->adapter) {
729 BT_ERR("Allocate buffer for btmrvl_adapter failed!");
733 btmrvl_init_adapter(priv);
735 BT_DBG("Starting kthread...");
736 priv->main_thread.priv = priv;
737 spin_lock_init(&priv->driver_lock);
739 init_waitqueue_head(&priv->main_thread.wait_q);
740 priv->main_thread.task = kthread_run(btmrvl_service_main_thread,
741 &priv->main_thread, "btmrvl_main_service");
742 if (IS_ERR(priv->main_thread.task))
745 priv->btmrvl_dev.card = card;
746 priv->btmrvl_dev.tx_dnld_rdy = true;
751 btmrvl_free_adapter(priv);
759 EXPORT_SYMBOL_GPL(btmrvl_add_card);
761 int btmrvl_remove_card(struct btmrvl_private *priv)
763 struct hci_dev *hdev;
765 hdev = priv->btmrvl_dev.hcidev;
767 wake_up_interruptible(&priv->adapter->cmd_wait_q);
768 wake_up_interruptible(&priv->adapter->event_hs_wait_q);
770 kthread_stop(priv->main_thread.task);
772 #ifdef CONFIG_DEBUG_FS
773 btmrvl_debugfs_remove(hdev);
776 hci_unregister_dev(hdev);
780 priv->btmrvl_dev.hcidev = NULL;
782 btmrvl_free_adapter(priv);
788 EXPORT_SYMBOL_GPL(btmrvl_remove_card);
790 MODULE_AUTHOR("Marvell International Ltd.");
791 MODULE_DESCRIPTION("Marvell Bluetooth driver ver " VERSION);
792 MODULE_VERSION(VERSION);
793 MODULE_LICENSE("GPL v2");