2 * linux/drivers/net/wireless/libertas/if_sdio.c
4 * Copyright 2007-2008 Pierre Ossman
6 * Inspired by if_cs.c, Copyright 2007 Holger Schurig
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This hardware has more or less no CMD53 support, so all registers
14 * must be accessed using sdio_readb()/sdio_writeb().
16 * Transfers must be in one transaction or the firmware goes bonkers.
17 * This means that the transfer must either be small enough to do a
18 * byte based transfer or it must be padded to a multiple of the
21 * As SDIO is still new to the kernel, it is unfortunately common with
22 * bugs in the host controllers related to that. One such bug is that
23 * controllers cannot do transfers that aren't a multiple of 4 bytes.
24 * If you don't have time to fix the host controller driver, you can
25 * work around the problem by modifying if_sdio_host_to_card() and
26 * if_sdio_card_to_host() to pad the data.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/firmware.h>
35 #include <linux/netdevice.h>
36 #include <linux/delay.h>
37 #include <linux/mmc/card.h>
38 #include <linux/mmc/sdio_func.h>
39 #include <linux/mmc/sdio_ids.h>
40 #include <linux/mmc/sdio.h>
41 #include <linux/mmc/host.h>
42 #include <linux/pm_runtime.h>
51 static void if_sdio_interrupt(struct sdio_func *func);
53 /* The if_sdio_remove() callback function is called when
54 * user removes this module from kernel space or ejects
55 * the card from the slot. The driver handles these 2 cases
56 * differently for SD8688 combo chip.
57 * If the user is removing the module, the FUNC_SHUTDOWN
58 * command for SD8688 is sent to the firmware.
59 * If the card is removed, there is no need to send this command.
61 * The variable 'user_rmmod' is used to distinguish these two
62 * scenarios. This flag is initialized as FALSE in case the card
63 * is removed, and will be set to TRUE for module removal when
64 * module_exit function is called.
68 static const struct sdio_device_id if_sdio_ids[] = {
69 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
70 SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
71 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
72 SDIO_DEVICE_ID_MARVELL_8688WLAN) },
73 { /* end: all zeroes */ },
76 MODULE_DEVICE_TABLE(sdio, if_sdio_ids);
78 #define MODEL_8385 0x04
79 #define MODEL_8686 0x0b
80 #define MODEL_8688 0x10
82 static const struct lbs_fw_table fw_table[] = {
83 { MODEL_8385, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
84 { MODEL_8385, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
85 { MODEL_8686, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
86 { MODEL_8686, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
87 { MODEL_8686, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
88 { MODEL_8688, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
89 { MODEL_8688, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/" },
94 struct if_sdio_packet {
95 struct if_sdio_packet *next;
97 u8 buffer[0] __attribute__((aligned(4)));
100 struct if_sdio_card {
101 struct sdio_func *func;
102 struct lbs_private *priv;
105 unsigned long ioport;
106 unsigned int scratch_reg;
108 wait_queue_head_t pwron_waitq;
110 u8 buffer[65536] __attribute__((aligned(4)));
113 struct if_sdio_packet *packets;
115 struct workqueue_struct *workqueue;
116 struct work_struct packet_worker;
121 static void if_sdio_finish_power_on(struct if_sdio_card *card);
122 static int if_sdio_power_off(struct if_sdio_card *card);
124 /********************************************************************/
126 /********************************************************************/
129 * For SD8385/SD8686, this function reads firmware status after
130 * the image is downloaded, or reads RX packet length when
131 * interrupt (with IF_SDIO_H_INT_UPLD bit set) is received.
132 * For SD8688, this function reads firmware status only.
134 static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
139 scratch = sdio_readb(card->func, card->scratch_reg, &ret);
141 scratch |= sdio_readb(card->func, card->scratch_reg + 1,
153 static u8 if_sdio_read_rx_unit(struct if_sdio_card *card)
158 rx_unit = sdio_readb(card->func, IF_SDIO_RX_UNIT, &ret);
166 static u16 if_sdio_read_rx_len(struct if_sdio_card *card, int *err)
171 switch (card->model) {
174 rx_len = if_sdio_read_scratch(card, &ret);
177 default: /* for newer chipsets */
178 rx_len = sdio_readb(card->func, IF_SDIO_RX_LEN, &ret);
180 rx_len <<= card->rx_unit;
182 rx_len = 0xffff; /* invalid length */
193 static int if_sdio_handle_cmd(struct if_sdio_card *card,
194 u8 *buffer, unsigned size)
196 struct lbs_private *priv = card->priv;
201 lbs_deb_enter(LBS_DEB_SDIO);
203 if (size > LBS_CMD_BUFFER_SIZE) {
204 lbs_deb_sdio("response packet too large (%d bytes)\n",
210 spin_lock_irqsave(&priv->driver_lock, flags);
212 i = (priv->resp_idx == 0) ? 1 : 0;
213 BUG_ON(priv->resp_len[i]);
214 priv->resp_len[i] = size;
215 memcpy(priv->resp_buf[i], buffer, size);
216 lbs_notify_command_response(priv, i);
218 spin_unlock_irqrestore(&priv->driver_lock, flags);
223 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
227 static int if_sdio_handle_data(struct if_sdio_card *card,
228 u8 *buffer, unsigned size)
234 lbs_deb_enter(LBS_DEB_SDIO);
236 if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
237 lbs_deb_sdio("response packet too large (%d bytes)\n",
243 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
249 skb_reserve(skb, NET_IP_ALIGN);
251 data = skb_put(skb, size);
253 memcpy(data, buffer, size);
255 lbs_process_rxed_packet(card->priv, skb);
260 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
265 static int if_sdio_handle_event(struct if_sdio_card *card,
266 u8 *buffer, unsigned size)
271 lbs_deb_enter(LBS_DEB_SDIO);
273 if (card->model == MODEL_8385) {
274 event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
278 /* right shift 3 bits to get the event id */
282 lbs_deb_sdio("event packet too small (%d bytes)\n",
287 event = buffer[3] << 24;
288 event |= buffer[2] << 16;
289 event |= buffer[1] << 8;
290 event |= buffer[0] << 0;
293 lbs_queue_event(card->priv, event & 0xFF);
297 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
302 static int if_sdio_wait_status(struct if_sdio_card *card, const u8 condition)
305 unsigned long timeout;
308 timeout = jiffies + HZ;
310 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
313 if ((status & condition) == condition)
315 if (time_after(jiffies, timeout))
322 static int if_sdio_card_to_host(struct if_sdio_card *card)
325 u16 size, type, chunk;
327 lbs_deb_enter(LBS_DEB_SDIO);
329 size = if_sdio_read_rx_len(card, &ret);
334 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
340 ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
345 * The transfer must be in one transaction or the firmware
346 * goes suicidal. There's no way to guarantee that for all
347 * controllers, but we can at least try.
349 chunk = sdio_align_size(card->func, size);
351 ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
355 chunk = card->buffer[0] | (card->buffer[1] << 8);
356 type = card->buffer[2] | (card->buffer[3] << 8);
358 lbs_deb_sdio("packet of type %d and size %d bytes\n",
359 (int)type, (int)chunk);
362 lbs_deb_sdio("packet fragment (%d > %d)\n",
363 (int)chunk, (int)size);
369 lbs_deb_sdio("packet fragment (%d < %d)\n",
370 (int)chunk, (int)size);
375 ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
380 ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
385 ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
390 lbs_deb_sdio("invalid type (%d) from firmware\n",
398 pr_err("problem fetching packet from firmware\n");
400 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
405 static void if_sdio_host_to_card_worker(struct work_struct *work)
407 struct if_sdio_card *card;
408 struct if_sdio_packet *packet;
412 lbs_deb_enter(LBS_DEB_SDIO);
414 card = container_of(work, struct if_sdio_card, packet_worker);
417 spin_lock_irqsave(&card->lock, flags);
418 packet = card->packets;
420 card->packets = packet->next;
421 spin_unlock_irqrestore(&card->lock, flags);
426 sdio_claim_host(card->func);
428 ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
430 ret = sdio_writesb(card->func, card->ioport,
431 packet->buffer, packet->nb);
435 pr_err("error %d sending packet to firmware\n", ret);
437 sdio_release_host(card->func);
442 lbs_deb_leave(LBS_DEB_SDIO);
445 /********************************************************************/
447 /********************************************************************/
449 #define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY)
451 static int if_sdio_prog_helper(struct if_sdio_card *card,
452 const struct firmware *fw)
455 unsigned long timeout;
461 lbs_deb_enter(LBS_DEB_SDIO);
463 chunk_buffer = kzalloc(64, GFP_KERNEL);
469 sdio_claim_host(card->func);
471 ret = sdio_set_block_size(card->func, 32);
479 ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
483 /* On some platforms (like Davinci) the chip needs more time
484 * between helper blocks.
488 chunk_size = min_t(size_t, size, 60);
490 *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
491 memcpy(chunk_buffer + 4, firmware, chunk_size);
493 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
495 ret = sdio_writesb(card->func, card->ioport,
500 firmware += chunk_size;
504 /* an empty block marks the end of the transfer */
505 memset(chunk_buffer, 0, 4);
506 ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
510 lbs_deb_sdio("waiting for helper to boot...\n");
512 /* wait for the helper to boot by looking at the size register */
513 timeout = jiffies + HZ;
517 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
521 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
528 if (time_after(jiffies, timeout)) {
539 sdio_release_host(card->func);
544 pr_err("failed to load helper firmware\n");
546 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
550 static int if_sdio_prog_real(struct if_sdio_card *card,
551 const struct firmware *fw)
554 unsigned long timeout;
558 size_t size, req_size;
560 lbs_deb_enter(LBS_DEB_SDIO);
562 chunk_buffer = kzalloc(512, GFP_KERNEL);
568 sdio_claim_host(card->func);
570 ret = sdio_set_block_size(card->func, 32);
578 timeout = jiffies + HZ;
580 ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
584 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE,
589 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1,
595 * For SD8688 wait until the length is not 0, 1 or 2
596 * before downloading the first FW block,
597 * since BOOT code writes the register to indicate the
598 * helper/FW download winner,
599 * the value could be 1 or 2 (Func1 or Func2).
601 if ((size != fw->size) || (req_size > 2))
603 if (time_after(jiffies, timeout)) {
611 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
614 lbs_deb_sdio("firmware helper gave up early\n");
619 if (req_size & 0x01) {
620 lbs_deb_sdio("firmware helper signalled error\n");
629 chunk_size = min_t(size_t, req_size, 512);
631 memcpy(chunk_buffer, firmware, chunk_size);
633 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
634 chunk_size, (chunk_size + 31) / 32 * 32);
636 ret = sdio_writesb(card->func, card->ioport,
637 chunk_buffer, roundup(chunk_size, 32));
641 firmware += chunk_size;
643 req_size -= chunk_size;
649 lbs_deb_sdio("waiting for firmware to boot...\n");
651 /* wait for the firmware to boot */
652 timeout = jiffies + HZ;
656 scratch = if_sdio_read_scratch(card, &ret);
660 if (scratch == IF_SDIO_FIRMWARE_OK)
663 if (time_after(jiffies, timeout)) {
674 sdio_release_host(card->func);
679 pr_err("failed to load firmware\n");
681 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
685 static void if_sdio_do_prog_firmware(struct lbs_private *priv, int ret,
686 const struct firmware *helper,
687 const struct firmware *mainfw)
689 struct if_sdio_card *card = priv->card;
692 pr_err("failed to find firmware (%d)\n", ret);
696 ret = if_sdio_prog_helper(card, helper);
700 lbs_deb_sdio("Helper firmware loaded\n");
702 ret = if_sdio_prog_real(card, mainfw);
706 lbs_deb_sdio("Firmware loaded\n");
707 if_sdio_finish_power_on(card);
710 static int if_sdio_prog_firmware(struct if_sdio_card *card)
715 lbs_deb_enter(LBS_DEB_SDIO);
720 sdio_claim_host(card->func);
721 sdio_writeb(card->func, 0x00, IF_SDIO_H_INT_MASK, &ret);
722 sdio_release_host(card->func);
724 sdio_claim_host(card->func);
725 scratch = if_sdio_read_scratch(card, &ret);
726 sdio_release_host(card->func);
728 lbs_deb_sdio("firmware status = %#x\n", scratch);
729 lbs_deb_sdio("scratch ret = %d\n", ret);
736 * The manual clearly describes that FEDC is the right code to use
737 * to detect firmware presence, but for SD8686 it is not that simple.
738 * Scratch is also used to store the RX packet length, so we lose
739 * the FEDC value early on. So we use a non-zero check in order
740 * to validate firmware presence.
741 * Additionally, the SD8686 in the Gumstix always has the high scratch
742 * bit set, even when the firmware is not loaded. So we have to
743 * exclude that from the test.
745 if (scratch == IF_SDIO_FIRMWARE_OK) {
746 lbs_deb_sdio("firmware already loaded\n");
747 if_sdio_finish_power_on(card);
749 } else if ((card->model == MODEL_8686) && (scratch & 0x7fff)) {
750 lbs_deb_sdio("firmware may be running\n");
751 if_sdio_finish_power_on(card);
755 ret = lbs_get_firmware_async(card->priv, &card->func->dev, card->model,
756 fw_table, if_sdio_do_prog_firmware);
759 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
763 /********************************************************************/
764 /* Power management */
765 /********************************************************************/
767 /* Finish power on sequence (after firmware is loaded) */
768 static void if_sdio_finish_power_on(struct if_sdio_card *card)
770 struct sdio_func *func = card->func;
771 struct lbs_private *priv = card->priv;
774 sdio_claim_host(func);
775 sdio_set_block_size(card->func, IF_SDIO_BLOCK_SIZE);
778 * Get rx_unit if the chip is SD8688 or newer.
779 * SD8385 & SD8686 do not have rx_unit.
781 if ((card->model != MODEL_8385)
782 && (card->model != MODEL_8686))
783 card->rx_unit = if_sdio_read_rx_unit(card);
788 * Set up the interrupt handler late.
790 * If we set it up earlier, the (buggy) hardware generates a spurious
791 * interrupt, even before the interrupt has been enabled, with
794 * We register the interrupt handler late so that we can handle any
795 * spurious interrupts, and also to avoid generation of that known
796 * spurious interrupt in the first place.
798 ret = sdio_claim_irq(func, if_sdio_interrupt);
803 * Enable interrupts now that everything is set up
805 sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
809 sdio_release_host(func);
811 /* Set fw_ready before queuing any commands so that
812 * lbs_thread won't block from sending them to firmware.
817 * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
819 if (card->model == MODEL_8688) {
820 struct cmd_header cmd;
822 memset(&cmd, 0, sizeof(cmd));
824 lbs_deb_sdio("send function INIT command\n");
825 if (__lbs_cmd(priv, CMD_FUNC_INIT, &cmd, sizeof(cmd),
826 lbs_cmd_copyback, (unsigned long) &cmd))
827 netdev_alert(priv->dev, "CMD_FUNC_INIT cmd failed\n");
830 wake_up(&card->pwron_waitq);
832 if (!card->started) {
833 ret = lbs_start_card(priv);
834 if_sdio_power_off(card);
836 card->started = true;
837 /* Tell PM core that we don't need the card to be
839 pm_runtime_put(&func->dev);
846 sdio_release_irq(func);
848 sdio_release_host(func);
851 static int if_sdio_power_on(struct if_sdio_card *card)
853 struct sdio_func *func = card->func;
854 struct mmc_host *host = func->card->host;
857 sdio_claim_host(func);
859 ret = sdio_enable_func(func);
863 /* For 1-bit transfers to the 8686 model, we need to enable the
864 * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0
865 * bit to allow access to non-vendor registers. */
866 if ((card->model == MODEL_8686) &&
867 (host->caps & MMC_CAP_SDIO_IRQ) &&
868 (host->ios.bus_width == MMC_BUS_WIDTH_1)) {
871 func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
872 reg = sdio_f0_readb(func, SDIO_CCCR_IF, &ret);
876 reg |= SDIO_BUS_ECSI;
877 sdio_f0_writeb(func, reg, SDIO_CCCR_IF, &ret);
882 card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
886 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
890 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
894 sdio_release_host(func);
895 ret = if_sdio_prog_firmware(card);
897 sdio_claim_host(func);
904 sdio_disable_func(func);
906 sdio_release_host(func);
910 static int if_sdio_power_off(struct if_sdio_card *card)
912 struct sdio_func *func = card->func;
913 struct lbs_private *priv = card->priv;
917 sdio_claim_host(func);
918 sdio_release_irq(func);
919 sdio_disable_func(func);
920 sdio_release_host(func);
925 /*******************************************************************/
926 /* Libertas callbacks */
927 /*******************************************************************/
929 static int if_sdio_host_to_card(struct lbs_private *priv,
930 u8 type, u8 *buf, u16 nb)
933 struct if_sdio_card *card;
934 struct if_sdio_packet *packet, *cur;
938 lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb);
942 if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
948 * The transfer must be in one transaction or the firmware
949 * goes suicidal. There's no way to guarantee that for all
950 * controllers, but we can at least try.
952 size = sdio_align_size(card->func, nb + 4);
954 packet = kzalloc(sizeof(struct if_sdio_packet) + size,
965 * SDIO specific header.
967 packet->buffer[0] = (nb + 4) & 0xff;
968 packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
969 packet->buffer[2] = type;
970 packet->buffer[3] = 0;
972 memcpy(packet->buffer + 4, buf, nb);
974 spin_lock_irqsave(&card->lock, flags);
977 card->packets = packet;
987 priv->dnld_sent = DNLD_CMD_SENT;
990 priv->dnld_sent = DNLD_DATA_SENT;
993 lbs_deb_sdio("unknown packet type %d\n", (int)type);
996 spin_unlock_irqrestore(&card->lock, flags);
998 queue_work(card->workqueue, &card->packet_worker);
1003 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1008 static int if_sdio_enter_deep_sleep(struct lbs_private *priv)
1011 struct cmd_header cmd;
1013 memset(&cmd, 0, sizeof(cmd));
1015 lbs_deb_sdio("send DEEP_SLEEP command\n");
1016 ret = __lbs_cmd(priv, CMD_802_11_DEEP_SLEEP, &cmd, sizeof(cmd),
1017 lbs_cmd_copyback, (unsigned long) &cmd);
1019 netdev_err(priv->dev, "DEEP_SLEEP cmd failed\n");
1025 static int if_sdio_exit_deep_sleep(struct lbs_private *priv)
1027 struct if_sdio_card *card = priv->card;
1030 lbs_deb_enter(LBS_DEB_SDIO);
1031 sdio_claim_host(card->func);
1033 sdio_writeb(card->func, HOST_POWER_UP, CONFIGURATION_REG, &ret);
1035 netdev_err(priv->dev, "sdio_writeb failed!\n");
1037 sdio_release_host(card->func);
1038 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1042 static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private *priv)
1044 struct if_sdio_card *card = priv->card;
1047 lbs_deb_enter(LBS_DEB_SDIO);
1048 sdio_claim_host(card->func);
1050 sdio_writeb(card->func, 0, CONFIGURATION_REG, &ret);
1052 netdev_err(priv->dev, "sdio_writeb failed!\n");
1054 sdio_release_host(card->func);
1055 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1060 static struct mmc_host *reset_host;
1062 static void if_sdio_reset_card_worker(struct work_struct *work)
1065 * The actual reset operation must be run outside of lbs_thread. This
1066 * is because mmc_remove_host() will cause the device to be instantly
1067 * destroyed, and the libertas driver then needs to end lbs_thread,
1068 * leading to a deadlock.
1070 * We run it in a workqueue totally independent from the if_sdio_card
1071 * instance for that reason.
1074 pr_info("Resetting card...");
1075 mmc_remove_host(reset_host);
1076 mmc_add_host(reset_host);
1078 static DECLARE_WORK(card_reset_work, if_sdio_reset_card_worker);
1080 static void if_sdio_reset_card(struct lbs_private *priv)
1082 struct if_sdio_card *card = priv->card;
1084 if (work_pending(&card_reset_work))
1087 reset_host = card->func->card->host;
1088 schedule_work(&card_reset_work);
1091 static int if_sdio_power_save(struct lbs_private *priv)
1093 struct if_sdio_card *card = priv->card;
1096 flush_workqueue(card->workqueue);
1098 ret = if_sdio_power_off(card);
1100 /* Let runtime PM know the card is powered off */
1101 pm_runtime_put_sync(&card->func->dev);
1106 static int if_sdio_power_restore(struct lbs_private *priv)
1108 struct if_sdio_card *card = priv->card;
1111 /* Make sure the card will not be powered off by runtime PM */
1112 pm_runtime_get_sync(&card->func->dev);
1114 r = if_sdio_power_on(card);
1118 wait_event(card->pwron_waitq, priv->fw_ready);
1123 /*******************************************************************/
1124 /* SDIO callbacks */
1125 /*******************************************************************/
1127 static void if_sdio_interrupt(struct sdio_func *func)
1130 struct if_sdio_card *card;
1133 lbs_deb_enter(LBS_DEB_SDIO);
1135 card = sdio_get_drvdata(func);
1137 cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
1141 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
1143 sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
1148 * Ignore the define name, this really means the card has
1149 * successfully received the command.
1151 card->priv->is_activity_detected = 1;
1152 if (cause & IF_SDIO_H_INT_DNLD)
1153 lbs_host_to_card_done(card->priv);
1156 if (cause & IF_SDIO_H_INT_UPLD) {
1157 ret = if_sdio_card_to_host(card);
1165 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1168 static int if_sdio_probe(struct sdio_func *func,
1169 const struct sdio_device_id *id)
1171 struct if_sdio_card *card;
1172 struct lbs_private *priv;
1175 struct if_sdio_packet *packet;
1177 lbs_deb_enter(LBS_DEB_SDIO);
1179 for (i = 0;i < func->card->num_info;i++) {
1180 if (sscanf(func->card->info[i],
1181 "802.11 SDIO ID: %x", &model) == 1)
1183 if (sscanf(func->card->info[i],
1184 "ID: %x", &model) == 1)
1186 if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
1192 if (i == func->card->num_info) {
1193 pr_err("unable to identify card model\n");
1197 card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
1202 card->model = model;
1204 switch (card->model) {
1206 card->scratch_reg = IF_SDIO_SCRATCH_OLD;
1209 card->scratch_reg = IF_SDIO_SCRATCH;
1212 default: /* for newer chipsets */
1213 card->scratch_reg = IF_SDIO_FW_STATUS;
1217 spin_lock_init(&card->lock);
1218 card->workqueue = alloc_workqueue("libertas_sdio", WQ_MEM_RECLAIM, 0);
1219 if (unlikely(!card->workqueue)) {
1223 INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
1224 init_waitqueue_head(&card->pwron_waitq);
1226 /* Check if we support this card */
1227 for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1228 if (card->model == fw_table[i].model)
1231 if (i == ARRAY_SIZE(fw_table)) {
1232 pr_err("unknown card model 0x%x\n", card->model);
1237 sdio_set_drvdata(func, card);
1239 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
1240 "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
1241 func->class, func->vendor, func->device,
1242 model, (unsigned)card->ioport);
1245 priv = lbs_add_card(card, &func->dev);
1254 priv->hw_host_to_card = if_sdio_host_to_card;
1255 priv->enter_deep_sleep = if_sdio_enter_deep_sleep;
1256 priv->exit_deep_sleep = if_sdio_exit_deep_sleep;
1257 priv->reset_deep_sleep_wakeup = if_sdio_reset_deep_sleep_wakeup;
1258 priv->reset_card = if_sdio_reset_card;
1259 priv->power_save = if_sdio_power_save;
1260 priv->power_restore = if_sdio_power_restore;
1261 priv->is_polling = !(func->card->host->caps & MMC_CAP_SDIO_IRQ);
1262 ret = if_sdio_power_on(card);
1264 goto err_activate_card;
1267 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1272 flush_workqueue(card->workqueue);
1273 lbs_remove_card(priv);
1275 destroy_workqueue(card->workqueue);
1277 while (card->packets) {
1278 packet = card->packets;
1279 card->packets = card->packets->next;
1288 static void if_sdio_remove(struct sdio_func *func)
1290 struct if_sdio_card *card;
1291 struct if_sdio_packet *packet;
1293 lbs_deb_enter(LBS_DEB_SDIO);
1295 card = sdio_get_drvdata(func);
1297 /* Undo decrement done above in if_sdio_probe */
1298 pm_runtime_get_noresume(&func->dev);
1300 if (user_rmmod && (card->model == MODEL_8688)) {
1302 * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
1303 * multiple functions
1305 struct cmd_header cmd;
1307 memset(&cmd, 0, sizeof(cmd));
1309 lbs_deb_sdio("send function SHUTDOWN command\n");
1310 if (__lbs_cmd(card->priv, CMD_FUNC_SHUTDOWN,
1311 &cmd, sizeof(cmd), lbs_cmd_copyback,
1312 (unsigned long) &cmd))
1313 pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1317 lbs_deb_sdio("call remove card\n");
1318 lbs_stop_card(card->priv);
1319 lbs_remove_card(card->priv);
1321 destroy_workqueue(card->workqueue);
1323 while (card->packets) {
1324 packet = card->packets;
1325 card->packets = card->packets->next;
1330 lbs_deb_leave(LBS_DEB_SDIO);
1333 static int if_sdio_suspend(struct device *dev)
1335 struct sdio_func *func = dev_to_sdio_func(dev);
1336 struct if_sdio_card *card = sdio_get_drvdata(func);
1337 struct lbs_private *priv = card->priv;
1340 mmc_pm_flag_t flags = sdio_get_host_pm_caps(func);
1341 priv->power_up_on_resume = false;
1343 /* If we're powered off anyway, just let the mmc layer remove the
1345 if (!lbs_iface_active(priv)) {
1346 if (priv->fw_ready) {
1347 priv->power_up_on_resume = true;
1348 if_sdio_power_off(card);
1354 dev_info(dev, "%s: suspend: PM flags = 0x%x\n",
1355 sdio_func_id(func), flags);
1357 /* If we aren't being asked to wake on anything, we should bail out
1358 * and let the SD stack power down the card.
1360 if (priv->wol_criteria == EHS_REMOVE_WAKEUP) {
1361 dev_info(dev, "Suspend without wake params -- powering down card\n");
1362 if (priv->fw_ready) {
1363 ret = lbs_suspend(priv);
1367 priv->power_up_on_resume = true;
1368 if_sdio_power_off(card);
1374 if (!(flags & MMC_PM_KEEP_POWER)) {
1375 dev_err(dev, "%s: cannot remain alive while host is suspended\n",
1376 sdio_func_id(func));
1380 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1384 ret = lbs_suspend(priv);
1388 return sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
1391 static int if_sdio_resume(struct device *dev)
1393 struct sdio_func *func = dev_to_sdio_func(dev);
1394 struct if_sdio_card *card = sdio_get_drvdata(func);
1397 dev_info(dev, "%s: resume: we're back\n", sdio_func_id(func));
1399 if (card->priv->power_up_on_resume) {
1400 if_sdio_power_on(card);
1401 wait_event(card->pwron_waitq, card->priv->fw_ready);
1404 ret = lbs_resume(card->priv);
1409 static const struct dev_pm_ops if_sdio_pm_ops = {
1410 .suspend = if_sdio_suspend,
1411 .resume = if_sdio_resume,
1414 static struct sdio_driver if_sdio_driver = {
1415 .name = "libertas_sdio",
1416 .id_table = if_sdio_ids,
1417 .probe = if_sdio_probe,
1418 .remove = if_sdio_remove,
1420 .pm = &if_sdio_pm_ops,
1424 /*******************************************************************/
1425 /* Module functions */
1426 /*******************************************************************/
1428 static int __init if_sdio_init_module(void)
1432 lbs_deb_enter(LBS_DEB_SDIO);
1434 printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1435 printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1437 ret = sdio_register_driver(&if_sdio_driver);
1439 /* Clear the flag in case user removes the card. */
1442 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1447 static void __exit if_sdio_exit_module(void)
1449 lbs_deb_enter(LBS_DEB_SDIO);
1451 /* Set the flag as user is removing this module. */
1454 cancel_work_sync(&card_reset_work);
1456 sdio_unregister_driver(&if_sdio_driver);
1458 lbs_deb_leave(LBS_DEB_SDIO);
1461 module_init(if_sdio_init_module);
1462 module_exit(if_sdio_exit_module);
1464 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1465 MODULE_AUTHOR("Pierre Ossman");
1466 MODULE_LICENSE("GPL");