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)
934 skb_pull(skb, INTF_HEADER_LEN);
937 case MWIFIEX_TYPE_DATA:
938 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
939 mwifiex_handle_rx_packet(adapter, skb);
942 case MWIFIEX_TYPE_CMD:
943 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
944 /* take care of curr_cmd = NULL case */
945 if (!adapter->curr_cmd) {
946 cmd_buf = adapter->upld_buf;
948 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
949 mwifiex_process_sleep_confirm_resp(adapter,
953 memcpy(cmd_buf, skb->data,
954 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
957 dev_kfree_skb_any(skb);
959 adapter->cmd_resp_received = true;
960 adapter->curr_cmd->resp_skb = skb;
964 case MWIFIEX_TYPE_EVENT:
965 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
966 adapter->event_cause = *(u32 *) skb->data;
968 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
969 memcpy(adapter->event_body,
970 skb->data + MWIFIEX_EVENT_HEADER_LEN,
973 /* event cause has been saved to adapter->event_cause */
974 adapter->event_received = true;
975 adapter->event_skb = skb;
980 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
981 dev_kfree_skb_any(skb);
989 * This function transfers received packets from card to driver, performing
990 * aggregation if required.
992 * For data received on control port, or if aggregation is disabled, the
993 * received buffers are uploaded as separate packets. However, if aggregation
994 * is enabled and required, the buffers are copied onto an aggregation buffer,
995 * provided there is space left, processed and finally uploaded.
997 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
998 struct sk_buff *skb, u8 port)
1000 struct sdio_mmc_card *card = adapter->card;
1001 s32 f_do_rx_aggr = 0;
1002 s32 f_do_rx_cur = 0;
1004 struct sk_buff *skb_deaggr;
1006 u32 pkt_len, pkt_type = 0;
1008 u32 rx_len = skb->len;
1010 if (port == CTRL_PORT) {
1011 /* Read the command Resp without aggr */
1012 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1013 "response\n", __func__);
1016 goto rx_curr_single;
1019 if (!card->mpa_rx.enabled) {
1020 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1024 goto rx_curr_single;
1027 if (card->mp_rd_bitmap & (~((u16) CTRL_PORT_MASK))) {
1028 /* Some more data RX pending */
1029 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1031 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1032 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1035 /* No room in Aggr buf, do rx aggr now */
1040 /* Rx aggr not in progress */
1045 /* No more data RX pending */
1046 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1048 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1050 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1053 /* No room in Aggr buf, do rx aggr now */
1061 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1062 /* Curr pkt can be aggregated */
1063 MP_RX_AGGR_SETUP(card, skb, port);
1065 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1066 MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
1067 dev_dbg(adapter->dev, "info: %s: aggregated packet "
1068 "limit reached\n", __func__);
1069 /* No more pkts allowed in Aggr buf, rx it */
1075 /* do aggr RX now */
1076 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1077 card->mpa_rx.pkt_cnt);
1079 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1080 card->mpa_rx.buf_len,
1081 (adapter->ioport | 0x1000 |
1082 (card->mpa_rx.ports << 4)) +
1083 card->mpa_rx.start_port, 1))
1086 curr_ptr = card->mpa_rx.buf;
1088 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1090 /* get curr PKT len & type */
1091 pkt_len = *(u16 *) &curr_ptr[0];
1092 pkt_type = *(u16 *) &curr_ptr[2];
1094 /* copy pkt to deaggr buf */
1095 skb_deaggr = card->mpa_rx.skb_arr[pind];
1097 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1098 card->mpa_rx.len_arr[pind])) {
1100 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1102 skb_trim(skb_deaggr, pkt_len);
1104 /* Process de-aggr packet */
1105 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1108 dev_err(adapter->dev, "wrong aggr pkt:"
1109 " type=%d len=%d max_len=%d\n",
1111 card->mpa_rx.len_arr[pind]);
1112 dev_kfree_skb_any(skb_deaggr);
1114 curr_ptr += card->mpa_rx.len_arr[pind];
1116 MP_RX_AGGR_BUF_RESET(card);
1121 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1124 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1125 skb->data, skb->len,
1126 adapter->ioport + port))
1129 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1135 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1136 /* Multiport-aggregation transfer failed - cleanup */
1137 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1138 /* copy pkt to deaggr buf */
1139 skb_deaggr = card->mpa_rx.skb_arr[pind];
1140 dev_kfree_skb_any(skb_deaggr);
1142 MP_RX_AGGR_BUF_RESET(card);
1146 /* Single transfer pending. Free curr buff also */
1147 dev_kfree_skb_any(skb);
1153 * This function checks the current interrupt status.
1155 * The following interrupts are checked and handled by this function -
1158 * - Packets received
1160 * Since the firmware does not generate download ready interrupt if the
1161 * port updated is command port only, command sent interrupt checking
1162 * should be done manually, and for every SDIO interrupt.
1164 * In case of Rx packets received, the packets are uploaded from card to
1165 * host and processed accordingly.
1167 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1169 struct sdio_mmc_card *card = adapter->card;
1172 struct sk_buff *skb;
1173 u8 port = CTRL_PORT;
1174 u32 len_reg_l, len_reg_u;
1177 unsigned long flags;
1179 spin_lock_irqsave(&adapter->int_lock, flags);
1180 sdio_ireg = adapter->int_status;
1181 adapter->int_status = 0;
1182 spin_unlock_irqrestore(&adapter->int_lock, flags);
1187 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1188 card->mp_wr_bitmap = ((u16) card->mp_regs[WR_BITMAP_U]) << 8;
1189 card->mp_wr_bitmap |= (u16) card->mp_regs[WR_BITMAP_L];
1190 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%04x\n",
1191 card->mp_wr_bitmap);
1192 if (adapter->data_sent &&
1193 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1194 dev_dbg(adapter->dev,
1195 "info: <--- Tx DONE Interrupt --->\n");
1196 adapter->data_sent = false;
1200 /* As firmware will not generate download ready interrupt if the port
1201 updated is command port only, cmd_sent should be done for any SDIO
1203 if (adapter->cmd_sent) {
1204 /* Check if firmware has attach buffer at command port and
1205 update just that in wr_bit_map. */
1206 card->mp_wr_bitmap |=
1207 (u16) card->mp_regs[WR_BITMAP_L] & CTRL_PORT_MASK;
1208 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1209 adapter->cmd_sent = false;
1212 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1213 adapter->cmd_sent, adapter->data_sent);
1214 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1215 card->mp_rd_bitmap = ((u16) card->mp_regs[RD_BITMAP_U]) << 8;
1216 card->mp_rd_bitmap |= (u16) card->mp_regs[RD_BITMAP_L];
1217 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%04x\n",
1218 card->mp_rd_bitmap);
1221 ret = mwifiex_get_rd_port(adapter, &port);
1223 dev_dbg(adapter->dev,
1224 "info: no more rd_port available\n");
1227 len_reg_l = RD_LEN_P0_L + (port << 1);
1228 len_reg_u = RD_LEN_P0_U + (port << 1);
1229 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1230 rx_len |= (u16) card->mp_regs[len_reg_l];
1231 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1234 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1235 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1236 if (rx_len <= INTF_HEADER_LEN ||
1237 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1238 MWIFIEX_RX_DATA_BUF_SIZE) {
1239 dev_err(adapter->dev, "invalid rx_len=%d\n",
1243 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1245 skb = dev_alloc_skb(rx_len);
1248 dev_err(adapter->dev, "%s: failed to alloc skb",
1253 skb_put(skb, rx_len);
1255 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1258 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1262 dev_err(adapter->dev, "card_to_host_mpa failed:"
1263 " int status=%#x\n", sdio_ireg);
1264 if (mwifiex_read_reg(adapter,
1265 CONFIGURATION_REG, &cr))
1266 dev_err(adapter->dev,
1267 "read CFG reg failed\n");
1269 dev_dbg(adapter->dev,
1270 "info: CFG reg val = %d\n", cr);
1271 if (mwifiex_write_reg(adapter,
1274 dev_err(adapter->dev,
1275 "write CFG reg failed\n");
1277 dev_dbg(adapter->dev, "info: write success\n");
1278 if (mwifiex_read_reg(adapter,
1279 CONFIGURATION_REG, &cr))
1280 dev_err(adapter->dev,
1281 "read CFG reg failed\n");
1283 dev_dbg(adapter->dev,
1284 "info: CFG reg val =%x\n", cr);
1294 * This function aggregates transmission buffers in driver and downloads
1295 * the aggregated packet to card.
1297 * The individual packets are aggregated by copying into an aggregation
1298 * buffer and then downloaded to the card. Previous unsent packets in the
1299 * aggregation buffer are pre-copied first before new packets are added.
1300 * Aggregation is done till there is space left in the aggregation buffer,
1301 * or till new packets are available.
1303 * The function will only download the packet to the card when aggregation
1304 * stops, otherwise it will just aggregate the packet in aggregation buffer
1307 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1308 u8 *payload, u32 pkt_len, u8 port,
1311 struct sdio_mmc_card *card = adapter->card;
1313 s32 f_send_aggr_buf = 0;
1314 s32 f_send_cur_buf = 0;
1315 s32 f_precopy_cur_buf = 0;
1316 s32 f_postcopy_cur_buf = 0;
1318 if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
1319 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1323 goto tx_curr_single;
1327 /* More pkt in TX queue */
1328 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1331 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1332 if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
1333 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1334 f_precopy_cur_buf = 1;
1336 if (!(card->mp_wr_bitmap &
1337 (1 << card->curr_wr_port)) ||
1338 !MP_TX_AGGR_BUF_HAS_ROOM(
1339 card, pkt_len + next_pkt_len))
1340 f_send_aggr_buf = 1;
1342 /* No room in Aggr buf, send it */
1343 f_send_aggr_buf = 1;
1345 if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
1346 !(card->mp_wr_bitmap &
1347 (1 << card->curr_wr_port)))
1350 f_postcopy_cur_buf = 1;
1353 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1354 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1355 f_precopy_cur_buf = 1;
1360 /* Last pkt in TX queue */
1361 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1364 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1365 /* some packs in Aggr buf already */
1366 f_send_aggr_buf = 1;
1368 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1369 f_precopy_cur_buf = 1;
1371 /* No room in Aggr buf, send it */
1378 if (f_precopy_cur_buf) {
1379 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1381 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1383 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1384 MP_TX_AGGR_PORT_LIMIT_REACHED(card))
1385 /* No more pkts allowed in Aggr buf, send it */
1386 f_send_aggr_buf = 1;
1389 if (f_send_aggr_buf) {
1390 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1392 card->mpa_tx.start_port, card->mpa_tx.ports);
1393 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1394 card->mpa_tx.buf_len,
1395 (adapter->ioport | 0x1000 |
1396 (card->mpa_tx.ports << 4)) +
1397 card->mpa_tx.start_port);
1399 MP_TX_AGGR_BUF_RESET(card);
1403 if (f_send_cur_buf) {
1404 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1406 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1407 adapter->ioport + port);
1410 if (f_postcopy_cur_buf) {
1411 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1413 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1420 * This function downloads data from driver to card.
1422 * Both commands and data packets are transferred to the card by this
1425 * This function adds the SDIO specific header to the front of the buffer
1426 * before transferring. The header contains the length of the packet and
1427 * the type. The firmware handles the packets based upon this set type.
1429 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1430 u8 type, struct sk_buff *skb,
1431 struct mwifiex_tx_param *tx_param)
1433 struct sdio_mmc_card *card = adapter->card;
1437 u8 port = CTRL_PORT;
1438 u8 *payload = (u8 *)skb->data;
1439 u32 pkt_len = skb->len;
1441 /* Allocate buffer and copy payload */
1442 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1443 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1444 *(u16 *) &payload[0] = (u16) pkt_len;
1445 *(u16 *) &payload[2] = type;
1448 * This is SDIO specific header
1450 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1451 * MWIFIEX_TYPE_EVENT = 3)
1453 if (type == MWIFIEX_TYPE_DATA) {
1454 ret = mwifiex_get_wr_port_data(adapter, &port);
1456 dev_err(adapter->dev, "%s: no wr_port available\n",
1461 adapter->cmd_sent = true;
1462 /* Type must be MWIFIEX_TYPE_CMD */
1464 if (pkt_len <= INTF_HEADER_LEN ||
1465 pkt_len > MWIFIEX_UPLD_SIZE)
1466 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1467 __func__, payload, pkt_len);
1470 /* Transfer data to card */
1471 pkt_len = buf_block_len * blk_size;
1474 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1475 port, tx_param->next_pkt_len
1478 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1482 if (type == MWIFIEX_TYPE_CMD)
1483 adapter->cmd_sent = false;
1484 if (type == MWIFIEX_TYPE_DATA)
1485 adapter->data_sent = false;
1487 if (type == MWIFIEX_TYPE_DATA) {
1488 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1489 adapter->data_sent = true;
1491 adapter->data_sent = false;
1499 * This function allocates the MPA Tx and Rx buffers.
1501 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1502 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1504 struct sdio_mmc_card *card = adapter->card;
1507 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1508 if (!card->mpa_tx.buf) {
1513 card->mpa_tx.buf_size = mpa_tx_buf_size;
1515 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1516 if (!card->mpa_rx.buf) {
1521 card->mpa_rx.buf_size = mpa_rx_buf_size;
1525 kfree(card->mpa_tx.buf);
1526 kfree(card->mpa_rx.buf);
1533 * This function unregisters the SDIO device.
1535 * The SDIO IRQ is released, the function is disabled and driver
1536 * data is set to null.
1539 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1541 struct sdio_mmc_card *card = adapter->card;
1543 if (adapter->card) {
1544 /* Release the SDIO IRQ */
1545 sdio_claim_host(card->func);
1546 sdio_release_irq(card->func);
1547 sdio_disable_func(card->func);
1548 sdio_release_host(card->func);
1549 sdio_set_drvdata(card->func, NULL);
1554 * This function registers the SDIO device.
1556 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1558 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1561 struct sdio_mmc_card *card = adapter->card;
1562 struct sdio_func *func = card->func;
1564 /* save adapter pointer in card */
1565 card->adapter = adapter;
1567 sdio_claim_host(func);
1569 /* Request the SDIO IRQ */
1570 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1572 pr_err("claim irq failed: ret=%d\n", ret);
1576 /* Set block size */
1577 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1579 pr_err("cannot set SDIO block size\n");
1584 sdio_release_host(func);
1585 sdio_set_drvdata(func, card);
1587 adapter->dev = &func->dev;
1589 switch (func->device) {
1590 case SDIO_DEVICE_ID_MARVELL_8786:
1591 strcpy(adapter->fw_name, SD8786_DEFAULT_FW_NAME);
1593 case SDIO_DEVICE_ID_MARVELL_8797:
1594 strcpy(adapter->fw_name, SD8797_DEFAULT_FW_NAME);
1596 case SDIO_DEVICE_ID_MARVELL_8787:
1598 strcpy(adapter->fw_name, SD8787_DEFAULT_FW_NAME);
1605 sdio_release_irq(func);
1607 sdio_disable_func(func);
1608 sdio_release_host(func);
1609 adapter->card = NULL;
1615 * This function initializes the SDIO driver.
1617 * The following initializations steps are followed -
1618 * - Read the Host interrupt status register to acknowledge
1619 * the first interrupt got from bootloader
1620 * - Disable host interrupt mask register
1622 * - Initialize SDIO variables in card
1623 * - Allocate MP registers
1624 * - Allocate MPA Tx and Rx buffers
1626 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1628 struct sdio_mmc_card *card = adapter->card;
1633 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1634 * from the bootloader. If we don't do this we get a interrupt
1635 * as soon as we register the irq.
1637 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1639 /* Disable host interrupt mask register for SDIO */
1640 mwifiex_sdio_disable_host_int(adapter);
1642 /* Get SDIO ioport */
1643 mwifiex_init_sdio_ioport(adapter);
1645 /* Initialize SDIO variables in card */
1646 card->mp_rd_bitmap = 0;
1647 card->mp_wr_bitmap = 0;
1648 card->curr_rd_port = 1;
1649 card->curr_wr_port = 1;
1651 card->mp_data_port_mask = DATA_PORT_MASK;
1653 card->mpa_tx.buf_len = 0;
1654 card->mpa_tx.pkt_cnt = 0;
1655 card->mpa_tx.start_port = 0;
1657 card->mpa_tx.enabled = 1;
1658 card->mpa_tx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1660 card->mpa_rx.buf_len = 0;
1661 card->mpa_rx.pkt_cnt = 0;
1662 card->mpa_rx.start_port = 0;
1664 card->mpa_rx.enabled = 1;
1665 card->mpa_rx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1667 /* Allocate buffers for SDIO MP-A */
1668 card->mp_regs = kzalloc(MAX_MP_REGS, GFP_KERNEL);
1672 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1673 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1674 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1676 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1677 kfree(card->mp_regs);
1685 * This function resets the MPA Tx and Rx buffers.
1687 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1689 struct sdio_mmc_card *card = adapter->card;
1691 MP_TX_AGGR_BUF_RESET(card);
1692 MP_RX_AGGR_BUF_RESET(card);
1696 * This function cleans up the allocated card buffers.
1698 * The following are freed by this function -
1703 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1705 struct sdio_mmc_card *card = adapter->card;
1707 kfree(card->mp_regs);
1708 kfree(card->mpa_tx.buf);
1709 kfree(card->mpa_rx.buf);
1713 * This function updates the MP end port in card.
1716 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1718 struct sdio_mmc_card *card = adapter->card;
1721 card->mp_end_port = port;
1723 card->mp_data_port_mask = DATA_PORT_MASK;
1725 for (i = 1; i <= MAX_PORT - card->mp_end_port; i++)
1726 card->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
1728 card->curr_wr_port = 1;
1730 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1731 port, card->mp_data_port_mask);
1734 static struct mmc_host *reset_host;
1735 static void sdio_card_reset_worker(struct work_struct *work)
1737 struct mmc_host *target = reset_host;
1739 /* The actual reset operation must be run outside of driver thread.
1740 * This is because mmc_remove_host() will cause the device to be
1741 * instantly destroyed, and the driver then needs to end its thread,
1742 * leading to a deadlock.
1744 * We run it in a totally independent workqueue.
1747 pr_err("Resetting card...\n");
1748 mmc_remove_host(target);
1749 /* 20ms delay is based on experiment with sdhci controller */
1751 mmc_add_host(target);
1753 static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
1755 /* This function resets the card */
1756 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1758 struct sdio_mmc_card *card = adapter->card;
1760 reset_host = card->func->card->host;
1761 schedule_work(&card_reset_work);
1764 static struct mwifiex_if_ops sdio_ops = {
1765 .init_if = mwifiex_init_sdio,
1766 .cleanup_if = mwifiex_cleanup_sdio,
1767 .check_fw_status = mwifiex_check_fw_status,
1768 .prog_fw = mwifiex_prog_fw_w_helper,
1769 .register_dev = mwifiex_register_dev,
1770 .unregister_dev = mwifiex_unregister_dev,
1771 .enable_int = mwifiex_sdio_enable_host_int,
1772 .process_int_status = mwifiex_process_int_status,
1773 .host_to_card = mwifiex_sdio_host_to_card,
1774 .wakeup = mwifiex_pm_wakeup_card,
1775 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1778 .update_mp_end_port = mwifiex_update_mp_end_port,
1779 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
1780 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1781 .event_complete = mwifiex_sdio_event_complete,
1782 .card_reset = mwifiex_sdio_card_reset,
1786 * This function initializes the SDIO driver.
1788 * This initiates the semaphore and registers the device with
1792 mwifiex_sdio_init_module(void)
1794 sema_init(&add_remove_card_sem, 1);
1796 /* Clear the flag in case user removes the card. */
1799 return sdio_register_driver(&mwifiex_sdio);
1803 * This function cleans up the SDIO driver.
1805 * The following major steps are followed for cleanup -
1806 * - Resume the device if its suspended
1807 * - Disconnect the device if connected
1808 * - Shutdown the firmware
1809 * - Unregister the device from SDIO bus.
1812 mwifiex_sdio_cleanup_module(void)
1814 if (!down_interruptible(&add_remove_card_sem))
1815 up(&add_remove_card_sem);
1817 /* Set the flag as user is removing this module. */
1820 cancel_work_sync(&card_reset_work);
1821 sdio_unregister_driver(&mwifiex_sdio);
1824 module_init(mwifiex_sdio_init_module);
1825 module_exit(mwifiex_sdio_cleanup_module);
1827 MODULE_AUTHOR("Marvell International Ltd.");
1828 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
1829 MODULE_VERSION(SDIO_VERSION);
1830 MODULE_LICENSE("GPL v2");
1831 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
1832 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
1833 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);