// SPDX-License-Identifier: GPL-2.0// Copyright (c) 2018 MediaTek Inc. /* * Bluetooth support for MediaTek serial devices * * Author: Sean Wang <sean.wang@mediatek.com> * */ #include <linux/unaligned.h>#include <linux/atomic.h>#include <linux/clk.h>#include <linux/firmware.h>#include <linux/gpio/consumer.h>#include <linux/iopoll.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/of.h>#include <linux/pinctrl/consumer.h>#include <linux/pm_runtime.h>#include <linux/regulator/consumer.h>#include <linux/serdev.h>#include <linux/skbuff.h>#include <linux/usb.h> #include <net/bluetooth/bluetooth.h>#include <net/bluetooth/hci_core.h> #include "hci_uart.h"#include "btmtk.h" #define VERSION "0.2" #define MTK_STP_TLR_SIZE 2 #define BTMTKUART_TX_STATE_ACTIVE 1#define BTMTKUART_TX_STATE_WAKEUP 2#define BTMTKUART_TX_WAIT_VND_EVT 3#define BTMTKUART_REQUIRED_WAKEUP 4 #define BTMTKUART_FLAG_STANDALONE_HW BIT(0) struct mtk_stp_hdr { u8 prefix; __be16 dlen; u8 cs;} __packed; struct btmtkuart_data { unsigned int flags; const char *fwname;}; struct btmtkuart_dev { struct hci_dev *hdev; struct serdev_device *serdev; struct clk *clk; struct clk *osc; struct regulator *vcc; struct gpio_desc *reset; struct gpio_desc *boot; struct pinctrl *pinctrl; struct pinctrl_state *pins_runtime; struct pinctrl_state *pins_boot; speed_t desired_speed; speed_t curr_speed; struct work_struct tx_work; unsigned long tx_state; struct sk_buff_head txq; struct sk_buff *rx_skb; struct sk_buff *evt_skb; u8 stp_pad[6]; u8 stp_cursor; u16 stp_dlen; const struct btmtkuart_data *data; struct hci_uart hu;}; #define btmtkuart_is_standalone(bdev) \ ((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW)#define btmtkuart_is_builtin_soc(bdev) \ !((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW) static int mtk_hci_wmt_sync(struct hci_dev *hdev, struct btmtk_hci_wmt_params *wmt_params){ struct btmtkuart_dev *bdev = hci_get_drvdata(hdev); struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc; u32 hlen, status = BTMTK_WMT_INVALID; struct btmtk_hci_wmt_evt *wmt_evt; struct btmtk_hci_wmt_cmd *wc; struct btmtk_wmt_hdr *hdr; int err; /* Send the WMT command and wait until the WMT event returns */ hlen = sizeof(*hdr) + wmt_params->dlen; if (hlen > 255) { err = -EINVAL; goto err_free_skb; } wc = kzalloc(hlen, GFP_KERNEL); if (!wc) { err = -ENOMEM; goto err_free_skb; } hdr = &wc->hdr; hdr->dir = 1; hdr->op = wmt_params->op; hdr->dlen = cpu_to_le16(wmt_params->dlen + 1); hdr->flag = wmt_params->flag; memcpy(wc->data, wmt_params->data, wmt_params->dlen); set_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state); err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc); if (err < 0) { clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state); goto err_free_wc; } /* The vendor specific WMT commands are all answered by a vendor * specific event and will not have the Command Status or Command * Complete as with usual HCI command flow control. * * After sending the command, wait for BTMTKUART_TX_WAIT_VND_EVT * state to be cleared. The driver specific event receive routine * will clear that state and with that indicate completion of the * WMT command. */ err = wait_on_bit_timeout(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT, TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT); if (err == -EINTR) { bt_dev_err(hdev, "Execution of wmt command interrupted"); clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state); goto err_free_wc; } if (err) { bt_dev_err(hdev, "Execution of wmt command timed out"); clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state); err = -ETIMEDOUT; goto err_free_wc; } /* Parse and handle the return WMT event */ wmt_evt = (struct btmtk_hci_wmt_evt *)bdev->evt_skb->data; if (wmt_evt->whdr.op != hdr->op) { bt_dev_err(hdev, "Wrong op received %d expected %d", wmt_evt->whdr.op, hdr->op); err = -EIO; goto err_free_wc; } switch (wmt_evt->whdr.op) { case BTMTK_WMT_SEMAPHORE: if (wmt_evt->whdr.flag == 2) status = BTMTK_WMT_PATCH_UNDONE; else status = BTMTK_WMT_PATCH_DONE; break; case BTMTK_WMT_FUNC_CTRL: wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt; if (be16_to_cpu(wmt_evt_funcc->status) == 0x404) status = BTMTK_WMT_ON_DONE; else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420) status = BTMTK_WMT_ON_PROGRESS; else status = BTMTK_WMT_ON_UNDONE; break; } if (wmt_params->status) *wmt_params->status = status; err_free_wc: kfree(wc);err_free_skb: kfree_skb(bdev->evt_skb); bdev->evt_skb = NULL; return err;} static int btmtkuart_recv_event(struct hci_dev *hdev, struct sk_buff *skb){ struct btmtkuart_dev *bdev = hci_get_drvdata(hdev); struct hci_event_hdr *hdr = (void *)skb->data; int err; /* When someone waits for the WMT event, the skb is being cloned * and being processed the events from there then. */ if (test_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state)) { bdev->evt_skb = skb_clone(skb, GFP_KERNEL); if (!bdev->evt_skb) { err = -ENOMEM; goto err_out; } } err = hci_recv_frame(hdev, skb); if (err < 0) goto err_free_skb; if (hdr->evt == HCI_EV_WMT) { if (test_and_clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state)) { /* Barrier to sync with other CPUs */ smp_mb__after_atomic(); wake_up_bit(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT); } } return 0; err_free_skb: kfree_skb(bdev->evt_skb); bdev->evt_skb = NULL; err_out: return err;} static const struct h4_recv_pkt mtk_recv_pkts[] = { { H4_RECV_ACL, .recv = hci_recv_frame }, { H4_RECV_SCO, .recv = hci_recv_frame }, { H4_RECV_EVENT, .recv = btmtkuart_recv_event },}; static void btmtkuart_tx_work(struct work_struct *work){ struct btmtkuart_dev *bdev = container_of(work, struct btmtkuart_dev, tx_work); struct serdev_device *serdev = bdev->serdev; struct hci_dev *hdev = bdev->hdev; while (1) { clear_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state); while (1) { struct sk_buff *skb = skb_dequeue(&bdev->txq); int len; if (!skb) break; len = serdev_device_write_buf(serdev, skb->data, skb->len); hdev->stat.byte_tx += len; skb_pull(skb, len); if (skb->len > 0) { skb_queue_head(&bdev->txq, skb); break; } switch (hci_skb_pkt_type(skb)) { case HCI_COMMAND_PKT: hdev->stat.cmd_tx++; break; case HCI_ACLDATA_PKT: hdev->stat.acl_tx++; break; case HCI_SCODATA_PKT: hdev->stat.sco_tx++; break; } kfree_skb(skb); } if (!test_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state)) break; } clear_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state);} static void btmtkuart_tx_wakeup(struct btmtkuart_dev *bdev){ if (test_and_set_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state)) set_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state); schedule_work(&bdev->tx_work);} static const unsigned char *mtk_stp_split(struct btmtkuart_dev *bdev, const unsigned char *data, int count, int *sz_h4){ struct mtk_stp_hdr *shdr; /* The cursor is reset when all the data of STP is consumed out */ if (!bdev->stp_dlen && bdev->stp_cursor >= 6) bdev->stp_cursor = 0; /* Filling pad until all STP info is obtained */ while (bdev->stp_cursor < 6 && count > 0) { bdev->stp_pad[bdev->stp_cursor] = *data; bdev->stp_cursor++; data++; count--; } /* Retrieve STP info and have a sanity check */ if (!bdev->stp_dlen && bdev->stp_cursor >= 6) { shdr = (struct mtk_stp_hdr *)&bdev->stp_pad[2]; bdev->stp_dlen = be16_to_cpu(shdr->dlen) & 0x0fff; /* Resync STP when unexpected data is being read */ if (shdr->prefix != 0x80 || bdev->stp_dlen > 2048) { bt_dev_err(bdev->hdev, "stp format unexpected (%d, %d)", shdr->prefix, bdev->stp_dlen); bdev->stp_cursor = 2; bdev->stp_dlen = 0; } } /* Directly quit when there's no data found for H4 can process */ if (count <= 0) return NULL; /* Translate to how much the size of data H4 can handle so far */ *sz_h4 = min_t(int, count, bdev->stp_dlen); /* Update the remaining size of STP packet */ bdev->stp_dlen -= *sz_h4; /* Data points to STP payload which can be handled by H4 */ return data;} static void btmtkuart_recv(struct hci_dev *hdev, const u8 *data, size_t count){ struct btmtkuart_dev *bdev = hci_get_drvdata(hdev); const unsigned char *p_left = data, *p_h4; int sz_left = count, sz_h4, adv; int err; while (sz_left > 0) { /* The serial data received from MT7622 BT controller is * at all time padded around with the STP header and tailer. * * A full STP packet is looking like * ----------------------------------- * | STP header | H:4 | STP tailer | * ----------------------------------- * but it doesn't guarantee to contain a full H:4 packet which * means that it's possible for multiple STP packets forms a * full H:4 packet that means extra STP header + length doesn't * indicate a full H:4 frame, things can fragment. Whose length * recorded in STP header just shows up the most length the * H:4 engine can handle currently. */ p_h4 = mtk_stp_split(bdev, p_left, sz_left, &sz_h4); if (!p_h4) break; adv = p_h4 - p_left; sz_left -= adv; p_left += adv; bdev->rx_skb = h4_recv_buf(&bdev->hu, bdev->rx_skb, p_h4, sz_h4, mtk_recv_pkts, ARRAY_SIZE(mtk_recv_pkts)); if (IS_ERR(bdev->rx_skb)) { err = PTR_ERR(bdev->rx_skb); bt_dev_err(bdev->hdev, "Frame reassembly failed (%d)", err); bdev->rx_skb = NULL; return; } sz_left -= sz_h4; p_left += sz_h4; }} static size_t btmtkuart_receive_buf(struct serdev_device *serdev, const u8 *data, size_t count){ struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev); btmtkuart_recv(bdev->hdev, data, count); bdev->hdev->stat.byte_rx += count; return count;} static void btmtkuart_write_wakeup(struct serdev_device *serdev){ struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev); btmtkuart_tx_wakeup(bdev);} static const struct serdev_device_ops btmtkuart_client_ops = { .receive_buf = btmtkuart_receive_buf, .write_wakeup = btmtkuart_write_wakeup,}; static int btmtkuart_open(struct hci_dev *hdev){ struct btmtkuart_dev *bdev = hci_get_drvdata(hdev); struct device *dev; int err; err = serdev_device_open(bdev->serdev); if (err) { bt_dev_err(hdev, "Unable to open UART device %s", dev_name(&bdev->serdev->dev)); goto err_open; } if (btmtkuart_is_standalone(bdev)) { if (bdev->curr_speed != bdev->desired_speed) err = serdev_device_set_baudrate(bdev->serdev, 115200); else err = serdev_device_set_baudrate(bdev->serdev, bdev->desired_speed); if (err < 0) { bt_dev_err(hdev, "Unable to set baudrate UART device %s", dev_name(&bdev->serdev->dev)); goto err_serdev_close; } serdev_device_set_flow_control(bdev->serdev, false); } bdev->stp_cursor = 2; bdev->stp_dlen = 0; dev = &bdev->serdev->dev; /* Enable the power domain and clock the device requires */ pm_runtime_enable(dev); err = pm_runtime_resume_and_get(dev); if (err < 0) goto err_disable_rpm; err = clk_prepare_enable(bdev->clk); if (err < 0) goto err_put_rpm; return 0; err_put_rpm: pm_runtime_put_sync(dev);err_disable_rpm: pm_runtime_disable(dev);err_serdev_close: serdev_device_close(bdev->serdev);err_open: return err;} static int btmtkuart_close(struct hci_dev *hdev){ struct btmtkuart_dev *bdev = hci_get_drvdata(hdev); struct device *dev = &bdev->serdev->dev; /* Shutdown the clock and power domain the device requires */ clk_disable_unprepare(bdev->clk); pm_runtime_put_sync(dev); pm_runtime_disable(dev); serdev_device_close(bdev->serdev); return 0;} static int btmtkuart_flush(struct hci_dev *hdev){ struct btmtkuart_dev *bdev = hci_get_drvdata(hdev); /* Flush any pending characters */ serdev_device_write_flush(bdev->serdev); skb_queue_purge(&bdev->txq); cancel_work_sync(&bdev->tx_work); kfree_skb(bdev->rx_skb); bdev->rx_skb = NULL; bdev->stp_cursor = 2; bdev->stp_dlen = 0; return 0;Lines 1–500 of 997. Load more
// SPDX-License-Identifier: GPL-2.0 // Copyright (c) 2018 MediaTek Inc. /* * Bluetooth support for MediaTek serial devices * * Author: Sean Wang <sean.wang@mediatek.com> * */ #include <linux/unaligned.h> #include <linux/atomic.h> #include <linux/clk.h> #include <linux/firmware.h> #include <linux/gpio/consumer.h> #include <linux/iopoll.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/of.h> #include <linux/pinctrl/consumer.h> #include <linux/pm_runtime.h> #include <linux/regulator/consumer.h> #include <linux/serdev.h> #include <linux/skbuff.h> #include <linux/usb.h> #include <net/bluetooth/bluetooth.h> #include <net/bluetooth/hci_core.h> #include "hci_uart.h" #include "btmtk.h" #define VERSION "0.2" #define MTK_STP_TLR_SIZE 2 #define BTMTKUART_TX_STATE_ACTIVE 1 #define BTMTKUART_TX_STATE_WAKEUP 2 #define BTMTKUART_TX_WAIT_VND_EVT 3 #define BTMTKUART_REQUIRED_WAKEUP 4 #define BTMTKUART_FLAG_STANDALONE_HW BIT(0) struct mtk_stp_hdr { u8 prefix; __be16 dlen; u8 cs; } __packed; struct btmtkuart_data { unsigned int flags; const char *fwname; }; struct btmtkuart_dev { struct hci_dev *hdev; struct serdev_device *serdev; struct clk *clk; struct clk *osc; struct regulator *vcc; struct gpio_desc *reset; struct gpio_desc *boot; struct pinctrl *pinctrl; struct pinctrl_state *pins_runtime; struct pinctrl_state *pins_boot; speed_t desired_speed; speed_t curr_speed; struct work_struct tx_work; unsigned long tx_state; struct sk_buff_head txq; struct sk_buff *rx_skb; struct sk_buff *evt_skb; u8 stp_pad[6]; u8 stp_cursor; u16 stp_dlen; const struct btmtkuart_data *data; struct hci_uart hu; }; #define btmtkuart_is_standalone(bdev) \ ((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW) #define btmtkuart_is_builtin_soc(bdev) \ !((bdev)->data->flags & BTMTKUART_FLAG_STANDALONE_HW) static int mtk_hci_wmt_sync(struct hci_dev *hdev, struct btmtk_hci_wmt_params *wmt_params) { struct btmtkuart_dev *bdev = hci_get_drvdata(hdev); struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc; u32 hlen, status = BTMTK_WMT_INVALID; struct btmtk_hci_wmt_evt *wmt_evt; struct btmtk_hci_wmt_cmd *wc; struct btmtk_wmt_hdr *hdr; int err; /* Send the WMT command and wait until the WMT event returns */ hlen = sizeof(*hdr) + wmt_params->dlen; if (hlen > 255) { err = -EINVAL; goto err_free_skb; } wc = kzalloc(hlen, GFP_KERNEL); if (!wc) { err = -ENOMEM; goto err_free_skb; } hdr = &wc->hdr; hdr->dir = 1; hdr->op = wmt_params->op; hdr->dlen = cpu_to_le16(wmt_params->dlen + 1); hdr->flag = wmt_params->flag; memcpy(wc->data, wmt_params->data, wmt_params->dlen); set_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state); err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc); if (err < 0) { clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state); goto err_free_wc; } /* The vendor specific WMT commands are all answered by a vendor * specific event and will not have the Command Status or Command * Complete as with usual HCI command flow control. * * After sending the command, wait for BTMTKUART_TX_WAIT_VND_EVT * state to be cleared. The driver specific event receive routine * will clear that state and with that indicate completion of the * WMT command. */ err = wait_on_bit_timeout(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT, TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT); if (err == -EINTR) { bt_dev_err(hdev, "Execution of wmt command interrupted"); clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state); goto err_free_wc; } if (err) { bt_dev_err(hdev, "Execution of wmt command timed out"); clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state); err = -ETIMEDOUT; goto err_free_wc; } /* Parse and handle the return WMT event */ wmt_evt = (struct btmtk_hci_wmt_evt *)bdev->evt_skb->data; if (wmt_evt->whdr.op != hdr->op) { bt_dev_err(hdev, "Wrong op received %d expected %d", wmt_evt->whdr.op, hdr->op); err = -EIO; goto err_free_wc; } switch (wmt_evt->whdr.op) { case BTMTK_WMT_SEMAPHORE: if (wmt_evt->whdr.flag == 2) status = BTMTK_WMT_PATCH_UNDONE; else status = BTMTK_WMT_PATCH_DONE; break; case BTMTK_WMT_FUNC_CTRL: wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt; if (be16_to_cpu(wmt_evt_funcc->status) == 0x404) status = BTMTK_WMT_ON_DONE; else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420) status = BTMTK_WMT_ON_PROGRESS; else status = BTMTK_WMT_ON_UNDONE; break; } if (wmt_params->status) *wmt_params->status = status; err_free_wc: kfree(wc); err_free_skb: kfree_skb(bdev->evt_skb); bdev->evt_skb = NULL; return err; } static int btmtkuart_recv_event(struct hci_dev *hdev, struct sk_buff *skb) { struct btmtkuart_dev *bdev = hci_get_drvdata(hdev); struct hci_event_hdr *hdr = (void *)skb->data; int err; /* When someone waits for the WMT event, the skb is being cloned * and being processed the events from there then. */ if (test_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state)) { bdev->evt_skb = skb_clone(skb, GFP_KERNEL); if (!bdev->evt_skb) { err = -ENOMEM; goto err_out; } } err = hci_recv_frame(hdev, skb); if (err < 0) goto err_free_skb; if (hdr->evt == HCI_EV_WMT) { if (test_and_clear_bit(BTMTKUART_TX_WAIT_VND_EVT, &bdev->tx_state)) { /* Barrier to sync with other CPUs */ smp_mb__after_atomic(); wake_up_bit(&bdev->tx_state, BTMTKUART_TX_WAIT_VND_EVT); } } return 0; err_free_skb: kfree_skb(bdev->evt_skb); bdev->evt_skb = NULL; err_out: return err; } static const struct h4_recv_pkt mtk_recv_pkts[] = { { H4_RECV_ACL, .recv = hci_recv_frame }, { H4_RECV_SCO, .recv = hci_recv_frame }, { H4_RECV_EVENT, .recv = btmtkuart_recv_event }, }; static void btmtkuart_tx_work(struct work_struct *work) { struct btmtkuart_dev *bdev = container_of(work, struct btmtkuart_dev, tx_work); struct serdev_device *serdev = bdev->serdev; struct hci_dev *hdev = bdev->hdev; while (1) { clear_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state); while (1) { struct sk_buff *skb = skb_dequeue(&bdev->txq); int len; if (!skb) break; len = serdev_device_write_buf(serdev, skb->data, skb->len); hdev->stat.byte_tx += len; skb_pull(skb, len); if (skb->len > 0) { skb_queue_head(&bdev->txq, skb); break; } switch (hci_skb_pkt_type(skb)) { case HCI_COMMAND_PKT: hdev->stat.cmd_tx++; break; case HCI_ACLDATA_PKT: hdev->stat.acl_tx++; break; case HCI_SCODATA_PKT: hdev->stat.sco_tx++; break; } kfree_skb(skb); } if (!test_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state)) break; } clear_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state); } static void btmtkuart_tx_wakeup(struct btmtkuart_dev *bdev) { if (test_and_set_bit(BTMTKUART_TX_STATE_ACTIVE, &bdev->tx_state)) set_bit(BTMTKUART_TX_STATE_WAKEUP, &bdev->tx_state); schedule_work(&bdev->tx_work); } static const unsigned char * mtk_stp_split(struct btmtkuart_dev *bdev, const unsigned char *data, int count, int *sz_h4) { struct mtk_stp_hdr *shdr; /* The cursor is reset when all the data of STP is consumed out */ if (!bdev->stp_dlen && bdev->stp_cursor >= 6) bdev->stp_cursor = 0; /* Filling pad until all STP info is obtained */ while (bdev->stp_cursor < 6 && count > 0) { bdev->stp_pad[bdev->stp_cursor] = *data; bdev->stp_cursor++; data++; count--; } /* Retrieve STP info and have a sanity check */ if (!bdev->stp_dlen && bdev->stp_cursor >= 6) { shdr = (struct mtk_stp_hdr *)&bdev->stp_pad[2]; bdev->stp_dlen = be16_to_cpu(shdr->dlen) & 0x0fff; /* Resync STP when unexpected data is being read */ if (shdr->prefix != 0x80 || bdev->stp_dlen > 2048) { bt_dev_err(bdev->hdev, "stp format unexpected (%d, %d)", shdr->prefix, bdev->stp_dlen); bdev->stp_cursor = 2; bdev->stp_dlen = 0; } } /* Directly quit when there's no data found for H4 can process */ if (count <= 0) return NULL; /* Translate to how much the size of data H4 can handle so far */ *sz_h4 = min_t(int, count, bdev->stp_dlen); /* Update the remaining size of STP packet */ bdev->stp_dlen -= *sz_h4; /* Data points to STP payload which can be handled by H4 */ return data; } static void btmtkuart_recv(struct hci_dev *hdev, const u8 *data, size_t count) { struct btmtkuart_dev *bdev = hci_get_drvdata(hdev); const unsigned char *p_left = data, *p_h4; int sz_left = count, sz_h4, adv; int err; while (sz_left > 0) { /* The serial data received from MT7622 BT controller is * at all time padded around with the STP header and tailer. * * A full STP packet is looking like * ----------------------------------- * | STP header | H:4 | STP tailer | * ----------------------------------- * but it doesn't guarantee to contain a full H:4 packet which * means that it's possible for multiple STP packets forms a * full H:4 packet that means extra STP header + length doesn't * indicate a full H:4 frame, things can fragment. Whose length * recorded in STP header just shows up the most length the * H:4 engine can handle currently. */ p_h4 = mtk_stp_split(bdev, p_left, sz_left, &sz_h4); if (!p_h4) break; adv = p_h4 - p_left; sz_left -= adv; p_left += adv; bdev->rx_skb = h4_recv_buf(&bdev->hu, bdev->rx_skb, p_h4, sz_h4, mtk_recv_pkts, ARRAY_SIZE(mtk_recv_pkts)); if (IS_ERR(bdev->rx_skb)) { err = PTR_ERR(bdev->rx_skb); bt_dev_err(bdev->hdev, "Frame reassembly failed (%d)", err); bdev->rx_skb = NULL; return; } sz_left -= sz_h4; p_left += sz_h4; } } static size_t btmtkuart_receive_buf(struct serdev_device *serdev, const u8 *data, size_t count) { struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev); btmtkuart_recv(bdev->hdev, data, count); bdev->hdev->stat.byte_rx += count; return count; } static void btmtkuart_write_wakeup(struct serdev_device *serdev) { struct btmtkuart_dev *bdev = serdev_device_get_drvdata(serdev); btmtkuart_tx_wakeup(bdev); } static const struct serdev_device_ops btmtkuart_client_ops = { .receive_buf = btmtkuart_receive_buf, .write_wakeup = btmtkuart_write_wakeup, }; static int btmtkuart_open(struct hci_dev *hdev) { struct btmtkuart_dev *bdev = hci_get_drvdata(hdev); struct device *dev; int err; err = serdev_device_open(bdev->serdev); if (err) { bt_dev_err(hdev, "Unable to open UART device %s", dev_name(&bdev->serdev->dev)); goto err_open; } if (btmtkuart_is_standalone(bdev)) { if (bdev->curr_speed != bdev->desired_speed) err = serdev_device_set_baudrate(bdev->serdev, 115200); else err = serdev_device_set_baudrate(bdev->serdev, bdev->desired_speed); if (err < 0) { bt_dev_err(hdev, "Unable to set baudrate UART device %s", dev_name(&bdev->serdev->dev)); goto err_serdev_close; } serdev_device_set_flow_control(bdev->serdev, false); } bdev->stp_cursor = 2; bdev->stp_dlen = 0; dev = &bdev->serdev->dev; /* Enable the power domain and clock the device requires */ pm_runtime_enable(dev); err = pm_runtime_resume_and_get(dev); if (err < 0) goto err_disable_rpm; err = clk_prepare_enable(bdev->clk); if (err < 0) goto err_put_rpm; return 0; err_put_rpm: pm_runtime_put_sync(dev); err_disable_rpm: pm_runtime_disable(dev); err_serdev_close: serdev_device_close(bdev->serdev); err_open: return err; } static int btmtkuart_close(struct hci_dev *hdev) { struct btmtkuart_dev *bdev = hci_get_drvdata(hdev); struct device *dev = &bdev->serdev->dev; /* Shutdown the clock and power domain the device requires */ clk_disable_unprepare(bdev->clk); pm_runtime_put_sync(dev); pm_runtime_disable(dev); serdev_device_close(bdev->serdev); return 0; } static int btmtkuart_flush(struct hci_dev *hdev) { struct btmtkuart_dev *bdev = hci_get_drvdata(hdev); /* Flush any pending characters */ serdev_device_write_flush(bdev->serdev); skb_queue_purge(&bdev->txq); cancel_work_sync(&bdev->tx_work); kfree_skb(bdev->rx_skb); bdev->rx_skb = NULL; bdev->stp_cursor = 2; bdev->stp_dlen = 0; return 0;