8ad602f22b7acf22d893a7eaf808d64c9e8e0bdc
[cascardo/linux.git] / drivers / net / wireless / mwifiex / sdio.c
1 /*
2  * Marvell Wireless LAN device driver: SDIO specific handling
3  *
4  * Copyright (C) 2011, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * 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.
18  */
19
20 #include <linux/firmware.h>
21
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"
30
31
32 #define SDIO_VERSION    "1.0"
33
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.
41  *
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.
46  */
47 static u8 user_rmmod;
48
49 static struct mwifiex_if_ops sdio_ops;
50
51 static struct semaphore add_remove_card_sem;
52
53 /* enum mwifiex_sdio_work_flags bitmap */
54 static unsigned long sdio_work_flags;
55
56 static struct mwifiex_adapter *reg_dbg_adapter;
57 static struct mwifiex_adapter *reset_adapter;
58
59 static int mwifiex_sdio_resume(struct device *dev);
60
61 /*
62  * SDIO probe.
63  *
64  * This function probes an mwifiex device and registers it. It allocates
65  * the card structure, enables SDIO function number and initiates the
66  * device registration and initialization procedure by adding a logical
67  * interface.
68  */
69 static int
70 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
71 {
72         int ret;
73         struct sdio_mmc_card *card = NULL;
74
75         pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
76                  func->vendor, func->device, func->class, func->num);
77
78         card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
79         if (!card)
80                 return -ENOMEM;
81
82         card->func = func;
83
84         func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
85
86         sdio_claim_host(func);
87         ret = sdio_enable_func(func);
88         sdio_release_host(func);
89
90         if (ret) {
91                 pr_err("%s: failed to enable function\n", __func__);
92                 kfree(card);
93                 return -EIO;
94         }
95
96         if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
97                              MWIFIEX_SDIO)) {
98                 pr_err("%s: add card failed\n", __func__);
99                 kfree(card);
100                 sdio_claim_host(func);
101                 ret = sdio_disable_func(func);
102                 sdio_release_host(func);
103                 ret = -1;
104         }
105
106         return ret;
107 }
108
109 /*
110  * SDIO remove.
111  *
112  * This function removes the interface and frees up the card structure.
113  */
114 static void
115 mwifiex_sdio_remove(struct sdio_func *func)
116 {
117         struct sdio_mmc_card *card;
118         struct mwifiex_adapter *adapter;
119         struct mwifiex_private *priv;
120         int i;
121
122         pr_debug("info: SDIO func num=%d\n", func->num);
123
124         card = sdio_get_drvdata(func);
125         if (!card)
126                 return;
127
128         adapter = card->adapter;
129         if (!adapter || !adapter->priv_num)
130                 return;
131
132         /* In case driver is removed when asynchronous FW load is in progress */
133         wait_for_completion(&adapter->fw_load);
134
135         if (user_rmmod) {
136                 if (adapter->is_suspended)
137                         mwifiex_sdio_resume(adapter->dev);
138
139                 for (i = 0; i < adapter->priv_num; i++)
140                         if ((GET_BSS_ROLE(adapter->priv[i]) ==
141                                                 MWIFIEX_BSS_ROLE_STA) &&
142                             adapter->priv[i]->media_connected)
143                                 mwifiex_deauthenticate(adapter->priv[i], NULL);
144
145                 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
146                 mwifiex_disable_auto_ds(priv);
147                 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
148         }
149
150         mwifiex_remove_card(card->adapter, &add_remove_card_sem);
151         kfree(card);
152 }
153
154 /*
155  * SDIO suspend.
156  *
157  * Kernel needs to suspend all functions separately. Therefore all
158  * registered functions must have drivers with suspend and resume
159  * methods. Failing that the kernel simply removes the whole card.
160  *
161  * If already not suspended, this function allocates and sends a host
162  * sleep activate request to the firmware and turns off the traffic.
163  */
164 static int mwifiex_sdio_suspend(struct device *dev)
165 {
166         struct sdio_func *func = dev_to_sdio_func(dev);
167         struct sdio_mmc_card *card;
168         struct mwifiex_adapter *adapter;
169         mmc_pm_flag_t pm_flag = 0;
170         int i;
171         int ret = 0;
172
173         if (func) {
174                 pm_flag = sdio_get_host_pm_caps(func);
175                 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
176                          sdio_func_id(func), pm_flag);
177                 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
178                         pr_err("%s: cannot remain alive while host is"
179                                 " suspended\n", sdio_func_id(func));
180                         return -ENOSYS;
181                 }
182
183                 card = sdio_get_drvdata(func);
184                 if (!card || !card->adapter) {
185                         pr_err("suspend: invalid card or adapter\n");
186                         return 0;
187                 }
188         } else {
189                 pr_err("suspend: sdio_func is not specified\n");
190                 return 0;
191         }
192
193         adapter = card->adapter;
194
195         /* Enable the Host Sleep */
196         if (!mwifiex_enable_hs(adapter)) {
197                 dev_err(adapter->dev, "cmd: failed to suspend\n");
198                 return -EFAULT;
199         }
200
201         dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
202         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
203
204         /* Indicate device suspended */
205         adapter->is_suspended = true;
206
207         for (i = 0; i < adapter->priv_num; i++)
208                 netif_carrier_off(adapter->priv[i]->netdev);
209
210         return ret;
211 }
212
213 /*
214  * SDIO resume.
215  *
216  * Kernel needs to suspend all functions separately. Therefore all
217  * registered functions must have drivers with suspend and resume
218  * methods. Failing that the kernel simply removes the whole card.
219  *
220  * If already not resumed, this function turns on the traffic and
221  * sends a host sleep cancel request to the firmware.
222  */
223 static int mwifiex_sdio_resume(struct device *dev)
224 {
225         struct sdio_func *func = dev_to_sdio_func(dev);
226         struct sdio_mmc_card *card;
227         struct mwifiex_adapter *adapter;
228         mmc_pm_flag_t pm_flag = 0;
229         int i;
230
231         if (func) {
232                 pm_flag = sdio_get_host_pm_caps(func);
233                 card = sdio_get_drvdata(func);
234                 if (!card || !card->adapter) {
235                         pr_err("resume: invalid card or adapter\n");
236                         return 0;
237                 }
238         } else {
239                 pr_err("resume: sdio_func is not specified\n");
240                 return 0;
241         }
242
243         adapter = card->adapter;
244
245         if (!adapter->is_suspended) {
246                 dev_warn(adapter->dev, "device already resumed\n");
247                 return 0;
248         }
249
250         adapter->is_suspended = false;
251
252         for (i = 0; i < adapter->priv_num; i++)
253                 if (adapter->priv[i]->media_connected)
254                         netif_carrier_on(adapter->priv[i]->netdev);
255
256         /* Disable Host Sleep */
257         mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
258                           MWIFIEX_ASYNC_CMD);
259
260         return 0;
261 }
262
263 /* Device ID for SD8787 */
264 #define SDIO_DEVICE_ID_MARVELL_8787   (0x9119)
265 /* Device ID for SD8797 */
266 #define SDIO_DEVICE_ID_MARVELL_8797   (0x9129)
267
268 /* WLAN IDs */
269 static const struct sdio_device_id mwifiex_ids[] = {
270         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787)},
271         {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797)},
272         {},
273 };
274
275 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
276
277 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
278         .suspend = mwifiex_sdio_suspend,
279         .resume = mwifiex_sdio_resume,
280 };
281
282 static struct sdio_driver mwifiex_sdio = {
283         .name = "mwifiex_sdio",
284         .id_table = mwifiex_ids,
285         .probe = mwifiex_sdio_probe,
286         .remove = mwifiex_sdio_remove,
287         .drv = {
288                 .owner = THIS_MODULE,
289                 .pm = &mwifiex_sdio_pm_ops,
290         }
291 };
292
293 /*
294  * This function writes data into SDIO card register.
295  */
296 static int
297 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u32 data)
298 {
299         struct sdio_mmc_card *card = adapter->card;
300         int ret = -1;
301
302         sdio_claim_host(card->func);
303         sdio_writeb(card->func, (u8) data, reg, &ret);
304         sdio_release_host(card->func);
305
306         return ret;
307 }
308
309 /*
310  * This function reads data from SDIO card register.
311  */
312 static int
313 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u32 *data)
314 {
315         struct sdio_mmc_card *card = adapter->card;
316         int ret = -1;
317         u8 val;
318
319         sdio_claim_host(card->func);
320         val = sdio_readb(card->func, reg, &ret);
321         sdio_release_host(card->func);
322
323         *data = val;
324
325         return ret;
326 }
327
328 /*
329  * This function writes multiple data into SDIO card memory.
330  *
331  * This does not work in suspended mode.
332  */
333 static int
334 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
335                         u8 *buffer, u32 pkt_len, u32 port)
336 {
337         struct sdio_mmc_card *card = adapter->card;
338         int ret = -1;
339         u8 blk_mode =
340                 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
341         u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
342         u32 blk_cnt =
343                 (blk_mode ==
344                  BLOCK_MODE) ? (pkt_len /
345                                 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
346         u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
347
348         if (adapter->is_suspended) {
349                 dev_err(adapter->dev,
350                         "%s: not allowed while suspended\n", __func__);
351                 return -1;
352         }
353
354         sdio_claim_host(card->func);
355
356         if (!sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size))
357                 ret = 0;
358
359         sdio_release_host(card->func);
360
361         return ret;
362 }
363
364 /*
365  * This function reads multiple data from SDIO card memory.
366  */
367 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
368                                   u32 len, u32 port, u8 claim)
369 {
370         struct sdio_mmc_card *card = adapter->card;
371         int ret = -1;
372         u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
373                        : BLOCK_MODE;
374         u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
375         u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
376                         : len;
377         u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
378
379         if (claim)
380                 sdio_claim_host(card->func);
381
382         if (!sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size))
383                 ret = 0;
384
385         if (claim)
386                 sdio_release_host(card->func);
387
388         return ret;
389 }
390
391 /*
392  * This function wakes up the card.
393  *
394  * A host power up command is written to the card configuration
395  * register to wake up the card.
396  */
397 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
398 {
399         dev_dbg(adapter->dev, "event: wakeup device...\n");
400
401         return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
402 }
403
404 /*
405  * This function is called after the card has woken up.
406  *
407  * The card configuration register is reset.
408  */
409 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
410 {
411         dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
412
413         return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
414 }
415
416 /*
417  * This function initializes the IO ports.
418  *
419  * The following operations are performed -
420  *      - Read the IO ports (0, 1 and 2)
421  *      - Set host interrupt Reset-To-Read to clear
422  *      - Set auto re-enable interrupt
423  */
424 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
425 {
426         u32 reg;
427
428         adapter->ioport = 0;
429
430         /* Read the IO port */
431         if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
432                 adapter->ioport |= (reg & 0xff);
433         else
434                 return -1;
435
436         if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
437                 adapter->ioport |= ((reg & 0xff) << 8);
438         else
439                 return -1;
440
441         if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
442                 adapter->ioport |= ((reg & 0xff) << 16);
443         else
444                 return -1;
445
446         pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
447
448         /* Set Host interrupt reset to read to clear */
449         if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
450                 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
451                                   reg | SDIO_INT_MASK);
452         else
453                 return -1;
454
455         /* Dnld/Upld ready set to auto reset */
456         if (!mwifiex_read_reg(adapter, CARD_MISC_CFG_REG, &reg))
457                 mwifiex_write_reg(adapter, CARD_MISC_CFG_REG,
458                                   reg | AUTO_RE_ENABLE_INT);
459         else
460                 return -1;
461
462         return 0;
463 }
464
465 /*
466  * This function sends data to the card.
467  */
468 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
469                                       u8 *payload, u32 pkt_len, u32 port)
470 {
471         u32 i = 0;
472         int ret;
473
474         do {
475                 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
476                 if (ret) {
477                         i++;
478                         dev_err(adapter->dev, "host_to_card, write iomem"
479                                         " (%d) failed: %d\n", i, ret);
480                         if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
481                                 dev_err(adapter->dev, "write CFG reg failed\n");
482
483                         ret = -1;
484                         if (i > MAX_WRITE_IOMEM_RETRY)
485                                 return ret;
486                 }
487         } while (ret == -1);
488
489         return ret;
490 }
491
492 /*
493  * This function gets the read port.
494  *
495  * If control port bit is set in MP read bitmap, the control port
496  * is returned, otherwise the current read port is returned and
497  * the value is increased (provided it does not reach the maximum
498  * limit, in which case it is reset to 1)
499  */
500 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
501 {
502         struct sdio_mmc_card *card = adapter->card;
503         u16 rd_bitmap = card->mp_rd_bitmap;
504
505         dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%04x\n", rd_bitmap);
506
507         if (!(rd_bitmap & (CTRL_PORT_MASK | DATA_PORT_MASK)))
508                 return -1;
509
510         if (card->mp_rd_bitmap & CTRL_PORT_MASK) {
511                 card->mp_rd_bitmap &= (u16) (~CTRL_PORT_MASK);
512                 *port = CTRL_PORT;
513                 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%04x\n",
514                         *port, card->mp_rd_bitmap);
515         } else {
516                 if (card->mp_rd_bitmap & (1 << card->curr_rd_port)) {
517                         card->mp_rd_bitmap &= (u16)
518                                                 (~(1 << card->curr_rd_port));
519                         *port = card->curr_rd_port;
520
521                         if (++card->curr_rd_port == MAX_PORT)
522                                 card->curr_rd_port = 1;
523                 } else {
524                         return -1;
525                 }
526
527                 dev_dbg(adapter->dev,
528                         "data: port=%d mp_rd_bitmap=0x%04x -> 0x%04x\n",
529                         *port, rd_bitmap, card->mp_rd_bitmap);
530         }
531         return 0;
532 }
533
534 /*
535  * This function gets the write port for data.
536  *
537  * The current write port is returned if available and the value is
538  * increased (provided it does not reach the maximum limit, in which
539  * case it is reset to 1)
540  */
541 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u8 *port)
542 {
543         struct sdio_mmc_card *card = adapter->card;
544         u16 wr_bitmap = card->mp_wr_bitmap;
545
546         dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%04x\n", wr_bitmap);
547
548         if (!(wr_bitmap & card->mp_data_port_mask))
549                 return -1;
550
551         if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
552                 card->mp_wr_bitmap &= (u16) (~(1 << card->curr_wr_port));
553                 *port = card->curr_wr_port;
554                 if (++card->curr_wr_port == card->mp_end_port)
555                         card->curr_wr_port = 1;
556         } else {
557                 adapter->data_sent = true;
558                 return -EBUSY;
559         }
560
561         if (*port == CTRL_PORT) {
562                 dev_err(adapter->dev, "invalid data port=%d cur port=%d"
563                         " mp_wr_bitmap=0x%04x -> 0x%04x\n",
564                         *port, card->curr_wr_port, wr_bitmap,
565                         card->mp_wr_bitmap);
566                 return -1;
567         }
568
569         dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%04x -> 0x%04x\n",
570                 *port, wr_bitmap, card->mp_wr_bitmap);
571
572         return 0;
573 }
574
575 /*
576  * This function polls the card status.
577  */
578 static int
579 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
580 {
581         u32 tries;
582         u32 cs;
583
584         for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
585                 if (mwifiex_read_reg(adapter, CARD_STATUS_REG, &cs))
586                         break;
587                 else if ((cs & bits) == bits)
588                         return 0;
589
590                 usleep_range(10, 20);
591         }
592
593         dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
594
595         return -1;
596 }
597
598 /*
599  * This function reads the firmware status.
600  */
601 static int
602 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
603 {
604         u32 fws0, fws1;
605
606         if (mwifiex_read_reg(adapter, CARD_FW_STATUS0_REG, &fws0))
607                 return -1;
608
609         if (mwifiex_read_reg(adapter, CARD_FW_STATUS1_REG, &fws1))
610                 return -1;
611
612         *dat = (u16) ((fws1 << 8) | fws0);
613
614         return 0;
615 }
616
617 /*
618  * This function disables the host interrupt.
619  *
620  * The host interrupt mask is read, the disable bit is reset and
621  * written back to the card host interrupt mask register.
622  */
623 static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
624 {
625         u32 host_int_mask;
626
627         /* Read back the host_int_mask register */
628         if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
629                 return -1;
630
631         /* Update with the mask and write back to the register */
632         host_int_mask &= ~HOST_INT_DISABLE;
633
634         if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
635                 dev_err(adapter->dev, "disable host interrupt failed\n");
636                 return -1;
637         }
638
639         return 0;
640 }
641
642 /*
643  * This function enables the host interrupt.
644  *
645  * The host interrupt enable mask is written to the card
646  * host interrupt mask register.
647  */
648 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
649 {
650         /* Simply write the mask to the register */
651         if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, HOST_INT_ENABLE)) {
652                 dev_err(adapter->dev, "enable host interrupt failed\n");
653                 return -1;
654         }
655         return 0;
656 }
657
658 /*
659  * This function sends a data buffer to the card.
660  */
661 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
662                                      u32 *type, u8 *buffer,
663                                      u32 npayload, u32 ioport)
664 {
665         int ret;
666         u32 nb;
667
668         if (!buffer) {
669                 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
670                 return -1;
671         }
672
673         ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
674
675         if (ret) {
676                 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
677                         ret);
678                 return -1;
679         }
680
681         nb = le16_to_cpu(*(__le16 *) (buffer));
682         if (nb > npayload) {
683                 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
684                         __func__, nb, npayload);
685                 return -1;
686         }
687
688         *type = le16_to_cpu(*(__le16 *) (buffer + 2));
689
690         return ret;
691 }
692
693 /*
694  * This function downloads the firmware to the card.
695  *
696  * Firmware is downloaded to the card in blocks. Every block download
697  * is tested for CRC errors, and retried a number of times before
698  * returning failure.
699  */
700 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
701                                     struct mwifiex_fw_image *fw)
702 {
703         int ret;
704         u8 *firmware = fw->fw_buf;
705         u32 firmware_len = fw->fw_len;
706         u32 offset = 0;
707         u32 base0, base1;
708         u8 *fwbuf;
709         u16 len = 0;
710         u32 txlen, tx_blocks = 0, tries;
711         u32 i = 0;
712
713         if (!firmware_len) {
714                 dev_err(adapter->dev,
715                         "firmware image not found! Terminating download\n");
716                 return -1;
717         }
718
719         dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
720                 firmware_len);
721
722         /* Assume that the allocated buffer is 8-byte aligned */
723         fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
724         if (!fwbuf) {
725                 dev_err(adapter->dev,
726                         "unable to alloc buffer for FW. Terminating dnld\n");
727                 return -ENOMEM;
728         }
729
730         /* Perform firmware data transfer */
731         do {
732                 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
733                    bits */
734                 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
735                                                     DN_LD_CARD_RDY);
736                 if (ret) {
737                         dev_err(adapter->dev, "FW download with helper:"
738                                 " poll status timeout @ %d\n", offset);
739                         goto done;
740                 }
741
742                 /* More data? */
743                 if (offset >= firmware_len)
744                         break;
745
746                 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
747                         ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_0,
748                                                &base0);
749                         if (ret) {
750                                 dev_err(adapter->dev,
751                                         "dev BASE0 register read failed: "
752                                         "base0=%#04X(%d). Terminating dnld\n",
753                                         base0, base0);
754                                 goto done;
755                         }
756                         ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_1,
757                                                &base1);
758                         if (ret) {
759                                 dev_err(adapter->dev,
760                                         "dev BASE1 register read failed: "
761                                         "base1=%#04X(%d). Terminating dnld\n",
762                                         base1, base1);
763                                 goto done;
764                         }
765                         len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
766
767                         if (len)
768                                 break;
769
770                         usleep_range(10, 20);
771                 }
772
773                 if (!len) {
774                         break;
775                 } else if (len > MWIFIEX_UPLD_SIZE) {
776                         dev_err(adapter->dev,
777                                 "FW dnld failed @ %d, invalid length %d\n",
778                                 offset, len);
779                         ret = -1;
780                         goto done;
781                 }
782
783                 txlen = len;
784
785                 if (len & BIT(0)) {
786                         i++;
787                         if (i > MAX_WRITE_IOMEM_RETRY) {
788                                 dev_err(adapter->dev,
789                                         "FW dnld failed @ %d, over max retry\n",
790                                         offset);
791                                 ret = -1;
792                                 goto done;
793                         }
794                         dev_err(adapter->dev, "CRC indicated by the helper:"
795                                 " len = 0x%04X, txlen = %d\n", len, txlen);
796                         len &= ~BIT(0);
797                         /* Setting this to 0 to resend from same offset */
798                         txlen = 0;
799                 } else {
800                         i = 0;
801
802                         /* Set blocksize to transfer - checking for last
803                            block */
804                         if (firmware_len - offset < txlen)
805                                 txlen = firmware_len - offset;
806
807                         tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
808                                     / MWIFIEX_SDIO_BLOCK_SIZE;
809
810                         /* Copy payload to buffer */
811                         memmove(fwbuf, &firmware[offset], txlen);
812                 }
813
814                 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
815                                               MWIFIEX_SDIO_BLOCK_SIZE,
816                                               adapter->ioport);
817                 if (ret) {
818                         dev_err(adapter->dev,
819                                 "FW download, write iomem (%d) failed @ %d\n",
820                                 i, offset);
821                         if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
822                                 dev_err(adapter->dev, "write CFG reg failed\n");
823
824                         ret = -1;
825                         goto done;
826                 }
827
828                 offset += txlen;
829         } while (true);
830
831         dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
832                 offset);
833
834         ret = 0;
835 done:
836         kfree(fwbuf);
837         return ret;
838 }
839
840 /*
841  * This function checks the firmware status in card.
842  *
843  * The winner interface is also determined by this function.
844  */
845 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
846                                    u32 poll_num)
847 {
848         int ret = 0;
849         u16 firmware_stat;
850         u32 tries;
851         u32 winner_status;
852
853         /* Wait for firmware initialization event */
854         for (tries = 0; tries < poll_num; tries++) {
855                 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
856                 if (ret)
857                         continue;
858                 if (firmware_stat == FIRMWARE_READY_SDIO) {
859                         ret = 0;
860                         break;
861                 } else {
862                         mdelay(100);
863                         ret = -1;
864                 }
865         }
866
867         if (ret) {
868                 if (mwifiex_read_reg
869                     (adapter, CARD_FW_STATUS0_REG, &winner_status))
870                         winner_status = 0;
871
872                 if (winner_status)
873                         adapter->winner = 0;
874                 else
875                         adapter->winner = 1;
876         }
877         return ret;
878 }
879
880 /*
881  * This function reads the interrupt status from card.
882  */
883 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
884 {
885         struct sdio_mmc_card *card = adapter->card;
886         u32 sdio_ireg;
887         unsigned long flags;
888
889         if (mwifiex_read_data_sync(adapter, card->mp_regs, MAX_MP_REGS,
890                                    REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK,
891                                    0)) {
892                 dev_err(adapter->dev, "read mp_regs failed\n");
893                 return;
894         }
895
896         sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
897         if (sdio_ireg) {
898                 /*
899                  * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
900                  * Clear the interrupt status register
901                  */
902                 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
903                 spin_lock_irqsave(&adapter->int_lock, flags);
904                 adapter->int_status |= sdio_ireg;
905                 spin_unlock_irqrestore(&adapter->int_lock, flags);
906         }
907 }
908
909 /*
910  * SDIO interrupt handler.
911  *
912  * This function reads the interrupt status from firmware and handles
913  * the interrupt in current thread (ksdioirqd) right away.
914  */
915 static void
916 mwifiex_sdio_interrupt(struct sdio_func *func)
917 {
918         struct mwifiex_adapter *adapter;
919         struct sdio_mmc_card *card;
920
921         card = sdio_get_drvdata(func);
922         if (!card || !card->adapter) {
923                 pr_debug("int: func=%p card=%p adapter=%p\n",
924                          func, card, card ? card->adapter : NULL);
925                 return;
926         }
927         adapter = card->adapter;
928
929         if (adapter->surprise_removed)
930                 return;
931
932         if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
933                 adapter->ps_state = PS_STATE_AWAKE;
934
935         mwifiex_interrupt_status(adapter);
936         mwifiex_main_process(adapter);
937 }
938
939 /*
940  * This function decodes a received packet.
941  *
942  * Based on the type, the packet is treated as either a data, or
943  * a command response, or an event, and the correct handler
944  * function is invoked.
945  */
946 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
947                                     struct sk_buff *skb, u32 upld_typ)
948 {
949         u8 *cmd_buf;
950
951         skb_pull(skb, INTF_HEADER_LEN);
952
953         switch (upld_typ) {
954         case MWIFIEX_TYPE_DATA:
955                 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
956                 mwifiex_handle_rx_packet(adapter, skb);
957                 break;
958
959         case MWIFIEX_TYPE_CMD:
960                 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
961                 /* take care of curr_cmd = NULL case */
962                 if (!adapter->curr_cmd) {
963                         cmd_buf = adapter->upld_buf;
964
965                         if (adapter->ps_state == PS_STATE_SLEEP_CFM)
966                                 mwifiex_process_sleep_confirm_resp(adapter,
967                                                                    skb->data,
968                                                                    skb->len);
969
970                         memcpy(cmd_buf, skb->data,
971                                min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
972                                      skb->len));
973
974                         dev_kfree_skb_any(skb);
975                 } else {
976                         adapter->cmd_resp_received = true;
977                         adapter->curr_cmd->resp_skb = skb;
978                 }
979                 break;
980
981         case MWIFIEX_TYPE_EVENT:
982                 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
983                 adapter->event_cause = *(u32 *) skb->data;
984
985                 skb_pull(skb, MWIFIEX_EVENT_HEADER_LEN);
986
987                 if ((skb->len > 0) && (skb->len  < MAX_EVENT_SIZE))
988                         memcpy(adapter->event_body, skb->data, skb->len);
989
990                 /* event cause has been saved to adapter->event_cause */
991                 adapter->event_received = true;
992                 adapter->event_skb = skb;
993
994                 break;
995
996         default:
997                 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
998                 dev_kfree_skb_any(skb);
999                 break;
1000         }
1001
1002         return 0;
1003 }
1004
1005 /*
1006  * This function transfers received packets from card to driver, performing
1007  * aggregation if required.
1008  *
1009  * For data received on control port, or if aggregation is disabled, the
1010  * received buffers are uploaded as separate packets. However, if aggregation
1011  * is enabled and required, the buffers are copied onto an aggregation buffer,
1012  * provided there is space left, processed and finally uploaded.
1013  */
1014 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1015                                              struct sk_buff *skb, u8 port)
1016 {
1017         struct sdio_mmc_card *card = adapter->card;
1018         s32 f_do_rx_aggr = 0;
1019         s32 f_do_rx_cur = 0;
1020         s32 f_aggr_cur = 0;
1021         struct sk_buff *skb_deaggr;
1022         u32 pind;
1023         u32 pkt_len, pkt_type = 0;
1024         u8 *curr_ptr;
1025         u32 rx_len = skb->len;
1026
1027         if (port == CTRL_PORT) {
1028                 /* Read the command Resp without aggr */
1029                 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
1030                         "response\n", __func__);
1031
1032                 f_do_rx_cur = 1;
1033                 goto rx_curr_single;
1034         }
1035
1036         if (!card->mpa_rx.enabled) {
1037                 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
1038                         __func__);
1039
1040                 f_do_rx_cur = 1;
1041                 goto rx_curr_single;
1042         }
1043
1044         if (card->mp_rd_bitmap & (~((u16) CTRL_PORT_MASK))) {
1045                 /* Some more data RX pending */
1046                 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1047
1048                 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1049                         if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1050                                 f_aggr_cur = 1;
1051                         } else {
1052                                 /* No room in Aggr buf, do rx aggr now */
1053                                 f_do_rx_aggr = 1;
1054                                 f_do_rx_cur = 1;
1055                         }
1056                 } else {
1057                         /* Rx aggr not in progress */
1058                         f_aggr_cur = 1;
1059                 }
1060
1061         } else {
1062                 /* No more data RX pending */
1063                 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1064
1065                 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1066                         f_do_rx_aggr = 1;
1067                         if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1068                                 f_aggr_cur = 1;
1069                         else
1070                                 /* No room in Aggr buf, do rx aggr now */
1071                                 f_do_rx_cur = 1;
1072                 } else {
1073                         f_do_rx_cur = 1;
1074                 }
1075         }
1076
1077         if (f_aggr_cur) {
1078                 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1079                 /* Curr pkt can be aggregated */
1080                 MP_RX_AGGR_SETUP(card, skb, port);
1081
1082                 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1083                     MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
1084                         dev_dbg(adapter->dev, "info: %s: aggregated packet "
1085                                 "limit reached\n", __func__);
1086                         /* No more pkts allowed in Aggr buf, rx it */
1087                         f_do_rx_aggr = 1;
1088                 }
1089         }
1090
1091         if (f_do_rx_aggr) {
1092                 /* do aggr RX now */
1093                 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
1094                         card->mpa_rx.pkt_cnt);
1095
1096                 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1097                                            card->mpa_rx.buf_len,
1098                                            (adapter->ioport | 0x1000 |
1099                                             (card->mpa_rx.ports << 4)) +
1100                                            card->mpa_rx.start_port, 1))
1101                         goto error;
1102
1103                 curr_ptr = card->mpa_rx.buf;
1104
1105                 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1106
1107                         /* get curr PKT len & type */
1108                         pkt_len = *(u16 *) &curr_ptr[0];
1109                         pkt_type = *(u16 *) &curr_ptr[2];
1110
1111                         /* copy pkt to deaggr buf */
1112                         skb_deaggr = card->mpa_rx.skb_arr[pind];
1113
1114                         if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1115                                          card->mpa_rx.len_arr[pind])) {
1116
1117                                 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1118
1119                                 skb_trim(skb_deaggr, pkt_len);
1120
1121                                 /* Process de-aggr packet */
1122                                 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1123                                                          pkt_type);
1124                         } else {
1125                                 dev_err(adapter->dev, "wrong aggr pkt:"
1126                                         " type=%d len=%d max_len=%d\n",
1127                                         pkt_type, pkt_len,
1128                                         card->mpa_rx.len_arr[pind]);
1129                                 dev_kfree_skb_any(skb_deaggr);
1130                         }
1131                         curr_ptr += card->mpa_rx.len_arr[pind];
1132                 }
1133                 MP_RX_AGGR_BUF_RESET(card);
1134         }
1135
1136 rx_curr_single:
1137         if (f_do_rx_cur) {
1138                 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1139                         port, rx_len);
1140
1141                 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1142                                               skb->data, skb->len,
1143                                               adapter->ioport + port))
1144                         goto error;
1145
1146                 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1147         }
1148
1149         return 0;
1150
1151 error:
1152         if (MP_RX_AGGR_IN_PROGRESS(card)) {
1153                 /* Multiport-aggregation transfer failed - cleanup */
1154                 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1155                         /* copy pkt to deaggr buf */
1156                         skb_deaggr = card->mpa_rx.skb_arr[pind];
1157                         dev_kfree_skb_any(skb_deaggr);
1158                 }
1159                 MP_RX_AGGR_BUF_RESET(card);
1160         }
1161
1162         if (f_do_rx_cur)
1163                 /* Single transfer pending. Free curr buff also */
1164                 dev_kfree_skb_any(skb);
1165
1166         return -1;
1167 }
1168
1169 /*
1170  * This function checks the current interrupt status.
1171  *
1172  * The following interrupts are checked and handled by this function -
1173  *      - Data sent
1174  *      - Command sent
1175  *      - Packets received
1176  *
1177  * Since the firmware does not generate download ready interrupt if the
1178  * port updated is command port only, command sent interrupt checking
1179  * should be done manually, and for every SDIO interrupt.
1180  *
1181  * In case of Rx packets received, the packets are uploaded from card to
1182  * host and processed accordingly.
1183  */
1184 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1185 {
1186         struct sdio_mmc_card *card = adapter->card;
1187         int ret = 0;
1188         u8 sdio_ireg;
1189         struct sk_buff *skb;
1190         u8 port = CTRL_PORT;
1191         u32 len_reg_l, len_reg_u;
1192         u32 rx_blocks;
1193         u16 rx_len;
1194         unsigned long flags;
1195
1196         spin_lock_irqsave(&adapter->int_lock, flags);
1197         sdio_ireg = adapter->int_status;
1198         adapter->int_status = 0;
1199         spin_unlock_irqrestore(&adapter->int_lock, flags);
1200
1201         if (!sdio_ireg)
1202                 return ret;
1203
1204         if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1205                 card->mp_wr_bitmap = ((u16) card->mp_regs[WR_BITMAP_U]) << 8;
1206                 card->mp_wr_bitmap |= (u16) card->mp_regs[WR_BITMAP_L];
1207                 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%04x\n",
1208                         card->mp_wr_bitmap);
1209                 if (adapter->data_sent &&
1210                     (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1211                         dev_dbg(adapter->dev,
1212                                 "info:  <--- Tx DONE Interrupt --->\n");
1213                         adapter->data_sent = false;
1214                 }
1215         }
1216
1217         /* As firmware will not generate download ready interrupt if the port
1218            updated is command port only, cmd_sent should be done for any SDIO
1219            interrupt. */
1220         if (adapter->cmd_sent) {
1221                 /* Check if firmware has attach buffer at command port and
1222                    update just that in wr_bit_map. */
1223                 card->mp_wr_bitmap |=
1224                         (u16) card->mp_regs[WR_BITMAP_L] & CTRL_PORT_MASK;
1225                 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1226                         adapter->cmd_sent = false;
1227         }
1228
1229         dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1230                 adapter->cmd_sent, adapter->data_sent);
1231         if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1232                 card->mp_rd_bitmap = ((u16) card->mp_regs[RD_BITMAP_U]) << 8;
1233                 card->mp_rd_bitmap |= (u16) card->mp_regs[RD_BITMAP_L];
1234                 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%04x\n",
1235                         card->mp_rd_bitmap);
1236
1237                 while (true) {
1238                         ret = mwifiex_get_rd_port(adapter, &port);
1239                         if (ret) {
1240                                 dev_dbg(adapter->dev,
1241                                         "info: no more rd_port available\n");
1242                                 break;
1243                         }
1244                         len_reg_l = RD_LEN_P0_L + (port << 1);
1245                         len_reg_u = RD_LEN_P0_U + (port << 1);
1246                         rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1247                         rx_len |= (u16) card->mp_regs[len_reg_l];
1248                         dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
1249                                 port, rx_len);
1250                         rx_blocks =
1251                                 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1252                                  1) / MWIFIEX_SDIO_BLOCK_SIZE;
1253                         if (rx_len <= INTF_HEADER_LEN ||
1254                             (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1255                              MWIFIEX_RX_DATA_BUF_SIZE) {
1256                                 dev_err(adapter->dev, "invalid rx_len=%d\n",
1257                                         rx_len);
1258                                 return -1;
1259                         }
1260                         rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1261
1262                         skb = dev_alloc_skb(rx_len);
1263
1264                         if (!skb) {
1265                                 dev_err(adapter->dev, "%s: failed to alloc skb",
1266                                         __func__);
1267                                 return -1;
1268                         }
1269
1270                         skb_put(skb, rx_len);
1271
1272                         dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
1273                                 rx_len, skb->len);
1274
1275                         if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1276                                                               port)) {
1277                                 u32 cr = 0;
1278
1279                                 dev_err(adapter->dev, "card_to_host_mpa failed:"
1280                                         " int status=%#x\n", sdio_ireg);
1281                                 if (mwifiex_read_reg(adapter,
1282                                                      CONFIGURATION_REG, &cr))
1283                                         dev_err(adapter->dev,
1284                                                 "read CFG reg failed\n");
1285
1286                                 dev_dbg(adapter->dev,
1287                                         "info: CFG reg val = %d\n", cr);
1288                                 if (mwifiex_write_reg(adapter,
1289                                                       CONFIGURATION_REG,
1290                                                       (cr | 0x04)))
1291                                         dev_err(adapter->dev,
1292                                                 "write CFG reg failed\n");
1293
1294                                 dev_dbg(adapter->dev, "info: write success\n");
1295                                 if (mwifiex_read_reg(adapter,
1296                                                      CONFIGURATION_REG, &cr))
1297                                         dev_err(adapter->dev,
1298                                                 "read CFG reg failed\n");
1299
1300                                 dev_dbg(adapter->dev,
1301                                         "info: CFG reg val =%x\n", cr);
1302                                 return -1;
1303                         }
1304                 }
1305         }
1306
1307         return 0;
1308 }
1309
1310 /*
1311  * This function aggregates transmission buffers in driver and downloads
1312  * the aggregated packet to card.
1313  *
1314  * The individual packets are aggregated by copying into an aggregation
1315  * buffer and then downloaded to the card. Previous unsent packets in the
1316  * aggregation buffer are pre-copied first before new packets are added.
1317  * Aggregation is done till there is space left in the aggregation buffer,
1318  * or till new packets are available.
1319  *
1320  * The function will only download the packet to the card when aggregation
1321  * stops, otherwise it will just aggregate the packet in aggregation buffer
1322  * and return.
1323  */
1324 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1325                                         u8 *payload, u32 pkt_len, u8 port,
1326                                         u32 next_pkt_len)
1327 {
1328         struct sdio_mmc_card *card = adapter->card;
1329         int ret = 0;
1330         s32 f_send_aggr_buf = 0;
1331         s32 f_send_cur_buf = 0;
1332         s32 f_precopy_cur_buf = 0;
1333         s32 f_postcopy_cur_buf = 0;
1334
1335         if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
1336                 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
1337                         __func__);
1338
1339                 f_send_cur_buf = 1;
1340                 goto tx_curr_single;
1341         }
1342
1343         if (next_pkt_len) {
1344                 /* More pkt in TX queue */
1345                 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
1346                         __func__);
1347
1348                 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1349                         if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
1350                             MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1351                                 f_precopy_cur_buf = 1;
1352
1353                                 if (!(card->mp_wr_bitmap &
1354                                       (1 << card->curr_wr_port)) ||
1355                                     !MP_TX_AGGR_BUF_HAS_ROOM(
1356                                             card, pkt_len + next_pkt_len))
1357                                         f_send_aggr_buf = 1;
1358                         } else {
1359                                 /* No room in Aggr buf, send it */
1360                                 f_send_aggr_buf = 1;
1361
1362                                 if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
1363                                     !(card->mp_wr_bitmap &
1364                                       (1 << card->curr_wr_port)))
1365                                         f_send_cur_buf = 1;
1366                                 else
1367                                         f_postcopy_cur_buf = 1;
1368                         }
1369                 } else {
1370                         if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1371                             (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1372                                 f_precopy_cur_buf = 1;
1373                         else
1374                                 f_send_cur_buf = 1;
1375                 }
1376         } else {
1377                 /* Last pkt in TX queue */
1378                 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
1379                         __func__);
1380
1381                 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1382                         /* some packs in Aggr buf already */
1383                         f_send_aggr_buf = 1;
1384
1385                         if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1386                                 f_precopy_cur_buf = 1;
1387                         else
1388                                 /* No room in Aggr buf, send it */
1389                                 f_send_cur_buf = 1;
1390                 } else {
1391                         f_send_cur_buf = 1;
1392                 }
1393         }
1394
1395         if (f_precopy_cur_buf) {
1396                 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
1397                         __func__);
1398                 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1399
1400                 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1401                     MP_TX_AGGR_PORT_LIMIT_REACHED(card))
1402                         /* No more pkts allowed in Aggr buf, send it */
1403                         f_send_aggr_buf = 1;
1404         }
1405
1406         if (f_send_aggr_buf) {
1407                 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
1408                         __func__,
1409                                 card->mpa_tx.start_port, card->mpa_tx.ports);
1410                 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1411                                                  card->mpa_tx.buf_len,
1412                                                  (adapter->ioport | 0x1000 |
1413                                                  (card->mpa_tx.ports << 4)) +
1414                                                   card->mpa_tx.start_port);
1415
1416                 MP_TX_AGGR_BUF_RESET(card);
1417         }
1418
1419 tx_curr_single:
1420         if (f_send_cur_buf) {
1421                 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
1422                         __func__, port);
1423                 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1424                                                  adapter->ioport + port);
1425         }
1426
1427         if (f_postcopy_cur_buf) {
1428                 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
1429                         __func__);
1430                 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1431         }
1432
1433         return ret;
1434 }
1435
1436 /*
1437  * This function downloads data from driver to card.
1438  *
1439  * Both commands and data packets are transferred to the card by this
1440  * function.
1441  *
1442  * This function adds the SDIO specific header to the front of the buffer
1443  * before transferring. The header contains the length of the packet and
1444  * the type. The firmware handles the packets based upon this set type.
1445  */
1446 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1447                                      u8 type, struct sk_buff *skb,
1448                                      struct mwifiex_tx_param *tx_param)
1449 {
1450         struct sdio_mmc_card *card = adapter->card;
1451         int ret;
1452         u32 buf_block_len;
1453         u32 blk_size;
1454         u8 port = CTRL_PORT;
1455         u8 *payload = (u8 *)skb->data;
1456         u32 pkt_len = skb->len;
1457
1458         /* Allocate buffer and copy payload */
1459         blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1460         buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1461         *(u16 *) &payload[0] = (u16) pkt_len;
1462         *(u16 *) &payload[2] = type;
1463
1464         /*
1465          * This is SDIO specific header
1466          *  u16 length,
1467          *  u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1468          *  MWIFIEX_TYPE_EVENT = 3)
1469          */
1470         if (type == MWIFIEX_TYPE_DATA) {
1471                 ret = mwifiex_get_wr_port_data(adapter, &port);
1472                 if (ret) {
1473                         dev_err(adapter->dev, "%s: no wr_port available\n",
1474                                 __func__);
1475                         return ret;
1476                 }
1477         } else {
1478                 adapter->cmd_sent = true;
1479                 /* Type must be MWIFIEX_TYPE_CMD */
1480
1481                 if (pkt_len <= INTF_HEADER_LEN ||
1482                     pkt_len > MWIFIEX_UPLD_SIZE)
1483                         dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
1484                                 __func__, payload, pkt_len);
1485         }
1486
1487         /* Transfer data to card */
1488         pkt_len = buf_block_len * blk_size;
1489
1490         if (tx_param)
1491                 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1492                                                    port, tx_param->next_pkt_len
1493                                                    );
1494         else
1495                 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1496                                                    port, 0);
1497
1498         if (ret) {
1499                 if (type == MWIFIEX_TYPE_CMD)
1500                         adapter->cmd_sent = false;
1501                 if (type == MWIFIEX_TYPE_DATA)
1502                         adapter->data_sent = false;
1503         } else {
1504                 if (type == MWIFIEX_TYPE_DATA) {
1505                         if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1506                                 adapter->data_sent = true;
1507                         else
1508                                 adapter->data_sent = false;
1509                 }
1510         }
1511
1512         return ret;
1513 }
1514
1515 /*
1516  * This function allocates the MPA Tx and Rx buffers.
1517  */
1518 static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1519                                    u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1520 {
1521         struct sdio_mmc_card *card = adapter->card;
1522         int ret = 0;
1523
1524         card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1525         if (!card->mpa_tx.buf) {
1526                 dev_err(adapter->dev, "could not alloc buffer for MP-A TX\n");
1527                 ret = -1;
1528                 goto error;
1529         }
1530
1531         card->mpa_tx.buf_size = mpa_tx_buf_size;
1532
1533         card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1534         if (!card->mpa_rx.buf) {
1535                 dev_err(adapter->dev, "could not alloc buffer for MP-A RX\n");
1536                 ret = -1;
1537                 goto error;
1538         }
1539
1540         card->mpa_rx.buf_size = mpa_rx_buf_size;
1541
1542 error:
1543         if (ret) {
1544                 kfree(card->mpa_tx.buf);
1545                 kfree(card->mpa_rx.buf);
1546         }
1547
1548         return ret;
1549 }
1550
1551 /*
1552  * This function unregisters the SDIO device.
1553  *
1554  * The SDIO IRQ is released, the function is disabled and driver
1555  * data is set to null.
1556  */
1557 static void
1558 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1559 {
1560         struct sdio_mmc_card *card = adapter->card;
1561
1562         if (adapter->card) {
1563                 /* Release the SDIO IRQ */
1564                 sdio_claim_host(card->func);
1565                 sdio_release_irq(card->func);
1566                 sdio_disable_func(card->func);
1567                 sdio_release_host(card->func);
1568                 sdio_set_drvdata(card->func, NULL);
1569         }
1570 }
1571
1572 /*
1573  * This function registers the SDIO device.
1574  *
1575  * SDIO IRQ is claimed, block size is set and driver data is initialized.
1576  */
1577 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1578 {
1579         int ret = 0;
1580         struct sdio_mmc_card *card = adapter->card;
1581         struct sdio_func *func = card->func;
1582
1583         /* save adapter pointer in card */
1584         card->adapter = adapter;
1585
1586         sdio_claim_host(func);
1587
1588         /* Request the SDIO IRQ */
1589         ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1590         if (ret) {
1591                 pr_err("claim irq failed: ret=%d\n", ret);
1592                 goto disable_func;
1593         }
1594
1595         /* Set block size */
1596         ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1597         if (ret) {
1598                 pr_err("cannot set SDIO block size\n");
1599                 ret = -1;
1600                 goto release_irq;
1601         }
1602
1603         sdio_release_host(func);
1604         sdio_set_drvdata(func, card);
1605
1606         adapter->dev = &func->dev;
1607
1608         switch (func->device) {
1609         case SDIO_DEVICE_ID_MARVELL_8797:
1610                 strcpy(adapter->fw_name, SD8797_DEFAULT_FW_NAME);
1611                 break;
1612         case SDIO_DEVICE_ID_MARVELL_8787:
1613         default:
1614                 strcpy(adapter->fw_name, SD8787_DEFAULT_FW_NAME);
1615                 break;
1616         }
1617
1618         return 0;
1619
1620 release_irq:
1621         sdio_release_irq(func);
1622 disable_func:
1623         sdio_disable_func(func);
1624         sdio_release_host(func);
1625         adapter->card = NULL;
1626
1627         return -1;
1628 }
1629
1630 /*
1631  * This function initializes the SDIO driver.
1632  *
1633  * The following initializations steps are followed -
1634  *      - Read the Host interrupt status register to acknowledge
1635  *        the first interrupt got from bootloader
1636  *      - Disable host interrupt mask register
1637  *      - Get SDIO port
1638  *      - Initialize SDIO variables in card
1639  *      - Allocate MP registers
1640  *      - Allocate MPA Tx and Rx buffers
1641  */
1642 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1643 {
1644         struct sdio_mmc_card *card = adapter->card;
1645         int ret;
1646         u32 sdio_ireg;
1647
1648         /*
1649          * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1650          * from the bootloader. If we don't do this we get a interrupt
1651          * as soon as we register the irq.
1652          */
1653         mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1654
1655         /* Disable host interrupt mask register for SDIO */
1656         mwifiex_sdio_disable_host_int(adapter);
1657
1658         /* Get SDIO ioport */
1659         mwifiex_init_sdio_ioport(adapter);
1660
1661         /* Initialize SDIO variables in card */
1662         card->mp_rd_bitmap = 0;
1663         card->mp_wr_bitmap = 0;
1664         card->curr_rd_port = 1;
1665         card->curr_wr_port = 1;
1666
1667         card->mp_data_port_mask = DATA_PORT_MASK;
1668
1669         card->mpa_tx.buf_len = 0;
1670         card->mpa_tx.pkt_cnt = 0;
1671         card->mpa_tx.start_port = 0;
1672
1673         card->mpa_tx.enabled = 1;
1674         card->mpa_tx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1675
1676         card->mpa_rx.buf_len = 0;
1677         card->mpa_rx.pkt_cnt = 0;
1678         card->mpa_rx.start_port = 0;
1679
1680         card->mpa_rx.enabled = 1;
1681         card->mpa_rx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1682
1683         /* Allocate buffers for SDIO MP-A */
1684         card->mp_regs = kzalloc(MAX_MP_REGS, GFP_KERNEL);
1685         if (!card->mp_regs) {
1686                 dev_err(adapter->dev, "failed to alloc mp_regs\n");
1687                 return -ENOMEM;
1688         }
1689
1690         ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1691                                              SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1692                                              SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1693         if (ret) {
1694                 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1695                 kfree(card->mp_regs);
1696                 return -1;
1697         }
1698
1699         return ret;
1700 }
1701
1702 /*
1703  * This function resets the MPA Tx and Rx buffers.
1704  */
1705 static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1706 {
1707         struct sdio_mmc_card *card = adapter->card;
1708
1709         MP_TX_AGGR_BUF_RESET(card);
1710         MP_RX_AGGR_BUF_RESET(card);
1711 }
1712
1713 /*
1714  * This function cleans up the allocated card buffers.
1715  *
1716  * The following are freed by this function -
1717  *      - MP registers
1718  *      - MPA Tx buffer
1719  *      - MPA Rx buffer
1720  */
1721 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1722 {
1723         struct sdio_mmc_card *card = adapter->card;
1724
1725         kfree(card->mp_regs);
1726         kfree(card->mpa_tx.buf);
1727         kfree(card->mpa_rx.buf);
1728 }
1729
1730 /*
1731  * This function updates the MP end port in card.
1732  */
1733 static void
1734 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1735 {
1736         struct sdio_mmc_card *card = adapter->card;
1737         int i;
1738
1739         card->mp_end_port = port;
1740
1741         card->mp_data_port_mask = DATA_PORT_MASK;
1742
1743         for (i = 1; i <= MAX_PORT - card->mp_end_port; i++)
1744                 card->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
1745
1746         card->curr_wr_port = 1;
1747
1748         dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
1749                 port, card->mp_data_port_mask);
1750 }
1751
1752 static bool mwifiex_sdio_mwi87xx_reset(void)
1753 {
1754         struct regulator *wifi_en, *wifi_rst;
1755         bool ok;
1756
1757         wifi_en = regulator_get(NULL, "wifi-en");
1758         wifi_rst = regulator_get(NULL, "wifi-rst-l");
1759         ok = !IS_ERR(wifi_en) && !IS_ERR(wifi_rst);
1760         if (ok) {
1761                 regulator_disable(wifi_rst);
1762                 regulator_disable(wifi_en);
1763                 regulator_enable(wifi_rst);
1764                 /* as per 8797 datasheet section 1.5.2 */
1765                 mdelay(1);
1766                 regulator_enable(wifi_en);
1767         }
1768
1769         if (!IS_ERR(wifi_rst))
1770                 regulator_put(wifi_rst);
1771         if (!IS_ERR(wifi_en))
1772                 regulator_put(wifi_en);
1773         return ok;
1774 }
1775
1776 static void mwifiex_sdio_reset_work(struct mwifiex_adapter *adapter)
1777 {
1778         struct sdio_mmc_card *card = adapter->card;
1779         struct mmc_host *host = card->func->card->host;
1780
1781         pr_err("Resetting card...\n");
1782         mmc_remove_host(host);
1783         if (!mwifiex_sdio_mwi87xx_reset())
1784                 pr_err("External card reset failed! Trying to reattach...\n");
1785         /* 20ms delay is based on experiment with sdhci controller */
1786         mdelay(20);
1787         mmc_add_host(host);
1788 }
1789
1790 /*
1791  * This function reads and displays SDIO registers for debugging.
1792  */
1793 static void mwifiex_sdio_reg_dbg_work(struct mwifiex_adapter *adapter)
1794 {
1795         struct sdio_mmc_card *card = adapter->card;
1796         struct sdio_func *func = card->func;
1797         struct sdio_reg_dbg cccr = {
1798                 "CCCR", 0, 0, 10,
1799                  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
1800         };
1801         struct sdio_reg_dbg fn1 = {
1802                 "FN1", 1, 0, 10,
1803                  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
1804         };
1805         struct sdio_reg_dbg fn1_status = {
1806                 "FN1 Status", 1, 0, 5, {
1807                         HOST_INT_STATUS_REG,
1808                         CARD_STATUS_REG,
1809                         HOST_INTERRUPT_MASK_REG,
1810                         CARD_INTERRUPT_STATUS_REG,
1811                         CARD_INTERRUPT_RSR_REG,
1812                 },
1813         };
1814         struct sdio_reg_dbg fn1_scratch = {
1815                 "FN1 Scratch", 1, 100, 11, {
1816                         CARD_FW_STATUS0_REG,
1817                         CARD_FW_STATUS0_REG + 1,
1818                         CARD_FW_STATUS0_REG + 2,
1819                         CARD_FW_STATUS0_REG + 3,
1820                         CARD_FW_STATUS0_REG + 4,
1821                         CARD_FW_STATUS0_REG + 5,
1822                         CARD_FW_STATUS0_REG + 6,
1823                         CARD_FW_STATUS0_REG + 7,
1824                         CARD_FW_STATUS0_REG + 8,
1825                         CARD_FW_STATUS0_REG + 9,
1826                         CARD_FW_STATUS0_REG + 10,
1827                 },
1828         };
1829         struct sdio_reg_dbg *regs[] = {
1830                 &cccr,
1831                 &fn1,
1832                 &fn1_status,
1833                 &fn1_scratch,
1834                 /* read fn1_scratch again for any changes */
1835                 &fn1_scratch,
1836                 NULL
1837         };
1838         int n, i, ret = 0;
1839         u32 reg;
1840
1841         sdio_claim_host(func);
1842
1843         for (n = 0; regs[n]; n++) {
1844                 char buf[REG_DBG_MAX_NUM*5+1];
1845                 char *p = buf;
1846
1847                 mdelay(regs[n]->delay);
1848
1849                 memset(buf, 0, sizeof(buf));
1850                 for (i = 0; i < regs[n]->num_regs; i++)
1851                         p += sprintf(p, "%04X ", regs[n]->reg[i]);
1852                 p += sprintf(p, "\n");
1853
1854                 dev_info(adapter->dev, "%s: %s", regs[n]->name, buf);
1855
1856                 memset(buf, 0, sizeof(buf));
1857                 p = buf;
1858                 for (i = 0; i < regs[n]->num_regs; i++) {
1859                         reg = regs[n]->reg[i];
1860                         regs[n]->val[i] = regs[n]->fn ?
1861                                           sdio_readb(func, reg, &ret) :
1862                                           sdio_f0_readb(func, reg, &ret);
1863                         if (!ret)
1864                                 p += sprintf(p, "%04X ", regs[n]->val[i]);
1865                         else
1866                                 p += sprintf(p, "ERR  ");
1867                 }
1868                 p += sprintf(p, "\n");
1869
1870                 dev_info(adapter->dev, "%s: %s", regs[n]->name, buf);
1871         }
1872
1873         sdio_release_host(func);
1874 }
1875
1876 static void sdio_work_worker(struct work_struct *work)
1877 {
1878         if (test_and_clear_bit(MWIFIEX_SDIO_WORK_REGDBG, &sdio_work_flags))
1879                 mwifiex_sdio_reg_dbg_work(reg_dbg_adapter);
1880         if (test_and_clear_bit(MWIFIEX_SDIO_WORK_RESET, &sdio_work_flags))
1881                 mwifiex_sdio_reset_work(reset_adapter);
1882 }
1883 static DECLARE_WORK(sdio_work, sdio_work_worker);
1884
1885 /* This function resets the card */
1886 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1887 {
1888         if (test_bit(MWIFIEX_SDIO_WORK_RESET, &sdio_work_flags))
1889                 return;
1890
1891         /* The actual reset operation must be run outside of driver thread.
1892          * This is because mmc_remove_host() will cause the device to be
1893          * instantly destroyed, and the driver then needs to end its thread,
1894          * leading to a deadlock.
1895          */
1896         reset_adapter = adapter;
1897         set_bit(MWIFIEX_SDIO_WORK_RESET, &sdio_work_flags);
1898         schedule_work(&sdio_work);
1899 }
1900
1901 static void mwifiex_sdio_reg_dbg(struct mwifiex_adapter *adapter)
1902 {
1903         if (test_bit(MWIFIEX_SDIO_WORK_REGDBG, &sdio_work_flags))
1904                 return;
1905
1906         reg_dbg_adapter = adapter;
1907         set_bit(MWIFIEX_SDIO_WORK_REGDBG, &sdio_work_flags);
1908         schedule_work(&sdio_work);
1909 }
1910
1911 static struct mwifiex_if_ops sdio_ops = {
1912         .init_if = mwifiex_init_sdio,
1913         .cleanup_if = mwifiex_cleanup_sdio,
1914         .check_fw_status = mwifiex_check_fw_status,
1915         .prog_fw = mwifiex_prog_fw_w_helper,
1916         .register_dev = mwifiex_register_dev,
1917         .unregister_dev = mwifiex_unregister_dev,
1918         .enable_int = mwifiex_sdio_enable_host_int,
1919         .process_int_status = mwifiex_process_int_status,
1920         .host_to_card = mwifiex_sdio_host_to_card,
1921         .wakeup = mwifiex_pm_wakeup_card,
1922         .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1923
1924         /* SDIO specific */
1925         .update_mp_end_port = mwifiex_update_mp_end_port,
1926         .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
1927         .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1928         .event_complete = mwifiex_sdio_event_complete,
1929         .reg_dbg = mwifiex_sdio_reg_dbg,
1930         .card_reset = mwifiex_sdio_card_reset,
1931 };
1932
1933 /*
1934  * This function initializes the SDIO driver.
1935  *
1936  * This initiates the semaphore and registers the device with
1937  * SDIO bus.
1938  */
1939 static int
1940 mwifiex_sdio_init_module(void)
1941 {
1942         sema_init(&add_remove_card_sem, 1);
1943
1944         /* Clear the flag in case user removes the card. */
1945         user_rmmod = 0;
1946
1947         return sdio_register_driver(&mwifiex_sdio);
1948 }
1949
1950 /*
1951  * This function cleans up the SDIO driver.
1952  *
1953  * The following major steps are followed for cleanup -
1954  *      - Resume the device if its suspended
1955  *      - Disconnect the device if connected
1956  *      - Shutdown the firmware
1957  *      - Unregister the device from SDIO bus.
1958  */
1959 static void
1960 mwifiex_sdio_cleanup_module(void)
1961 {
1962         if (!down_interruptible(&add_remove_card_sem))
1963                 up(&add_remove_card_sem);
1964
1965         /* Set the flag as user is removing this module. */
1966         user_rmmod = 1;
1967
1968         cancel_work_sync(&sdio_work);
1969         sdio_unregister_driver(&mwifiex_sdio);
1970 }
1971
1972 module_init(mwifiex_sdio_init_module);
1973 module_exit(mwifiex_sdio_cleanup_module);
1974
1975 MODULE_AUTHOR("Marvell International Ltd.");
1976 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
1977 MODULE_VERSION(SDIO_VERSION);
1978 MODULE_LICENSE("GPL v2");
1979 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
1980 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);