1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * 4 * Bluetooth HCI UART driver for marvell devices 5 * 6 * Copyright (C) 2016 Marvell International Ltd. 7 * Copyright (C) 2016 Intel Corporation 8 */ 9 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> 16 17 #include <net/bluetooth/bluetooth.h> 18 #include <net/bluetooth/hci_core.h> 19 20 #include "hci_uart.h" 21 22 #define HCI_FW_REQ_PKT 0xA5 23 #define HCI_CHIP_VER_PKT 0xAA 24 25 #define MRVL_ACK 0x5A 26 #define MRVL_NAK 0xBF 27 #define MRVL_RAW_DATA 0x1F 28 29 enum { 30 STATE_CHIP_VER_PENDING, 31 STATE_FW_REQ_PENDING, 32 }; 33 34 struct mrvl_data { 35 struct sk_buff *rx_skb; 36 struct sk_buff_head txq; 37 struct sk_buff_head rawq; 38 unsigned long flags; 39 unsigned int tx_len; 40 u8 id, rev; 41 }; 42 43 struct hci_mrvl_pkt { 44 __le16 lhs; 45 __le16 rhs; 46 } __packed; 47 #define HCI_MRVL_PKT_SIZE 4 48 49 static int mrvl_open(struct hci_uart *hu) 50 { 51 struct mrvl_data *mrvl; 52 53 BT_DBG("hu %p", hu); 54 55 mrvl = kzalloc(sizeof(*mrvl), GFP_KERNEL); 56 if (!mrvl) 57 return -ENOMEM; 58 59 skb_queue_head_init(&mrvl->txq); 60 skb_queue_head_init(&mrvl->rawq); 61 62 set_bit(STATE_CHIP_VER_PENDING, &mrvl->flags); 63 64 hu->priv = mrvl; 65 return 0; 66 } 67 68 static int mrvl_close(struct hci_uart *hu) 69 { 70 struct mrvl_data *mrvl = hu->priv; 71 72 BT_DBG("hu %p", hu); 73 74 skb_queue_purge(&mrvl->txq); 75 skb_queue_purge(&mrvl->rawq); 76 kfree_skb(mrvl->rx_skb); 77 kfree(mrvl); 78 79 hu->priv = NULL; 80 return 0; 81 } 82 83 static int mrvl_flush(struct hci_uart *hu) 84 { 85 struct mrvl_data *mrvl = hu->priv; 86 87 BT_DBG("hu %p", hu); 88 89 skb_queue_purge(&mrvl->txq); 90 skb_queue_purge(&mrvl->rawq); 91 92 return 0; 93 } 94 95 static struct sk_buff *mrvl_dequeue(struct hci_uart *hu) 96 { 97 struct mrvl_data *mrvl = hu->priv; 98 struct sk_buff *skb; 99 100 skb = skb_dequeue(&mrvl->txq); 101 if (!skb) { 102 /* Any raw data ? */ 103 skb = skb_dequeue(&mrvl->rawq); 104 } else { 105 /* Prepend skb with frame type */ 106 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); 107 } 108 109 return skb; 110 } 111 112 static int mrvl_enqueue(struct hci_uart *hu, struct sk_buff *skb) 113 { 114 struct mrvl_data *mrvl = hu->priv; 115 116 skb_queue_tail(&mrvl->txq, skb); 117 return 0; 118 } 119 120 static void mrvl_send_ack(struct hci_uart *hu, unsigned char type) 121 { 122 struct mrvl_data *mrvl = hu->priv; 123 struct sk_buff *skb; 124 125 /* No H4 payload, only 1 byte header */ 126 skb = bt_skb_alloc(0, GFP_ATOMIC); 127 if (!skb) { 128 bt_dev_err(hu->hdev, "Unable to alloc ack/nak packet"); 129 return; 130 } 131 hci_skb_pkt_type(skb) = type; 132 133 skb_queue_tail(&mrvl->txq, skb); 134 hci_uart_tx_wakeup(hu); 135 } 136 137 static int mrvl_recv_fw_req(struct hci_dev *hdev, struct sk_buff *skb) 138 { 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; 142 int ret = 0; 143 144 if ((pkt->lhs ^ pkt->rhs) != 0xffff) { 145 bt_dev_err(hdev, "Corrupted mrvl header"); 146 mrvl_send_ack(hu, MRVL_NAK); 147 ret = -EINVAL; 148 goto done; 149 } 150 mrvl_send_ack(hu, MRVL_ACK); 151 152 if (!test_bit(STATE_FW_REQ_PENDING, &mrvl->flags)) { 153 bt_dev_err(hdev, "Received unexpected firmware request"); 154 ret = -EINVAL; 155 goto done; 156 } 157 158 mrvl->tx_len = le16_to_cpu(pkt->lhs); 159 160 clear_bit(STATE_FW_REQ_PENDING, &mrvl->flags); 161 smp_mb__after_atomic(); 162 wake_up_bit(&mrvl->flags, STATE_FW_REQ_PENDING); 163 164 done: 165 kfree_skb(skb); 166 return ret; 167 } 168 169 static int mrvl_recv_chip_ver(struct hci_dev *hdev, struct sk_buff *skb) 170 { 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); 175 int ret = 0; 176 177 if ((pkt->lhs ^ pkt->rhs) != 0xffff) { 178 bt_dev_err(hdev, "Corrupted mrvl header"); 179 mrvl_send_ack(hu, MRVL_NAK); 180 ret = -EINVAL; 181 goto done; 182 } 183 mrvl_send_ack(hu, MRVL_ACK); 184 185 if (!test_bit(STATE_CHIP_VER_PENDING, &mrvl->flags)) { 186 bt_dev_err(hdev, "Received unexpected chip version"); 187 goto done; 188 } 189 190 mrvl->id = version; 191 mrvl->rev = version >> 8; 192 193 bt_dev_info(hdev, "Controller id = %x, rev = %x", mrvl->id, mrvl->rev); 194 195 clear_bit(STATE_CHIP_VER_PENDING, &mrvl->flags); 196 smp_mb__after_atomic(); 197 wake_up_bit(&mrvl->flags, STATE_CHIP_VER_PENDING); 198 199 done: 200 kfree_skb(skb); 201 return ret; 202 } 203 204 #define HCI_RECV_CHIP_VER \ 205 .type = HCI_CHIP_VER_PKT, \ 206 .hlen = HCI_MRVL_PKT_SIZE, \ 207 .loff = 0, \ 208 .lsize = 0, \ 209 .maxlen = HCI_MRVL_PKT_SIZE 210 211 #define HCI_RECV_FW_REQ \ 212 .type = HCI_FW_REQ_PKT, \ 213 .hlen = HCI_MRVL_PKT_SIZE, \ 214 .loff = 0, \ 215 .lsize = 0, \ 216 .maxlen = HCI_MRVL_PKT_SIZE 217 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 }, 224 }; 225 226 static int mrvl_recv(struct hci_uart *hu, const void *data, int count) 227 { 228 struct mrvl_data *mrvl = hu->priv; 229 230 if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) 231 return -EUNATCH; 232 233 mrvl->rx_skb = h4_recv_buf(hu->hdev, mrvl->rx_skb, data, count, 234 mrvl_recv_pkts, 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); 239 mrvl->rx_skb = NULL; 240 return err; 241 } 242 243 return count; 244 } 245 246 static int mrvl_load_firmware(struct hci_dev *hdev, const char *name) 247 { 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; 252 int err; 253 254 err = request_firmware(&fw, name, &hdev->dev); 255 if (err < 0) { 256 bt_dev_err(hdev, "Failed to load firmware file %s", name); 257 return err; 258 } 259 260 fw_ptr = fw->data; 261 fw_max = fw->data + fw->size; 262 263 bt_dev_info(hdev, "Loading %s", name); 264 265 set_bit(STATE_FW_REQ_PENDING, &mrvl->flags); 266 267 while (fw_ptr <= fw_max) { 268 struct sk_buff *skb; 269 270 /* Controller drives the firmware load by sending firmware 271 * request packets containing the expected fragment size. 272 */ 273 err = wait_on_bit_timeout(&mrvl->flags, STATE_FW_REQ_PENDING, 274 TASK_INTERRUPTIBLE, 275 msecs_to_jiffies(2000)); 276 if (err == 1) { 277 bt_dev_err(hdev, "Firmware load interrupted"); 278 err = -EINTR; 279 break; 280 } else if (err) { 281 bt_dev_err(hdev, "Firmware request timeout"); 282 err = -ETIMEDOUT; 283 break; 284 } 285 286 bt_dev_dbg(hdev, "Firmware request, expecting %d bytes", 287 mrvl->tx_len); 288 289 if (fw_ptr == fw_max) { 290 /* Controller requests a null size once firmware is 291 * fully loaded. If controller expects more data, there 292 * is an issue. 293 */ 294 if (!mrvl->tx_len) { 295 bt_dev_info(hdev, "Firmware loading complete"); 296 } else { 297 bt_dev_err(hdev, "Firmware loading failure"); 298 err = -EINVAL; 299 } 300 break; 301 } 302 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", 306 mrvl->tx_len); 307 } 308 309 skb = bt_skb_alloc(mrvl->tx_len, GFP_KERNEL); 310 if (!skb) { 311 bt_dev_err(hdev, "Failed to alloc mem for FW packet"); 312 err = -ENOMEM; 313 break; 314 } 315 bt_cb(skb)->pkt_type = MRVL_RAW_DATA; 316 317 skb_put_data(skb, fw_ptr, mrvl->tx_len); 318 fw_ptr += mrvl->tx_len; 319 320 set_bit(STATE_FW_REQ_PENDING, &mrvl->flags); 321 322 skb_queue_tail(&mrvl->rawq, skb); 323 hci_uart_tx_wakeup(hu); 324 } 325 326 release_firmware(fw); 327 return err; 328 } 329 330 static int mrvl_setup(struct hci_uart *hu) 331 { 332 int err; 333 334 hci_uart_set_flow_control(hu, true); 335 336 err = mrvl_load_firmware(hu->hdev, "mrvl/helper_uart_3000000.bin"); 337 if (err) { 338 bt_dev_err(hu->hdev, "Unable to download firmware helper"); 339 return -EINVAL; 340 } 341 342 hci_uart_set_baudrate(hu, 3000000); 343 hci_uart_set_flow_control(hu, false); 344 345 err = mrvl_load_firmware(hu->hdev, "mrvl/uart8897_bt.bin"); 346 if (err) 347 return err; 348 349 return 0; 350 } 351 352 static const struct hci_uart_proto mrvl_proto = { 353 .id = HCI_UART_MRVL, 354 .name = "Marvell", 355 .init_speed = 115200, 356 .open = mrvl_open, 357 .close = mrvl_close, 358 .flush = mrvl_flush, 359 .setup = mrvl_setup, 360 .recv = mrvl_recv, 361 .enqueue = mrvl_enqueue, 362 .dequeue = mrvl_dequeue, 363 }; 364 365 int __init mrvl_init(void) 366 { 367 return hci_uart_register_proto(&mrvl_proto); 368 } 369 370 int __exit mrvl_deinit(void) 371 { 372 return hci_uart_unregister_proto(&mrvl_proto); 373 } 374