Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  *  Bluetooth HCI UART driver for marvell devices
0005  *
0006  *  Copyright (C) 2016  Marvell International Ltd.
0007  *  Copyright (C) 2016  Intel Corporation
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/errno.h>
0012 #include <linux/skbuff.h>
0013 #include <linux/firmware.h>
0014 #include <linux/module.h>
0015 #include <linux/tty.h>
0016 #include <linux/of.h>
0017 #include <linux/serdev.h>
0018 
0019 #include <net/bluetooth/bluetooth.h>
0020 #include <net/bluetooth/hci_core.h>
0021 
0022 #include "hci_uart.h"
0023 
0024 #define HCI_FW_REQ_PKT 0xA5
0025 #define HCI_CHIP_VER_PKT 0xAA
0026 
0027 #define MRVL_ACK 0x5A
0028 #define MRVL_NAK 0xBF
0029 #define MRVL_RAW_DATA 0x1F
0030 
0031 enum {
0032     STATE_CHIP_VER_PENDING,
0033     STATE_FW_REQ_PENDING,
0034 };
0035 
0036 struct mrvl_data {
0037     struct sk_buff *rx_skb;
0038     struct sk_buff_head txq;
0039     struct sk_buff_head rawq;
0040     unsigned long flags;
0041     unsigned int tx_len;
0042     u8 id, rev;
0043 };
0044 
0045 struct mrvl_serdev {
0046     struct hci_uart hu;
0047 };
0048 
0049 struct hci_mrvl_pkt {
0050     __le16 lhs;
0051     __le16 rhs;
0052 } __packed;
0053 #define HCI_MRVL_PKT_SIZE 4
0054 
0055 static int mrvl_open(struct hci_uart *hu)
0056 {
0057     struct mrvl_data *mrvl;
0058     int ret;
0059 
0060     BT_DBG("hu %p", hu);
0061 
0062     if (!hci_uart_has_flow_control(hu))
0063         return -EOPNOTSUPP;
0064 
0065     mrvl = kzalloc(sizeof(*mrvl), GFP_KERNEL);
0066     if (!mrvl)
0067         return -ENOMEM;
0068 
0069     skb_queue_head_init(&mrvl->txq);
0070     skb_queue_head_init(&mrvl->rawq);
0071 
0072     set_bit(STATE_CHIP_VER_PENDING, &mrvl->flags);
0073 
0074     hu->priv = mrvl;
0075 
0076     if (hu->serdev) {
0077         ret = serdev_device_open(hu->serdev);
0078         if (ret)
0079             goto err;
0080     }
0081 
0082     return 0;
0083 err:
0084     kfree(mrvl);
0085 
0086     return ret;
0087 }
0088 
0089 static int mrvl_close(struct hci_uart *hu)
0090 {
0091     struct mrvl_data *mrvl = hu->priv;
0092 
0093     BT_DBG("hu %p", hu);
0094 
0095     if (hu->serdev)
0096         serdev_device_close(hu->serdev);
0097 
0098     skb_queue_purge(&mrvl->txq);
0099     skb_queue_purge(&mrvl->rawq);
0100     kfree_skb(mrvl->rx_skb);
0101     kfree(mrvl);
0102 
0103     hu->priv = NULL;
0104     return 0;
0105 }
0106 
0107 static int mrvl_flush(struct hci_uart *hu)
0108 {
0109     struct mrvl_data *mrvl = hu->priv;
0110 
0111     BT_DBG("hu %p", hu);
0112 
0113     skb_queue_purge(&mrvl->txq);
0114     skb_queue_purge(&mrvl->rawq);
0115 
0116     return 0;
0117 }
0118 
0119 static struct sk_buff *mrvl_dequeue(struct hci_uart *hu)
0120 {
0121     struct mrvl_data *mrvl = hu->priv;
0122     struct sk_buff *skb;
0123 
0124     skb = skb_dequeue(&mrvl->txq);
0125     if (!skb) {
0126         /* Any raw data ? */
0127         skb = skb_dequeue(&mrvl->rawq);
0128     } else {
0129         /* Prepend skb with frame type */
0130         memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
0131     }
0132 
0133     return skb;
0134 }
0135 
0136 static int mrvl_enqueue(struct hci_uart *hu, struct sk_buff *skb)
0137 {
0138     struct mrvl_data *mrvl = hu->priv;
0139 
0140     skb_queue_tail(&mrvl->txq, skb);
0141     return 0;
0142 }
0143 
0144 static void mrvl_send_ack(struct hci_uart *hu, unsigned char type)
0145 {
0146     struct mrvl_data *mrvl = hu->priv;
0147     struct sk_buff *skb;
0148 
0149     /* No H4 payload, only 1 byte header */
0150     skb = bt_skb_alloc(0, GFP_ATOMIC);
0151     if (!skb) {
0152         bt_dev_err(hu->hdev, "Unable to alloc ack/nak packet");
0153         return;
0154     }
0155     hci_skb_pkt_type(skb) = type;
0156 
0157     skb_queue_tail(&mrvl->txq, skb);
0158     hci_uart_tx_wakeup(hu);
0159 }
0160 
0161 static int mrvl_recv_fw_req(struct hci_dev *hdev, struct sk_buff *skb)
0162 {
0163     struct hci_mrvl_pkt *pkt = (void *)skb->data;
0164     struct hci_uart *hu = hci_get_drvdata(hdev);
0165     struct mrvl_data *mrvl = hu->priv;
0166     int ret = 0;
0167 
0168     if ((pkt->lhs ^ pkt->rhs) != 0xffff) {
0169         bt_dev_err(hdev, "Corrupted mrvl header");
0170         mrvl_send_ack(hu, MRVL_NAK);
0171         ret = -EINVAL;
0172         goto done;
0173     }
0174     mrvl_send_ack(hu, MRVL_ACK);
0175 
0176     if (!test_bit(STATE_FW_REQ_PENDING, &mrvl->flags)) {
0177         bt_dev_err(hdev, "Received unexpected firmware request");
0178         ret = -EINVAL;
0179         goto done;
0180     }
0181 
0182     mrvl->tx_len = le16_to_cpu(pkt->lhs);
0183 
0184     clear_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
0185     smp_mb__after_atomic();
0186     wake_up_bit(&mrvl->flags, STATE_FW_REQ_PENDING);
0187 
0188 done:
0189     kfree_skb(skb);
0190     return ret;
0191 }
0192 
0193 static int mrvl_recv_chip_ver(struct hci_dev *hdev, struct sk_buff *skb)
0194 {
0195     struct hci_mrvl_pkt *pkt = (void *)skb->data;
0196     struct hci_uart *hu = hci_get_drvdata(hdev);
0197     struct mrvl_data *mrvl = hu->priv;
0198     u16 version = le16_to_cpu(pkt->lhs);
0199     int ret = 0;
0200 
0201     if ((pkt->lhs ^ pkt->rhs) != 0xffff) {
0202         bt_dev_err(hdev, "Corrupted mrvl header");
0203         mrvl_send_ack(hu, MRVL_NAK);
0204         ret = -EINVAL;
0205         goto done;
0206     }
0207     mrvl_send_ack(hu, MRVL_ACK);
0208 
0209     if (!test_bit(STATE_CHIP_VER_PENDING, &mrvl->flags)) {
0210         bt_dev_err(hdev, "Received unexpected chip version");
0211         goto done;
0212     }
0213 
0214     mrvl->id = version;
0215     mrvl->rev = version >> 8;
0216 
0217     bt_dev_info(hdev, "Controller id = %x, rev = %x", mrvl->id, mrvl->rev);
0218 
0219     clear_bit(STATE_CHIP_VER_PENDING, &mrvl->flags);
0220     smp_mb__after_atomic();
0221     wake_up_bit(&mrvl->flags, STATE_CHIP_VER_PENDING);
0222 
0223 done:
0224     kfree_skb(skb);
0225     return ret;
0226 }
0227 
0228 #define HCI_RECV_CHIP_VER \
0229     .type = HCI_CHIP_VER_PKT, \
0230     .hlen = HCI_MRVL_PKT_SIZE, \
0231     .loff = 0, \
0232     .lsize = 0, \
0233     .maxlen = HCI_MRVL_PKT_SIZE
0234 
0235 #define HCI_RECV_FW_REQ \
0236     .type = HCI_FW_REQ_PKT, \
0237     .hlen = HCI_MRVL_PKT_SIZE, \
0238     .loff = 0, \
0239     .lsize = 0, \
0240     .maxlen = HCI_MRVL_PKT_SIZE
0241 
0242 static const struct h4_recv_pkt mrvl_recv_pkts[] = {
0243     { H4_RECV_ACL,       .recv = hci_recv_frame     },
0244     { H4_RECV_SCO,       .recv = hci_recv_frame     },
0245     { H4_RECV_EVENT,     .recv = hci_recv_frame     },
0246     { HCI_RECV_FW_REQ,   .recv = mrvl_recv_fw_req   },
0247     { HCI_RECV_CHIP_VER, .recv = mrvl_recv_chip_ver },
0248 };
0249 
0250 static int mrvl_recv(struct hci_uart *hu, const void *data, int count)
0251 {
0252     struct mrvl_data *mrvl = hu->priv;
0253 
0254     if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
0255         return -EUNATCH;
0256 
0257     mrvl->rx_skb = h4_recv_buf(hu->hdev, mrvl->rx_skb, data, count,
0258                     mrvl_recv_pkts,
0259                     ARRAY_SIZE(mrvl_recv_pkts));
0260     if (IS_ERR(mrvl->rx_skb)) {
0261         int err = PTR_ERR(mrvl->rx_skb);
0262         bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
0263         mrvl->rx_skb = NULL;
0264         return err;
0265     }
0266 
0267     return count;
0268 }
0269 
0270 static int mrvl_load_firmware(struct hci_dev *hdev, const char *name)
0271 {
0272     struct hci_uart *hu = hci_get_drvdata(hdev);
0273     struct mrvl_data *mrvl = hu->priv;
0274     const struct firmware *fw = NULL;
0275     const u8 *fw_ptr, *fw_max;
0276     int err;
0277 
0278     err = request_firmware(&fw, name, &hdev->dev);
0279     if (err < 0) {
0280         bt_dev_err(hdev, "Failed to load firmware file %s", name);
0281         return err;
0282     }
0283 
0284     fw_ptr = fw->data;
0285     fw_max = fw->data + fw->size;
0286 
0287     bt_dev_info(hdev, "Loading %s", name);
0288 
0289     set_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
0290 
0291     while (fw_ptr <= fw_max) {
0292         struct sk_buff *skb;
0293 
0294         /* Controller drives the firmware load by sending firmware
0295          * request packets containing the expected fragment size.
0296          */
0297         err = wait_on_bit_timeout(&mrvl->flags, STATE_FW_REQ_PENDING,
0298                       TASK_INTERRUPTIBLE,
0299                       msecs_to_jiffies(2000));
0300         if (err == 1) {
0301             bt_dev_err(hdev, "Firmware load interrupted");
0302             err = -EINTR;
0303             break;
0304         } else if (err) {
0305             bt_dev_err(hdev, "Firmware request timeout");
0306             err = -ETIMEDOUT;
0307             break;
0308         }
0309 
0310         bt_dev_dbg(hdev, "Firmware request, expecting %d bytes",
0311                mrvl->tx_len);
0312 
0313         if (fw_ptr == fw_max) {
0314             /* Controller requests a null size once firmware is
0315              * fully loaded. If controller expects more data, there
0316              * is an issue.
0317              */
0318             if (!mrvl->tx_len) {
0319                 bt_dev_info(hdev, "Firmware loading complete");
0320             } else {
0321                 bt_dev_err(hdev, "Firmware loading failure");
0322                 err = -EINVAL;
0323             }
0324             break;
0325         }
0326 
0327         if (fw_ptr + mrvl->tx_len > fw_max) {
0328             mrvl->tx_len = fw_max - fw_ptr;
0329             bt_dev_dbg(hdev, "Adjusting tx_len to %d",
0330                    mrvl->tx_len);
0331         }
0332 
0333         skb = bt_skb_alloc(mrvl->tx_len, GFP_KERNEL);
0334         if (!skb) {
0335             bt_dev_err(hdev, "Failed to alloc mem for FW packet");
0336             err = -ENOMEM;
0337             break;
0338         }
0339         bt_cb(skb)->pkt_type = MRVL_RAW_DATA;
0340 
0341         skb_put_data(skb, fw_ptr, mrvl->tx_len);
0342         fw_ptr += mrvl->tx_len;
0343 
0344         set_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
0345 
0346         skb_queue_tail(&mrvl->rawq, skb);
0347         hci_uart_tx_wakeup(hu);
0348     }
0349 
0350     release_firmware(fw);
0351     return err;
0352 }
0353 
0354 static int mrvl_setup(struct hci_uart *hu)
0355 {
0356     int err;
0357 
0358     hci_uart_set_flow_control(hu, true);
0359 
0360     err = mrvl_load_firmware(hu->hdev, "mrvl/helper_uart_3000000.bin");
0361     if (err) {
0362         bt_dev_err(hu->hdev, "Unable to download firmware helper");
0363         return -EINVAL;
0364     }
0365 
0366     /* Let the final ack go out before switching the baudrate */
0367     hci_uart_wait_until_sent(hu);
0368 
0369     if (hu->serdev)
0370         serdev_device_set_baudrate(hu->serdev, 3000000);
0371     else
0372         hci_uart_set_baudrate(hu, 3000000);
0373 
0374     hci_uart_set_flow_control(hu, false);
0375 
0376     err = mrvl_load_firmware(hu->hdev, "mrvl/uart8897_bt.bin");
0377     if (err)
0378         return err;
0379 
0380     return 0;
0381 }
0382 
0383 static const struct hci_uart_proto mrvl_proto = {
0384     .id     = HCI_UART_MRVL,
0385     .name       = "Marvell",
0386     .init_speed = 115200,
0387     .open       = mrvl_open,
0388     .close      = mrvl_close,
0389     .flush      = mrvl_flush,
0390     .setup      = mrvl_setup,
0391     .recv       = mrvl_recv,
0392     .enqueue    = mrvl_enqueue,
0393     .dequeue    = mrvl_dequeue,
0394 };
0395 
0396 static int mrvl_serdev_probe(struct serdev_device *serdev)
0397 {
0398     struct mrvl_serdev *mrvldev;
0399 
0400     mrvldev = devm_kzalloc(&serdev->dev, sizeof(*mrvldev), GFP_KERNEL);
0401     if (!mrvldev)
0402         return -ENOMEM;
0403 
0404     mrvldev->hu.serdev = serdev;
0405     serdev_device_set_drvdata(serdev, mrvldev);
0406 
0407     return hci_uart_register_device(&mrvldev->hu, &mrvl_proto);
0408 }
0409 
0410 static void mrvl_serdev_remove(struct serdev_device *serdev)
0411 {
0412     struct mrvl_serdev *mrvldev = serdev_device_get_drvdata(serdev);
0413 
0414     hci_uart_unregister_device(&mrvldev->hu);
0415 }
0416 
0417 #ifdef CONFIG_OF
0418 static const struct of_device_id mrvl_bluetooth_of_match[] = {
0419     { .compatible = "mrvl,88w8897" },
0420     { },
0421 };
0422 MODULE_DEVICE_TABLE(of, mrvl_bluetooth_of_match);
0423 #endif
0424 
0425 static struct serdev_device_driver mrvl_serdev_driver = {
0426     .probe = mrvl_serdev_probe,
0427     .remove = mrvl_serdev_remove,
0428     .driver = {
0429         .name = "hci_uart_mrvl",
0430         .of_match_table = of_match_ptr(mrvl_bluetooth_of_match),
0431     },
0432 };
0433 
0434 int __init mrvl_init(void)
0435 {
0436     serdev_device_driver_register(&mrvl_serdev_driver);
0437 
0438     return hci_uart_register_proto(&mrvl_proto);
0439 }
0440 
0441 int __exit mrvl_deinit(void)
0442 {
0443     serdev_device_driver_unregister(&mrvl_serdev_driver);
0444 
0445     return hci_uart_unregister_proto(&mrvl_proto);
0446 }