1 /** 2 * Marvell BT-over-SDIO driver: SDIO interface related functions. 3 * 4 * Copyright (C) 2009, Marvell International Ltd. 5 * 6 * This software file (the "File") is distributed by Marvell International 7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991 8 * (the "License"). You may use, redistribute and/or modify this File in 9 * accordance with the terms and conditions of the License, a copy of which 10 * is available by writing to the Free Software Foundation, Inc., 11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the 12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 13 * 14 * 15 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE 17 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about 18 * this warranty disclaimer. 19 **/ 20 21 #include <linux/firmware.h> 22 #include <linux/slab.h> 23 24 #include <linux/mmc/sdio_ids.h> 25 #include <linux/mmc/sdio_func.h> 26 #include <linux/module.h> 27 #include <linux/devcoredump.h> 28 29 #include <net/bluetooth/bluetooth.h> 30 #include <net/bluetooth/hci_core.h> 31 32 #include "btmrvl_drv.h" 33 #include "btmrvl_sdio.h" 34 35 #define VERSION "1.0" 36 37 static struct memory_type_mapping mem_type_mapping_tbl[] = { 38 {"ITCM", NULL, 0, 0xF0}, 39 {"DTCM", NULL, 0, 0xF1}, 40 {"SQRAM", NULL, 0, 0xF2}, 41 {"APU", NULL, 0, 0xF3}, 42 {"CIU", NULL, 0, 0xF4}, 43 {"ICU", NULL, 0, 0xF5}, 44 {"MAC", NULL, 0, 0xF6}, 45 {"EXT7", NULL, 0, 0xF7}, 46 {"EXT8", NULL, 0, 0xF8}, 47 {"EXT9", NULL, 0, 0xF9}, 48 {"EXT10", NULL, 0, 0xFA}, 49 {"EXT11", NULL, 0, 0xFB}, 50 {"EXT12", NULL, 0, 0xFC}, 51 {"EXT13", NULL, 0, 0xFD}, 52 {"EXTLAST", NULL, 0, 0xFE}, 53 }; 54 55 /* The btmrvl_sdio_remove() callback function is called 56 * when user removes this module from kernel space or ejects 57 * the card from the slot. The driver handles these 2 cases 58 * differently. 59 * If the user is removing the module, a MODULE_SHUTDOWN_REQ 60 * command is sent to firmware and interrupt will be disabled. 61 * If the card is removed, there is no need to send command 62 * or disable interrupt. 63 * 64 * The variable 'user_rmmod' is used to distinguish these two 65 * scenarios. This flag is initialized as FALSE in case the card 66 * is removed, and will be set to TRUE for module removal when 67 * module_exit function is called. 68 */ 69 static u8 user_rmmod; 70 static u8 sdio_ireg; 71 72 static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = { 73 .cfg = 0x03, 74 .host_int_mask = 0x04, 75 .host_intstatus = 0x05, 76 .card_status = 0x20, 77 .sq_read_base_addr_a0 = 0x10, 78 .sq_read_base_addr_a1 = 0x11, 79 .card_fw_status0 = 0x40, 80 .card_fw_status1 = 0x41, 81 .card_rx_len = 0x42, 82 .card_rx_unit = 0x43, 83 .io_port_0 = 0x00, 84 .io_port_1 = 0x01, 85 .io_port_2 = 0x02, 86 .int_read_to_clear = false, 87 }; 88 static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = { 89 .cfg = 0x00, 90 .host_int_mask = 0x02, 91 .host_intstatus = 0x03, 92 .card_status = 0x30, 93 .sq_read_base_addr_a0 = 0x40, 94 .sq_read_base_addr_a1 = 0x41, 95 .card_revision = 0x5c, 96 .card_fw_status0 = 0x60, 97 .card_fw_status1 = 0x61, 98 .card_rx_len = 0x62, 99 .card_rx_unit = 0x63, 100 .io_port_0 = 0x78, 101 .io_port_1 = 0x79, 102 .io_port_2 = 0x7a, 103 .int_read_to_clear = false, 104 }; 105 106 static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = { 107 .cfg = 0x00, 108 .host_int_mask = 0x08, 109 .host_intstatus = 0x0C, 110 .card_status = 0x5C, 111 .sq_read_base_addr_a0 = 0x6C, 112 .sq_read_base_addr_a1 = 0x6D, 113 .card_revision = 0xC8, 114 .card_fw_status0 = 0x88, 115 .card_fw_status1 = 0x89, 116 .card_rx_len = 0x8A, 117 .card_rx_unit = 0x8B, 118 .io_port_0 = 0xE4, 119 .io_port_1 = 0xE5, 120 .io_port_2 = 0xE6, 121 .int_read_to_clear = true, 122 .host_int_rsr = 0x04, 123 .card_misc_cfg = 0xD8, 124 }; 125 126 static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = { 127 .cfg = 0x00, 128 .host_int_mask = 0x02, 129 .host_intstatus = 0x03, 130 .card_status = 0x50, 131 .sq_read_base_addr_a0 = 0x60, 132 .sq_read_base_addr_a1 = 0x61, 133 .card_revision = 0xbc, 134 .card_fw_status0 = 0xc0, 135 .card_fw_status1 = 0xc1, 136 .card_rx_len = 0xc2, 137 .card_rx_unit = 0xc3, 138 .io_port_0 = 0xd8, 139 .io_port_1 = 0xd9, 140 .io_port_2 = 0xda, 141 .int_read_to_clear = true, 142 .host_int_rsr = 0x01, 143 .card_misc_cfg = 0xcc, 144 .fw_dump_ctrl = 0xe2, 145 .fw_dump_start = 0xe3, 146 .fw_dump_end = 0xea, 147 }; 148 149 static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = { 150 .helper = "mrvl/sd8688_helper.bin", 151 .firmware = "mrvl/sd8688.bin", 152 .reg = &btmrvl_reg_8688, 153 .support_pscan_win_report = false, 154 .sd_blksz_fw_dl = 64, 155 .supports_fw_dump = false, 156 }; 157 158 static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = { 159 .helper = NULL, 160 .firmware = "mrvl/sd8787_uapsta.bin", 161 .reg = &btmrvl_reg_87xx, 162 .support_pscan_win_report = false, 163 .sd_blksz_fw_dl = 256, 164 .supports_fw_dump = false, 165 }; 166 167 static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = { 168 .helper = NULL, 169 .firmware = "mrvl/sd8797_uapsta.bin", 170 .reg = &btmrvl_reg_87xx, 171 .support_pscan_win_report = false, 172 .sd_blksz_fw_dl = 256, 173 .supports_fw_dump = false, 174 }; 175 176 static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = { 177 .helper = NULL, 178 .firmware = "mrvl/sd8887_uapsta.bin", 179 .reg = &btmrvl_reg_8887, 180 .support_pscan_win_report = true, 181 .sd_blksz_fw_dl = 256, 182 .supports_fw_dump = false, 183 }; 184 185 static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = { 186 .helper = NULL, 187 .firmware = "mrvl/sd8897_uapsta.bin", 188 .reg = &btmrvl_reg_8897, 189 .support_pscan_win_report = true, 190 .sd_blksz_fw_dl = 256, 191 .supports_fw_dump = true, 192 }; 193 194 static const struct sdio_device_id btmrvl_sdio_ids[] = { 195 /* Marvell SD8688 Bluetooth device */ 196 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105), 197 .driver_data = (unsigned long) &btmrvl_sdio_sd8688 }, 198 /* Marvell SD8787 Bluetooth device */ 199 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A), 200 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 }, 201 /* Marvell SD8787 Bluetooth AMP device */ 202 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B), 203 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 }, 204 /* Marvell SD8797 Bluetooth device */ 205 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A), 206 .driver_data = (unsigned long) &btmrvl_sdio_sd8797 }, 207 /* Marvell SD8887 Bluetooth device */ 208 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9136), 209 .driver_data = (unsigned long)&btmrvl_sdio_sd8887 }, 210 /* Marvell SD8897 Bluetooth device */ 211 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E), 212 .driver_data = (unsigned long) &btmrvl_sdio_sd8897 }, 213 214 { } /* Terminating entry */ 215 }; 216 217 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids); 218 219 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card) 220 { 221 u8 reg; 222 int ret; 223 224 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret); 225 if (!ret) 226 card->rx_unit = reg; 227 228 return ret; 229 } 230 231 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat) 232 { 233 u8 fws0, fws1; 234 int ret; 235 236 *dat = 0; 237 238 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret); 239 if (ret) 240 return -EIO; 241 242 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret); 243 if (ret) 244 return -EIO; 245 246 *dat = (((u16) fws1) << 8) | fws0; 247 248 return 0; 249 } 250 251 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat) 252 { 253 u8 reg; 254 int ret; 255 256 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret); 257 if (!ret) 258 *dat = (u16) reg << card->rx_unit; 259 260 return ret; 261 } 262 263 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card, 264 u8 mask) 265 { 266 int ret; 267 268 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret); 269 if (ret) { 270 BT_ERR("Unable to enable the host interrupt!"); 271 ret = -EIO; 272 } 273 274 return ret; 275 } 276 277 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card, 278 u8 mask) 279 { 280 u8 host_int_mask; 281 int ret; 282 283 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret); 284 if (ret) 285 return -EIO; 286 287 host_int_mask &= ~mask; 288 289 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret); 290 if (ret < 0) { 291 BT_ERR("Unable to disable the host interrupt!"); 292 return -EIO; 293 } 294 295 return 0; 296 } 297 298 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits) 299 { 300 unsigned int tries; 301 u8 status; 302 int ret; 303 304 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) { 305 status = sdio_readb(card->func, card->reg->card_status, &ret); 306 if (ret) 307 goto failed; 308 if ((status & bits) == bits) 309 return ret; 310 311 udelay(1); 312 } 313 314 ret = -ETIMEDOUT; 315 316 failed: 317 BT_ERR("FAILED! ret=%d", ret); 318 319 return ret; 320 } 321 322 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card, 323 int pollnum) 324 { 325 u16 firmwarestat; 326 int tries, ret; 327 328 /* Wait for firmware to become ready */ 329 for (tries = 0; tries < pollnum; tries++) { 330 sdio_claim_host(card->func); 331 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat); 332 sdio_release_host(card->func); 333 if (ret < 0) 334 continue; 335 336 if (firmwarestat == FIRMWARE_READY) 337 return 0; 338 339 msleep(10); 340 } 341 342 return -ETIMEDOUT; 343 } 344 345 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card) 346 { 347 const struct firmware *fw_helper = NULL; 348 const u8 *helper = NULL; 349 int ret; 350 void *tmphlprbuf = NULL; 351 int tmphlprbufsz, hlprblknow, helperlen; 352 u8 *helperbuf; 353 u32 tx_len; 354 355 ret = request_firmware(&fw_helper, card->helper, 356 &card->func->dev); 357 if ((ret < 0) || !fw_helper) { 358 BT_ERR("request_firmware(helper) failed, error code = %d", 359 ret); 360 ret = -ENOENT; 361 goto done; 362 } 363 364 helper = fw_helper->data; 365 helperlen = fw_helper->size; 366 367 BT_DBG("Downloading helper image (%d bytes), block size %d bytes", 368 helperlen, SDIO_BLOCK_SIZE); 369 370 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN); 371 372 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL); 373 if (!tmphlprbuf) { 374 BT_ERR("Unable to allocate buffer for helper." 375 " Terminating download"); 376 ret = -ENOMEM; 377 goto done; 378 } 379 380 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN); 381 382 /* Perform helper data transfer */ 383 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE) 384 - SDIO_HEADER_LEN; 385 hlprblknow = 0; 386 387 do { 388 ret = btmrvl_sdio_poll_card_status(card, 389 CARD_IO_READY | DN_LD_CARD_RDY); 390 if (ret < 0) { 391 BT_ERR("Helper download poll status timeout @ %d", 392 hlprblknow); 393 goto done; 394 } 395 396 /* Check if there is more data? */ 397 if (hlprblknow >= helperlen) 398 break; 399 400 if (helperlen - hlprblknow < tx_len) 401 tx_len = helperlen - hlprblknow; 402 403 /* Little-endian */ 404 helperbuf[0] = ((tx_len & 0x000000ff) >> 0); 405 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8); 406 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16); 407 helperbuf[3] = ((tx_len & 0xff000000) >> 24); 408 409 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow], 410 tx_len); 411 412 /* Now send the data */ 413 ret = sdio_writesb(card->func, card->ioport, helperbuf, 414 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE); 415 if (ret < 0) { 416 BT_ERR("IO error during helper download @ %d", 417 hlprblknow); 418 goto done; 419 } 420 421 hlprblknow += tx_len; 422 } while (true); 423 424 BT_DBG("Transferring helper image EOF block"); 425 426 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE); 427 428 ret = sdio_writesb(card->func, card->ioport, helperbuf, 429 SDIO_BLOCK_SIZE); 430 if (ret < 0) { 431 BT_ERR("IO error in writing helper image EOF block"); 432 goto done; 433 } 434 435 ret = 0; 436 437 done: 438 kfree(tmphlprbuf); 439 release_firmware(fw_helper); 440 return ret; 441 } 442 443 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card) 444 { 445 const struct firmware *fw_firmware = NULL; 446 const u8 *firmware = NULL; 447 int firmwarelen, tmpfwbufsz, ret; 448 unsigned int tries, offset; 449 u8 base0, base1; 450 void *tmpfwbuf = NULL; 451 u8 *fwbuf; 452 u16 len, blksz_dl = card->sd_blksz_fw_dl; 453 int txlen = 0, tx_blocks = 0, count = 0; 454 455 ret = request_firmware(&fw_firmware, card->firmware, 456 &card->func->dev); 457 if ((ret < 0) || !fw_firmware) { 458 BT_ERR("request_firmware(firmware) failed, error code = %d", 459 ret); 460 ret = -ENOENT; 461 goto done; 462 } 463 464 firmware = fw_firmware->data; 465 firmwarelen = fw_firmware->size; 466 467 BT_DBG("Downloading FW image (%d bytes)", firmwarelen); 468 469 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN); 470 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL); 471 if (!tmpfwbuf) { 472 BT_ERR("Unable to allocate buffer for firmware." 473 " Terminating download"); 474 ret = -ENOMEM; 475 goto done; 476 } 477 478 /* Ensure aligned firmware buffer */ 479 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN); 480 481 /* Perform firmware data transfer */ 482 offset = 0; 483 do { 484 ret = btmrvl_sdio_poll_card_status(card, 485 CARD_IO_READY | DN_LD_CARD_RDY); 486 if (ret < 0) { 487 BT_ERR("FW download with helper poll status" 488 " timeout @ %d", offset); 489 goto done; 490 } 491 492 /* Check if there is more data ? */ 493 if (offset >= firmwarelen) 494 break; 495 496 for (tries = 0; tries < MAX_POLL_TRIES; tries++) { 497 base0 = sdio_readb(card->func, 498 card->reg->sq_read_base_addr_a0, &ret); 499 if (ret) { 500 BT_ERR("BASE0 register read failed:" 501 " base0 = 0x%04X(%d)." 502 " Terminating download", 503 base0, base0); 504 ret = -EIO; 505 goto done; 506 } 507 base1 = sdio_readb(card->func, 508 card->reg->sq_read_base_addr_a1, &ret); 509 if (ret) { 510 BT_ERR("BASE1 register read failed:" 511 " base1 = 0x%04X(%d)." 512 " Terminating download", 513 base1, base1); 514 ret = -EIO; 515 goto done; 516 } 517 518 len = (((u16) base1) << 8) | base0; 519 if (len) 520 break; 521 522 udelay(10); 523 } 524 525 if (!len) 526 break; 527 else if (len > BTM_UPLD_SIZE) { 528 BT_ERR("FW download failure @%d, invalid length %d", 529 offset, len); 530 ret = -EINVAL; 531 goto done; 532 } 533 534 txlen = len; 535 536 if (len & BIT(0)) { 537 count++; 538 if (count > MAX_WRITE_IOMEM_RETRY) { 539 BT_ERR("FW download failure @%d, " 540 "over max retry count", offset); 541 ret = -EIO; 542 goto done; 543 } 544 BT_ERR("FW CRC error indicated by the helper: " 545 "len = 0x%04X, txlen = %d", len, txlen); 546 len &= ~BIT(0); 547 /* Set txlen to 0 so as to resend from same offset */ 548 txlen = 0; 549 } else { 550 count = 0; 551 552 /* Last block ? */ 553 if (firmwarelen - offset < txlen) 554 txlen = firmwarelen - offset; 555 556 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl); 557 558 memcpy(fwbuf, &firmware[offset], txlen); 559 } 560 561 ret = sdio_writesb(card->func, card->ioport, fwbuf, 562 tx_blocks * blksz_dl); 563 564 if (ret < 0) { 565 BT_ERR("FW download, writesb(%d) failed @%d", 566 count, offset); 567 sdio_writeb(card->func, HOST_CMD53_FIN, 568 card->reg->cfg, &ret); 569 if (ret) 570 BT_ERR("writeb failed (CFG)"); 571 } 572 573 offset += txlen; 574 } while (true); 575 576 BT_DBG("FW download over, size %d bytes", offset); 577 578 ret = 0; 579 580 done: 581 kfree(tmpfwbuf); 582 release_firmware(fw_firmware); 583 return ret; 584 } 585 586 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv) 587 { 588 u16 buf_len = 0; 589 int ret, num_blocks, blksz; 590 struct sk_buff *skb = NULL; 591 u32 type; 592 u8 *payload = NULL; 593 struct hci_dev *hdev = priv->btmrvl_dev.hcidev; 594 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 595 596 if (!card || !card->func) { 597 BT_ERR("card or function is NULL!"); 598 ret = -EINVAL; 599 goto exit; 600 } 601 602 /* Read the length of data to be transferred */ 603 ret = btmrvl_sdio_read_rx_len(card, &buf_len); 604 if (ret < 0) { 605 BT_ERR("read rx_len failed"); 606 ret = -EIO; 607 goto exit; 608 } 609 610 blksz = SDIO_BLOCK_SIZE; 611 num_blocks = DIV_ROUND_UP(buf_len, blksz); 612 613 if (buf_len <= SDIO_HEADER_LEN 614 || (num_blocks * blksz) > ALLOC_BUF_SIZE) { 615 BT_ERR("invalid packet length: %d", buf_len); 616 ret = -EINVAL; 617 goto exit; 618 } 619 620 /* Allocate buffer */ 621 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC); 622 if (skb == NULL) { 623 BT_ERR("No free skb"); 624 ret = -ENOMEM; 625 goto exit; 626 } 627 628 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) { 629 skb_put(skb, (unsigned long) skb->data & 630 (BTSDIO_DMA_ALIGN - 1)); 631 skb_pull(skb, (unsigned long) skb->data & 632 (BTSDIO_DMA_ALIGN - 1)); 633 } 634 635 payload = skb->data; 636 637 ret = sdio_readsb(card->func, payload, card->ioport, 638 num_blocks * blksz); 639 if (ret < 0) { 640 BT_ERR("readsb failed: %d", ret); 641 ret = -EIO; 642 goto exit; 643 } 644 645 /* This is SDIO specific header length: byte[2][1][0], type: byte[3] 646 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor) 647 */ 648 649 buf_len = payload[0]; 650 buf_len |= payload[1] << 8; 651 buf_len |= payload[2] << 16; 652 653 if (buf_len > blksz * num_blocks) { 654 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d", 655 buf_len, blksz * num_blocks); 656 ret = -EIO; 657 goto exit; 658 } 659 660 type = payload[3]; 661 662 switch (type) { 663 case HCI_ACLDATA_PKT: 664 case HCI_SCODATA_PKT: 665 case HCI_EVENT_PKT: 666 bt_cb(skb)->pkt_type = type; 667 skb_put(skb, buf_len); 668 skb_pull(skb, SDIO_HEADER_LEN); 669 670 if (type == HCI_EVENT_PKT) { 671 if (btmrvl_check_evtpkt(priv, skb)) 672 hci_recv_frame(hdev, skb); 673 } else { 674 hci_recv_frame(hdev, skb); 675 } 676 677 hdev->stat.byte_rx += buf_len; 678 break; 679 680 case MRVL_VENDOR_PKT: 681 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT; 682 skb_put(skb, buf_len); 683 skb_pull(skb, SDIO_HEADER_LEN); 684 685 if (btmrvl_process_event(priv, skb)) 686 hci_recv_frame(hdev, skb); 687 688 hdev->stat.byte_rx += buf_len; 689 break; 690 691 default: 692 BT_ERR("Unknown packet type:%d", type); 693 BT_ERR("hex: %*ph", blksz * num_blocks, payload); 694 695 kfree_skb(skb); 696 skb = NULL; 697 break; 698 } 699 700 exit: 701 if (ret) { 702 hdev->stat.err_rx++; 703 kfree_skb(skb); 704 } 705 706 return ret; 707 } 708 709 static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv) 710 { 711 ulong flags; 712 u8 ireg; 713 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 714 715 spin_lock_irqsave(&priv->driver_lock, flags); 716 ireg = sdio_ireg; 717 sdio_ireg = 0; 718 spin_unlock_irqrestore(&priv->driver_lock, flags); 719 720 sdio_claim_host(card->func); 721 if (ireg & DN_LD_HOST_INT_STATUS) { 722 if (priv->btmrvl_dev.tx_dnld_rdy) 723 BT_DBG("tx_done already received: " 724 " int_status=0x%x", ireg); 725 else 726 priv->btmrvl_dev.tx_dnld_rdy = true; 727 } 728 729 if (ireg & UP_LD_HOST_INT_STATUS) 730 btmrvl_sdio_card_to_host(priv); 731 732 sdio_release_host(card->func); 733 734 return 0; 735 } 736 737 static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg) 738 { 739 struct btmrvl_adapter *adapter = card->priv->adapter; 740 int ret; 741 742 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE); 743 if (ret) { 744 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret); 745 return ret; 746 } 747 748 *ireg = adapter->hw_regs[card->reg->host_intstatus]; 749 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg); 750 751 return 0; 752 } 753 754 static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg) 755 { 756 int ret; 757 758 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret); 759 if (ret) { 760 BT_ERR("sdio_readb: read int status failed: %d", ret); 761 return ret; 762 } 763 764 if (*ireg) { 765 /* 766 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS 767 * Clear the interrupt status register and re-enable the 768 * interrupt. 769 */ 770 BT_DBG("int_status = 0x%x", *ireg); 771 772 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS | 773 UP_LD_HOST_INT_STATUS), 774 card->reg->host_intstatus, &ret); 775 if (ret) { 776 BT_ERR("sdio_writeb: clear int status failed: %d", ret); 777 return ret; 778 } 779 } 780 781 return 0; 782 } 783 784 static void btmrvl_sdio_interrupt(struct sdio_func *func) 785 { 786 struct btmrvl_private *priv; 787 struct btmrvl_sdio_card *card; 788 ulong flags; 789 u8 ireg = 0; 790 int ret; 791 792 card = sdio_get_drvdata(func); 793 if (!card || !card->priv) { 794 BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p", 795 func, card); 796 return; 797 } 798 799 priv = card->priv; 800 801 if (card->reg->int_read_to_clear) 802 ret = btmrvl_sdio_read_to_clear(card, &ireg); 803 else 804 ret = btmrvl_sdio_write_to_clear(card, &ireg); 805 806 if (ret) 807 return; 808 809 spin_lock_irqsave(&priv->driver_lock, flags); 810 sdio_ireg |= ireg; 811 spin_unlock_irqrestore(&priv->driver_lock, flags); 812 813 btmrvl_interrupt(priv); 814 } 815 816 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card) 817 { 818 struct sdio_func *func; 819 u8 reg; 820 int ret = 0; 821 822 if (!card || !card->func) { 823 BT_ERR("Error: card or function is NULL!"); 824 ret = -EINVAL; 825 goto failed; 826 } 827 828 func = card->func; 829 830 sdio_claim_host(func); 831 832 ret = sdio_enable_func(func); 833 if (ret) { 834 BT_ERR("sdio_enable_func() failed: ret=%d", ret); 835 ret = -EIO; 836 goto release_host; 837 } 838 839 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt); 840 if (ret) { 841 BT_ERR("sdio_claim_irq failed: ret=%d", ret); 842 ret = -EIO; 843 goto disable_func; 844 } 845 846 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE); 847 if (ret) { 848 BT_ERR("cannot set SDIO block size"); 849 ret = -EIO; 850 goto release_irq; 851 } 852 853 reg = sdio_readb(func, card->reg->io_port_0, &ret); 854 if (ret < 0) { 855 ret = -EIO; 856 goto release_irq; 857 } 858 859 card->ioport = reg; 860 861 reg = sdio_readb(func, card->reg->io_port_1, &ret); 862 if (ret < 0) { 863 ret = -EIO; 864 goto release_irq; 865 } 866 867 card->ioport |= (reg << 8); 868 869 reg = sdio_readb(func, card->reg->io_port_2, &ret); 870 if (ret < 0) { 871 ret = -EIO; 872 goto release_irq; 873 } 874 875 card->ioport |= (reg << 16); 876 877 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport); 878 879 if (card->reg->int_read_to_clear) { 880 reg = sdio_readb(func, card->reg->host_int_rsr, &ret); 881 if (ret < 0) { 882 ret = -EIO; 883 goto release_irq; 884 } 885 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret); 886 if (ret < 0) { 887 ret = -EIO; 888 goto release_irq; 889 } 890 891 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret); 892 if (ret < 0) { 893 ret = -EIO; 894 goto release_irq; 895 } 896 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret); 897 if (ret < 0) { 898 ret = -EIO; 899 goto release_irq; 900 } 901 } 902 903 sdio_set_drvdata(func, card); 904 905 sdio_release_host(func); 906 907 return 0; 908 909 release_irq: 910 sdio_release_irq(func); 911 912 disable_func: 913 sdio_disable_func(func); 914 915 release_host: 916 sdio_release_host(func); 917 918 failed: 919 return ret; 920 } 921 922 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card) 923 { 924 if (card && card->func) { 925 sdio_claim_host(card->func); 926 sdio_release_irq(card->func); 927 sdio_disable_func(card->func); 928 sdio_release_host(card->func); 929 sdio_set_drvdata(card->func, NULL); 930 } 931 932 return 0; 933 } 934 935 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card) 936 { 937 int ret; 938 939 if (!card || !card->func) 940 return -EINVAL; 941 942 sdio_claim_host(card->func); 943 944 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE); 945 946 btmrvl_sdio_get_rx_unit(card); 947 948 sdio_release_host(card->func); 949 950 return ret; 951 } 952 953 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card) 954 { 955 int ret; 956 957 if (!card || !card->func) 958 return -EINVAL; 959 960 sdio_claim_host(card->func); 961 962 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE); 963 964 sdio_release_host(card->func); 965 966 return ret; 967 } 968 969 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv, 970 u8 *payload, u16 nb) 971 { 972 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 973 int ret = 0; 974 int buf_block_len; 975 int blksz; 976 int i = 0; 977 u8 *buf = NULL; 978 void *tmpbuf = NULL; 979 int tmpbufsz; 980 981 if (!card || !card->func) { 982 BT_ERR("card or function is NULL!"); 983 return -EINVAL; 984 } 985 986 buf = payload; 987 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) { 988 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN); 989 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL); 990 if (!tmpbuf) 991 return -ENOMEM; 992 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN); 993 memcpy(buf, payload, nb); 994 } 995 996 blksz = SDIO_BLOCK_SIZE; 997 buf_block_len = DIV_ROUND_UP(nb, blksz); 998 999 sdio_claim_host(card->func); 1000 1001 do { 1002 /* Transfer data to card */ 1003 ret = sdio_writesb(card->func, card->ioport, buf, 1004 buf_block_len * blksz); 1005 if (ret < 0) { 1006 i++; 1007 BT_ERR("i=%d writesb failed: %d", i, ret); 1008 BT_ERR("hex: %*ph", nb, payload); 1009 ret = -EIO; 1010 if (i > MAX_WRITE_IOMEM_RETRY) 1011 goto exit; 1012 } 1013 } while (ret); 1014 1015 priv->btmrvl_dev.tx_dnld_rdy = false; 1016 1017 exit: 1018 sdio_release_host(card->func); 1019 kfree(tmpbuf); 1020 1021 return ret; 1022 } 1023 1024 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card) 1025 { 1026 int ret; 1027 u8 fws0; 1028 int pollnum = MAX_POLL_TRIES; 1029 1030 if (!card || !card->func) { 1031 BT_ERR("card or function is NULL!"); 1032 return -EINVAL; 1033 } 1034 1035 if (!btmrvl_sdio_verify_fw_download(card, 1)) { 1036 BT_DBG("Firmware already downloaded!"); 1037 return 0; 1038 } 1039 1040 sdio_claim_host(card->func); 1041 1042 /* Check if other function driver is downloading the firmware */ 1043 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret); 1044 if (ret) { 1045 BT_ERR("Failed to read FW downloading status!"); 1046 ret = -EIO; 1047 goto done; 1048 } 1049 if (fws0) { 1050 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0); 1051 1052 /* Give other function more time to download the firmware */ 1053 pollnum *= 10; 1054 } else { 1055 if (card->helper) { 1056 ret = btmrvl_sdio_download_helper(card); 1057 if (ret) { 1058 BT_ERR("Failed to download helper!"); 1059 ret = -EIO; 1060 goto done; 1061 } 1062 } 1063 1064 if (btmrvl_sdio_download_fw_w_helper(card)) { 1065 BT_ERR("Failed to download firmware!"); 1066 ret = -EIO; 1067 goto done; 1068 } 1069 } 1070 1071 sdio_release_host(card->func); 1072 1073 /* 1074 * winner or not, with this test the FW synchronizes when the 1075 * module can continue its initialization 1076 */ 1077 if (btmrvl_sdio_verify_fw_download(card, pollnum)) { 1078 BT_ERR("FW failed to be active in time!"); 1079 return -ETIMEDOUT; 1080 } 1081 1082 return 0; 1083 1084 done: 1085 sdio_release_host(card->func); 1086 return ret; 1087 } 1088 1089 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv) 1090 { 1091 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 1092 int ret = 0; 1093 1094 if (!card || !card->func) { 1095 BT_ERR("card or function is NULL!"); 1096 return -EINVAL; 1097 } 1098 1099 sdio_claim_host(card->func); 1100 1101 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret); 1102 1103 sdio_release_host(card->func); 1104 1105 BT_DBG("wake up firmware"); 1106 1107 return ret; 1108 } 1109 1110 static void btmrvl_sdio_dump_regs(struct btmrvl_private *priv) 1111 { 1112 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 1113 int ret = 0; 1114 unsigned int reg, reg_start, reg_end; 1115 char buf[256], *ptr; 1116 u8 loop, func, data; 1117 int MAX_LOOP = 2; 1118 1119 btmrvl_sdio_wakeup_fw(priv); 1120 sdio_claim_host(card->func); 1121 1122 for (loop = 0; loop < MAX_LOOP; loop++) { 1123 memset(buf, 0, sizeof(buf)); 1124 ptr = buf; 1125 1126 if (loop == 0) { 1127 /* Read the registers of SDIO function0 */ 1128 func = loop; 1129 reg_start = 0; 1130 reg_end = 9; 1131 } else { 1132 func = 2; 1133 reg_start = 0; 1134 reg_end = 0x09; 1135 } 1136 1137 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ", 1138 func, reg_start, reg_end); 1139 for (reg = reg_start; reg <= reg_end; reg++) { 1140 if (func == 0) 1141 data = sdio_f0_readb(card->func, reg, &ret); 1142 else 1143 data = sdio_readb(card->func, reg, &ret); 1144 1145 if (!ret) { 1146 ptr += sprintf(ptr, "%02x ", data); 1147 } else { 1148 ptr += sprintf(ptr, "ERR"); 1149 break; 1150 } 1151 } 1152 1153 BT_INFO("%s", buf); 1154 } 1155 1156 sdio_release_host(card->func); 1157 } 1158 1159 /* This function read/write firmware */ 1160 static enum 1161 rdwr_status btmrvl_sdio_rdwr_firmware(struct btmrvl_private *priv, 1162 u8 doneflag) 1163 { 1164 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 1165 int ret, tries; 1166 u8 ctrl_data = 0; 1167 1168 sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl, 1169 &ret); 1170 1171 if (ret) { 1172 BT_ERR("SDIO write err"); 1173 return RDWR_STATUS_FAILURE; 1174 } 1175 1176 for (tries = 0; tries < MAX_POLL_TRIES; tries++) { 1177 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl, 1178 &ret); 1179 1180 if (ret) { 1181 BT_ERR("SDIO read err"); 1182 return RDWR_STATUS_FAILURE; 1183 } 1184 1185 if (ctrl_data == FW_DUMP_DONE) 1186 break; 1187 if (doneflag && ctrl_data == doneflag) 1188 return RDWR_STATUS_DONE; 1189 if (ctrl_data != FW_DUMP_HOST_READY) { 1190 BT_INFO("The ctrl reg was changed, re-try again!"); 1191 sdio_writeb(card->func, FW_DUMP_HOST_READY, 1192 card->reg->fw_dump_ctrl, &ret); 1193 if (ret) { 1194 BT_ERR("SDIO write err"); 1195 return RDWR_STATUS_FAILURE; 1196 } 1197 } 1198 usleep_range(100, 200); 1199 } 1200 1201 if (ctrl_data == FW_DUMP_HOST_READY) { 1202 BT_ERR("Fail to pull ctrl_data"); 1203 return RDWR_STATUS_FAILURE; 1204 } 1205 1206 return RDWR_STATUS_SUCCESS; 1207 } 1208 1209 /* This function dump sdio register and memory data */ 1210 static void btmrvl_sdio_dump_firmware(struct btmrvl_private *priv) 1211 { 1212 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; 1213 int ret = 0; 1214 unsigned int reg, reg_start, reg_end; 1215 enum rdwr_status stat; 1216 u8 *dbg_ptr, *end_ptr, *fw_dump_data, *fw_dump_ptr; 1217 u8 dump_num, idx, i, read_reg, doneflag = 0; 1218 u32 memory_size, fw_dump_len = 0; 1219 1220 /* dump sdio register first */ 1221 btmrvl_sdio_dump_regs(priv); 1222 1223 if (!card->supports_fw_dump) { 1224 BT_ERR("Firmware dump not supported for this card!"); 1225 return; 1226 } 1227 1228 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) { 1229 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx]; 1230 1231 if (entry->mem_ptr) { 1232 vfree(entry->mem_ptr); 1233 entry->mem_ptr = NULL; 1234 } 1235 entry->mem_size = 0; 1236 } 1237 1238 btmrvl_sdio_wakeup_fw(priv); 1239 sdio_claim_host(card->func); 1240 1241 BT_INFO("== btmrvl firmware dump start =="); 1242 1243 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag); 1244 if (stat == RDWR_STATUS_FAILURE) 1245 goto done; 1246 1247 reg = card->reg->fw_dump_start; 1248 /* Read the number of the memories which will dump */ 1249 dump_num = sdio_readb(card->func, reg, &ret); 1250 1251 if (ret) { 1252 BT_ERR("SDIO read memory length err"); 1253 goto done; 1254 } 1255 1256 /* Read the length of every memory which will dump */ 1257 for (idx = 0; idx < dump_num; idx++) { 1258 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx]; 1259 1260 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag); 1261 if (stat == RDWR_STATUS_FAILURE) 1262 goto done; 1263 1264 memory_size = 0; 1265 reg = card->reg->fw_dump_start; 1266 for (i = 0; i < 4; i++) { 1267 read_reg = sdio_readb(card->func, reg, &ret); 1268 if (ret) { 1269 BT_ERR("SDIO read err"); 1270 goto done; 1271 } 1272 memory_size |= (read_reg << i*8); 1273 reg++; 1274 } 1275 1276 if (memory_size == 0) { 1277 BT_INFO("Firmware dump finished!"); 1278 break; 1279 } 1280 1281 BT_INFO("%s_SIZE=0x%x", entry->mem_name, memory_size); 1282 entry->mem_ptr = vzalloc(memory_size + 1); 1283 entry->mem_size = memory_size; 1284 if (!entry->mem_ptr) { 1285 BT_ERR("Vzalloc %s failed", entry->mem_name); 1286 goto done; 1287 } 1288 1289 fw_dump_len += (strlen("========Start dump ") + 1290 strlen(entry->mem_name) + 1291 strlen("========\n") + 1292 (memory_size + 1) + 1293 strlen("\n========End dump========\n")); 1294 1295 dbg_ptr = entry->mem_ptr; 1296 end_ptr = dbg_ptr + memory_size; 1297 1298 doneflag = entry->done_flag; 1299 BT_INFO("Start %s output, please wait...", 1300 entry->mem_name); 1301 1302 do { 1303 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag); 1304 if (stat == RDWR_STATUS_FAILURE) 1305 goto done; 1306 1307 reg_start = card->reg->fw_dump_start; 1308 reg_end = card->reg->fw_dump_end; 1309 for (reg = reg_start; reg <= reg_end; reg++) { 1310 *dbg_ptr = sdio_readb(card->func, reg, &ret); 1311 if (ret) { 1312 BT_ERR("SDIO read err"); 1313 goto done; 1314 } 1315 if (dbg_ptr < end_ptr) 1316 dbg_ptr++; 1317 else 1318 BT_ERR("Allocated buffer not enough"); 1319 } 1320 1321 if (stat != RDWR_STATUS_DONE) { 1322 continue; 1323 } else { 1324 BT_INFO("%s done: size=0x%tx", 1325 entry->mem_name, 1326 dbg_ptr - entry->mem_ptr); 1327 break; 1328 } 1329 } while (1); 1330 } 1331 1332 BT_INFO("== btmrvl firmware dump end =="); 1333 1334 done: 1335 sdio_release_host(card->func); 1336 1337 if (fw_dump_len == 0) 1338 return; 1339 1340 fw_dump_data = vzalloc(fw_dump_len+1); 1341 if (!fw_dump_data) { 1342 BT_ERR("Vzalloc fw_dump_data fail!"); 1343 return; 1344 } 1345 fw_dump_ptr = fw_dump_data; 1346 1347 /* Dump all the memory data into single file, a userspace script will 1348 be used to split all the memory data to multiple files*/ 1349 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start"); 1350 for (idx = 0; idx < dump_num; idx++) { 1351 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx]; 1352 1353 if (entry->mem_ptr) { 1354 strcpy(fw_dump_ptr, "========Start dump "); 1355 fw_dump_ptr += strlen("========Start dump "); 1356 1357 strcpy(fw_dump_ptr, entry->mem_name); 1358 fw_dump_ptr += strlen(entry->mem_name); 1359 1360 strcpy(fw_dump_ptr, "========\n"); 1361 fw_dump_ptr += strlen("========\n"); 1362 1363 memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size); 1364 fw_dump_ptr += entry->mem_size; 1365 1366 strcpy(fw_dump_ptr, "\n========End dump========\n"); 1367 fw_dump_ptr += strlen("\n========End dump========\n"); 1368 1369 vfree(mem_type_mapping_tbl[idx].mem_ptr); 1370 mem_type_mapping_tbl[idx].mem_ptr = NULL; 1371 } 1372 } 1373 1374 /* fw_dump_data will be free in device coredump release function 1375 after 5 min*/ 1376 dev_coredumpv(&priv->btmrvl_dev.hcidev->dev, fw_dump_data, 1377 fw_dump_len, GFP_KERNEL); 1378 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end"); 1379 } 1380 1381 static int btmrvl_sdio_probe(struct sdio_func *func, 1382 const struct sdio_device_id *id) 1383 { 1384 int ret = 0; 1385 struct btmrvl_private *priv = NULL; 1386 struct btmrvl_sdio_card *card = NULL; 1387 1388 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d", 1389 id->vendor, id->device, id->class, func->num); 1390 1391 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL); 1392 if (!card) 1393 return -ENOMEM; 1394 1395 card->func = func; 1396 1397 if (id->driver_data) { 1398 struct btmrvl_sdio_device *data = (void *) id->driver_data; 1399 card->helper = data->helper; 1400 card->firmware = data->firmware; 1401 card->reg = data->reg; 1402 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl; 1403 card->support_pscan_win_report = data->support_pscan_win_report; 1404 card->supports_fw_dump = data->supports_fw_dump; 1405 } 1406 1407 if (btmrvl_sdio_register_dev(card) < 0) { 1408 BT_ERR("Failed to register BT device!"); 1409 return -ENODEV; 1410 } 1411 1412 /* Disable the interrupts on the card */ 1413 btmrvl_sdio_disable_host_int(card); 1414 1415 if (btmrvl_sdio_download_fw(card)) { 1416 BT_ERR("Downloading firmware failed!"); 1417 ret = -ENODEV; 1418 goto unreg_dev; 1419 } 1420 1421 btmrvl_sdio_enable_host_int(card); 1422 1423 priv = btmrvl_add_card(card); 1424 if (!priv) { 1425 BT_ERR("Initializing card failed!"); 1426 ret = -ENODEV; 1427 goto disable_host_int; 1428 } 1429 1430 card->priv = priv; 1431 1432 /* Initialize the interface specific function pointers */ 1433 priv->hw_host_to_card = btmrvl_sdio_host_to_card; 1434 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw; 1435 priv->hw_process_int_status = btmrvl_sdio_process_int_status; 1436 priv->firmware_dump = btmrvl_sdio_dump_firmware; 1437 1438 if (btmrvl_register_hdev(priv)) { 1439 BT_ERR("Register hdev failed!"); 1440 ret = -ENODEV; 1441 goto disable_host_int; 1442 } 1443 1444 return 0; 1445 1446 disable_host_int: 1447 btmrvl_sdio_disable_host_int(card); 1448 unreg_dev: 1449 btmrvl_sdio_unregister_dev(card); 1450 return ret; 1451 } 1452 1453 static void btmrvl_sdio_remove(struct sdio_func *func) 1454 { 1455 struct btmrvl_sdio_card *card; 1456 1457 if (func) { 1458 card = sdio_get_drvdata(func); 1459 if (card) { 1460 /* Send SHUTDOWN command & disable interrupt 1461 * if user removes the module. 1462 */ 1463 if (user_rmmod) { 1464 btmrvl_send_module_cfg_cmd(card->priv, 1465 MODULE_SHUTDOWN_REQ); 1466 btmrvl_sdio_disable_host_int(card); 1467 } 1468 BT_DBG("unregester dev"); 1469 btmrvl_sdio_unregister_dev(card); 1470 btmrvl_remove_card(card->priv); 1471 } 1472 } 1473 } 1474 1475 static int btmrvl_sdio_suspend(struct device *dev) 1476 { 1477 struct sdio_func *func = dev_to_sdio_func(dev); 1478 struct btmrvl_sdio_card *card; 1479 struct btmrvl_private *priv; 1480 mmc_pm_flag_t pm_flags; 1481 struct hci_dev *hcidev; 1482 1483 if (func) { 1484 pm_flags = sdio_get_host_pm_caps(func); 1485 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func), 1486 pm_flags); 1487 if (!(pm_flags & MMC_PM_KEEP_POWER)) { 1488 BT_ERR("%s: cannot remain alive while suspended", 1489 sdio_func_id(func)); 1490 return -ENOSYS; 1491 } 1492 card = sdio_get_drvdata(func); 1493 if (!card || !card->priv) { 1494 BT_ERR("card or priv structure is not valid"); 1495 return 0; 1496 } 1497 } else { 1498 BT_ERR("sdio_func is not specified"); 1499 return 0; 1500 } 1501 1502 priv = card->priv; 1503 hcidev = priv->btmrvl_dev.hcidev; 1504 BT_DBG("%s: SDIO suspend", hcidev->name); 1505 hci_suspend_dev(hcidev); 1506 skb_queue_purge(&priv->adapter->tx_queue); 1507 1508 if (priv->adapter->hs_state != HS_ACTIVATED) { 1509 if (btmrvl_enable_hs(priv)) { 1510 BT_ERR("HS not actived, suspend failed!"); 1511 return -EBUSY; 1512 } 1513 } 1514 1515 priv->adapter->is_suspended = true; 1516 1517 /* We will keep the power when hs enabled successfully */ 1518 if (priv->adapter->hs_state == HS_ACTIVATED) { 1519 BT_DBG("suspend with MMC_PM_KEEP_POWER"); 1520 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER); 1521 } else { 1522 BT_DBG("suspend without MMC_PM_KEEP_POWER"); 1523 return 0; 1524 } 1525 } 1526 1527 static int btmrvl_sdio_resume(struct device *dev) 1528 { 1529 struct sdio_func *func = dev_to_sdio_func(dev); 1530 struct btmrvl_sdio_card *card; 1531 struct btmrvl_private *priv; 1532 mmc_pm_flag_t pm_flags; 1533 struct hci_dev *hcidev; 1534 1535 if (func) { 1536 pm_flags = sdio_get_host_pm_caps(func); 1537 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func), 1538 pm_flags); 1539 card = sdio_get_drvdata(func); 1540 if (!card || !card->priv) { 1541 BT_ERR("card or priv structure is not valid"); 1542 return 0; 1543 } 1544 } else { 1545 BT_ERR("sdio_func is not specified"); 1546 return 0; 1547 } 1548 priv = card->priv; 1549 1550 if (!priv->adapter->is_suspended) { 1551 BT_DBG("device already resumed"); 1552 return 0; 1553 } 1554 1555 priv->hw_wakeup_firmware(priv); 1556 priv->adapter->hs_state = HS_DEACTIVATED; 1557 hcidev = priv->btmrvl_dev.hcidev; 1558 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name); 1559 priv->adapter->is_suspended = false; 1560 BT_DBG("%s: SDIO resume", hcidev->name); 1561 hci_resume_dev(hcidev); 1562 1563 return 0; 1564 } 1565 1566 static const struct dev_pm_ops btmrvl_sdio_pm_ops = { 1567 .suspend = btmrvl_sdio_suspend, 1568 .resume = btmrvl_sdio_resume, 1569 }; 1570 1571 static struct sdio_driver bt_mrvl_sdio = { 1572 .name = "btmrvl_sdio", 1573 .id_table = btmrvl_sdio_ids, 1574 .probe = btmrvl_sdio_probe, 1575 .remove = btmrvl_sdio_remove, 1576 .drv = { 1577 .owner = THIS_MODULE, 1578 .pm = &btmrvl_sdio_pm_ops, 1579 } 1580 }; 1581 1582 static int __init btmrvl_sdio_init_module(void) 1583 { 1584 if (sdio_register_driver(&bt_mrvl_sdio) != 0) { 1585 BT_ERR("SDIO Driver Registration Failed"); 1586 return -ENODEV; 1587 } 1588 1589 /* Clear the flag in case user removes the card. */ 1590 user_rmmod = 0; 1591 1592 return 0; 1593 } 1594 1595 static void __exit btmrvl_sdio_exit_module(void) 1596 { 1597 /* Set the flag as user is removing this module. */ 1598 user_rmmod = 1; 1599 1600 sdio_unregister_driver(&bt_mrvl_sdio); 1601 } 1602 1603 module_init(btmrvl_sdio_init_module); 1604 module_exit(btmrvl_sdio_exit_module); 1605 1606 MODULE_AUTHOR("Marvell International Ltd."); 1607 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION); 1608 MODULE_VERSION(VERSION); 1609 MODULE_LICENSE("GPL v2"); 1610 MODULE_FIRMWARE("mrvl/sd8688_helper.bin"); 1611 MODULE_FIRMWARE("mrvl/sd8688.bin"); 1612 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin"); 1613 MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin"); 1614 MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin"); 1615 MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin"); 1616