1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Bluetooth HCI UART driver for marvell devices
6 * Copyright (C) 2016 Marvell International Ltd.
7 * Copyright (C) 2016 Intel Corporation
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/skbuff.h>
13 #include <linux/firmware.h>
14 #include <linux/module.h>
15 #include <linux/tty.h>
17 #include <net/bluetooth/bluetooth.h>
18 #include <net/bluetooth/hci_core.h>
22 #define HCI_FW_REQ_PKT 0xA5
23 #define HCI_CHIP_VER_PKT 0xAA
27 #define MRVL_RAW_DATA 0x1F
30 STATE_CHIP_VER_PENDING,
35 struct sk_buff *rx_skb;
36 struct sk_buff_head txq;
37 struct sk_buff_head rawq;
47 #define HCI_MRVL_PKT_SIZE 4
49 static int mrvl_open(struct hci_uart *hu)
51 struct mrvl_data *mrvl;
55 mrvl = kzalloc(sizeof(*mrvl), GFP_KERNEL);
59 skb_queue_head_init(&mrvl->txq);
60 skb_queue_head_init(&mrvl->rawq);
62 set_bit(STATE_CHIP_VER_PENDING, &mrvl->flags);
68 static int mrvl_close(struct hci_uart *hu)
70 struct mrvl_data *mrvl = hu->priv;
74 skb_queue_purge(&mrvl->txq);
75 skb_queue_purge(&mrvl->rawq);
76 kfree_skb(mrvl->rx_skb);
83 static int mrvl_flush(struct hci_uart *hu)
85 struct mrvl_data *mrvl = hu->priv;
89 skb_queue_purge(&mrvl->txq);
90 skb_queue_purge(&mrvl->rawq);
95 static struct sk_buff *mrvl_dequeue(struct hci_uart *hu)
97 struct mrvl_data *mrvl = hu->priv;
100 skb = skb_dequeue(&mrvl->txq);
103 skb = skb_dequeue(&mrvl->rawq);
105 /* Prepend skb with frame type */
106 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
112 static int mrvl_enqueue(struct hci_uart *hu, struct sk_buff *skb)
114 struct mrvl_data *mrvl = hu->priv;
116 skb_queue_tail(&mrvl->txq, skb);
120 static void mrvl_send_ack(struct hci_uart *hu, unsigned char type)
122 struct mrvl_data *mrvl = hu->priv;
125 /* No H4 payload, only 1 byte header */
126 skb = bt_skb_alloc(0, GFP_ATOMIC);
128 bt_dev_err(hu->hdev, "Unable to alloc ack/nak packet");
131 hci_skb_pkt_type(skb) = type;
133 skb_queue_tail(&mrvl->txq, skb);
134 hci_uart_tx_wakeup(hu);
137 static int mrvl_recv_fw_req(struct hci_dev *hdev, struct sk_buff *skb)
139 struct hci_mrvl_pkt *pkt = (void *)skb->data;
140 struct hci_uart *hu = hci_get_drvdata(hdev);
141 struct mrvl_data *mrvl = hu->priv;
144 if ((pkt->lhs ^ pkt->rhs) != 0xffff) {
145 bt_dev_err(hdev, "Corrupted mrvl header");
146 mrvl_send_ack(hu, MRVL_NAK);
150 mrvl_send_ack(hu, MRVL_ACK);
152 if (!test_bit(STATE_FW_REQ_PENDING, &mrvl->flags)) {
153 bt_dev_err(hdev, "Received unexpected firmware request");
158 mrvl->tx_len = le16_to_cpu(pkt->lhs);
160 clear_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
161 smp_mb__after_atomic();
162 wake_up_bit(&mrvl->flags, STATE_FW_REQ_PENDING);
169 static int mrvl_recv_chip_ver(struct hci_dev *hdev, struct sk_buff *skb)
171 struct hci_mrvl_pkt *pkt = (void *)skb->data;
172 struct hci_uart *hu = hci_get_drvdata(hdev);
173 struct mrvl_data *mrvl = hu->priv;
174 u16 version = le16_to_cpu(pkt->lhs);
177 if ((pkt->lhs ^ pkt->rhs) != 0xffff) {
178 bt_dev_err(hdev, "Corrupted mrvl header");
179 mrvl_send_ack(hu, MRVL_NAK);
183 mrvl_send_ack(hu, MRVL_ACK);
185 if (!test_bit(STATE_CHIP_VER_PENDING, &mrvl->flags)) {
186 bt_dev_err(hdev, "Received unexpected chip version");
191 mrvl->rev = version >> 8;
193 bt_dev_info(hdev, "Controller id = %x, rev = %x", mrvl->id, mrvl->rev);
195 clear_bit(STATE_CHIP_VER_PENDING, &mrvl->flags);
196 smp_mb__after_atomic();
197 wake_up_bit(&mrvl->flags, STATE_CHIP_VER_PENDING);
204 #define HCI_RECV_CHIP_VER \
205 .type = HCI_CHIP_VER_PKT, \
206 .hlen = HCI_MRVL_PKT_SIZE, \
209 .maxlen = HCI_MRVL_PKT_SIZE
211 #define HCI_RECV_FW_REQ \
212 .type = HCI_FW_REQ_PKT, \
213 .hlen = HCI_MRVL_PKT_SIZE, \
216 .maxlen = HCI_MRVL_PKT_SIZE
218 static const struct h4_recv_pkt mrvl_recv_pkts[] = {
219 { H4_RECV_ACL, .recv = hci_recv_frame },
220 { H4_RECV_SCO, .recv = hci_recv_frame },
221 { H4_RECV_EVENT, .recv = hci_recv_frame },
222 { HCI_RECV_FW_REQ, .recv = mrvl_recv_fw_req },
223 { HCI_RECV_CHIP_VER, .recv = mrvl_recv_chip_ver },
226 static int mrvl_recv(struct hci_uart *hu, const void *data, int count)
228 struct mrvl_data *mrvl = hu->priv;
230 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
233 mrvl->rx_skb = h4_recv_buf(hu->hdev, mrvl->rx_skb, data, count,
235 ARRAY_SIZE(mrvl_recv_pkts));
236 if (IS_ERR(mrvl->rx_skb)) {
237 int err = PTR_ERR(mrvl->rx_skb);
238 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
246 static int mrvl_load_firmware(struct hci_dev *hdev, const char *name)
248 struct hci_uart *hu = hci_get_drvdata(hdev);
249 struct mrvl_data *mrvl = hu->priv;
250 const struct firmware *fw = NULL;
251 const u8 *fw_ptr, *fw_max;
254 err = request_firmware(&fw, name, &hdev->dev);
256 bt_dev_err(hdev, "Failed to load firmware file %s", name);
261 fw_max = fw->data + fw->size;
263 bt_dev_info(hdev, "Loading %s", name);
265 set_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
267 while (fw_ptr <= fw_max) {
270 /* Controller drives the firmware load by sending firmware
271 * request packets containing the expected fragment size.
273 err = wait_on_bit_timeout(&mrvl->flags, STATE_FW_REQ_PENDING,
275 msecs_to_jiffies(2000));
277 bt_dev_err(hdev, "Firmware load interrupted");
281 bt_dev_err(hdev, "Firmware request timeout");
286 bt_dev_dbg(hdev, "Firmware request, expecting %d bytes",
289 if (fw_ptr == fw_max) {
290 /* Controller requests a null size once firmware is
291 * fully loaded. If controller expects more data, there
295 bt_dev_info(hdev, "Firmware loading complete");
297 bt_dev_err(hdev, "Firmware loading failure");
303 if (fw_ptr + mrvl->tx_len > fw_max) {
304 mrvl->tx_len = fw_max - fw_ptr;
305 bt_dev_dbg(hdev, "Adjusting tx_len to %d",
309 skb = bt_skb_alloc(mrvl->tx_len, GFP_KERNEL);
311 bt_dev_err(hdev, "Failed to alloc mem for FW packet");
315 bt_cb(skb)->pkt_type = MRVL_RAW_DATA;
317 skb_put_data(skb, fw_ptr, mrvl->tx_len);
318 fw_ptr += mrvl->tx_len;
320 set_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
322 skb_queue_tail(&mrvl->rawq, skb);
323 hci_uart_tx_wakeup(hu);
326 release_firmware(fw);
330 static int mrvl_setup(struct hci_uart *hu)
334 hci_uart_set_flow_control(hu, true);
336 err = mrvl_load_firmware(hu->hdev, "mrvl/helper_uart_3000000.bin");
338 bt_dev_err(hu->hdev, "Unable to download firmware helper");
342 hci_uart_set_baudrate(hu, 3000000);
343 hci_uart_set_flow_control(hu, false);
345 err = mrvl_load_firmware(hu->hdev, "mrvl/uart8897_bt.bin");
352 static const struct hci_uart_proto mrvl_proto = {
355 .init_speed = 115200,
361 .enqueue = mrvl_enqueue,
362 .dequeue = mrvl_dequeue,
365 int __init mrvl_init(void)
367 return hci_uart_register_proto(&mrvl_proto);
370 int __exit mrvl_deinit(void)
372 return hci_uart_unregister_proto(&mrvl_proto);