Staging: adis16255: add proper section markings to hotplug funcs
[linux-kbuild.git] / drivers / bluetooth / btmrvl_sdio.c
blobdf0773ebd9e4fc4db3ef8d9d897adfd497d2eb31
1 /**
2 * Marvell BT-over-SDIO driver: SDIO interface related functions.
4 * Copyright (C) 2009, Marvell International Ltd.
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.
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 **/
21 #include <linux/firmware.h>
22 #include <linux/slab.h>
24 #include <linux/mmc/sdio_ids.h>
25 #include <linux/mmc/sdio_func.h>
27 #include <net/bluetooth/bluetooth.h>
28 #include <net/bluetooth/hci_core.h>
30 #include "btmrvl_drv.h"
31 #include "btmrvl_sdio.h"
33 #define VERSION "1.0"
35 /* The btmrvl_sdio_remove() callback function is called
36 * when user removes this module from kernel space or ejects
37 * the card from the slot. The driver handles these 2 cases
38 * differently.
39 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
40 * command is sent to firmware and interrupt will be disabled.
41 * If the card is removed, there is no need to send command
42 * or disable interrupt.
44 * The variable 'user_rmmod' is used to distinguish these two
45 * scenarios. This flag is initialized as FALSE in case the card
46 * is removed, and will be set to TRUE for module removal when
47 * module_exit function is called.
49 static u8 user_rmmod;
51 static const struct btmrvl_sdio_device btmrvl_sdio_sd6888 = {
52 .helper = "sd8688_helper.bin",
53 .firmware = "sd8688.bin",
56 static const struct sdio_device_id btmrvl_sdio_ids[] = {
57 /* Marvell SD8688 Bluetooth device */
58 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
59 .driver_data = (unsigned long) &btmrvl_sdio_sd6888 },
61 { } /* Terminating entry */
64 MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
66 static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
68 u8 reg;
69 int ret;
71 reg = sdio_readb(card->func, CARD_RX_UNIT_REG, &ret);
72 if (!ret)
73 card->rx_unit = reg;
75 return ret;
78 static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
80 u8 fws0, fws1;
81 int ret;
83 *dat = 0;
85 fws0 = sdio_readb(card->func, CARD_FW_STATUS0_REG, &ret);
87 if (!ret)
88 fws1 = sdio_readb(card->func, CARD_FW_STATUS1_REG, &ret);
90 if (ret)
91 return -EIO;
93 *dat = (((u16) fws1) << 8) | fws0;
95 return 0;
98 static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
100 u8 reg;
101 int ret;
103 reg = sdio_readb(card->func, CARD_RX_LEN_REG, &ret);
104 if (!ret)
105 *dat = (u16) reg << card->rx_unit;
107 return ret;
110 static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
111 u8 mask)
113 int ret;
115 sdio_writeb(card->func, mask, HOST_INT_MASK_REG, &ret);
116 if (ret) {
117 BT_ERR("Unable to enable the host interrupt!");
118 ret = -EIO;
121 return ret;
124 static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
125 u8 mask)
127 u8 host_int_mask;
128 int ret;
130 host_int_mask = sdio_readb(card->func, HOST_INT_MASK_REG, &ret);
131 if (ret)
132 return -EIO;
134 host_int_mask &= ~mask;
136 sdio_writeb(card->func, host_int_mask, HOST_INT_MASK_REG, &ret);
137 if (ret < 0) {
138 BT_ERR("Unable to disable the host interrupt!");
139 return -EIO;
142 return 0;
145 static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
147 unsigned int tries;
148 u8 status;
149 int ret;
151 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
152 status = sdio_readb(card->func, CARD_STATUS_REG, &ret);
153 if (ret)
154 goto failed;
155 if ((status & bits) == bits)
156 return ret;
158 udelay(1);
161 ret = -ETIMEDOUT;
163 failed:
164 BT_ERR("FAILED! ret=%d", ret);
166 return ret;
169 static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
170 int pollnum)
172 int ret = -ETIMEDOUT;
173 u16 firmwarestat;
174 unsigned int tries;
176 /* Wait for firmware to become ready */
177 for (tries = 0; tries < pollnum; tries++) {
178 if (btmrvl_sdio_read_fw_status(card, &firmwarestat) < 0)
179 continue;
181 if (firmwarestat == FIRMWARE_READY) {
182 ret = 0;
183 break;
184 } else {
185 msleep(10);
189 return ret;
192 static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
194 const struct firmware *fw_helper = NULL;
195 const u8 *helper = NULL;
196 int ret;
197 void *tmphlprbuf = NULL;
198 int tmphlprbufsz, hlprblknow, helperlen;
199 u8 *helperbuf;
200 u32 tx_len;
202 ret = request_firmware(&fw_helper, card->helper,
203 &card->func->dev);
204 if ((ret < 0) || !fw_helper) {
205 BT_ERR("request_firmware(helper) failed, error code = %d",
206 ret);
207 ret = -ENOENT;
208 goto done;
211 helper = fw_helper->data;
212 helperlen = fw_helper->size;
214 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
215 helperlen, SDIO_BLOCK_SIZE);
217 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
219 tmphlprbuf = kmalloc(tmphlprbufsz, GFP_KERNEL);
220 if (!tmphlprbuf) {
221 BT_ERR("Unable to allocate buffer for helper."
222 " Terminating download");
223 ret = -ENOMEM;
224 goto done;
227 memset(tmphlprbuf, 0, tmphlprbufsz);
229 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
231 /* Perform helper data transfer */
232 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
233 - SDIO_HEADER_LEN;
234 hlprblknow = 0;
236 do {
237 ret = btmrvl_sdio_poll_card_status(card,
238 CARD_IO_READY | DN_LD_CARD_RDY);
239 if (ret < 0) {
240 BT_ERR("Helper download poll status timeout @ %d",
241 hlprblknow);
242 goto done;
245 /* Check if there is more data? */
246 if (hlprblknow >= helperlen)
247 break;
249 if (helperlen - hlprblknow < tx_len)
250 tx_len = helperlen - hlprblknow;
252 /* Little-endian */
253 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
254 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
255 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
256 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
258 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
259 tx_len);
261 /* Now send the data */
262 ret = sdio_writesb(card->func, card->ioport, helperbuf,
263 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
264 if (ret < 0) {
265 BT_ERR("IO error during helper download @ %d",
266 hlprblknow);
267 goto done;
270 hlprblknow += tx_len;
271 } while (true);
273 BT_DBG("Transferring helper image EOF block");
275 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
277 ret = sdio_writesb(card->func, card->ioport, helperbuf,
278 SDIO_BLOCK_SIZE);
279 if (ret < 0) {
280 BT_ERR("IO error in writing helper image EOF block");
281 goto done;
284 ret = 0;
286 done:
287 kfree(tmphlprbuf);
288 if (fw_helper)
289 release_firmware(fw_helper);
291 return ret;
294 static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
296 const struct firmware *fw_firmware = NULL;
297 const u8 *firmware = NULL;
298 int firmwarelen, tmpfwbufsz, ret;
299 unsigned int tries, offset;
300 u8 base0, base1;
301 void *tmpfwbuf = NULL;
302 u8 *fwbuf;
303 u16 len;
304 int txlen = 0, tx_blocks = 0, count = 0;
306 ret = request_firmware(&fw_firmware, card->firmware,
307 &card->func->dev);
308 if ((ret < 0) || !fw_firmware) {
309 BT_ERR("request_firmware(firmware) failed, error code = %d",
310 ret);
311 ret = -ENOENT;
312 goto done;
315 firmware = fw_firmware->data;
316 firmwarelen = fw_firmware->size;
318 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
320 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
321 tmpfwbuf = kmalloc(tmpfwbufsz, GFP_KERNEL);
322 if (!tmpfwbuf) {
323 BT_ERR("Unable to allocate buffer for firmware."
324 " Terminating download");
325 ret = -ENOMEM;
326 goto done;
329 memset(tmpfwbuf, 0, tmpfwbufsz);
331 /* Ensure aligned firmware buffer */
332 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
334 /* Perform firmware data transfer */
335 offset = 0;
336 do {
337 ret = btmrvl_sdio_poll_card_status(card,
338 CARD_IO_READY | DN_LD_CARD_RDY);
339 if (ret < 0) {
340 BT_ERR("FW download with helper poll status"
341 " timeout @ %d", offset);
342 goto done;
345 /* Check if there is more data ? */
346 if (offset >= firmwarelen)
347 break;
349 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
350 base0 = sdio_readb(card->func,
351 SQ_READ_BASE_ADDRESS_A0_REG, &ret);
352 if (ret) {
353 BT_ERR("BASE0 register read failed:"
354 " base0 = 0x%04X(%d)."
355 " Terminating download",
356 base0, base0);
357 ret = -EIO;
358 goto done;
360 base1 = sdio_readb(card->func,
361 SQ_READ_BASE_ADDRESS_A1_REG, &ret);
362 if (ret) {
363 BT_ERR("BASE1 register read failed:"
364 " base1 = 0x%04X(%d)."
365 " Terminating download",
366 base1, base1);
367 ret = -EIO;
368 goto done;
371 len = (((u16) base1) << 8) | base0;
372 if (len)
373 break;
375 udelay(10);
378 if (!len)
379 break;
380 else if (len > BTM_UPLD_SIZE) {
381 BT_ERR("FW download failure @%d, invalid length %d",
382 offset, len);
383 ret = -EINVAL;
384 goto done;
387 txlen = len;
389 if (len & BIT(0)) {
390 count++;
391 if (count > MAX_WRITE_IOMEM_RETRY) {
392 BT_ERR("FW download failure @%d, "
393 "over max retry count", offset);
394 ret = -EIO;
395 goto done;
397 BT_ERR("FW CRC error indicated by the helper: "
398 "len = 0x%04X, txlen = %d", len, txlen);
399 len &= ~BIT(0);
400 /* Set txlen to 0 so as to resend from same offset */
401 txlen = 0;
402 } else {
403 count = 0;
405 /* Last block ? */
406 if (firmwarelen - offset < txlen)
407 txlen = firmwarelen - offset;
409 tx_blocks =
410 (txlen + SDIO_BLOCK_SIZE - 1) / SDIO_BLOCK_SIZE;
412 memcpy(fwbuf, &firmware[offset], txlen);
415 ret = sdio_writesb(card->func, card->ioport, fwbuf,
416 tx_blocks * SDIO_BLOCK_SIZE);
418 if (ret < 0) {
419 BT_ERR("FW download, writesb(%d) failed @%d",
420 count, offset);
421 sdio_writeb(card->func, HOST_CMD53_FIN, CONFIG_REG,
422 &ret);
423 if (ret)
424 BT_ERR("writeb failed (CFG)");
427 offset += txlen;
428 } while (true);
430 BT_DBG("FW download over, size %d bytes", offset);
432 ret = 0;
434 done:
435 kfree(tmpfwbuf);
437 if (fw_firmware)
438 release_firmware(fw_firmware);
440 return ret;
443 static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
445 u16 buf_len = 0;
446 int ret, buf_block_len, blksz;
447 struct sk_buff *skb = NULL;
448 u32 type;
449 u8 *payload = NULL;
450 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
451 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
453 if (!card || !card->func) {
454 BT_ERR("card or function is NULL!");
455 ret = -EINVAL;
456 goto exit;
459 /* Read the length of data to be transferred */
460 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
461 if (ret < 0) {
462 BT_ERR("read rx_len failed");
463 ret = -EIO;
464 goto exit;
467 blksz = SDIO_BLOCK_SIZE;
468 buf_block_len = (buf_len + blksz - 1) / blksz;
470 if (buf_len <= SDIO_HEADER_LEN
471 || (buf_block_len * blksz) > ALLOC_BUF_SIZE) {
472 BT_ERR("invalid packet length: %d", buf_len);
473 ret = -EINVAL;
474 goto exit;
477 /* Allocate buffer */
478 skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN,
479 GFP_ATOMIC);
480 if (skb == NULL) {
481 BT_ERR("No free skb");
482 goto exit;
485 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
486 skb_put(skb, (unsigned long) skb->data &
487 (BTSDIO_DMA_ALIGN - 1));
488 skb_pull(skb, (unsigned long) skb->data &
489 (BTSDIO_DMA_ALIGN - 1));
492 payload = skb->data;
494 ret = sdio_readsb(card->func, payload, card->ioport,
495 buf_block_len * blksz);
496 if (ret < 0) {
497 BT_ERR("readsb failed: %d", ret);
498 ret = -EIO;
499 goto exit;
502 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
503 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
506 buf_len = payload[0];
507 buf_len |= (u16) payload[1] << 8;
508 type = payload[3];
510 switch (type) {
511 case HCI_ACLDATA_PKT:
512 case HCI_SCODATA_PKT:
513 case HCI_EVENT_PKT:
514 bt_cb(skb)->pkt_type = type;
515 skb->dev = (void *)hdev;
516 skb_put(skb, buf_len);
517 skb_pull(skb, SDIO_HEADER_LEN);
519 if (type == HCI_EVENT_PKT)
520 btmrvl_check_evtpkt(priv, skb);
522 hci_recv_frame(skb);
523 hdev->stat.byte_rx += buf_len;
524 break;
526 case MRVL_VENDOR_PKT:
527 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
528 skb->dev = (void *)hdev;
529 skb_put(skb, buf_len);
530 skb_pull(skb, SDIO_HEADER_LEN);
532 if (btmrvl_process_event(priv, skb))
533 hci_recv_frame(skb);
535 hdev->stat.byte_rx += buf_len;
536 break;
538 default:
539 BT_ERR("Unknown packet type:%d", type);
540 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload,
541 blksz * buf_block_len);
543 kfree_skb(skb);
544 skb = NULL;
545 break;
548 exit:
549 if (ret) {
550 hdev->stat.err_rx++;
551 if (skb)
552 kfree_skb(skb);
555 return ret;
558 static int btmrvl_sdio_get_int_status(struct btmrvl_private *priv, u8 * ireg)
560 int ret;
561 u8 sdio_ireg = 0;
562 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
564 *ireg = 0;
566 sdio_ireg = sdio_readb(card->func, HOST_INTSTATUS_REG, &ret);
567 if (ret) {
568 BT_ERR("sdio_readb: read int status register failed");
569 ret = -EIO;
570 goto done;
573 if (sdio_ireg != 0) {
575 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
576 * Clear the interrupt status register and re-enable the
577 * interrupt.
579 BT_DBG("sdio_ireg = 0x%x", sdio_ireg);
581 sdio_writeb(card->func, ~(sdio_ireg) & (DN_LD_HOST_INT_STATUS |
582 UP_LD_HOST_INT_STATUS),
583 HOST_INTSTATUS_REG, &ret);
584 if (ret) {
585 BT_ERR("sdio_writeb: clear int status register "
586 "failed");
587 ret = -EIO;
588 goto done;
592 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
593 if (priv->btmrvl_dev.tx_dnld_rdy)
594 BT_DBG("tx_done already received: "
595 " int_status=0x%x", sdio_ireg);
596 else
597 priv->btmrvl_dev.tx_dnld_rdy = true;
600 if (sdio_ireg & UP_LD_HOST_INT_STATUS)
601 btmrvl_sdio_card_to_host(priv);
603 *ireg = sdio_ireg;
605 ret = 0;
607 done:
608 return ret;
611 static void btmrvl_sdio_interrupt(struct sdio_func *func)
613 struct btmrvl_private *priv;
614 struct hci_dev *hcidev;
615 struct btmrvl_sdio_card *card;
616 u8 ireg = 0;
618 card = sdio_get_drvdata(func);
619 if (card && card->priv) {
620 priv = card->priv;
621 hcidev = priv->btmrvl_dev.hcidev;
623 if (btmrvl_sdio_get_int_status(priv, &ireg))
624 BT_ERR("reading HOST_INT_STATUS_REG failed");
625 else
626 BT_DBG("HOST_INT_STATUS_REG %#x", ireg);
628 btmrvl_interrupt(priv);
632 static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
634 struct sdio_func *func;
635 u8 reg;
636 int ret = 0;
638 if (!card || !card->func) {
639 BT_ERR("Error: card or function is NULL!");
640 ret = -EINVAL;
641 goto failed;
644 func = card->func;
646 sdio_claim_host(func);
648 ret = sdio_enable_func(func);
649 if (ret) {
650 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
651 ret = -EIO;
652 goto release_host;
655 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
656 if (ret) {
657 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
658 ret = -EIO;
659 goto disable_func;
662 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
663 if (ret) {
664 BT_ERR("cannot set SDIO block size");
665 ret = -EIO;
666 goto release_irq;
669 reg = sdio_readb(func, IO_PORT_0_REG, &ret);
670 if (ret < 0) {
671 ret = -EIO;
672 goto release_irq;
675 card->ioport = reg;
677 reg = sdio_readb(func, IO_PORT_1_REG, &ret);
678 if (ret < 0) {
679 ret = -EIO;
680 goto release_irq;
683 card->ioport |= (reg << 8);
685 reg = sdio_readb(func, IO_PORT_2_REG, &ret);
686 if (ret < 0) {
687 ret = -EIO;
688 goto release_irq;
691 card->ioport |= (reg << 16);
693 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
695 sdio_set_drvdata(func, card);
697 sdio_release_host(func);
699 return 0;
701 release_irq:
702 sdio_release_irq(func);
704 disable_func:
705 sdio_disable_func(func);
707 release_host:
708 sdio_release_host(func);
710 failed:
711 return ret;
714 static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
716 if (card && card->func) {
717 sdio_claim_host(card->func);
718 sdio_release_irq(card->func);
719 sdio_disable_func(card->func);
720 sdio_release_host(card->func);
721 sdio_set_drvdata(card->func, NULL);
724 return 0;
727 static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
729 int ret;
731 if (!card || !card->func)
732 return -EINVAL;
734 sdio_claim_host(card->func);
736 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
738 btmrvl_sdio_get_rx_unit(card);
740 sdio_release_host(card->func);
742 return ret;
745 static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
747 int ret;
749 if (!card || !card->func)
750 return -EINVAL;
752 sdio_claim_host(card->func);
754 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
756 sdio_release_host(card->func);
758 return ret;
761 static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
762 u8 *payload, u16 nb)
764 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
765 int ret = 0;
766 int buf_block_len;
767 int blksz;
768 int i = 0;
769 u8 *buf = NULL;
770 void *tmpbuf = NULL;
771 int tmpbufsz;
773 if (!card || !card->func) {
774 BT_ERR("card or function is NULL!");
775 return -EINVAL;
778 buf = payload;
779 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
780 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
781 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
782 if (!tmpbuf)
783 return -ENOMEM;
784 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
785 memcpy(buf, payload, nb);
788 blksz = SDIO_BLOCK_SIZE;
789 buf_block_len = (nb + blksz - 1) / blksz;
791 sdio_claim_host(card->func);
793 do {
794 /* Transfer data to card */
795 ret = sdio_writesb(card->func, card->ioport, buf,
796 buf_block_len * blksz);
797 if (ret < 0) {
798 i++;
799 BT_ERR("i=%d writesb failed: %d", i, ret);
800 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
801 payload, nb);
802 ret = -EIO;
803 if (i > MAX_WRITE_IOMEM_RETRY)
804 goto exit;
806 } while (ret);
808 priv->btmrvl_dev.tx_dnld_rdy = false;
810 exit:
811 sdio_release_host(card->func);
812 kfree(tmpbuf);
814 return ret;
817 static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
819 int ret = 0;
821 if (!card || !card->func) {
822 BT_ERR("card or function is NULL!");
823 return -EINVAL;
825 sdio_claim_host(card->func);
827 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
828 BT_DBG("Firmware already downloaded!");
829 goto done;
832 ret = btmrvl_sdio_download_helper(card);
833 if (ret) {
834 BT_ERR("Failed to download helper!");
835 ret = -EIO;
836 goto done;
839 if (btmrvl_sdio_download_fw_w_helper(card)) {
840 BT_ERR("Failed to download firmware!");
841 ret = -EIO;
842 goto done;
845 if (btmrvl_sdio_verify_fw_download(card, MAX_POLL_TRIES)) {
846 BT_ERR("FW failed to be active in time!");
847 ret = -ETIMEDOUT;
848 goto done;
851 done:
852 sdio_release_host(card->func);
854 return ret;
857 static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
859 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
860 int ret = 0;
862 if (!card || !card->func) {
863 BT_ERR("card or function is NULL!");
864 return -EINVAL;
867 sdio_claim_host(card->func);
869 sdio_writeb(card->func, HOST_POWER_UP, CONFIG_REG, &ret);
871 sdio_release_host(card->func);
873 BT_DBG("wake up firmware");
875 return ret;
878 static int btmrvl_sdio_probe(struct sdio_func *func,
879 const struct sdio_device_id *id)
881 int ret = 0;
882 struct btmrvl_private *priv = NULL;
883 struct btmrvl_sdio_card *card = NULL;
885 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
886 id->vendor, id->device, id->class, func->num);
888 card = kzalloc(sizeof(*card), GFP_KERNEL);
889 if (!card) {
890 ret = -ENOMEM;
891 goto done;
894 card->func = func;
896 if (id->driver_data) {
897 struct btmrvl_sdio_device *data = (void *) id->driver_data;
898 card->helper = data->helper;
899 card->firmware = data->firmware;
902 if (btmrvl_sdio_register_dev(card) < 0) {
903 BT_ERR("Failed to register BT device!");
904 ret = -ENODEV;
905 goto free_card;
908 /* Disable the interrupts on the card */
909 btmrvl_sdio_disable_host_int(card);
911 if (btmrvl_sdio_download_fw(card)) {
912 BT_ERR("Downloading firmware failed!");
913 ret = -ENODEV;
914 goto unreg_dev;
917 msleep(100);
919 btmrvl_sdio_enable_host_int(card);
921 priv = btmrvl_add_card(card);
922 if (!priv) {
923 BT_ERR("Initializing card failed!");
924 ret = -ENODEV;
925 goto disable_host_int;
928 card->priv = priv;
930 /* Initialize the interface specific function pointers */
931 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
932 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
934 if (btmrvl_register_hdev(priv)) {
935 BT_ERR("Register hdev failed!");
936 ret = -ENODEV;
937 goto disable_host_int;
940 priv->btmrvl_dev.psmode = 1;
941 btmrvl_enable_ps(priv);
943 return 0;
945 disable_host_int:
946 btmrvl_sdio_disable_host_int(card);
947 unreg_dev:
948 btmrvl_sdio_unregister_dev(card);
949 free_card:
950 kfree(card);
951 done:
952 return ret;
955 static void btmrvl_sdio_remove(struct sdio_func *func)
957 struct btmrvl_sdio_card *card;
959 if (func) {
960 card = sdio_get_drvdata(func);
961 if (card) {
962 /* Send SHUTDOWN command & disable interrupt
963 * if user removes the module.
965 if (user_rmmod) {
966 btmrvl_send_module_cfg_cmd(card->priv,
967 MODULE_SHUTDOWN_REQ);
968 btmrvl_sdio_disable_host_int(card);
970 BT_DBG("unregester dev");
971 btmrvl_sdio_unregister_dev(card);
972 btmrvl_remove_card(card->priv);
973 kfree(card);
978 static struct sdio_driver bt_mrvl_sdio = {
979 .name = "btmrvl_sdio",
980 .id_table = btmrvl_sdio_ids,
981 .probe = btmrvl_sdio_probe,
982 .remove = btmrvl_sdio_remove,
985 static int __init btmrvl_sdio_init_module(void)
987 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
988 BT_ERR("SDIO Driver Registration Failed");
989 return -ENODEV;
992 /* Clear the flag in case user removes the card. */
993 user_rmmod = 0;
995 return 0;
998 static void __exit btmrvl_sdio_exit_module(void)
1000 /* Set the flag as user is removing this module. */
1001 user_rmmod = 1;
1003 sdio_unregister_driver(&bt_mrvl_sdio);
1006 module_init(btmrvl_sdio_init_module);
1007 module_exit(btmrvl_sdio_exit_module);
1009 MODULE_AUTHOR("Marvell International Ltd.");
1010 MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
1011 MODULE_VERSION(VERSION);
1012 MODULE_LICENSE("GPL v2");
1013 MODULE_FIRMWARE("sd8688_helper.bin");
1014 MODULE_FIRMWARE("sd8688.bin");