Merge branch 'for-linus' of git://git.infradead.org/users/sameo/mfd-2.6
[linux-btrfs-devel.git] / drivers / net / wireless / mwifiex / sdio.c
blob82098ac483b87097aece2ea67b62dad38afc4c46
1 /*
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>
22 #include "decl.h"
23 #include "ioctl.h"
24 #include "util.h"
25 #include "fw.h"
26 #include "main.h"
27 #include "wmm.h"
28 #include "11n.h"
29 #include "sdio.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
37 * differently.
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.
47 static u8 user_rmmod;
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);
56 * SDIO probe.
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
61 * interface.
63 static int
64 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
66 int ret;
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);
73 if (!card) {
74 pr_err("%s: failed to alloc memory\n", __func__);
75 return -ENOMEM;
78 card->func = func;
80 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
82 sdio_claim_host(func);
83 ret = sdio_enable_func(func);
84 sdio_release_host(func);
86 if (ret) {
87 pr_err("%s: failed to enable function\n", __func__);
88 kfree(card);
89 return -EIO;
92 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops)) {
93 pr_err("%s: add card failed\n", __func__);
94 kfree(card);
95 sdio_claim_host(func);
96 ret = sdio_disable_func(func);
97 sdio_release_host(func);
98 ret = -1;
101 return ret;
105 * SDIO remove.
107 * This function removes the interface and frees up the card structure.
109 static void
110 mwifiex_sdio_remove(struct sdio_func *func)
112 struct sdio_mmc_card *card;
113 struct mwifiex_adapter *adapter;
114 int i;
116 pr_debug("info: SDIO func num=%d\n", func->num);
118 card = sdio_get_drvdata(func);
119 if (!card)
120 return;
122 adapter = card->adapter;
123 if (!adapter || !adapter->priv_num)
124 return;
126 if (user_rmmod) {
127 if (adapter->is_suspended)
128 mwifiex_sdio_resume(adapter->dev);
130 for (i = 0; i < adapter->priv_num; i++)
131 if ((GET_BSS_ROLE(adapter->priv[i]) ==
132 MWIFIEX_BSS_ROLE_STA) &&
133 adapter->priv[i]->media_connected)
134 mwifiex_deauthenticate(adapter->priv[i], NULL);
136 mwifiex_disable_auto_ds(mwifiex_get_priv(adapter,
137 MWIFIEX_BSS_ROLE_ANY));
139 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
140 MWIFIEX_BSS_ROLE_ANY),
141 MWIFIEX_FUNC_SHUTDOWN);
144 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
145 kfree(card);
149 * SDIO suspend.
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;
164 int hs_actived = 0;
165 int i;
166 int ret = 0;
168 if (func) {
169 pm_flag = sdio_get_host_pm_caps(func);
170 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
171 sdio_func_id(func), pm_flag);
172 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
173 pr_err("%s: cannot remain alive while host is"
174 " suspended\n", sdio_func_id(func));
175 return -ENOSYS;
178 card = sdio_get_drvdata(func);
179 if (!card || !card->adapter) {
180 pr_err("suspend: invalid card or adapter\n");
181 return 0;
183 } else {
184 pr_err("suspend: sdio_func is not specified\n");
185 return 0;
188 adapter = card->adapter;
190 /* Enable the Host Sleep */
191 hs_actived = mwifiex_enable_hs(adapter);
192 if (hs_actived) {
193 pr_debug("cmd: suspend with MMC_PM_KEEP_POWER\n");
194 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
197 /* Indicate device suspended */
198 adapter->is_suspended = true;
200 for (i = 0; i < adapter->priv_num; i++)
201 netif_carrier_off(adapter->priv[i]->netdev);
203 return ret;
207 * SDIO resume.
209 * Kernel needs to suspend all functions separately. Therefore all
210 * registered functions must have drivers with suspend and resume
211 * methods. Failing that the kernel simply removes the whole card.
213 * If already not resumed, this function turns on the traffic and
214 * sends a host sleep cancel request to the firmware.
216 static int mwifiex_sdio_resume(struct device *dev)
218 struct sdio_func *func = dev_to_sdio_func(dev);
219 struct sdio_mmc_card *card;
220 struct mwifiex_adapter *adapter;
221 mmc_pm_flag_t pm_flag = 0;
222 int i;
224 if (func) {
225 pm_flag = sdio_get_host_pm_caps(func);
226 card = sdio_get_drvdata(func);
227 if (!card || !card->adapter) {
228 pr_err("resume: invalid card or adapter\n");
229 return 0;
231 } else {
232 pr_err("resume: sdio_func is not specified\n");
233 return 0;
236 adapter = card->adapter;
238 if (!adapter->is_suspended) {
239 dev_warn(adapter->dev, "device already resumed\n");
240 return 0;
243 adapter->is_suspended = false;
245 for (i = 0; i < adapter->priv_num; i++)
246 if (adapter->priv[i]->media_connected)
247 netif_carrier_on(adapter->priv[i]->netdev);
249 /* Disable Host Sleep */
250 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
251 MWIFIEX_ASYNC_CMD);
253 return 0;
256 /* Device ID for SD8787 */
257 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
259 /* WLAN IDs */
260 static const struct sdio_device_id mwifiex_ids[] = {
261 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787)},
265 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
267 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
268 .suspend = mwifiex_sdio_suspend,
269 .resume = mwifiex_sdio_resume,
272 static struct sdio_driver mwifiex_sdio = {
273 .name = "mwifiex_sdio",
274 .id_table = mwifiex_ids,
275 .probe = mwifiex_sdio_probe,
276 .remove = mwifiex_sdio_remove,
277 .drv = {
278 .owner = THIS_MODULE,
279 .pm = &mwifiex_sdio_pm_ops,
284 * This function writes data into SDIO card register.
286 static int
287 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u32 data)
289 struct sdio_mmc_card *card = adapter->card;
290 int ret = -1;
292 sdio_claim_host(card->func);
293 sdio_writeb(card->func, (u8) data, reg, &ret);
294 sdio_release_host(card->func);
296 return ret;
300 * This function reads data from SDIO card register.
302 static int
303 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u32 *data)
305 struct sdio_mmc_card *card = adapter->card;
306 int ret = -1;
307 u8 val;
309 sdio_claim_host(card->func);
310 val = sdio_readb(card->func, reg, &ret);
311 sdio_release_host(card->func);
313 *data = val;
315 return ret;
319 * This function writes multiple data into SDIO card memory.
321 * This does not work in suspended mode.
323 static int
324 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
325 u8 *buffer, u32 pkt_len, u32 port)
327 struct sdio_mmc_card *card = adapter->card;
328 int ret = -1;
329 u8 blk_mode =
330 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
331 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
332 u32 blk_cnt =
333 (blk_mode ==
334 BLOCK_MODE) ? (pkt_len /
335 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
336 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
338 if (adapter->is_suspended) {
339 dev_err(adapter->dev,
340 "%s: not allowed while suspended\n", __func__);
341 return -1;
344 sdio_claim_host(card->func);
346 if (!sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size))
347 ret = 0;
349 sdio_release_host(card->func);
351 return ret;
355 * This function reads multiple data from SDIO card memory.
357 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
358 u32 len, u32 port, u8 claim)
360 struct sdio_mmc_card *card = adapter->card;
361 int ret = -1;
362 u8 blk_mode =
363 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
364 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
365 u32 blk_cnt =
366 (blk_mode ==
367 BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE) : len;
368 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
370 if (claim)
371 sdio_claim_host(card->func);
373 if (!sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size))
374 ret = 0;
376 if (claim)
377 sdio_release_host(card->func);
379 return ret;
383 * This function wakes up the card.
385 * A host power up command is written to the card configuration
386 * register to wake up the card.
388 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
390 dev_dbg(adapter->dev, "event: wakeup device...\n");
392 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
396 * This function is called after the card has woken up.
398 * The card configuration register is reset.
400 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
402 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
404 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
408 * This function initializes the IO ports.
410 * The following operations are performed -
411 * - Read the IO ports (0, 1 and 2)
412 * - Set host interrupt Reset-To-Read to clear
413 * - Set auto re-enable interrupt
415 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
417 u32 reg;
419 adapter->ioport = 0;
421 /* Read the IO port */
422 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
423 adapter->ioport |= (reg & 0xff);
424 else
425 return -1;
427 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
428 adapter->ioport |= ((reg & 0xff) << 8);
429 else
430 return -1;
432 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
433 adapter->ioport |= ((reg & 0xff) << 16);
434 else
435 return -1;
437 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
439 /* Set Host interrupt reset to read to clear */
440 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
441 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
442 reg | SDIO_INT_MASK);
443 else
444 return -1;
446 /* Dnld/Upld ready set to auto reset */
447 if (!mwifiex_read_reg(adapter, CARD_MISC_CFG_REG, &reg))
448 mwifiex_write_reg(adapter, CARD_MISC_CFG_REG,
449 reg | AUTO_RE_ENABLE_INT);
450 else
451 return -1;
453 return 0;
457 * This function sends data to the card.
459 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
460 u8 *payload, u32 pkt_len, u32 port)
462 u32 i = 0;
463 int ret;
465 do {
466 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
467 if (ret) {
468 i++;
469 dev_err(adapter->dev, "host_to_card, write iomem"
470 " (%d) failed: %d\n", i, ret);
471 if (mwifiex_write_reg(adapter,
472 CONFIGURATION_REG, 0x04))
473 dev_err(adapter->dev, "write CFG reg failed\n");
475 ret = -1;
476 if (i > MAX_WRITE_IOMEM_RETRY)
477 return ret;
479 } while (ret == -1);
481 return ret;
485 * This function gets the read port.
487 * If control port bit is set in MP read bitmap, the control port
488 * is returned, otherwise the current read port is returned and
489 * the value is increased (provided it does not reach the maximum
490 * limit, in which case it is reset to 1)
492 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
494 struct sdio_mmc_card *card = adapter->card;
495 u16 rd_bitmap = card->mp_rd_bitmap;
497 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%04x\n", rd_bitmap);
499 if (!(rd_bitmap & (CTRL_PORT_MASK | DATA_PORT_MASK)))
500 return -1;
502 if (card->mp_rd_bitmap & CTRL_PORT_MASK) {
503 card->mp_rd_bitmap &= (u16) (~CTRL_PORT_MASK);
504 *port = CTRL_PORT;
505 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%04x\n",
506 *port, card->mp_rd_bitmap);
507 } else {
508 if (card->mp_rd_bitmap & (1 << card->curr_rd_port)) {
509 card->mp_rd_bitmap &=
510 (u16) (~(1 << card->curr_rd_port));
511 *port = card->curr_rd_port;
513 if (++card->curr_rd_port == MAX_PORT)
514 card->curr_rd_port = 1;
515 } else {
516 return -1;
519 dev_dbg(adapter->dev,
520 "data: port=%d mp_rd_bitmap=0x%04x -> 0x%04x\n",
521 *port, rd_bitmap, card->mp_rd_bitmap);
523 return 0;
527 * This function gets the write port for data.
529 * The current write port is returned if available and the value is
530 * increased (provided it does not reach the maximum limit, in which
531 * case it is reset to 1)
533 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u8 *port)
535 struct sdio_mmc_card *card = adapter->card;
536 u16 wr_bitmap = card->mp_wr_bitmap;
538 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%04x\n", wr_bitmap);
540 if (!(wr_bitmap & card->mp_data_port_mask))
541 return -1;
543 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
544 card->mp_wr_bitmap &= (u16) (~(1 << card->curr_wr_port));
545 *port = card->curr_wr_port;
546 if (++card->curr_wr_port == card->mp_end_port)
547 card->curr_wr_port = 1;
548 } else {
549 adapter->data_sent = true;
550 return -EBUSY;
553 if (*port == CTRL_PORT) {
554 dev_err(adapter->dev, "invalid data port=%d cur port=%d"
555 " mp_wr_bitmap=0x%04x -> 0x%04x\n",
556 *port, card->curr_wr_port, wr_bitmap,
557 card->mp_wr_bitmap);
558 return -1;
561 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%04x -> 0x%04x\n",
562 *port, wr_bitmap, card->mp_wr_bitmap);
564 return 0;
568 * This function polls the card status.
570 static int
571 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
573 u32 tries;
574 u32 cs;
576 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
577 if (mwifiex_read_reg(adapter, CARD_STATUS_REG, &cs))
578 break;
579 else if ((cs & bits) == bits)
580 return 0;
582 udelay(10);
585 dev_err(adapter->dev, "poll card status failed, tries = %d\n",
586 tries);
587 return -1;
591 * This function reads the firmware status.
593 static int
594 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
596 u32 fws0, fws1;
598 if (mwifiex_read_reg(adapter, CARD_FW_STATUS0_REG, &fws0))
599 return -1;
601 if (mwifiex_read_reg(adapter, CARD_FW_STATUS1_REG, &fws1))
602 return -1;
604 *dat = (u16) ((fws1 << 8) | fws0);
606 return 0;
610 * This function disables the host interrupt.
612 * The host interrupt mask is read, the disable bit is reset and
613 * written back to the card host interrupt mask register.
615 static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
617 u32 host_int_mask;
619 /* Read back the host_int_mask register */
620 if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
621 return -1;
623 /* Update with the mask and write back to the register */
624 host_int_mask &= ~HOST_INT_DISABLE;
626 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
627 dev_err(adapter->dev, "disable host interrupt failed\n");
628 return -1;
631 return 0;
635 * This function enables the host interrupt.
637 * The host interrupt enable mask is written to the card
638 * host interrupt mask register.
640 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
642 /* Simply write the mask to the register */
643 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, HOST_INT_ENABLE)) {
644 dev_err(adapter->dev, "enable host interrupt failed\n");
645 return -1;
647 return 0;
651 * This function sends a data buffer to the card.
653 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
654 u32 *type, u8 *buffer,
655 u32 npayload, u32 ioport)
657 int ret;
658 u32 nb;
660 if (!buffer) {
661 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
662 return -1;
665 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
667 if (ret) {
668 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
669 ret);
670 return -1;
673 nb = le16_to_cpu(*(__le16 *) (buffer));
674 if (nb > npayload) {
675 dev_err(adapter->dev, "%s: invalid packet, nb=%d, npayload=%d\n",
676 __func__, nb, npayload);
677 return -1;
680 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
682 return ret;
686 * This function downloads the firmware to the card.
688 * Firmware is downloaded to the card in blocks. Every block download
689 * is tested for CRC errors, and retried a number of times before
690 * returning failure.
692 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
693 struct mwifiex_fw_image *fw)
695 int ret;
696 u8 *firmware = fw->fw_buf;
697 u32 firmware_len = fw->fw_len;
698 u32 offset = 0;
699 u32 base0, base1;
700 u8 *fwbuf;
701 u16 len = 0;
702 u32 txlen, tx_blocks = 0, tries;
703 u32 i = 0;
705 if (!firmware_len) {
706 dev_err(adapter->dev, "firmware image not found!"
707 " Terminating download\n");
708 return -1;
711 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
712 firmware_len);
714 /* Assume that the allocated buffer is 8-byte aligned */
715 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
716 if (!fwbuf) {
717 dev_err(adapter->dev, "unable to alloc buffer for firmware."
718 " Terminating download\n");
719 return -ENOMEM;
722 /* Perform firmware data transfer */
723 do {
724 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
725 bits */
726 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
727 DN_LD_CARD_RDY);
728 if (ret) {
729 dev_err(adapter->dev, "FW download with helper:"
730 " poll status timeout @ %d\n", offset);
731 goto done;
734 /* More data? */
735 if (offset >= firmware_len)
736 break;
738 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
739 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_0,
740 &base0);
741 if (ret) {
742 dev_err(adapter->dev, "dev BASE0 register read"
743 " failed: base0=0x%04X(%d). Terminating "
744 "download\n", base0, base0);
745 goto done;
747 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_1,
748 &base1);
749 if (ret) {
750 dev_err(adapter->dev, "dev BASE1 register read"
751 " failed: base1=0x%04X(%d). Terminating "
752 "download\n", base1, base1);
753 goto done;
755 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
757 if (len)
758 break;
760 udelay(10);
763 if (!len) {
764 break;
765 } else if (len > MWIFIEX_UPLD_SIZE) {
766 dev_err(adapter->dev, "FW download failed @ %d,"
767 " invalid length %d\n", offset, len);
768 ret = -1;
769 goto done;
772 txlen = len;
774 if (len & BIT(0)) {
775 i++;
776 if (i > MAX_WRITE_IOMEM_RETRY) {
777 dev_err(adapter->dev, "FW download failed @"
778 " %d, over max retry count\n", offset);
779 ret = -1;
780 goto done;
782 dev_err(adapter->dev, "CRC indicated by the helper:"
783 " len = 0x%04X, txlen = %d\n", len, txlen);
784 len &= ~BIT(0);
785 /* Setting this to 0 to resend from same offset */
786 txlen = 0;
787 } else {
788 i = 0;
790 /* Set blocksize to transfer - checking for last
791 block */
792 if (firmware_len - offset < txlen)
793 txlen = firmware_len - offset;
795 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE -
796 1) / MWIFIEX_SDIO_BLOCK_SIZE;
798 /* Copy payload to buffer */
799 memmove(fwbuf, &firmware[offset], txlen);
802 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
803 MWIFIEX_SDIO_BLOCK_SIZE,
804 adapter->ioport);
805 if (ret) {
806 dev_err(adapter->dev, "FW download, write iomem (%d)"
807 " failed @ %d\n", i, offset);
808 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
809 dev_err(adapter->dev, "write CFG reg failed\n");
811 ret = -1;
812 goto done;
815 offset += txlen;
816 } while (true);
818 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
819 offset);
821 ret = 0;
822 done:
823 kfree(fwbuf);
824 return ret;
828 * This function checks the firmware status in card.
830 * The winner interface is also determined by this function.
832 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
833 u32 poll_num, int *winner)
835 int ret = 0;
836 u16 firmware_stat;
837 u32 tries;
838 u32 winner_status;
840 /* Wait for firmware initialization event */
841 for (tries = 0; tries < poll_num; tries++) {
842 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
843 if (ret)
844 continue;
845 if (firmware_stat == FIRMWARE_READY) {
846 ret = 0;
847 break;
848 } else {
849 mdelay(100);
850 ret = -1;
854 if (winner && ret) {
855 if (mwifiex_read_reg
856 (adapter, CARD_FW_STATUS0_REG, &winner_status))
857 winner_status = 0;
859 if (winner_status)
860 *winner = 0;
861 else
862 *winner = 1;
864 return ret;
868 * This function reads the interrupt status from card.
870 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
872 struct sdio_mmc_card *card = adapter->card;
873 u32 sdio_ireg;
874 unsigned long flags;
876 if (mwifiex_read_data_sync(adapter, card->mp_regs, MAX_MP_REGS,
877 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK,
878 0)) {
879 dev_err(adapter->dev, "read mp_regs failed\n");
880 return;
883 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
884 if (sdio_ireg) {
886 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
887 * Clear the interrupt status register
889 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
890 spin_lock_irqsave(&adapter->int_lock, flags);
891 adapter->int_status |= sdio_ireg;
892 spin_unlock_irqrestore(&adapter->int_lock, flags);
897 * SDIO interrupt handler.
899 * This function reads the interrupt status from firmware and assigns
900 * the main process in workqueue which will handle the interrupt.
902 static void
903 mwifiex_sdio_interrupt(struct sdio_func *func)
905 struct mwifiex_adapter *adapter;
906 struct sdio_mmc_card *card;
908 card = sdio_get_drvdata(func);
909 if (!card || !card->adapter) {
910 pr_debug("int: func=%p card=%p adapter=%p\n",
911 func, card, card ? card->adapter : NULL);
912 return;
914 adapter = card->adapter;
916 if (adapter->surprise_removed)
917 return;
919 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
920 adapter->ps_state = PS_STATE_AWAKE;
922 mwifiex_interrupt_status(adapter);
923 queue_work(adapter->workqueue, &adapter->main_work);
927 * This function decodes a received packet.
929 * Based on the type, the packet is treated as either a data, or
930 * a command response, or an event, and the correct handler
931 * function is invoked.
933 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
934 struct sk_buff *skb, u32 upld_typ)
936 u8 *cmd_buf;
938 skb_pull(skb, INTF_HEADER_LEN);
940 switch (upld_typ) {
941 case MWIFIEX_TYPE_DATA:
942 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
943 mwifiex_handle_rx_packet(adapter, skb);
944 break;
946 case MWIFIEX_TYPE_CMD:
947 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
948 /* take care of curr_cmd = NULL case */
949 if (!adapter->curr_cmd) {
950 cmd_buf = adapter->upld_buf;
952 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
953 mwifiex_process_sleep_confirm_resp(adapter,
954 skb->data, skb->len);
956 memcpy(cmd_buf, skb->data, min_t(u32,
957 MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
959 dev_kfree_skb_any(skb);
960 } else {
961 adapter->cmd_resp_received = true;
962 adapter->curr_cmd->resp_skb = skb;
964 break;
966 case MWIFIEX_TYPE_EVENT:
967 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
968 adapter->event_cause = *(u32 *) skb->data;
970 skb_pull(skb, MWIFIEX_EVENT_HEADER_LEN);
972 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
973 memcpy(adapter->event_body, skb->data, skb->len);
975 /* event cause has been saved to adapter->event_cause */
976 adapter->event_received = true;
977 adapter->event_skb = skb;
979 break;
981 default:
982 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
983 dev_kfree_skb_any(skb);
984 break;
987 return 0;
991 * This function transfers received packets from card to driver, performing
992 * aggregation if required.
994 * For data received on control port, or if aggregation is disabled, the
995 * received buffers are uploaded as separate packets. However, if aggregation
996 * is enabled and required, the buffers are copied onto an aggregation buffer,
997 * provided there is space left, processed and finally uploaded.
999 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1000 struct sk_buff *skb, u8 port)
1002 struct sdio_mmc_card *card = adapter->card;
1003 s32 f_do_rx_aggr = 0;
1004 s32 f_do_rx_cur = 0;
1005 s32 f_aggr_cur = 0;
1006 struct sk_buff *skb_deaggr;
1007 u32 pind;
1008 u32 pkt_len, pkt_type = 0;
1009 u8 *curr_ptr;
1010 u32 rx_len = skb->len;
1012 if (port == CTRL_PORT) {
1013 /* Read the command Resp without aggr */
1014 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1015 "response\n", __func__);
1017 f_do_rx_cur = 1;
1018 goto rx_curr_single;
1021 if (!card->mpa_rx.enabled) {
1022 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1023 __func__);
1025 f_do_rx_cur = 1;
1026 goto rx_curr_single;
1029 if (card->mp_rd_bitmap & (~((u16) CTRL_PORT_MASK))) {
1030 /* Some more data RX pending */
1031 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1033 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1034 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1035 f_aggr_cur = 1;
1036 } else {
1037 /* No room in Aggr buf, do rx aggr now */
1038 f_do_rx_aggr = 1;
1039 f_do_rx_cur = 1;
1041 } else {
1042 /* Rx aggr not in progress */
1043 f_aggr_cur = 1;
1046 } else {
1047 /* No more data RX pending */
1048 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1050 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1051 f_do_rx_aggr = 1;
1052 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1053 f_aggr_cur = 1;
1054 else
1055 /* No room in Aggr buf, do rx aggr now */
1056 f_do_rx_cur = 1;
1057 } else {
1058 f_do_rx_cur = 1;
1062 if (f_aggr_cur) {
1063 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1064 /* Curr pkt can be aggregated */
1065 MP_RX_AGGR_SETUP(card, skb, port);
1067 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1068 MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
1069 dev_dbg(adapter->dev, "info: %s: aggregated packet "
1070 "limit reached\n", __func__);
1071 /* No more pkts allowed in Aggr buf, rx it */
1072 f_do_rx_aggr = 1;
1076 if (f_do_rx_aggr) {
1077 /* do aggr RX now */
1078 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1079 card->mpa_rx.pkt_cnt);
1081 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1082 card->mpa_rx.buf_len,
1083 (adapter->ioport | 0x1000 |
1084 (card->mpa_rx.ports << 4)) +
1085 card->mpa_rx.start_port, 1))
1086 return -1;
1088 curr_ptr = card->mpa_rx.buf;
1090 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1092 /* get curr PKT len & type */
1093 pkt_len = *(u16 *) &curr_ptr[0];
1094 pkt_type = *(u16 *) &curr_ptr[2];
1096 /* copy pkt to deaggr buf */
1097 skb_deaggr = card->mpa_rx.skb_arr[pind];
1099 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1100 card->mpa_rx.len_arr[pind])) {
1102 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1104 skb_trim(skb_deaggr, pkt_len);
1106 /* Process de-aggr packet */
1107 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1108 pkt_type);
1109 } else {
1110 dev_err(adapter->dev, "wrong aggr pkt:"
1111 " type=%d len=%d max_len=%d\n",
1112 pkt_type, pkt_len,
1113 card->mpa_rx.len_arr[pind]);
1114 dev_kfree_skb_any(skb_deaggr);
1116 curr_ptr += card->mpa_rx.len_arr[pind];
1118 MP_RX_AGGR_BUF_RESET(card);
1121 rx_curr_single:
1122 if (f_do_rx_cur) {
1123 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1124 port, rx_len);
1126 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1127 skb->data, skb->len,
1128 adapter->ioport + port))
1129 return -1;
1131 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1134 return 0;
1138 * This function checks the current interrupt status.
1140 * The following interrupts are checked and handled by this function -
1141 * - Data sent
1142 * - Command sent
1143 * - Packets received
1145 * Since the firmware does not generate download ready interrupt if the
1146 * port updated is command port only, command sent interrupt checking
1147 * should be done manually, and for every SDIO interrupt.
1149 * In case of Rx packets received, the packets are uploaded from card to
1150 * host and processed accordingly.
1152 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1154 struct sdio_mmc_card *card = adapter->card;
1155 int ret = 0;
1156 u8 sdio_ireg;
1157 struct sk_buff *skb;
1158 u8 port = CTRL_PORT;
1159 u32 len_reg_l, len_reg_u;
1160 u32 rx_blocks;
1161 u16 rx_len;
1162 unsigned long flags;
1164 spin_lock_irqsave(&adapter->int_lock, flags);
1165 sdio_ireg = adapter->int_status;
1166 adapter->int_status = 0;
1167 spin_unlock_irqrestore(&adapter->int_lock, flags);
1169 if (!sdio_ireg)
1170 return ret;
1172 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1173 card->mp_wr_bitmap = ((u16) card->mp_regs[WR_BITMAP_U]) << 8;
1174 card->mp_wr_bitmap |= (u16) card->mp_regs[WR_BITMAP_L];
1175 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%04x\n",
1176 card->mp_wr_bitmap);
1177 if (adapter->data_sent &&
1178 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1179 dev_dbg(adapter->dev,
1180 "info: <--- Tx DONE Interrupt --->\n");
1181 adapter->data_sent = false;
1185 /* As firmware will not generate download ready interrupt if the port
1186 updated is command port only, cmd_sent should be done for any SDIO
1187 interrupt. */
1188 if (adapter->cmd_sent) {
1189 /* Check if firmware has attach buffer at command port and
1190 update just that in wr_bit_map. */
1191 card->mp_wr_bitmap |=
1192 (u16) card->mp_regs[WR_BITMAP_L] & CTRL_PORT_MASK;
1193 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1194 adapter->cmd_sent = false;
1197 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1198 adapter->cmd_sent, adapter->data_sent);
1199 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1200 card->mp_rd_bitmap = ((u16) card->mp_regs[RD_BITMAP_U]) << 8;
1201 card->mp_rd_bitmap |= (u16) card->mp_regs[RD_BITMAP_L];
1202 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%04x\n",
1203 card->mp_rd_bitmap);
1205 while (true) {
1206 ret = mwifiex_get_rd_port(adapter, &port);
1207 if (ret) {
1208 dev_dbg(adapter->dev,
1209 "info: no more rd_port available\n");
1210 break;
1212 len_reg_l = RD_LEN_P0_L + (port << 1);
1213 len_reg_u = RD_LEN_P0_U + (port << 1);
1214 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1215 rx_len |= (u16) card->mp_regs[len_reg_l];
1216 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1217 port, rx_len);
1218 rx_blocks =
1219 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1220 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1221 if (rx_len <= INTF_HEADER_LEN
1222 || (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1223 MWIFIEX_RX_DATA_BUF_SIZE) {
1224 dev_err(adapter->dev, "invalid rx_len=%d\n",
1225 rx_len);
1226 return -1;
1228 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1230 skb = dev_alloc_skb(rx_len);
1232 if (!skb) {
1233 dev_err(adapter->dev, "%s: failed to alloc skb",
1234 __func__);
1235 return -1;
1238 skb_put(skb, rx_len);
1240 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1241 rx_len, skb->len);
1243 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1244 port)) {
1245 u32 cr = 0;
1247 dev_err(adapter->dev, "card_to_host_mpa failed:"
1248 " int status=%#x\n", sdio_ireg);
1249 if (mwifiex_read_reg(adapter,
1250 CONFIGURATION_REG, &cr))
1251 dev_err(adapter->dev,
1252 "read CFG reg failed\n");
1254 dev_dbg(adapter->dev,
1255 "info: CFG reg val = %d\n", cr);
1256 if (mwifiex_write_reg(adapter,
1257 CONFIGURATION_REG,
1258 (cr | 0x04)))
1259 dev_err(adapter->dev,
1260 "write CFG reg failed\n");
1262 dev_dbg(adapter->dev, "info: write success\n");
1263 if (mwifiex_read_reg(adapter,
1264 CONFIGURATION_REG, &cr))
1265 dev_err(adapter->dev,
1266 "read CFG reg failed\n");
1268 dev_dbg(adapter->dev,
1269 "info: CFG reg val =%x\n", cr);
1270 dev_kfree_skb_any(skb);
1271 return -1;
1276 return 0;
1280 * This function aggregates transmission buffers in driver and downloads
1281 * the aggregated packet to card.
1283 * The individual packets are aggregated by copying into an aggregation
1284 * buffer and then downloaded to the card. Previous unsent packets in the
1285 * aggregation buffer are pre-copied first before new packets are added.
1286 * Aggregation is done till there is space left in the aggregation buffer,
1287 * or till new packets are available.
1289 * The function will only download the packet to the card when aggregation
1290 * stops, otherwise it will just aggregate the packet in aggregation buffer
1291 * and return.
1293 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1294 u8 *payload, u32 pkt_len, u8 port,
1295 u32 next_pkt_len)
1297 struct sdio_mmc_card *card = adapter->card;
1298 int ret = 0;
1299 s32 f_send_aggr_buf = 0;
1300 s32 f_send_cur_buf = 0;
1301 s32 f_precopy_cur_buf = 0;
1302 s32 f_postcopy_cur_buf = 0;
1304 if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
1305 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1306 __func__);
1308 f_send_cur_buf = 1;
1309 goto tx_curr_single;
1312 if (next_pkt_len) {
1313 /* More pkt in TX queue */
1314 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1315 __func__);
1317 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1318 if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
1319 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1320 f_precopy_cur_buf = 1;
1322 if (!(card->mp_wr_bitmap &
1323 (1 << card->curr_wr_port))
1324 || !MP_TX_AGGR_BUF_HAS_ROOM(
1325 card, pkt_len + next_pkt_len))
1326 f_send_aggr_buf = 1;
1327 } else {
1328 /* No room in Aggr buf, send it */
1329 f_send_aggr_buf = 1;
1331 if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
1332 !(card->mp_wr_bitmap &
1333 (1 << card->curr_wr_port)))
1334 f_send_cur_buf = 1;
1335 else
1336 f_postcopy_cur_buf = 1;
1338 } else {
1339 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)
1340 && (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1341 f_precopy_cur_buf = 1;
1342 else
1343 f_send_cur_buf = 1;
1345 } else {
1346 /* Last pkt in TX queue */
1347 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1348 __func__);
1350 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1351 /* some packs in Aggr buf already */
1352 f_send_aggr_buf = 1;
1354 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1355 f_precopy_cur_buf = 1;
1356 else
1357 /* No room in Aggr buf, send it */
1358 f_send_cur_buf = 1;
1359 } else {
1360 f_send_cur_buf = 1;
1364 if (f_precopy_cur_buf) {
1365 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1366 __func__);
1367 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1369 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1370 MP_TX_AGGR_PORT_LIMIT_REACHED(card))
1371 /* No more pkts allowed in Aggr buf, send it */
1372 f_send_aggr_buf = 1;
1375 if (f_send_aggr_buf) {
1376 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1377 __func__,
1378 card->mpa_tx.start_port, card->mpa_tx.ports);
1379 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1380 card->mpa_tx.buf_len,
1381 (adapter->ioport | 0x1000 |
1382 (card->mpa_tx.ports << 4)) +
1383 card->mpa_tx.start_port);
1385 MP_TX_AGGR_BUF_RESET(card);
1388 tx_curr_single:
1389 if (f_send_cur_buf) {
1390 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1391 __func__, port);
1392 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1393 adapter->ioport + port);
1396 if (f_postcopy_cur_buf) {
1397 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1398 __func__);
1399 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1402 return ret;
1406 * This function downloads data from driver to card.
1408 * Both commands and data packets are transferred to the card by this
1409 * function.
1411 * This function adds the SDIO specific header to the front of the buffer
1412 * before transferring. The header contains the length of the packet and
1413 * the type. The firmware handles the packets based upon this set type.
1415 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1416 u8 type, u8 *payload, u32 pkt_len,
1417 struct mwifiex_tx_param *tx_param)
1419 struct sdio_mmc_card *card = adapter->card;
1420 int ret;
1421 u32 buf_block_len;
1422 u32 blk_size;
1423 u8 port = CTRL_PORT;
1425 /* Allocate buffer and copy payload */
1426 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1427 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1428 *(u16 *) &payload[0] = (u16) pkt_len;
1429 *(u16 *) &payload[2] = type;
1432 * This is SDIO specific header
1433 * u16 length,
1434 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1435 * MWIFIEX_TYPE_EVENT = 3)
1437 if (type == MWIFIEX_TYPE_DATA) {
1438 ret = mwifiex_get_wr_port_data(adapter, &port);
1439 if (ret) {
1440 dev_err(adapter->dev, "%s: no wr_port available\n",
1441 __func__);
1442 return ret;
1444 } else {
1445 adapter->cmd_sent = true;
1446 /* Type must be MWIFIEX_TYPE_CMD */
1448 if (pkt_len <= INTF_HEADER_LEN ||
1449 pkt_len > MWIFIEX_UPLD_SIZE)
1450 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1451 __func__, payload, pkt_len);
1454 /* Transfer data to card */
1455 pkt_len = buf_block_len * blk_size;
1457 if (tx_param)
1458 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1459 port, tx_param->next_pkt_len);
1460 else
1461 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1462 port, 0);
1464 if (ret) {
1465 if (type == MWIFIEX_TYPE_CMD)
1466 adapter->cmd_sent = false;
1467 if (type == MWIFIEX_TYPE_DATA)
1468 adapter->data_sent = false;
1469 } else {
1470 if (type == MWIFIEX_TYPE_DATA) {
1471 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1472 adapter->data_sent = true;
1473 else
1474 adapter->data_sent = false;
1478 return ret;
1482 * This function allocates the MPA Tx and Rx buffers.
1484 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1485 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1487 struct sdio_mmc_card *card = adapter->card;
1488 int ret = 0;
1490 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1491 if (!card->mpa_tx.buf) {
1492 dev_err(adapter->dev, "could not alloc buffer for MP-A TX\n");
1493 ret = -1;
1494 goto error;
1497 card->mpa_tx.buf_size = mpa_tx_buf_size;
1499 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1500 if (!card->mpa_rx.buf) {
1501 dev_err(adapter->dev, "could not alloc buffer for MP-A RX\n");
1502 ret = -1;
1503 goto error;
1506 card->mpa_rx.buf_size = mpa_rx_buf_size;
1508 error:
1509 if (ret) {
1510 kfree(card->mpa_tx.buf);
1511 kfree(card->mpa_rx.buf);
1514 return ret;
1518 * This function unregisters the SDIO device.
1520 * The SDIO IRQ is released, the function is disabled and driver
1521 * data is set to null.
1523 static void
1524 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1526 struct sdio_mmc_card *card = adapter->card;
1528 if (adapter->card) {
1529 /* Release the SDIO IRQ */
1530 sdio_claim_host(card->func);
1531 sdio_release_irq(card->func);
1532 sdio_disable_func(card->func);
1533 sdio_release_host(card->func);
1534 sdio_set_drvdata(card->func, NULL);
1539 * This function registers the SDIO device.
1541 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1543 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1545 int ret = 0;
1546 struct sdio_mmc_card *card = adapter->card;
1547 struct sdio_func *func = card->func;
1549 /* save adapter pointer in card */
1550 card->adapter = adapter;
1552 sdio_claim_host(func);
1554 /* Request the SDIO IRQ */
1555 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1556 if (ret) {
1557 pr_err("claim irq failed: ret=%d\n", ret);
1558 goto disable_func;
1561 /* Set block size */
1562 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1563 if (ret) {
1564 pr_err("cannot set SDIO block size\n");
1565 ret = -1;
1566 goto release_irq;
1569 sdio_release_host(func);
1570 sdio_set_drvdata(func, card);
1572 adapter->dev = &func->dev;
1573 strcpy(adapter->fw_name, SD8787_DEFAULT_FW_NAME);
1575 return 0;
1577 release_irq:
1578 sdio_release_irq(func);
1579 disable_func:
1580 sdio_disable_func(func);
1581 sdio_release_host(func);
1582 adapter->card = NULL;
1584 return -1;
1588 * This function initializes the SDIO driver.
1590 * The following initializations steps are followed -
1591 * - Read the Host interrupt status register to acknowledge
1592 * the first interrupt got from bootloader
1593 * - Disable host interrupt mask register
1594 * - Get SDIO port
1595 * - Initialize SDIO variables in card
1596 * - Allocate MP registers
1597 * - Allocate MPA Tx and Rx buffers
1599 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1601 struct sdio_mmc_card *card = adapter->card;
1602 int ret;
1603 u32 sdio_ireg;
1606 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1607 * from the bootloader. If we don't do this we get a interrupt
1608 * as soon as we register the irq.
1610 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1612 /* Disable host interrupt mask register for SDIO */
1613 mwifiex_sdio_disable_host_int(adapter);
1615 /* Get SDIO ioport */
1616 mwifiex_init_sdio_ioport(adapter);
1618 /* Initialize SDIO variables in card */
1619 card->mp_rd_bitmap = 0;
1620 card->mp_wr_bitmap = 0;
1621 card->curr_rd_port = 1;
1622 card->curr_wr_port = 1;
1624 card->mp_data_port_mask = DATA_PORT_MASK;
1626 card->mpa_tx.buf_len = 0;
1627 card->mpa_tx.pkt_cnt = 0;
1628 card->mpa_tx.start_port = 0;
1630 card->mpa_tx.enabled = 0;
1631 card->mpa_tx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1633 card->mpa_rx.buf_len = 0;
1634 card->mpa_rx.pkt_cnt = 0;
1635 card->mpa_rx.start_port = 0;
1637 card->mpa_rx.enabled = 0;
1638 card->mpa_rx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1640 /* Allocate buffers for SDIO MP-A */
1641 card->mp_regs = kzalloc(MAX_MP_REGS, GFP_KERNEL);
1642 if (!card->mp_regs) {
1643 dev_err(adapter->dev, "failed to alloc mp_regs\n");
1644 return -ENOMEM;
1647 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1648 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1649 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1650 if (ret) {
1651 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1652 kfree(card->mp_regs);
1653 return -1;
1656 return ret;
1660 * This function resets the MPA Tx and Rx buffers.
1662 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1664 struct sdio_mmc_card *card = adapter->card;
1666 MP_TX_AGGR_BUF_RESET(card);
1667 MP_RX_AGGR_BUF_RESET(card);
1671 * This function cleans up the allocated card buffers.
1673 * The following are freed by this function -
1674 * - MP registers
1675 * - MPA Tx buffer
1676 * - MPA Rx buffer
1678 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1680 struct sdio_mmc_card *card = adapter->card;
1682 kfree(card->mp_regs);
1683 kfree(card->mpa_tx.buf);
1684 kfree(card->mpa_rx.buf);
1688 * This function updates the MP end port in card.
1690 static void
1691 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1693 struct sdio_mmc_card *card = adapter->card;
1694 int i;
1696 card->mp_end_port = port;
1698 card->mp_data_port_mask = DATA_PORT_MASK;
1700 for (i = 1; i <= MAX_PORT - card->mp_end_port; i++)
1701 card->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
1703 card->curr_wr_port = 1;
1705 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1706 port, card->mp_data_port_mask);
1709 static struct mwifiex_if_ops sdio_ops = {
1710 .init_if = mwifiex_init_sdio,
1711 .cleanup_if = mwifiex_cleanup_sdio,
1712 .check_fw_status = mwifiex_check_fw_status,
1713 .prog_fw = mwifiex_prog_fw_w_helper,
1714 .register_dev = mwifiex_register_dev,
1715 .unregister_dev = mwifiex_unregister_dev,
1716 .enable_int = mwifiex_sdio_enable_host_int,
1717 .process_int_status = mwifiex_process_int_status,
1718 .host_to_card = mwifiex_sdio_host_to_card,
1719 .wakeup = mwifiex_pm_wakeup_card,
1720 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1722 /* SDIO specific */
1723 .update_mp_end_port = mwifiex_update_mp_end_port,
1724 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
1728 * This function initializes the SDIO driver.
1730 * This initiates the semaphore and registers the device with
1731 * SDIO bus.
1733 static int
1734 mwifiex_sdio_init_module(void)
1736 sema_init(&add_remove_card_sem, 1);
1738 /* Clear the flag in case user removes the card. */
1739 user_rmmod = 0;
1741 return sdio_register_driver(&mwifiex_sdio);
1745 * This function cleans up the SDIO driver.
1747 * The following major steps are followed for cleanup -
1748 * - Resume the device if its suspended
1749 * - Disconnect the device if connected
1750 * - Shutdown the firmware
1751 * - Unregister the device from SDIO bus.
1753 static void
1754 mwifiex_sdio_cleanup_module(void)
1756 if (!down_interruptible(&add_remove_card_sem))
1757 up(&add_remove_card_sem);
1759 /* Set the flag as user is removing this module. */
1760 user_rmmod = 1;
1762 sdio_unregister_driver(&mwifiex_sdio);
1765 module_init(mwifiex_sdio_init_module);
1766 module_exit(mwifiex_sdio_cleanup_module);
1768 MODULE_AUTHOR("Marvell International Ltd.");
1769 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
1770 MODULE_VERSION(SDIO_VERSION);
1771 MODULE_LICENSE("GPL v2");
1772 MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");