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
, "libertas/sd8385_helper.bin", "libertas/sd8385.bin" },
84 { MODEL_8385
, "sd8385_helper.bin", "sd8385.bin" },
85 { MODEL_8686
, "libertas/sd8686_v9_helper.bin", "libertas/sd8686_v9.bin" },
86 { MODEL_8686
, "libertas/sd8686_v8_helper.bin", "libertas/sd8686_v8.bin" },
87 { MODEL_8686
, "sd8686_helper.bin", "sd8686.bin" },
88 { MODEL_8688
, "libertas/sd8688_helper.bin", "libertas/sd8688.bin" },
89 { MODEL_8688
, "sd8688_helper.bin", "sd8688.bin" },
92 MODULE_FIRMWARE("libertas/sd8385_helper.bin");
93 MODULE_FIRMWARE("libertas/sd8385.bin");
94 MODULE_FIRMWARE("sd8385_helper.bin");
95 MODULE_FIRMWARE("sd8385.bin");
96 MODULE_FIRMWARE("libertas/sd8686_v9_helper.bin");
97 MODULE_FIRMWARE("libertas/sd8686_v9.bin");
98 MODULE_FIRMWARE("libertas/sd8686_v8_helper.bin");
99 MODULE_FIRMWARE("libertas/sd8686_v8.bin");
100 MODULE_FIRMWARE("sd8686_helper.bin");
101 MODULE_FIRMWARE("sd8686.bin");
102 MODULE_FIRMWARE("libertas/sd8688_helper.bin");
103 MODULE_FIRMWARE("libertas/sd8688.bin");
104 MODULE_FIRMWARE("sd8688_helper.bin");
105 MODULE_FIRMWARE("sd8688.bin");
107 struct if_sdio_packet
{
108 struct if_sdio_packet
*next
;
110 u8 buffer
[0] __attribute__((aligned(4)));
113 struct if_sdio_card
{
114 struct sdio_func
*func
;
115 struct lbs_private
*priv
;
118 unsigned long ioport
;
119 unsigned int scratch_reg
;
121 wait_queue_head_t pwron_waitq
;
123 u8 buffer
[65536] __attribute__((aligned(4)));
126 struct if_sdio_packet
*packets
;
128 struct workqueue_struct
*workqueue
;
129 struct work_struct packet_worker
;
134 static void if_sdio_finish_power_on(struct if_sdio_card
*card
);
135 static int if_sdio_power_off(struct if_sdio_card
*card
);
137 /********************************************************************/
139 /********************************************************************/
142 * For SD8385/SD8686, this function reads firmware status after
143 * the image is downloaded, or reads RX packet length when
144 * interrupt (with IF_SDIO_H_INT_UPLD bit set) is received.
145 * For SD8688, this function reads firmware status only.
147 static u16
if_sdio_read_scratch(struct if_sdio_card
*card
, int *err
)
152 scratch
= sdio_readb(card
->func
, card
->scratch_reg
, &ret
);
154 scratch
|= sdio_readb(card
->func
, card
->scratch_reg
+ 1,
166 static u8
if_sdio_read_rx_unit(struct if_sdio_card
*card
)
171 rx_unit
= sdio_readb(card
->func
, IF_SDIO_RX_UNIT
, &ret
);
179 static u16
if_sdio_read_rx_len(struct if_sdio_card
*card
, int *err
)
184 switch (card
->model
) {
187 rx_len
= if_sdio_read_scratch(card
, &ret
);
190 default: /* for newer chipsets */
191 rx_len
= sdio_readb(card
->func
, IF_SDIO_RX_LEN
, &ret
);
193 rx_len
<<= card
->rx_unit
;
195 rx_len
= 0xffff; /* invalid length */
206 static int if_sdio_handle_cmd(struct if_sdio_card
*card
,
207 u8
*buffer
, unsigned size
)
209 struct lbs_private
*priv
= card
->priv
;
214 if (size
> LBS_CMD_BUFFER_SIZE
) {
215 lbs_deb_sdio("response packet too large (%d bytes)\n",
221 spin_lock_irqsave(&priv
->driver_lock
, flags
);
223 i
= (priv
->resp_idx
== 0) ? 1 : 0;
224 BUG_ON(priv
->resp_len
[i
]);
225 priv
->resp_len
[i
] = size
;
226 memcpy(priv
->resp_buf
[i
], buffer
, size
);
227 lbs_notify_command_response(priv
, i
);
229 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
237 static int if_sdio_handle_data(struct if_sdio_card
*card
,
238 u8
*buffer
, unsigned size
)
243 if (size
> MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
) {
244 lbs_deb_sdio("response packet too large (%d bytes)\n",
250 skb
= dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
+ NET_IP_ALIGN
);
256 skb_reserve(skb
, NET_IP_ALIGN
);
258 skb_put_data(skb
, buffer
, size
);
260 lbs_process_rxed_packet(card
->priv
, skb
);
268 static int if_sdio_handle_event(struct if_sdio_card
*card
,
269 u8
*buffer
, unsigned size
)
274 if (card
->model
== MODEL_8385
) {
275 event
= sdio_readb(card
->func
, IF_SDIO_EVENT
, &ret
);
279 /* right shift 3 bits to get the event id */
283 lbs_deb_sdio("event packet too small (%d bytes)\n",
288 event
= buffer
[3] << 24;
289 event
|= buffer
[2] << 16;
290 event
|= buffer
[1] << 8;
291 event
|= buffer
[0] << 0;
294 lbs_queue_event(card
->priv
, event
& 0xFF);
301 static int if_sdio_wait_status(struct if_sdio_card
*card
, const u8 condition
)
304 unsigned long timeout
;
307 timeout
= jiffies
+ HZ
;
309 status
= sdio_readb(card
->func
, IF_SDIO_STATUS
, &ret
);
312 if ((status
& condition
) == condition
)
314 if (time_after(jiffies
, timeout
))
321 static int if_sdio_card_to_host(struct if_sdio_card
*card
)
324 u16 size
, type
, chunk
;
326 size
= if_sdio_read_rx_len(card
, &ret
);
331 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
337 ret
= if_sdio_wait_status(card
, IF_SDIO_IO_RDY
);
342 * The transfer must be in one transaction or the firmware
343 * goes suicidal. There's no way to guarantee that for all
344 * controllers, but we can at least try.
346 chunk
= sdio_align_size(card
->func
, size
);
348 ret
= sdio_readsb(card
->func
, card
->buffer
, card
->ioport
, chunk
);
352 chunk
= card
->buffer
[0] | (card
->buffer
[1] << 8);
353 type
= card
->buffer
[2] | (card
->buffer
[3] << 8);
355 lbs_deb_sdio("packet of type %d and size %d bytes\n",
356 (int)type
, (int)chunk
);
359 lbs_deb_sdio("packet fragment (%d > %d)\n",
360 (int)chunk
, (int)size
);
366 lbs_deb_sdio("packet fragment (%d < %d)\n",
367 (int)chunk
, (int)size
);
372 ret
= if_sdio_handle_cmd(card
, card
->buffer
+ 4, chunk
- 4);
377 ret
= if_sdio_handle_data(card
, card
->buffer
+ 4, chunk
- 4);
382 ret
= if_sdio_handle_event(card
, card
->buffer
+ 4, chunk
- 4);
387 lbs_deb_sdio("invalid type (%d) from firmware\n",
395 pr_err("problem fetching packet from firmware\n");
400 static void if_sdio_host_to_card_worker(struct work_struct
*work
)
402 struct if_sdio_card
*card
;
403 struct if_sdio_packet
*packet
;
407 card
= container_of(work
, struct if_sdio_card
, packet_worker
);
410 spin_lock_irqsave(&card
->lock
, flags
);
411 packet
= card
->packets
;
413 card
->packets
= packet
->next
;
414 spin_unlock_irqrestore(&card
->lock
, flags
);
419 sdio_claim_host(card
->func
);
421 ret
= if_sdio_wait_status(card
, IF_SDIO_IO_RDY
);
423 ret
= sdio_writesb(card
->func
, card
->ioport
,
424 packet
->buffer
, packet
->nb
);
428 pr_err("error %d sending packet to firmware\n", ret
);
430 sdio_release_host(card
->func
);
436 /********************************************************************/
438 /********************************************************************/
440 #define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY)
442 static int if_sdio_prog_helper(struct if_sdio_card
*card
,
443 const struct firmware
*fw
)
446 unsigned long timeout
;
452 chunk_buffer
= kzalloc(64, GFP_KERNEL
);
458 sdio_claim_host(card
->func
);
460 ret
= sdio_set_block_size(card
->func
, 32);
468 ret
= if_sdio_wait_status(card
, FW_DL_READY_STATUS
);
472 /* On some platforms (like Davinci) the chip needs more time
473 * between helper blocks.
477 chunk_size
= min_t(size_t, size
, 60);
479 *((__le32
*)chunk_buffer
) = cpu_to_le32(chunk_size
);
480 memcpy(chunk_buffer
+ 4, firmware
, chunk_size
);
482 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
484 ret
= sdio_writesb(card
->func
, card
->ioport
,
489 firmware
+= chunk_size
;
493 /* an empty block marks the end of the transfer */
494 memset(chunk_buffer
, 0, 4);
495 ret
= sdio_writesb(card
->func
, card
->ioport
, chunk_buffer
, 64);
499 lbs_deb_sdio("waiting for helper to boot...\n");
501 /* wait for the helper to boot by looking at the size register */
502 timeout
= jiffies
+ HZ
;
506 req_size
= sdio_readb(card
->func
, IF_SDIO_RD_BASE
, &ret
);
510 req_size
|= sdio_readb(card
->func
, IF_SDIO_RD_BASE
+ 1, &ret
) << 8;
517 if (time_after(jiffies
, timeout
)) {
528 sdio_release_host(card
->func
);
533 pr_err("failed to load helper firmware\n");
538 static int if_sdio_prog_real(struct if_sdio_card
*card
,
539 const struct firmware
*fw
)
542 unsigned long timeout
;
546 size_t size
, req_size
;
548 chunk_buffer
= kzalloc(512, GFP_KERNEL
);
554 sdio_claim_host(card
->func
);
556 ret
= sdio_set_block_size(card
->func
, 32);
564 timeout
= jiffies
+ HZ
;
566 ret
= if_sdio_wait_status(card
, FW_DL_READY_STATUS
);
570 req_size
= sdio_readb(card
->func
, IF_SDIO_RD_BASE
,
575 req_size
|= sdio_readb(card
->func
, IF_SDIO_RD_BASE
+ 1,
581 * For SD8688 wait until the length is not 0, 1 or 2
582 * before downloading the first FW block,
583 * since BOOT code writes the register to indicate the
584 * helper/FW download winner,
585 * the value could be 1 or 2 (Func1 or Func2).
587 if ((size
!= fw
->size
) || (req_size
> 2))
589 if (time_after(jiffies
, timeout
)) {
597 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
600 lbs_deb_sdio("firmware helper gave up early\n");
605 if (req_size
& 0x01) {
606 lbs_deb_sdio("firmware helper signalled error\n");
615 chunk_size
= min_t(size_t, req_size
, 512);
617 memcpy(chunk_buffer
, firmware
, chunk_size
);
619 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
620 chunk_size, (chunk_size + 31) / 32 * 32);
622 ret
= sdio_writesb(card
->func
, card
->ioport
,
623 chunk_buffer
, roundup(chunk_size
, 32));
627 firmware
+= chunk_size
;
629 req_size
-= chunk_size
;
635 lbs_deb_sdio("waiting for firmware to boot...\n");
637 /* wait for the firmware to boot */
638 timeout
= jiffies
+ HZ
;
642 scratch
= if_sdio_read_scratch(card
, &ret
);
646 if (scratch
== IF_SDIO_FIRMWARE_OK
)
649 if (time_after(jiffies
, timeout
)) {
660 sdio_release_host(card
->func
);
665 pr_err("failed to load firmware\n");
670 static void if_sdio_do_prog_firmware(struct lbs_private
*priv
, int ret
,
671 const struct firmware
*helper
,
672 const struct firmware
*mainfw
)
674 struct if_sdio_card
*card
= priv
->card
;
677 pr_err("failed to find firmware (%d)\n", ret
);
681 ret
= if_sdio_prog_helper(card
, helper
);
685 lbs_deb_sdio("Helper firmware loaded\n");
687 ret
= if_sdio_prog_real(card
, mainfw
);
691 lbs_deb_sdio("Firmware loaded\n");
692 if_sdio_finish_power_on(card
);
695 static int if_sdio_prog_firmware(struct if_sdio_card
*card
)
703 sdio_claim_host(card
->func
);
704 sdio_writeb(card
->func
, 0x00, IF_SDIO_H_INT_MASK
, &ret
);
705 sdio_release_host(card
->func
);
707 sdio_claim_host(card
->func
);
708 scratch
= if_sdio_read_scratch(card
, &ret
);
709 sdio_release_host(card
->func
);
711 lbs_deb_sdio("firmware status = %#x\n", scratch
);
712 lbs_deb_sdio("scratch ret = %d\n", ret
);
719 * The manual clearly describes that FEDC is the right code to use
720 * to detect firmware presence, but for SD8686 it is not that simple.
721 * Scratch is also used to store the RX packet length, so we lose
722 * the FEDC value early on. So we use a non-zero check in order
723 * to validate firmware presence.
724 * Additionally, the SD8686 in the Gumstix always has the high scratch
725 * bit set, even when the firmware is not loaded. So we have to
726 * exclude that from the test.
728 if (scratch
== IF_SDIO_FIRMWARE_OK
) {
729 lbs_deb_sdio("firmware already loaded\n");
730 if_sdio_finish_power_on(card
);
732 } else if ((card
->model
== MODEL_8686
) && (scratch
& 0x7fff)) {
733 lbs_deb_sdio("firmware may be running\n");
734 if_sdio_finish_power_on(card
);
738 ret
= lbs_get_firmware_async(card
->priv
, &card
->func
->dev
, card
->model
,
739 fw_table
, if_sdio_do_prog_firmware
);
745 /********************************************************************/
746 /* Power management */
747 /********************************************************************/
749 /* Finish power on sequence (after firmware is loaded) */
750 static void if_sdio_finish_power_on(struct if_sdio_card
*card
)
752 struct sdio_func
*func
= card
->func
;
753 struct lbs_private
*priv
= card
->priv
;
756 sdio_claim_host(func
);
757 sdio_set_block_size(card
->func
, IF_SDIO_BLOCK_SIZE
);
760 * Get rx_unit if the chip is SD8688 or newer.
761 * SD8385 & SD8686 do not have rx_unit.
763 if ((card
->model
!= MODEL_8385
)
764 && (card
->model
!= MODEL_8686
))
765 card
->rx_unit
= if_sdio_read_rx_unit(card
);
770 * Set up the interrupt handler late.
772 * If we set it up earlier, the (buggy) hardware generates a spurious
773 * interrupt, even before the interrupt has been enabled, with
776 * We register the interrupt handler late so that we can handle any
777 * spurious interrupts, and also to avoid generation of that known
778 * spurious interrupt in the first place.
780 ret
= sdio_claim_irq(func
, if_sdio_interrupt
);
785 * Enable interrupts now that everything is set up
787 sdio_writeb(func
, 0x0f, IF_SDIO_H_INT_MASK
, &ret
);
791 sdio_release_host(func
);
793 /* Set fw_ready before queuing any commands so that
794 * lbs_thread won't block from sending them to firmware.
799 * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
801 if (card
->model
== MODEL_8688
) {
802 struct cmd_header cmd
;
804 memset(&cmd
, 0, sizeof(cmd
));
806 lbs_deb_sdio("send function INIT command\n");
807 if (__lbs_cmd(priv
, CMD_FUNC_INIT
, &cmd
, sizeof(cmd
),
808 lbs_cmd_copyback
, (unsigned long) &cmd
))
809 netdev_alert(priv
->dev
, "CMD_FUNC_INIT cmd failed\n");
812 wake_up(&card
->pwron_waitq
);
814 if (!card
->started
) {
815 ret
= lbs_start_card(priv
);
816 if_sdio_power_off(card
);
818 card
->started
= true;
819 /* Tell PM core that we don't need the card to be
821 pm_runtime_put(&func
->dev
);
828 sdio_release_irq(func
);
830 sdio_release_host(func
);
833 static int if_sdio_power_on(struct if_sdio_card
*card
)
835 struct sdio_func
*func
= card
->func
;
836 struct mmc_host
*host
= func
->card
->host
;
839 sdio_claim_host(func
);
841 ret
= sdio_enable_func(func
);
845 /* For 1-bit transfers to the 8686 model, we need to enable the
846 * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0
847 * bit to allow access to non-vendor registers. */
848 if ((card
->model
== MODEL_8686
) &&
849 (host
->caps
& MMC_CAP_SDIO_IRQ
) &&
850 (host
->ios
.bus_width
== MMC_BUS_WIDTH_1
)) {
853 func
->card
->quirks
|= MMC_QUIRK_LENIENT_FN0
;
854 reg
= sdio_f0_readb(func
, SDIO_CCCR_IF
, &ret
);
858 reg
|= SDIO_BUS_ECSI
;
859 sdio_f0_writeb(func
, reg
, SDIO_CCCR_IF
, &ret
);
864 card
->ioport
= sdio_readb(func
, IF_SDIO_IOPORT
, &ret
);
868 card
->ioport
|= sdio_readb(func
, IF_SDIO_IOPORT
+ 1, &ret
) << 8;
872 card
->ioport
|= sdio_readb(func
, IF_SDIO_IOPORT
+ 2, &ret
) << 16;
876 sdio_release_host(func
);
877 ret
= if_sdio_prog_firmware(card
);
879 sdio_claim_host(func
);
886 sdio_disable_func(func
);
888 sdio_release_host(func
);
892 static int if_sdio_power_off(struct if_sdio_card
*card
)
894 struct sdio_func
*func
= card
->func
;
895 struct lbs_private
*priv
= card
->priv
;
899 sdio_claim_host(func
);
900 sdio_release_irq(func
);
901 sdio_disable_func(func
);
902 sdio_release_host(func
);
907 /*******************************************************************/
908 /* Libertas callbacks */
909 /*******************************************************************/
911 static int if_sdio_host_to_card(struct lbs_private
*priv
,
912 u8 type
, u8
*buf
, u16 nb
)
915 struct if_sdio_card
*card
;
916 struct if_sdio_packet
*packet
, *cur
;
922 if (nb
> (65536 - sizeof(struct if_sdio_packet
) - 4)) {
928 * The transfer must be in one transaction or the firmware
929 * goes suicidal. There's no way to guarantee that for all
930 * controllers, but we can at least try.
932 size
= sdio_align_size(card
->func
, nb
+ 4);
934 packet
= kzalloc(sizeof(struct if_sdio_packet
) + size
,
945 * SDIO specific header.
947 packet
->buffer
[0] = (nb
+ 4) & 0xff;
948 packet
->buffer
[1] = ((nb
+ 4) >> 8) & 0xff;
949 packet
->buffer
[2] = type
;
950 packet
->buffer
[3] = 0;
952 memcpy(packet
->buffer
+ 4, buf
, nb
);
954 spin_lock_irqsave(&card
->lock
, flags
);
957 card
->packets
= packet
;
967 priv
->dnld_sent
= DNLD_CMD_SENT
;
970 priv
->dnld_sent
= DNLD_DATA_SENT
;
973 lbs_deb_sdio("unknown packet type %d\n", (int)type
);
976 spin_unlock_irqrestore(&card
->lock
, flags
);
978 queue_work(card
->workqueue
, &card
->packet_worker
);
986 static int if_sdio_enter_deep_sleep(struct lbs_private
*priv
)
989 struct cmd_header cmd
;
991 memset(&cmd
, 0, sizeof(cmd
));
993 lbs_deb_sdio("send DEEP_SLEEP command\n");
994 ret
= __lbs_cmd(priv
, CMD_802_11_DEEP_SLEEP
, &cmd
, sizeof(cmd
),
995 lbs_cmd_copyback
, (unsigned long) &cmd
);
997 netdev_err(priv
->dev
, "DEEP_SLEEP cmd failed\n");
1003 static int if_sdio_exit_deep_sleep(struct lbs_private
*priv
)
1005 struct if_sdio_card
*card
= priv
->card
;
1008 sdio_claim_host(card
->func
);
1010 sdio_writeb(card
->func
, HOST_POWER_UP
, CONFIGURATION_REG
, &ret
);
1012 netdev_err(priv
->dev
, "sdio_writeb failed!\n");
1014 sdio_release_host(card
->func
);
1019 static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private
*priv
)
1021 struct if_sdio_card
*card
= priv
->card
;
1024 sdio_claim_host(card
->func
);
1026 sdio_writeb(card
->func
, 0, CONFIGURATION_REG
, &ret
);
1028 netdev_err(priv
->dev
, "sdio_writeb failed!\n");
1030 sdio_release_host(card
->func
);
1036 static struct mmc_host
*reset_host
;
1038 static void if_sdio_reset_card_worker(struct work_struct
*work
)
1041 * The actual reset operation must be run outside of lbs_thread. This
1042 * is because mmc_remove_host() will cause the device to be instantly
1043 * destroyed, and the libertas driver then needs to end lbs_thread,
1044 * leading to a deadlock.
1046 * We run it in a workqueue totally independent from the if_sdio_card
1047 * instance for that reason.
1050 pr_info("Resetting card...");
1051 mmc_remove_host(reset_host
);
1052 mmc_add_host(reset_host
);
1054 static DECLARE_WORK(card_reset_work
, if_sdio_reset_card_worker
);
1056 static void if_sdio_reset_card(struct lbs_private
*priv
)
1058 struct if_sdio_card
*card
= priv
->card
;
1060 if (work_pending(&card_reset_work
))
1063 reset_host
= card
->func
->card
->host
;
1064 schedule_work(&card_reset_work
);
1067 static int if_sdio_power_save(struct lbs_private
*priv
)
1069 struct if_sdio_card
*card
= priv
->card
;
1072 flush_workqueue(card
->workqueue
);
1074 ret
= if_sdio_power_off(card
);
1076 /* Let runtime PM know the card is powered off */
1077 pm_runtime_put_sync(&card
->func
->dev
);
1082 static int if_sdio_power_restore(struct lbs_private
*priv
)
1084 struct if_sdio_card
*card
= priv
->card
;
1087 /* Make sure the card will not be powered off by runtime PM */
1088 pm_runtime_get_sync(&card
->func
->dev
);
1090 r
= if_sdio_power_on(card
);
1094 wait_event(card
->pwron_waitq
, priv
->fw_ready
);
1099 /*******************************************************************/
1100 /* SDIO callbacks */
1101 /*******************************************************************/
1103 static void if_sdio_interrupt(struct sdio_func
*func
)
1106 struct if_sdio_card
*card
;
1109 card
= sdio_get_drvdata(func
);
1111 cause
= sdio_readb(card
->func
, IF_SDIO_H_INT_STATUS
, &ret
);
1115 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause
);
1117 sdio_writeb(card
->func
, ~cause
, IF_SDIO_H_INT_STATUS
, &ret
);
1122 * Ignore the define name, this really means the card has
1123 * successfully received the command.
1125 card
->priv
->is_activity_detected
= 1;
1126 if (cause
& IF_SDIO_H_INT_DNLD
)
1127 lbs_host_to_card_done(card
->priv
);
1130 if (cause
& IF_SDIO_H_INT_UPLD
) {
1131 ret
= if_sdio_card_to_host(card
);
1137 static int if_sdio_probe(struct sdio_func
*func
,
1138 const struct sdio_device_id
*id
)
1140 struct if_sdio_card
*card
;
1141 struct lbs_private
*priv
;
1144 struct if_sdio_packet
*packet
;
1146 for (i
= 0;i
< func
->card
->num_info
;i
++) {
1147 if (sscanf(func
->card
->info
[i
],
1148 "802.11 SDIO ID: %x", &model
) == 1)
1150 if (sscanf(func
->card
->info
[i
],
1151 "ID: %x", &model
) == 1)
1153 if (!strcmp(func
->card
->info
[i
], "IBIS Wireless SDIO Card")) {
1159 if (i
== func
->card
->num_info
) {
1160 pr_err("unable to identify card model\n");
1164 card
= kzalloc(sizeof(struct if_sdio_card
), GFP_KERNEL
);
1169 card
->model
= model
;
1171 switch (card
->model
) {
1173 card
->scratch_reg
= IF_SDIO_SCRATCH_OLD
;
1176 card
->scratch_reg
= IF_SDIO_SCRATCH
;
1179 default: /* for newer chipsets */
1180 card
->scratch_reg
= IF_SDIO_FW_STATUS
;
1184 spin_lock_init(&card
->lock
);
1185 card
->workqueue
= alloc_workqueue("libertas_sdio", WQ_MEM_RECLAIM
, 0);
1186 INIT_WORK(&card
->packet_worker
, if_sdio_host_to_card_worker
);
1187 init_waitqueue_head(&card
->pwron_waitq
);
1189 /* Check if we support this card */
1190 for (i
= 0; i
< ARRAY_SIZE(fw_table
); i
++) {
1191 if (card
->model
== fw_table
[i
].model
)
1194 if (i
== ARRAY_SIZE(fw_table
)) {
1195 pr_err("unknown card model 0x%x\n", card
->model
);
1200 sdio_set_drvdata(func
, card
);
1202 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
1203 "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
1204 func
->class, func
->vendor
, func
->device
,
1205 model
, (unsigned)card
->ioport
);
1208 priv
= lbs_add_card(card
, &func
->dev
);
1217 priv
->hw_host_to_card
= if_sdio_host_to_card
;
1218 priv
->enter_deep_sleep
= if_sdio_enter_deep_sleep
;
1219 priv
->exit_deep_sleep
= if_sdio_exit_deep_sleep
;
1220 priv
->reset_deep_sleep_wakeup
= if_sdio_reset_deep_sleep_wakeup
;
1221 priv
->reset_card
= if_sdio_reset_card
;
1222 priv
->power_save
= if_sdio_power_save
;
1223 priv
->power_restore
= if_sdio_power_restore
;
1224 priv
->is_polling
= !(func
->card
->host
->caps
& MMC_CAP_SDIO_IRQ
);
1225 ret
= if_sdio_power_on(card
);
1227 goto err_activate_card
;
1233 flush_workqueue(card
->workqueue
);
1234 lbs_remove_card(priv
);
1236 destroy_workqueue(card
->workqueue
);
1237 while (card
->packets
) {
1238 packet
= card
->packets
;
1239 card
->packets
= card
->packets
->next
;
1248 static void if_sdio_remove(struct sdio_func
*func
)
1250 struct if_sdio_card
*card
;
1251 struct if_sdio_packet
*packet
;
1253 card
= sdio_get_drvdata(func
);
1255 /* Undo decrement done above in if_sdio_probe */
1256 pm_runtime_get_noresume(&func
->dev
);
1258 if (user_rmmod
&& (card
->model
== MODEL_8688
)) {
1260 * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
1261 * multiple functions
1263 struct cmd_header cmd
;
1265 memset(&cmd
, 0, sizeof(cmd
));
1267 lbs_deb_sdio("send function SHUTDOWN command\n");
1268 if (__lbs_cmd(card
->priv
, CMD_FUNC_SHUTDOWN
,
1269 &cmd
, sizeof(cmd
), lbs_cmd_copyback
,
1270 (unsigned long) &cmd
))
1271 pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1275 lbs_deb_sdio("call remove card\n");
1276 lbs_stop_card(card
->priv
);
1277 lbs_remove_card(card
->priv
);
1279 destroy_workqueue(card
->workqueue
);
1281 while (card
->packets
) {
1282 packet
= card
->packets
;
1283 card
->packets
= card
->packets
->next
;
1290 static int if_sdio_suspend(struct device
*dev
)
1292 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1294 struct if_sdio_card
*card
= sdio_get_drvdata(func
);
1296 mmc_pm_flag_t flags
= sdio_get_host_pm_caps(func
);
1298 /* If we're powered off anyway, just let the mmc layer remove the
1300 if (!lbs_iface_active(card
->priv
))
1303 dev_info(dev
, "%s: suspend: PM flags = 0x%x\n",
1304 sdio_func_id(func
), flags
);
1306 /* If we aren't being asked to wake on anything, we should bail out
1307 * and let the SD stack power down the card.
1309 if (card
->priv
->wol_criteria
== EHS_REMOVE_WAKEUP
) {
1310 dev_info(dev
, "Suspend without wake params -- powering down card\n");
1314 if (!(flags
& MMC_PM_KEEP_POWER
)) {
1315 dev_err(dev
, "%s: cannot remain alive while host is suspended\n",
1316 sdio_func_id(func
));
1320 ret
= sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
1324 ret
= lbs_suspend(card
->priv
);
1328 return sdio_set_host_pm_flags(func
, MMC_PM_WAKE_SDIO_IRQ
);
1331 static int if_sdio_resume(struct device
*dev
)
1333 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1334 struct if_sdio_card
*card
= sdio_get_drvdata(func
);
1337 dev_info(dev
, "%s: resume: we're back\n", sdio_func_id(func
));
1339 ret
= lbs_resume(card
->priv
);
1344 static const struct dev_pm_ops if_sdio_pm_ops
= {
1345 .suspend
= if_sdio_suspend
,
1346 .resume
= if_sdio_resume
,
1349 static struct sdio_driver if_sdio_driver
= {
1350 .name
= "libertas_sdio",
1351 .id_table
= if_sdio_ids
,
1352 .probe
= if_sdio_probe
,
1353 .remove
= if_sdio_remove
,
1355 .pm
= &if_sdio_pm_ops
,
1359 /*******************************************************************/
1360 /* Module functions */
1361 /*******************************************************************/
1363 static int __init
if_sdio_init_module(void)
1367 printk(KERN_INFO
"libertas_sdio: Libertas SDIO driver\n");
1368 printk(KERN_INFO
"libertas_sdio: Copyright Pierre Ossman\n");
1370 ret
= sdio_register_driver(&if_sdio_driver
);
1372 /* Clear the flag in case user removes the card. */
1378 static void __exit
if_sdio_exit_module(void)
1380 /* Set the flag as user is removing this module. */
1383 cancel_work_sync(&card_reset_work
);
1385 sdio_unregister_driver(&if_sdio_driver
);
1388 module_init(if_sdio_init_module
);
1389 module_exit(if_sdio_exit_module
);
1391 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1392 MODULE_AUTHOR("Pierre Ossman");
1393 MODULE_LICENSE("GPL");