2 * Marvell Wireless LAN device driver: SDIO specific handling
4 * Copyright (C) 2011, 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.
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
17 * this warranty disclaimer.
20 #include <linux/firmware.h>
32 #define SDIO_VERSION "1.0"
34 /* The mwifiex_sdio_remove() callback function is called when
35 * user removes this module from kernel space or ejects
36 * the card from the slot. The driver handles these 2 cases
38 * If the user is removing the module, the few commands (FUNC_SHUTDOWN,
39 * HS_CANCEL etc.) are sent to the firmware.
40 * If the card is removed, there is no need to send these command.
42 * The variable 'user_rmmod' is used to distinguish these two
43 * scenarios. This flag is initialized as FALSE in case the card
44 * is removed, and will be set to TRUE for module removal when
45 * module_exit function is called.
49 static struct mwifiex_if_ops sdio_ops;
51 static struct semaphore add_remove_card_sem;
53 static int mwifiex_sdio_resume(struct device *dev);
58 * This function probes an mwifiex device and registers it. It allocates
59 * the card structure, enables SDIO function number and initiates the
60 * device registration and initialization procedure by adding a logical
64 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
67 struct sdio_mmc_card *card = NULL;
69 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
70 func->vendor, func->device, func->class, func->num);
72 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
78 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
80 sdio_claim_host(func);
81 ret = sdio_enable_func(func);
82 sdio_release_host(func);
85 pr_err("%s: failed to enable function\n", __func__);
90 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
92 pr_err("%s: add card failed\n", __func__);
94 sdio_claim_host(func);
95 ret = sdio_disable_func(func);
96 sdio_release_host(func);
106 * This function removes the interface and frees up the card structure.
109 mwifiex_sdio_remove(struct sdio_func *func)
111 struct sdio_mmc_card *card;
112 struct mwifiex_adapter *adapter;
113 struct mwifiex_private *priv;
116 pr_debug("info: SDIO func num=%d\n", func->num);
118 card = sdio_get_drvdata(func);
122 adapter = card->adapter;
123 if (!adapter || !adapter->priv_num)
126 /* In case driver is removed when asynchronous FW load is in progress */
127 wait_for_completion(&adapter->fw_load);
130 if (adapter->is_suspended)
131 mwifiex_sdio_resume(adapter->dev);
133 for (i = 0; i < adapter->priv_num; i++)
134 if ((GET_BSS_ROLE(adapter->priv[i]) ==
135 MWIFIEX_BSS_ROLE_STA) &&
136 adapter->priv[i]->media_connected)
137 mwifiex_deauthenticate(adapter->priv[i], NULL);
139 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
140 mwifiex_disable_auto_ds(priv);
141 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
144 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
151 * Kernel needs to suspend all functions separately. Therefore all
152 * registered functions must have drivers with suspend and resume
153 * methods. Failing that the kernel simply removes the whole card.
155 * If already not suspended, this function allocates and sends a host
156 * sleep activate request to the firmware and turns off the traffic.
158 static int mwifiex_sdio_suspend(struct device *dev)
160 struct sdio_func *func = dev_to_sdio_func(dev);
161 struct sdio_mmc_card *card;
162 struct mwifiex_adapter *adapter;
163 mmc_pm_flag_t pm_flag = 0;
167 pm_flag = sdio_get_host_pm_caps(func);
168 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
169 sdio_func_id(func), pm_flag);
170 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
171 pr_err("%s: cannot remain alive while host is"
172 " suspended\n", sdio_func_id(func));
176 card = sdio_get_drvdata(func);
177 if (!card || !card->adapter) {
178 pr_err("suspend: invalid card or adapter\n");
182 pr_err("suspend: sdio_func is not specified\n");
186 adapter = card->adapter;
188 /* Enable the Host Sleep */
189 if (!mwifiex_enable_hs(adapter)) {
190 dev_err(adapter->dev, "cmd: failed to suspend\n");
194 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
195 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
197 /* Indicate device suspended */
198 adapter->is_suspended = true;
206 * Kernel needs to suspend all functions separately. Therefore all
207 * registered functions must have drivers with suspend and resume
208 * methods. Failing that the kernel simply removes the whole card.
210 * If already not resumed, this function turns on the traffic and
211 * sends a host sleep cancel request to the firmware.
213 static int mwifiex_sdio_resume(struct device *dev)
215 struct sdio_func *func = dev_to_sdio_func(dev);
216 struct sdio_mmc_card *card;
217 struct mwifiex_adapter *adapter;
218 mmc_pm_flag_t pm_flag = 0;
221 pm_flag = sdio_get_host_pm_caps(func);
222 card = sdio_get_drvdata(func);
223 if (!card || !card->adapter) {
224 pr_err("resume: invalid card or adapter\n");
228 pr_err("resume: sdio_func is not specified\n");
232 adapter = card->adapter;
234 if (!adapter->is_suspended) {
235 dev_warn(adapter->dev, "device already resumed\n");
239 adapter->is_suspended = false;
241 /* Disable Host Sleep */
242 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
248 /* Device ID for SD8786 */
249 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
250 /* Device ID for SD8787 */
251 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
252 /* Device ID for SD8797 */
253 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
256 static const struct sdio_device_id mwifiex_ids[] = {
257 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786)},
258 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787)},
259 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797)},
263 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
265 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
266 .suspend = mwifiex_sdio_suspend,
267 .resume = mwifiex_sdio_resume,
270 static struct sdio_driver mwifiex_sdio = {
271 .name = "mwifiex_sdio",
272 .id_table = mwifiex_ids,
273 .probe = mwifiex_sdio_probe,
274 .remove = mwifiex_sdio_remove,
276 .owner = THIS_MODULE,
277 .pm = &mwifiex_sdio_pm_ops,
282 * This function writes data into SDIO card register.
285 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u32 data)
287 struct sdio_mmc_card *card = adapter->card;
290 sdio_claim_host(card->func);
291 sdio_writeb(card->func, (u8) data, reg, &ret);
292 sdio_release_host(card->func);
298 * This function reads data from SDIO card register.
301 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u32 *data)
303 struct sdio_mmc_card *card = adapter->card;
307 sdio_claim_host(card->func);
308 val = sdio_readb(card->func, reg, &ret);
309 sdio_release_host(card->func);
317 * This function writes multiple data into SDIO card memory.
319 * This does not work in suspended mode.
322 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
323 u8 *buffer, u32 pkt_len, u32 port)
325 struct sdio_mmc_card *card = adapter->card;
328 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
329 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
332 BLOCK_MODE) ? (pkt_len /
333 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
334 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
336 if (adapter->is_suspended) {
337 dev_err(adapter->dev,
338 "%s: not allowed while suspended\n", __func__);
342 sdio_claim_host(card->func);
344 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
346 sdio_release_host(card->func);
352 * This function reads multiple data from SDIO card memory.
354 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
355 u32 len, u32 port, u8 claim)
357 struct sdio_mmc_card *card = adapter->card;
359 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
361 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
362 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
364 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
367 sdio_claim_host(card->func);
369 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
372 sdio_release_host(card->func);
378 * This function wakes up the card.
380 * A host power up command is written to the card configuration
381 * register to wake up the card.
383 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
385 dev_dbg(adapter->dev, "event: wakeup device...\n");
387 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
391 * This function is called after the card has woken up.
393 * The card configuration register is reset.
395 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
397 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
399 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
403 * This function initializes the IO ports.
405 * The following operations are performed -
406 * - Read the IO ports (0, 1 and 2)
407 * - Set host interrupt Reset-To-Read to clear
408 * - Set auto re-enable interrupt
410 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
416 /* Read the IO port */
417 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, ®))
418 adapter->ioport |= (reg & 0xff);
422 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, ®))
423 adapter->ioport |= ((reg & 0xff) << 8);
427 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, ®))
428 adapter->ioport |= ((reg & 0xff) << 16);
432 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
434 /* Set Host interrupt reset to read to clear */
435 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, ®))
436 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
437 reg | SDIO_INT_MASK);
441 /* Dnld/Upld ready set to auto reset */
442 if (!mwifiex_read_reg(adapter, CARD_MISC_CFG_REG, ®))
443 mwifiex_write_reg(adapter, CARD_MISC_CFG_REG,
444 reg | AUTO_RE_ENABLE_INT);
452 * This function sends data to the card.
454 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
455 u8 *payload, u32 pkt_len, u32 port)
461 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
464 dev_err(adapter->dev, "host_to_card, write iomem"
465 " (%d) failed: %d\n", i, ret);
466 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
467 dev_err(adapter->dev, "write CFG reg failed\n");
470 if (i > MAX_WRITE_IOMEM_RETRY)
479 * This function gets the read port.
481 * If control port bit is set in MP read bitmap, the control port
482 * is returned, otherwise the current read port is returned and
483 * the value is increased (provided it does not reach the maximum
484 * limit, in which case it is reset to 1)
486 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
488 struct sdio_mmc_card *card = adapter->card;
489 u16 rd_bitmap = card->mp_rd_bitmap;
491 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%04x\n", rd_bitmap);
493 if (!(rd_bitmap & (CTRL_PORT_MASK | DATA_PORT_MASK)))
496 if (card->mp_rd_bitmap & CTRL_PORT_MASK) {
497 card->mp_rd_bitmap &= (u16) (~CTRL_PORT_MASK);
499 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%04x\n",
500 *port, card->mp_rd_bitmap);
502 if (card->mp_rd_bitmap & (1 << card->curr_rd_port)) {
503 card->mp_rd_bitmap &= (u16)
504 (~(1 << card->curr_rd_port));
505 *port = card->curr_rd_port;
507 if (++card->curr_rd_port == MAX_PORT)
508 card->curr_rd_port = 1;
513 dev_dbg(adapter->dev,
514 "data: port=%d mp_rd_bitmap=0x%04x -> 0x%04x\n",
515 *port, rd_bitmap, card->mp_rd_bitmap);
521 * This function gets the write port for data.
523 * The current write port is returned if available and the value is
524 * increased (provided it does not reach the maximum limit, in which
525 * case it is reset to 1)
527 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u8 *port)
529 struct sdio_mmc_card *card = adapter->card;
530 u16 wr_bitmap = card->mp_wr_bitmap;
532 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%04x\n", wr_bitmap);
534 if (!(wr_bitmap & card->mp_data_port_mask))
537 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
538 card->mp_wr_bitmap &= (u16) (~(1 << card->curr_wr_port));
539 *port = card->curr_wr_port;
540 if (++card->curr_wr_port == card->mp_end_port)
541 card->curr_wr_port = 1;
543 adapter->data_sent = true;
547 if (*port == CTRL_PORT) {
548 dev_err(adapter->dev, "invalid data port=%d cur port=%d"
549 " mp_wr_bitmap=0x%04x -> 0x%04x\n",
550 *port, card->curr_wr_port, wr_bitmap,
555 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%04x -> 0x%04x\n",
556 *port, wr_bitmap, card->mp_wr_bitmap);
562 * This function polls the card status.
565 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
570 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
571 if (mwifiex_read_reg(adapter, CARD_STATUS_REG, &cs))
573 else if ((cs & bits) == bits)
576 usleep_range(10, 20);
579 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
585 * This function reads the firmware status.
588 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
592 if (mwifiex_read_reg(adapter, CARD_FW_STATUS0_REG, &fws0))
595 if (mwifiex_read_reg(adapter, CARD_FW_STATUS1_REG, &fws1))
598 *dat = (u16) ((fws1 << 8) | fws0);
604 * This function disables the host interrupt.
606 * The host interrupt mask is read, the disable bit is reset and
607 * written back to the card host interrupt mask register.
609 static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
613 /* Read back the host_int_mask register */
614 if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
617 /* Update with the mask and write back to the register */
618 host_int_mask &= ~HOST_INT_DISABLE;
620 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
621 dev_err(adapter->dev, "disable host interrupt failed\n");
629 * This function enables the host interrupt.
631 * The host interrupt enable mask is written to the card
632 * host interrupt mask register.
634 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
636 /* Simply write the mask to the register */
637 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, HOST_INT_ENABLE)) {
638 dev_err(adapter->dev, "enable host interrupt failed\n");
645 * This function sends a data buffer to the card.
647 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
648 u32 *type, u8 *buffer,
649 u32 npayload, u32 ioport)
655 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
659 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
662 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
667 nb = le16_to_cpu(*(__le16 *) (buffer));
669 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
670 __func__, nb, npayload);
674 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
680 * This function downloads the firmware to the card.
682 * Firmware is downloaded to the card in blocks. Every block download
683 * is tested for CRC errors, and retried a number of times before
686 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
687 struct mwifiex_fw_image *fw)
690 u8 *firmware = fw->fw_buf;
691 u32 firmware_len = fw->fw_len;
696 u32 txlen, tx_blocks = 0, tries;
700 dev_err(adapter->dev,
701 "firmware image not found! Terminating download\n");
705 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
708 /* Assume that the allocated buffer is 8-byte aligned */
709 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
713 /* Perform firmware data transfer */
715 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
717 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
720 dev_err(adapter->dev, "FW download with helper:"
721 " poll status timeout @ %d\n", offset);
726 if (offset >= firmware_len)
729 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
730 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_0,
733 dev_err(adapter->dev,
734 "dev BASE0 register read failed: "
735 "base0=%#04X(%d). Terminating dnld\n",
739 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_1,
742 dev_err(adapter->dev,
743 "dev BASE1 register read failed: "
744 "base1=%#04X(%d). Terminating dnld\n",
748 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
753 usleep_range(10, 20);
758 } else if (len > MWIFIEX_UPLD_SIZE) {
759 dev_err(adapter->dev,
760 "FW dnld failed @ %d, invalid length %d\n",
770 if (i > MAX_WRITE_IOMEM_RETRY) {
771 dev_err(adapter->dev,
772 "FW dnld failed @ %d, over max retry\n",
777 dev_err(adapter->dev, "CRC indicated by the helper:"
778 " len = 0x%04X, txlen = %d\n", len, txlen);
780 /* Setting this to 0 to resend from same offset */
785 /* Set blocksize to transfer - checking for last
787 if (firmware_len - offset < txlen)
788 txlen = firmware_len - offset;
790 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
791 / MWIFIEX_SDIO_BLOCK_SIZE;
793 /* Copy payload to buffer */
794 memmove(fwbuf, &firmware[offset], txlen);
797 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
798 MWIFIEX_SDIO_BLOCK_SIZE,
801 dev_err(adapter->dev,
802 "FW download, write iomem (%d) failed @ %d\n",
804 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
805 dev_err(adapter->dev, "write CFG reg failed\n");
814 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
824 * This function checks the firmware status in card.
826 * The winner interface is also determined by this function.
828 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
836 /* Wait for firmware initialization event */
837 for (tries = 0; tries < poll_num; tries++) {
838 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
841 if (firmware_stat == FIRMWARE_READY_SDIO) {
852 (adapter, CARD_FW_STATUS0_REG, &winner_status))
864 * This function reads the interrupt status from card.
866 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
868 struct sdio_mmc_card *card = adapter->card;
872 if (mwifiex_read_data_sync(adapter, card->mp_regs, MAX_MP_REGS,
873 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK,
875 dev_err(adapter->dev, "read mp_regs failed\n");
879 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
882 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
883 * Clear the interrupt status register
885 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
886 spin_lock_irqsave(&adapter->int_lock, flags);
887 adapter->int_status |= sdio_ireg;
888 spin_unlock_irqrestore(&adapter->int_lock, flags);
893 * SDIO interrupt handler.
895 * This function reads the interrupt status from firmware and handles
896 * the interrupt in current thread (ksdioirqd) right away.
899 mwifiex_sdio_interrupt(struct sdio_func *func)
901 struct mwifiex_adapter *adapter;
902 struct sdio_mmc_card *card;
904 card = sdio_get_drvdata(func);
905 if (!card || !card->adapter) {
906 pr_debug("int: func=%p card=%p adapter=%p\n",
907 func, card, card ? card->adapter : NULL);
910 adapter = card->adapter;
912 if (adapter->surprise_removed)
915 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
916 adapter->ps_state = PS_STATE_AWAKE;
918 mwifiex_interrupt_status(adapter);
919 mwifiex_main_process(adapter);
923 * This function decodes a received packet.
925 * Based on the type, the packet is treated as either a data, or
926 * a command response, or an event, and the correct handler
927 * function is invoked.
929 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
930 struct sk_buff *skb, u32 upld_typ)
933 __le16 *curr_ptr = (__le16 *)skb->data;
934 u16 pkt_len = le16_to_cpu(*curr_ptr);
936 skb_trim(skb, pkt_len);
937 skb_pull(skb, INTF_HEADER_LEN);
940 case MWIFIEX_TYPE_DATA:
941 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
942 mwifiex_handle_rx_packet(adapter, skb);
945 case MWIFIEX_TYPE_CMD:
946 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
947 /* take care of curr_cmd = NULL case */
948 if (!adapter->curr_cmd) {
949 cmd_buf = adapter->upld_buf;
951 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
952 mwifiex_process_sleep_confirm_resp(adapter,
956 memcpy(cmd_buf, skb->data,
957 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
960 dev_kfree_skb_any(skb);
962 adapter->cmd_resp_received = true;
963 adapter->curr_cmd->resp_skb = skb;
967 case MWIFIEX_TYPE_EVENT:
968 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
969 adapter->event_cause = *(u32 *) skb->data;
971 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
972 memcpy(adapter->event_body,
973 skb->data + MWIFIEX_EVENT_HEADER_LEN,
976 /* event cause has been saved to adapter->event_cause */
977 adapter->event_received = true;
978 adapter->event_skb = skb;
983 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
984 dev_kfree_skb_any(skb);
992 * This function transfers received packets from card to driver, performing
993 * aggregation if required.
995 * For data received on control port, or if aggregation is disabled, the
996 * received buffers are uploaded as separate packets. However, if aggregation
997 * is enabled and required, the buffers are copied onto an aggregation buffer,
998 * provided there is space left, processed and finally uploaded.
1000 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1001 struct sk_buff *skb, u8 port)
1003 struct sdio_mmc_card *card = adapter->card;
1004 s32 f_do_rx_aggr = 0;
1005 s32 f_do_rx_cur = 0;
1007 struct sk_buff *skb_deaggr;
1009 u32 pkt_len, pkt_type = 0;
1011 u32 rx_len = skb->len;
1013 if (port == CTRL_PORT) {
1014 /* Read the command Resp without aggr */
1015 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1016 "response\n", __func__);
1019 goto rx_curr_single;
1022 if (!card->mpa_rx.enabled) {
1023 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1027 goto rx_curr_single;
1030 if (card->mp_rd_bitmap & (~((u16) CTRL_PORT_MASK))) {
1031 /* Some more data RX pending */
1032 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1034 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1035 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1038 /* No room in Aggr buf, do rx aggr now */
1043 /* Rx aggr not in progress */
1048 /* No more data RX pending */
1049 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1051 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1053 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1056 /* No room in Aggr buf, do rx aggr now */
1064 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1065 /* Curr pkt can be aggregated */
1066 MP_RX_AGGR_SETUP(card, skb, port);
1068 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1069 MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
1070 dev_dbg(adapter->dev, "info: %s: aggregated packet "
1071 "limit reached\n", __func__);
1072 /* No more pkts allowed in Aggr buf, rx it */
1078 /* do aggr RX now */
1079 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1080 card->mpa_rx.pkt_cnt);
1082 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1083 card->mpa_rx.buf_len,
1084 (adapter->ioport | 0x1000 |
1085 (card->mpa_rx.ports << 4)) +
1086 card->mpa_rx.start_port, 1))
1089 curr_ptr = card->mpa_rx.buf;
1091 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1093 /* get curr PKT len & type */
1094 pkt_len = *(u16 *) &curr_ptr[0];
1095 pkt_type = *(u16 *) &curr_ptr[2];
1097 /* copy pkt to deaggr buf */
1098 skb_deaggr = card->mpa_rx.skb_arr[pind];
1100 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1101 card->mpa_rx.len_arr[pind])) {
1103 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1105 skb_trim(skb_deaggr, pkt_len);
1107 /* Process de-aggr packet */
1108 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1111 dev_err(adapter->dev, "wrong aggr pkt:"
1112 " type=%d len=%d max_len=%d\n",
1114 card->mpa_rx.len_arr[pind]);
1115 dev_kfree_skb_any(skb_deaggr);
1117 curr_ptr += card->mpa_rx.len_arr[pind];
1119 MP_RX_AGGR_BUF_RESET(card);
1124 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1127 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1128 skb->data, skb->len,
1129 adapter->ioport + port))
1132 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1138 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1139 /* Multiport-aggregation transfer failed - cleanup */
1140 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1141 /* copy pkt to deaggr buf */
1142 skb_deaggr = card->mpa_rx.skb_arr[pind];
1143 dev_kfree_skb_any(skb_deaggr);
1145 MP_RX_AGGR_BUF_RESET(card);
1149 /* Single transfer pending. Free curr buff also */
1150 dev_kfree_skb_any(skb);
1156 * This function checks the current interrupt status.
1158 * The following interrupts are checked and handled by this function -
1161 * - Packets received
1163 * Since the firmware does not generate download ready interrupt if the
1164 * port updated is command port only, command sent interrupt checking
1165 * should be done manually, and for every SDIO interrupt.
1167 * In case of Rx packets received, the packets are uploaded from card to
1168 * host and processed accordingly.
1170 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1172 struct sdio_mmc_card *card = adapter->card;
1175 struct sk_buff *skb;
1176 u8 port = CTRL_PORT;
1177 u32 len_reg_l, len_reg_u;
1180 unsigned long flags;
1182 spin_lock_irqsave(&adapter->int_lock, flags);
1183 sdio_ireg = adapter->int_status;
1184 adapter->int_status = 0;
1185 spin_unlock_irqrestore(&adapter->int_lock, flags);
1190 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1191 card->mp_wr_bitmap = ((u16) card->mp_regs[WR_BITMAP_U]) << 8;
1192 card->mp_wr_bitmap |= (u16) card->mp_regs[WR_BITMAP_L];
1193 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%04x\n",
1194 card->mp_wr_bitmap);
1195 if (adapter->data_sent &&
1196 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1197 dev_dbg(adapter->dev,
1198 "info: <--- Tx DONE Interrupt --->\n");
1199 adapter->data_sent = false;
1203 /* As firmware will not generate download ready interrupt if the port
1204 updated is command port only, cmd_sent should be done for any SDIO
1206 if (adapter->cmd_sent) {
1207 /* Check if firmware has attach buffer at command port and
1208 update just that in wr_bit_map. */
1209 card->mp_wr_bitmap |=
1210 (u16) card->mp_regs[WR_BITMAP_L] & CTRL_PORT_MASK;
1211 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1212 adapter->cmd_sent = false;
1215 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1216 adapter->cmd_sent, adapter->data_sent);
1217 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1218 card->mp_rd_bitmap = ((u16) card->mp_regs[RD_BITMAP_U]) << 8;
1219 card->mp_rd_bitmap |= (u16) card->mp_regs[RD_BITMAP_L];
1220 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%04x\n",
1221 card->mp_rd_bitmap);
1224 ret = mwifiex_get_rd_port(adapter, &port);
1226 dev_dbg(adapter->dev,
1227 "info: no more rd_port available\n");
1230 len_reg_l = RD_LEN_P0_L + (port << 1);
1231 len_reg_u = RD_LEN_P0_U + (port << 1);
1232 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1233 rx_len |= (u16) card->mp_regs[len_reg_l];
1234 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1237 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1238 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1239 if (rx_len <= INTF_HEADER_LEN ||
1240 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1241 MWIFIEX_RX_DATA_BUF_SIZE) {
1242 dev_err(adapter->dev, "invalid rx_len=%d\n",
1246 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1248 skb = dev_alloc_skb(rx_len);
1251 dev_err(adapter->dev, "%s: failed to alloc skb",
1256 skb_put(skb, rx_len);
1258 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1261 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1265 dev_err(adapter->dev, "card_to_host_mpa failed:"
1266 " int status=%#x\n", sdio_ireg);
1267 if (mwifiex_read_reg(adapter,
1268 CONFIGURATION_REG, &cr))
1269 dev_err(adapter->dev,
1270 "read CFG reg failed\n");
1272 dev_dbg(adapter->dev,
1273 "info: CFG reg val = %d\n", cr);
1274 if (mwifiex_write_reg(adapter,
1277 dev_err(adapter->dev,
1278 "write CFG reg failed\n");
1280 dev_dbg(adapter->dev, "info: write success\n");
1281 if (mwifiex_read_reg(adapter,
1282 CONFIGURATION_REG, &cr))
1283 dev_err(adapter->dev,
1284 "read CFG reg failed\n");
1286 dev_dbg(adapter->dev,
1287 "info: CFG reg val =%x\n", cr);
1297 * This function aggregates transmission buffers in driver and downloads
1298 * the aggregated packet to card.
1300 * The individual packets are aggregated by copying into an aggregation
1301 * buffer and then downloaded to the card. Previous unsent packets in the
1302 * aggregation buffer are pre-copied first before new packets are added.
1303 * Aggregation is done till there is space left in the aggregation buffer,
1304 * or till new packets are available.
1306 * The function will only download the packet to the card when aggregation
1307 * stops, otherwise it will just aggregate the packet in aggregation buffer
1310 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1311 u8 *payload, u32 pkt_len, u8 port,
1314 struct sdio_mmc_card *card = adapter->card;
1316 s32 f_send_aggr_buf = 0;
1317 s32 f_send_cur_buf = 0;
1318 s32 f_precopy_cur_buf = 0;
1319 s32 f_postcopy_cur_buf = 0;
1321 if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
1322 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1326 goto tx_curr_single;
1330 /* More pkt in TX queue */
1331 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1334 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1335 if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
1336 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1337 f_precopy_cur_buf = 1;
1339 if (!(card->mp_wr_bitmap &
1340 (1 << card->curr_wr_port)) ||
1341 !MP_TX_AGGR_BUF_HAS_ROOM(
1342 card, pkt_len + next_pkt_len))
1343 f_send_aggr_buf = 1;
1345 /* No room in Aggr buf, send it */
1346 f_send_aggr_buf = 1;
1348 if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
1349 !(card->mp_wr_bitmap &
1350 (1 << card->curr_wr_port)))
1353 f_postcopy_cur_buf = 1;
1356 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1357 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1358 f_precopy_cur_buf = 1;
1363 /* Last pkt in TX queue */
1364 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1367 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1368 /* some packs in Aggr buf already */
1369 f_send_aggr_buf = 1;
1371 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1372 f_precopy_cur_buf = 1;
1374 /* No room in Aggr buf, send it */
1381 if (f_precopy_cur_buf) {
1382 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1384 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1386 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1387 MP_TX_AGGR_PORT_LIMIT_REACHED(card))
1388 /* No more pkts allowed in Aggr buf, send it */
1389 f_send_aggr_buf = 1;
1392 if (f_send_aggr_buf) {
1393 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1395 card->mpa_tx.start_port, card->mpa_tx.ports);
1396 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1397 card->mpa_tx.buf_len,
1398 (adapter->ioport | 0x1000 |
1399 (card->mpa_tx.ports << 4)) +
1400 card->mpa_tx.start_port);
1402 MP_TX_AGGR_BUF_RESET(card);
1406 if (f_send_cur_buf) {
1407 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1409 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1410 adapter->ioport + port);
1413 if (f_postcopy_cur_buf) {
1414 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1416 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1423 * This function downloads data from driver to card.
1425 * Both commands and data packets are transferred to the card by this
1428 * This function adds the SDIO specific header to the front of the buffer
1429 * before transferring. The header contains the length of the packet and
1430 * the type. The firmware handles the packets based upon this set type.
1432 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1433 u8 type, struct sk_buff *skb,
1434 struct mwifiex_tx_param *tx_param)
1436 struct sdio_mmc_card *card = adapter->card;
1440 u8 port = CTRL_PORT;
1441 u8 *payload = (u8 *)skb->data;
1442 u32 pkt_len = skb->len;
1444 /* Allocate buffer and copy payload */
1445 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1446 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1447 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1448 *(__le16 *)&payload[2] = cpu_to_le16(type);
1451 * This is SDIO specific header
1453 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1454 * MWIFIEX_TYPE_EVENT = 3)
1456 if (type == MWIFIEX_TYPE_DATA) {
1457 ret = mwifiex_get_wr_port_data(adapter, &port);
1459 dev_err(adapter->dev, "%s: no wr_port available\n",
1464 adapter->cmd_sent = true;
1465 /* Type must be MWIFIEX_TYPE_CMD */
1467 if (pkt_len <= INTF_HEADER_LEN ||
1468 pkt_len > MWIFIEX_UPLD_SIZE)
1469 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1470 __func__, payload, pkt_len);
1473 /* Transfer data to card */
1474 pkt_len = buf_block_len * blk_size;
1477 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1478 port, tx_param->next_pkt_len
1481 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1485 if (type == MWIFIEX_TYPE_CMD)
1486 adapter->cmd_sent = false;
1487 if (type == MWIFIEX_TYPE_DATA)
1488 adapter->data_sent = false;
1490 if (type == MWIFIEX_TYPE_DATA) {
1491 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1492 adapter->data_sent = true;
1494 adapter->data_sent = false;
1502 * This function allocates the MPA Tx and Rx buffers.
1504 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1505 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1507 struct sdio_mmc_card *card = adapter->card;
1510 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1511 if (!card->mpa_tx.buf) {
1516 card->mpa_tx.buf_size = mpa_tx_buf_size;
1518 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1519 if (!card->mpa_rx.buf) {
1524 card->mpa_rx.buf_size = mpa_rx_buf_size;
1528 kfree(card->mpa_tx.buf);
1529 kfree(card->mpa_rx.buf);
1536 * This function unregisters the SDIO device.
1538 * The SDIO IRQ is released, the function is disabled and driver
1539 * data is set to null.
1542 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1544 struct sdio_mmc_card *card = adapter->card;
1546 if (adapter->card) {
1547 /* Release the SDIO IRQ */
1548 sdio_claim_host(card->func);
1549 sdio_release_irq(card->func);
1550 sdio_disable_func(card->func);
1551 sdio_release_host(card->func);
1552 sdio_set_drvdata(card->func, NULL);
1557 * This function registers the SDIO device.
1559 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1561 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1564 struct sdio_mmc_card *card = adapter->card;
1565 struct sdio_func *func = card->func;
1567 /* save adapter pointer in card */
1568 card->adapter = adapter;
1570 sdio_claim_host(func);
1572 /* Request the SDIO IRQ */
1573 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1575 pr_err("claim irq failed: ret=%d\n", ret);
1579 /* Set block size */
1580 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1582 pr_err("cannot set SDIO block size\n");
1587 sdio_release_host(func);
1588 sdio_set_drvdata(func, card);
1590 adapter->dev = &func->dev;
1592 switch (func->device) {
1593 case SDIO_DEVICE_ID_MARVELL_8786:
1594 strcpy(adapter->fw_name, SD8786_DEFAULT_FW_NAME);
1596 case SDIO_DEVICE_ID_MARVELL_8797:
1597 strcpy(adapter->fw_name, SD8797_DEFAULT_FW_NAME);
1599 case SDIO_DEVICE_ID_MARVELL_8787:
1601 strcpy(adapter->fw_name, SD8787_DEFAULT_FW_NAME);
1608 sdio_release_irq(func);
1610 sdio_disable_func(func);
1611 sdio_release_host(func);
1612 adapter->card = NULL;
1618 * This function initializes the SDIO driver.
1620 * The following initializations steps are followed -
1621 * - Read the Host interrupt status register to acknowledge
1622 * the first interrupt got from bootloader
1623 * - Disable host interrupt mask register
1625 * - Initialize SDIO variables in card
1626 * - Allocate MP registers
1627 * - Allocate MPA Tx and Rx buffers
1629 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1631 struct sdio_mmc_card *card = adapter->card;
1636 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1637 * from the bootloader. If we don't do this we get a interrupt
1638 * as soon as we register the irq.
1640 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1642 /* Disable host interrupt mask register for SDIO */
1643 mwifiex_sdio_disable_host_int(adapter);
1645 /* Get SDIO ioport */
1646 mwifiex_init_sdio_ioport(adapter);
1648 /* Initialize SDIO variables in card */
1649 card->mp_rd_bitmap = 0;
1650 card->mp_wr_bitmap = 0;
1651 card->curr_rd_port = 1;
1652 card->curr_wr_port = 1;
1654 card->mp_data_port_mask = DATA_PORT_MASK;
1656 card->mpa_tx.buf_len = 0;
1657 card->mpa_tx.pkt_cnt = 0;
1658 card->mpa_tx.start_port = 0;
1660 card->mpa_tx.enabled = 1;
1661 card->mpa_tx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1663 card->mpa_rx.buf_len = 0;
1664 card->mpa_rx.pkt_cnt = 0;
1665 card->mpa_rx.start_port = 0;
1667 card->mpa_rx.enabled = 1;
1668 card->mpa_rx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1670 /* Allocate buffers for SDIO MP-A */
1671 card->mp_regs = kzalloc(MAX_MP_REGS, GFP_KERNEL);
1675 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1676 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1677 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1679 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1680 kfree(card->mp_regs);
1688 * This function resets the MPA Tx and Rx buffers.
1690 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1692 struct sdio_mmc_card *card = adapter->card;
1694 MP_TX_AGGR_BUF_RESET(card);
1695 MP_RX_AGGR_BUF_RESET(card);
1699 * This function cleans up the allocated card buffers.
1701 * The following are freed by this function -
1706 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1708 struct sdio_mmc_card *card = adapter->card;
1710 kfree(card->mp_regs);
1711 kfree(card->mpa_tx.buf);
1712 kfree(card->mpa_rx.buf);
1716 * This function updates the MP end port in card.
1719 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1721 struct sdio_mmc_card *card = adapter->card;
1724 card->mp_end_port = port;
1726 card->mp_data_port_mask = DATA_PORT_MASK;
1728 for (i = 1; i <= MAX_PORT - card->mp_end_port; i++)
1729 card->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
1731 card->curr_wr_port = 1;
1733 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1734 port, card->mp_data_port_mask);
1737 static struct mmc_host *reset_host;
1738 static void sdio_card_reset_worker(struct work_struct *work)
1740 struct mmc_host *target = reset_host;
1742 /* The actual reset operation must be run outside of driver thread.
1743 * This is because mmc_remove_host() will cause the device to be
1744 * instantly destroyed, and the driver then needs to end its thread,
1745 * leading to a deadlock.
1747 * We run it in a totally independent workqueue.
1750 pr_err("Resetting card...\n");
1751 mmc_remove_host(target);
1752 /* 20ms delay is based on experiment with sdhci controller */
1754 mmc_add_host(target);
1756 static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
1758 /* This function resets the card */
1759 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1761 struct sdio_mmc_card *card = adapter->card;
1763 reset_host = card->func->card->host;
1764 schedule_work(&card_reset_work);
1767 static struct mwifiex_if_ops sdio_ops = {
1768 .init_if = mwifiex_init_sdio,
1769 .cleanup_if = mwifiex_cleanup_sdio,
1770 .check_fw_status = mwifiex_check_fw_status,
1771 .prog_fw = mwifiex_prog_fw_w_helper,
1772 .register_dev = mwifiex_register_dev,
1773 .unregister_dev = mwifiex_unregister_dev,
1774 .enable_int = mwifiex_sdio_enable_host_int,
1775 .process_int_status = mwifiex_process_int_status,
1776 .host_to_card = mwifiex_sdio_host_to_card,
1777 .wakeup = mwifiex_pm_wakeup_card,
1778 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1781 .update_mp_end_port = mwifiex_update_mp_end_port,
1782 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
1783 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1784 .event_complete = mwifiex_sdio_event_complete,
1785 .card_reset = mwifiex_sdio_card_reset,
1789 * This function initializes the SDIO driver.
1791 * This initiates the semaphore and registers the device with
1795 mwifiex_sdio_init_module(void)
1797 sema_init(&add_remove_card_sem, 1);
1799 /* Clear the flag in case user removes the card. */
1802 return sdio_register_driver(&mwifiex_sdio);
1806 * This function cleans up the SDIO driver.
1808 * The following major steps are followed for cleanup -
1809 * - Resume the device if its suspended
1810 * - Disconnect the device if connected
1811 * - Shutdown the firmware
1812 * - Unregister the device from SDIO bus.
1815 mwifiex_sdio_cleanup_module(void)
1817 if (!down_interruptible(&add_remove_card_sem))
1818 up(&add_remove_card_sem);
1820 /* Set the flag as user is removing this module. */
1823 cancel_work_sync(&card_reset_work);
1824 sdio_unregister_driver(&mwifiex_sdio);
1827 module_init(mwifiex_sdio_init_module);
1828 module_exit(mwifiex_sdio_cleanup_module);
1830 MODULE_AUTHOR("Marvell International Ltd.");
1831 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
1832 MODULE_VERSION(SDIO_VERSION);
1833 MODULE_LICENSE("GPL v2");
1834 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
1835 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
1836 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);