1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/net/wireless/libertas/if_sdio.c
5 * Copyright 2007-2008 Pierre Ossman
7 * Inspired by if_cs.c, Copyright 2007 Holger Schurig
9 * This hardware has more or less no CMD53 support, so all registers
10 * must be accessed using sdio_readb()/sdio_writeb().
12 * Transfers must be in one transaction or the firmware goes bonkers.
13 * This means that the transfer must either be small enough to do a
14 * byte based transfer or it must be padded to a multiple of the
17 * As SDIO is still new to the kernel, it is unfortunately common with
18 * bugs in the host controllers related to that. One such bug is that
19 * controllers cannot do transfers that aren't a multiple of 4 bytes.
20 * If you don't have time to fix the host controller driver, you can
21 * work around the problem by modifying if_sdio_host_to_card() and
22 * if_sdio_card_to_host() to pad the data.
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/firmware.h>
31 #include <linux/netdevice.h>
32 #include <linux/delay.h>
33 #include <linux/mmc/card.h>
34 #include <linux/mmc/sdio_func.h>
35 #include <linux/mmc/sdio_ids.h>
36 #include <linux/mmc/sdio.h>
37 #include <linux/mmc/host.h>
38 #include <linux/pm_runtime.h>
47 static void if_sdio_interrupt(struct sdio_func
*func
);
49 /* The if_sdio_remove() callback function is called when
50 * user removes this module from kernel space or ejects
51 * the card from the slot. The driver handles these 2 cases
52 * differently for SD8688 combo chip.
53 * If the user is removing the module, the FUNC_SHUTDOWN
54 * command for SD8688 is sent to the firmware.
55 * If the card is removed, there is no need to send this command.
57 * The variable 'user_rmmod' is used to distinguish these two
58 * scenarios. This flag is initialized as FALSE in case the card
59 * is removed, and will be set to TRUE for module removal when
60 * module_exit function is called.
64 static const struct sdio_device_id if_sdio_ids
[] = {
65 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
,
66 SDIO_DEVICE_ID_MARVELL_LIBERTAS
) },
67 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
,
68 SDIO_DEVICE_ID_MARVELL_8688_WLAN
) },
69 { /* end: all zeroes */ },
72 MODULE_DEVICE_TABLE(sdio
, if_sdio_ids
);
74 #define MODEL_8385 0x04
75 #define MODEL_8686 0x0b
76 #define MODEL_8688 0x10
78 static const struct lbs_fw_table fw_table
[] = {
79 { MODEL_8385
, "libertas/sd8385_helper.bin", "libertas/sd8385.bin" },
80 { MODEL_8385
, "sd8385_helper.bin", "sd8385.bin" },
81 { MODEL_8686
, "libertas/sd8686_v9_helper.bin", "libertas/sd8686_v9.bin" },
82 { MODEL_8686
, "libertas/sd8686_v8_helper.bin", "libertas/sd8686_v8.bin" },
83 { MODEL_8686
, "sd8686_helper.bin", "sd8686.bin" },
84 { MODEL_8688
, "libertas/sd8688_helper.bin", "libertas/sd8688.bin" },
85 { MODEL_8688
, "sd8688_helper.bin", "sd8688.bin" },
88 MODULE_FIRMWARE("libertas/sd8385_helper.bin");
89 MODULE_FIRMWARE("libertas/sd8385.bin");
90 MODULE_FIRMWARE("sd8385_helper.bin");
91 MODULE_FIRMWARE("sd8385.bin");
92 MODULE_FIRMWARE("libertas/sd8686_v9_helper.bin");
93 MODULE_FIRMWARE("libertas/sd8686_v9.bin");
94 MODULE_FIRMWARE("libertas/sd8686_v8_helper.bin");
95 MODULE_FIRMWARE("libertas/sd8686_v8.bin");
96 MODULE_FIRMWARE("sd8686_helper.bin");
97 MODULE_FIRMWARE("sd8686.bin");
98 MODULE_FIRMWARE("libertas/sd8688_helper.bin");
99 MODULE_FIRMWARE("libertas/sd8688.bin");
100 MODULE_FIRMWARE("sd8688_helper.bin");
101 MODULE_FIRMWARE("sd8688.bin");
103 struct if_sdio_packet
{
104 struct list_head list
;
106 u8 buffer
[] __aligned(4);
109 struct if_sdio_card
{
110 struct sdio_func
*func
;
111 struct lbs_private
*priv
;
114 unsigned long ioport
;
115 unsigned int scratch_reg
;
117 wait_queue_head_t pwron_waitq
;
119 u8 buffer
[65536] __attribute__((aligned(4)));
122 struct list_head packets
;
124 struct workqueue_struct
*workqueue
;
125 struct work_struct packet_worker
;
126 struct work_struct reset_worker
;
131 static void if_sdio_finish_power_on(struct if_sdio_card
*card
);
132 static int if_sdio_power_off(struct if_sdio_card
*card
);
134 /********************************************************************/
136 /********************************************************************/
139 * For SD8385/SD8686, this function reads firmware status after
140 * the image is downloaded, or reads RX packet length when
141 * interrupt (with IF_SDIO_H_INT_UPLD bit set) is received.
142 * For SD8688, this function reads firmware status only.
144 static u16
if_sdio_read_scratch(struct if_sdio_card
*card
, int *err
)
149 scratch
= sdio_readb(card
->func
, card
->scratch_reg
, &ret
);
151 scratch
|= sdio_readb(card
->func
, card
->scratch_reg
+ 1,
163 static u8
if_sdio_read_rx_unit(struct if_sdio_card
*card
)
168 rx_unit
= sdio_readb(card
->func
, IF_SDIO_RX_UNIT
, &ret
);
176 static u16
if_sdio_read_rx_len(struct if_sdio_card
*card
, int *err
)
181 switch (card
->model
) {
184 rx_len
= if_sdio_read_scratch(card
, &ret
);
187 default: /* for newer chipsets */
188 rx_len
= sdio_readb(card
->func
, IF_SDIO_RX_LEN
, &ret
);
190 rx_len
<<= card
->rx_unit
;
192 rx_len
= 0xffff; /* invalid length */
203 static int if_sdio_handle_cmd(struct if_sdio_card
*card
,
204 u8
*buffer
, unsigned size
)
206 struct lbs_private
*priv
= card
->priv
;
211 if (size
> LBS_CMD_BUFFER_SIZE
) {
212 lbs_deb_sdio("response packet too large (%d bytes)\n",
218 spin_lock_irqsave(&priv
->driver_lock
, flags
);
220 i
= (priv
->resp_idx
== 0) ? 1 : 0;
221 BUG_ON(priv
->resp_len
[i
]);
222 priv
->resp_len
[i
] = size
;
223 memcpy(priv
->resp_buf
[i
], buffer
, size
);
224 lbs_notify_command_response(priv
, i
);
226 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
234 static int if_sdio_handle_data(struct if_sdio_card
*card
,
235 u8
*buffer
, unsigned size
)
240 if (size
> MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
) {
241 lbs_deb_sdio("response packet too large (%d bytes)\n",
247 skb
= dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
+ NET_IP_ALIGN
);
253 skb_reserve(skb
, NET_IP_ALIGN
);
255 skb_put_data(skb
, buffer
, size
);
257 lbs_process_rxed_packet(card
->priv
, skb
);
265 static int if_sdio_handle_event(struct if_sdio_card
*card
,
266 u8
*buffer
, unsigned size
)
271 if (card
->model
== MODEL_8385
) {
272 event
= sdio_readb(card
->func
, IF_SDIO_EVENT
, &ret
);
276 /* right shift 3 bits to get the event id */
280 lbs_deb_sdio("event packet too small (%d bytes)\n",
285 event
= buffer
[3] << 24;
286 event
|= buffer
[2] << 16;
287 event
|= buffer
[1] << 8;
288 event
|= buffer
[0] << 0;
291 lbs_queue_event(card
->priv
, event
& 0xFF);
298 static int if_sdio_wait_status(struct if_sdio_card
*card
, const u8 condition
)
301 unsigned long timeout
;
304 timeout
= jiffies
+ HZ
;
306 status
= sdio_readb(card
->func
, IF_SDIO_STATUS
, &ret
);
309 if ((status
& condition
) == condition
)
311 if (time_after(jiffies
, timeout
))
318 static int if_sdio_card_to_host(struct if_sdio_card
*card
)
321 u16 size
, type
, chunk
;
323 size
= if_sdio_read_rx_len(card
, &ret
);
328 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
334 ret
= if_sdio_wait_status(card
, IF_SDIO_IO_RDY
);
339 * The transfer must be in one transaction or the firmware
340 * goes suicidal. There's no way to guarantee that for all
341 * controllers, but we can at least try.
343 chunk
= sdio_align_size(card
->func
, size
);
345 ret
= sdio_readsb(card
->func
, card
->buffer
, card
->ioport
, chunk
);
349 chunk
= card
->buffer
[0] | (card
->buffer
[1] << 8);
350 type
= card
->buffer
[2] | (card
->buffer
[3] << 8);
352 lbs_deb_sdio("packet of type %d and size %d bytes\n",
353 (int)type
, (int)chunk
);
356 lbs_deb_sdio("packet fragment (%d > %d)\n",
357 (int)chunk
, (int)size
);
363 lbs_deb_sdio("packet fragment (%d < %d)\n",
364 (int)chunk
, (int)size
);
369 ret
= if_sdio_handle_cmd(card
, card
->buffer
+ 4, chunk
- 4);
374 ret
= if_sdio_handle_data(card
, card
->buffer
+ 4, chunk
- 4);
379 ret
= if_sdio_handle_event(card
, card
->buffer
+ 4, chunk
- 4);
384 lbs_deb_sdio("invalid type (%d) from firmware\n",
392 pr_err("problem fetching packet from firmware\n");
397 static void if_sdio_host_to_card_worker(struct work_struct
*work
)
399 struct if_sdio_card
*card
;
400 struct if_sdio_packet
*packet
;
404 card
= container_of(work
, struct if_sdio_card
, packet_worker
);
407 spin_lock_irqsave(&card
->lock
, flags
);
408 packet
= list_first_entry_or_null(&card
->packets
,
409 struct if_sdio_packet
, list
);
411 list_del(&packet
->list
);
412 spin_unlock_irqrestore(&card
->lock
, flags
);
417 sdio_claim_host(card
->func
);
419 ret
= if_sdio_wait_status(card
, IF_SDIO_IO_RDY
);
421 ret
= sdio_writesb(card
->func
, card
->ioport
,
422 packet
->buffer
, packet
->nb
);
426 pr_err("error %d sending packet to firmware\n", ret
);
428 sdio_release_host(card
->func
);
434 /********************************************************************/
436 /********************************************************************/
438 #define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY)
440 static int if_sdio_prog_helper(struct if_sdio_card
*card
,
441 const struct firmware
*fw
)
444 unsigned long timeout
;
450 chunk_buffer
= kzalloc(64, GFP_KERNEL
);
456 sdio_claim_host(card
->func
);
458 ret
= sdio_set_block_size(card
->func
, 32);
466 ret
= if_sdio_wait_status(card
, FW_DL_READY_STATUS
);
470 /* On some platforms (like Davinci) the chip needs more time
471 * between helper blocks.
475 chunk_size
= min_t(size_t, size
, 60);
477 *((__le32
*)chunk_buffer
) = cpu_to_le32(chunk_size
);
478 memcpy(chunk_buffer
+ 4, firmware
, chunk_size
);
480 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
482 ret
= sdio_writesb(card
->func
, card
->ioport
,
487 firmware
+= chunk_size
;
491 /* an empty block marks the end of the transfer */
492 memset(chunk_buffer
, 0, 4);
493 ret
= sdio_writesb(card
->func
, card
->ioport
, chunk_buffer
, 64);
497 lbs_deb_sdio("waiting for helper to boot...\n");
499 /* wait for the helper to boot by looking at the size register */
500 timeout
= jiffies
+ HZ
;
504 req_size
= sdio_readb(card
->func
, IF_SDIO_RD_BASE
, &ret
);
508 req_size
|= sdio_readb(card
->func
, IF_SDIO_RD_BASE
+ 1, &ret
) << 8;
515 if (time_after(jiffies
, timeout
)) {
526 sdio_release_host(card
->func
);
531 pr_err("failed to load helper firmware\n");
536 static int if_sdio_prog_real(struct if_sdio_card
*card
,
537 const struct firmware
*fw
)
540 unsigned long timeout
;
544 size_t size
, req_size
;
546 chunk_buffer
= kzalloc(512, GFP_KERNEL
);
552 sdio_claim_host(card
->func
);
554 ret
= sdio_set_block_size(card
->func
, 32);
562 timeout
= jiffies
+ HZ
;
564 ret
= if_sdio_wait_status(card
, FW_DL_READY_STATUS
);
568 req_size
= sdio_readb(card
->func
, IF_SDIO_RD_BASE
,
573 req_size
|= sdio_readb(card
->func
, IF_SDIO_RD_BASE
+ 1,
579 * For SD8688 wait until the length is not 0, 1 or 2
580 * before downloading the first FW block,
581 * since BOOT code writes the register to indicate the
582 * helper/FW download winner,
583 * the value could be 1 or 2 (Func1 or Func2).
585 if ((size
!= fw
->size
) || (req_size
> 2))
587 if (time_after(jiffies
, timeout
)) {
595 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
598 lbs_deb_sdio("firmware helper gave up early\n");
603 if (req_size
& 0x01) {
604 lbs_deb_sdio("firmware helper signalled error\n");
613 chunk_size
= min_t(size_t, req_size
, 512);
615 memcpy(chunk_buffer
, firmware
, chunk_size
);
617 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
618 chunk_size, (chunk_size + 31) / 32 * 32);
620 ret
= sdio_writesb(card
->func
, card
->ioport
,
621 chunk_buffer
, roundup(chunk_size
, 32));
625 firmware
+= chunk_size
;
627 req_size
-= chunk_size
;
633 lbs_deb_sdio("waiting for firmware to boot...\n");
635 /* wait for the firmware to boot */
636 timeout
= jiffies
+ HZ
;
640 scratch
= if_sdio_read_scratch(card
, &ret
);
644 if (scratch
== IF_SDIO_FIRMWARE_OK
)
647 if (time_after(jiffies
, timeout
)) {
658 sdio_release_host(card
->func
);
663 pr_err("failed to load firmware\n");
668 static void if_sdio_do_prog_firmware(struct lbs_private
*priv
, int ret
,
669 const struct firmware
*helper
,
670 const struct firmware
*mainfw
)
672 struct if_sdio_card
*card
= priv
->card
;
675 pr_err("failed to find firmware (%d)\n", ret
);
679 ret
= if_sdio_prog_helper(card
, helper
);
683 lbs_deb_sdio("Helper firmware loaded\n");
685 ret
= if_sdio_prog_real(card
, mainfw
);
689 lbs_deb_sdio("Firmware loaded\n");
690 if_sdio_finish_power_on(card
);
693 static int if_sdio_prog_firmware(struct if_sdio_card
*card
)
701 sdio_claim_host(card
->func
);
702 sdio_writeb(card
->func
, 0x00, IF_SDIO_H_INT_MASK
, &ret
);
703 sdio_release_host(card
->func
);
705 sdio_claim_host(card
->func
);
706 scratch
= if_sdio_read_scratch(card
, &ret
);
707 sdio_release_host(card
->func
);
709 lbs_deb_sdio("firmware status = %#x\n", scratch
);
710 lbs_deb_sdio("scratch ret = %d\n", ret
);
717 * The manual clearly describes that FEDC is the right code to use
718 * to detect firmware presence, but for SD8686 it is not that simple.
719 * Scratch is also used to store the RX packet length, so we lose
720 * the FEDC value early on. So we use a non-zero check in order
721 * to validate firmware presence.
722 * Additionally, the SD8686 in the Gumstix always has the high scratch
723 * bit set, even when the firmware is not loaded. So we have to
724 * exclude that from the test.
726 if (scratch
== IF_SDIO_FIRMWARE_OK
) {
727 lbs_deb_sdio("firmware already loaded\n");
728 if_sdio_finish_power_on(card
);
730 } else if ((card
->model
== MODEL_8686
) && (scratch
& 0x7fff)) {
731 lbs_deb_sdio("firmware may be running\n");
732 if_sdio_finish_power_on(card
);
736 ret
= lbs_get_firmware_async(card
->priv
, &card
->func
->dev
, card
->model
,
737 fw_table
, if_sdio_do_prog_firmware
);
743 /********************************************************************/
744 /* Power management */
745 /********************************************************************/
747 /* Finish power on sequence (after firmware is loaded) */
748 static void if_sdio_finish_power_on(struct if_sdio_card
*card
)
750 struct sdio_func
*func
= card
->func
;
751 struct lbs_private
*priv
= card
->priv
;
754 sdio_claim_host(func
);
755 sdio_set_block_size(card
->func
, IF_SDIO_BLOCK_SIZE
);
758 * Get rx_unit if the chip is SD8688 or newer.
759 * SD8385 & SD8686 do not have rx_unit.
761 if ((card
->model
!= MODEL_8385
)
762 && (card
->model
!= MODEL_8686
))
763 card
->rx_unit
= if_sdio_read_rx_unit(card
);
768 * Set up the interrupt handler late.
770 * If we set it up earlier, the (buggy) hardware generates a spurious
771 * interrupt, even before the interrupt has been enabled, with
774 * We register the interrupt handler late so that we can handle any
775 * spurious interrupts, and also to avoid generation of that known
776 * spurious interrupt in the first place.
778 ret
= sdio_claim_irq(func
, if_sdio_interrupt
);
783 * Enable interrupts now that everything is set up
785 sdio_writeb(func
, 0x0f, IF_SDIO_H_INT_MASK
, &ret
);
789 sdio_release_host(func
);
791 /* Set fw_ready before queuing any commands so that
792 * lbs_thread won't block from sending them to firmware.
797 * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
799 if (card
->model
== MODEL_8688
) {
800 struct cmd_header cmd
;
802 memset(&cmd
, 0, sizeof(cmd
));
804 lbs_deb_sdio("send function INIT command\n");
805 if (__lbs_cmd(priv
, CMD_FUNC_INIT
, &cmd
, sizeof(cmd
),
806 lbs_cmd_copyback
, (unsigned long) &cmd
))
807 netdev_alert(priv
->dev
, "CMD_FUNC_INIT cmd failed\n");
810 wake_up(&card
->pwron_waitq
);
812 if (!card
->started
) {
813 ret
= lbs_start_card(priv
);
814 if_sdio_power_off(card
);
816 card
->started
= true;
817 /* Tell PM core that we don't need the card to be
819 pm_runtime_put(&func
->dev
);
826 sdio_release_irq(func
);
828 sdio_release_host(func
);
831 static int if_sdio_power_on(struct if_sdio_card
*card
)
833 struct sdio_func
*func
= card
->func
;
834 struct mmc_host
*host
= func
->card
->host
;
837 sdio_claim_host(func
);
839 ret
= sdio_enable_func(func
);
843 /* For 1-bit transfers to the 8686 model, we need to enable the
844 * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0
845 * bit to allow access to non-vendor registers. */
846 if ((card
->model
== MODEL_8686
) &&
847 (host
->caps
& MMC_CAP_SDIO_IRQ
) &&
848 (host
->ios
.bus_width
== MMC_BUS_WIDTH_1
)) {
851 func
->card
->quirks
|= MMC_QUIRK_LENIENT_FN0
;
852 reg
= sdio_f0_readb(func
, SDIO_CCCR_IF
, &ret
);
856 reg
|= SDIO_BUS_ECSI
;
857 sdio_f0_writeb(func
, reg
, SDIO_CCCR_IF
, &ret
);
862 card
->ioport
= sdio_readb(func
, IF_SDIO_IOPORT
, &ret
);
866 card
->ioport
|= sdio_readb(func
, IF_SDIO_IOPORT
+ 1, &ret
) << 8;
870 card
->ioport
|= sdio_readb(func
, IF_SDIO_IOPORT
+ 2, &ret
) << 16;
874 sdio_release_host(func
);
875 ret
= if_sdio_prog_firmware(card
);
877 sdio_claim_host(func
);
884 sdio_disable_func(func
);
886 sdio_release_host(func
);
890 static int if_sdio_power_off(struct if_sdio_card
*card
)
892 struct sdio_func
*func
= card
->func
;
893 struct lbs_private
*priv
= card
->priv
;
897 sdio_claim_host(func
);
898 sdio_release_irq(func
);
899 sdio_disable_func(func
);
900 sdio_release_host(func
);
905 /*******************************************************************/
906 /* Libertas callbacks */
907 /*******************************************************************/
909 static int if_sdio_host_to_card(struct lbs_private
*priv
,
910 u8 type
, u8
*buf
, u16 nb
)
913 struct if_sdio_card
*card
;
914 struct if_sdio_packet
*packet
;
920 if (nb
> (65536 - sizeof(struct if_sdio_packet
) - 4)) {
926 * The transfer must be in one transaction or the firmware
927 * goes suicidal. There's no way to guarantee that for all
928 * controllers, but we can at least try.
930 size
= sdio_align_size(card
->func
, nb
+ 4);
932 packet
= kzalloc(sizeof(struct if_sdio_packet
) + size
,
942 * SDIO specific header.
944 packet
->buffer
[0] = (nb
+ 4) & 0xff;
945 packet
->buffer
[1] = ((nb
+ 4) >> 8) & 0xff;
946 packet
->buffer
[2] = type
;
947 packet
->buffer
[3] = 0;
949 memcpy(packet
->buffer
+ 4, buf
, nb
);
951 spin_lock_irqsave(&card
->lock
, flags
);
953 list_add_tail(&packet
->list
, &card
->packets
);
957 priv
->dnld_sent
= DNLD_CMD_SENT
;
960 priv
->dnld_sent
= DNLD_DATA_SENT
;
963 lbs_deb_sdio("unknown packet type %d\n", (int)type
);
966 spin_unlock_irqrestore(&card
->lock
, flags
);
968 queue_work(card
->workqueue
, &card
->packet_worker
);
976 static int if_sdio_enter_deep_sleep(struct lbs_private
*priv
)
979 struct cmd_header cmd
;
981 memset(&cmd
, 0, sizeof(cmd
));
983 lbs_deb_sdio("send DEEP_SLEEP command\n");
984 ret
= __lbs_cmd(priv
, CMD_802_11_DEEP_SLEEP
, &cmd
, sizeof(cmd
),
985 lbs_cmd_copyback
, (unsigned long) &cmd
);
987 netdev_err(priv
->dev
, "DEEP_SLEEP cmd failed\n");
993 static int if_sdio_exit_deep_sleep(struct lbs_private
*priv
)
995 struct if_sdio_card
*card
= priv
->card
;
998 sdio_claim_host(card
->func
);
1000 sdio_writeb(card
->func
, HOST_POWER_UP
, CONFIGURATION_REG
, &ret
);
1002 netdev_err(priv
->dev
, "sdio_writeb failed!\n");
1004 sdio_release_host(card
->func
);
1009 static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private
*priv
)
1011 struct if_sdio_card
*card
= priv
->card
;
1014 sdio_claim_host(card
->func
);
1016 sdio_writeb(card
->func
, 0, CONFIGURATION_REG
, &ret
);
1018 netdev_err(priv
->dev
, "sdio_writeb failed!\n");
1020 sdio_release_host(card
->func
);
1026 static void if_sdio_reset_card_worker(struct work_struct
*work
)
1031 struct if_sdio_card
*card
;
1032 struct mmc_host
*reset_host
;
1034 card
= container_of(work
, struct if_sdio_card
, reset_worker
);
1035 reset_host
= card
->func
->card
->host
;
1036 name
= card
->priv
->dev
->name
;
1037 dev
= &card
->func
->dev
;
1040 * The actual reset operation must be run outside of lbs_thread. This
1041 * is because mmc_remove_host() will cause the device to be instantly
1042 * destroyed, and the libertas driver then needs to end lbs_thread,
1043 * leading to a deadlock.
1045 * We run it in a workqueue totally independent from the if_sdio_card
1046 * instance for that reason.
1049 dev_info(dev
, "resetting card %s...", name
);
1050 mmc_remove_host(reset_host
);
1051 ret
= mmc_add_host(reset_host
);
1053 dev_err(dev
, "%s: can't add mmc host, error %d\n", name
, ret
);
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_worker
))
1061 schedule_work(&card
->reset_worker
);
1064 static int if_sdio_power_save(struct lbs_private
*priv
)
1066 struct if_sdio_card
*card
= priv
->card
;
1069 flush_workqueue(card
->workqueue
);
1071 ret
= if_sdio_power_off(card
);
1073 /* Let runtime PM know the card is powered off */
1074 pm_runtime_put_sync(&card
->func
->dev
);
1079 static int if_sdio_power_restore(struct lbs_private
*priv
)
1081 struct if_sdio_card
*card
= priv
->card
;
1084 /* Make sure the card will not be powered off by runtime PM */
1085 pm_runtime_get_sync(&card
->func
->dev
);
1087 r
= if_sdio_power_on(card
);
1091 wait_event(card
->pwron_waitq
, priv
->fw_ready
);
1096 /*******************************************************************/
1097 /* SDIO callbacks */
1098 /*******************************************************************/
1100 static void if_sdio_interrupt(struct sdio_func
*func
)
1103 struct if_sdio_card
*card
;
1106 card
= sdio_get_drvdata(func
);
1108 cause
= sdio_readb(card
->func
, IF_SDIO_H_INT_STATUS
, &ret
);
1112 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause
);
1114 sdio_writeb(card
->func
, ~cause
, IF_SDIO_H_INT_STATUS
, &ret
);
1119 * Ignore the define name, this really means the card has
1120 * successfully received the command.
1122 card
->priv
->is_activity_detected
= 1;
1123 if (cause
& IF_SDIO_H_INT_DNLD
)
1124 lbs_host_to_card_done(card
->priv
);
1127 if (cause
& IF_SDIO_H_INT_UPLD
) {
1128 ret
= if_sdio_card_to_host(card
);
1134 static int if_sdio_probe(struct sdio_func
*func
,
1135 const struct sdio_device_id
*id
)
1137 struct if_sdio_card
*card
;
1138 struct lbs_private
*priv
;
1141 struct if_sdio_packet
*packet
, *tmp
;
1143 for (i
= 0;i
< func
->card
->num_info
;i
++) {
1144 if (sscanf(func
->card
->info
[i
],
1145 "802.11 SDIO ID: %x", &model
) == 1)
1147 if (sscanf(func
->card
->info
[i
],
1148 "ID: %x", &model
) == 1)
1150 if (!strcmp(func
->card
->info
[i
], "IBIS Wireless SDIO Card")) {
1156 if (i
== func
->card
->num_info
) {
1157 pr_err("unable to identify card model\n");
1161 card
= kzalloc(sizeof(struct if_sdio_card
), GFP_KERNEL
);
1166 card
->model
= model
;
1168 switch (card
->model
) {
1170 card
->scratch_reg
= IF_SDIO_SCRATCH_OLD
;
1173 card
->scratch_reg
= IF_SDIO_SCRATCH
;
1176 default: /* for newer chipsets */
1177 card
->scratch_reg
= IF_SDIO_FW_STATUS
;
1181 spin_lock_init(&card
->lock
);
1182 INIT_LIST_HEAD(&card
->packets
);
1184 card
->workqueue
= alloc_workqueue("libertas_sdio", WQ_MEM_RECLAIM
, 0);
1185 if (unlikely(!card
->workqueue
)) {
1190 INIT_WORK(&card
->reset_worker
, if_sdio_reset_card_worker
);
1191 INIT_WORK(&card
->packet_worker
, if_sdio_host_to_card_worker
);
1192 init_waitqueue_head(&card
->pwron_waitq
);
1194 /* Check if we support this card */
1195 for (i
= 0; i
< ARRAY_SIZE(fw_table
); i
++) {
1196 if (card
->model
== fw_table
[i
].model
)
1199 if (i
== ARRAY_SIZE(fw_table
)) {
1200 pr_err("unknown card model 0x%x\n", card
->model
);
1205 sdio_set_drvdata(func
, card
);
1207 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
1208 "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
1209 func
->class, func
->vendor
, func
->device
,
1210 model
, (unsigned)card
->ioport
);
1213 priv
= lbs_add_card(card
, &func
->dev
);
1215 ret
= PTR_ERR(priv
);
1222 priv
->hw_host_to_card
= if_sdio_host_to_card
;
1223 priv
->enter_deep_sleep
= if_sdio_enter_deep_sleep
;
1224 priv
->exit_deep_sleep
= if_sdio_exit_deep_sleep
;
1225 priv
->reset_deep_sleep_wakeup
= if_sdio_reset_deep_sleep_wakeup
;
1226 priv
->reset_card
= if_sdio_reset_card
;
1227 priv
->power_save
= if_sdio_power_save
;
1228 priv
->power_restore
= if_sdio_power_restore
;
1229 priv
->is_polling
= !(func
->card
->host
->caps
& MMC_CAP_SDIO_IRQ
);
1230 ret
= if_sdio_power_on(card
);
1232 goto err_activate_card
;
1238 flush_workqueue(card
->workqueue
);
1239 lbs_remove_card(priv
);
1241 cancel_work_sync(&card
->packet_worker
);
1242 cancel_work_sync(&card
->reset_worker
);
1243 destroy_workqueue(card
->workqueue
);
1245 list_for_each_entry_safe(packet
, tmp
, &card
->packets
, list
)
1253 static void if_sdio_remove(struct sdio_func
*func
)
1255 struct if_sdio_card
*card
;
1256 struct if_sdio_packet
*packet
, *tmp
;
1258 card
= sdio_get_drvdata(func
);
1260 /* Undo decrement done above in if_sdio_probe */
1261 pm_runtime_get_noresume(&func
->dev
);
1263 if (user_rmmod
&& (card
->model
== MODEL_8688
)) {
1265 * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
1266 * multiple functions
1268 struct cmd_header cmd
;
1270 memset(&cmd
, 0, sizeof(cmd
));
1272 lbs_deb_sdio("send function SHUTDOWN command\n");
1273 if (__lbs_cmd(card
->priv
, CMD_FUNC_SHUTDOWN
,
1274 &cmd
, sizeof(cmd
), lbs_cmd_copyback
,
1275 (unsigned long) &cmd
))
1276 pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1280 lbs_deb_sdio("call remove card\n");
1281 lbs_stop_card(card
->priv
);
1282 lbs_remove_card(card
->priv
);
1284 cancel_work_sync(&card
->packet_worker
);
1285 cancel_work_sync(&card
->reset_worker
);
1286 destroy_workqueue(card
->workqueue
);
1288 list_for_each_entry_safe(packet
, tmp
, &card
->packets
, list
)
1294 static int if_sdio_suspend(struct device
*dev
)
1296 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1297 struct if_sdio_card
*card
= sdio_get_drvdata(func
);
1298 struct lbs_private
*priv
= card
->priv
;
1301 mmc_pm_flag_t flags
= sdio_get_host_pm_caps(func
);
1302 priv
->power_up_on_resume
= false;
1304 /* If we're powered off anyway, just let the mmc layer remove the
1306 if (!lbs_iface_active(priv
)) {
1307 if (priv
->fw_ready
) {
1308 priv
->power_up_on_resume
= true;
1309 if_sdio_power_off(card
);
1315 dev_info(dev
, "%s: suspend: PM flags = 0x%x\n",
1316 sdio_func_id(func
), flags
);
1318 /* If we aren't being asked to wake on anything, we should bail out
1319 * and let the SD stack power down the card.
1321 if (priv
->wol_criteria
== EHS_REMOVE_WAKEUP
) {
1322 dev_info(dev
, "Suspend without wake params -- powering down card\n");
1323 if (priv
->fw_ready
) {
1324 ret
= lbs_suspend(priv
);
1328 priv
->power_up_on_resume
= true;
1329 if_sdio_power_off(card
);
1335 if (!(flags
& MMC_PM_KEEP_POWER
)) {
1336 dev_err(dev
, "%s: cannot remain alive while host is suspended\n",
1337 sdio_func_id(func
));
1341 ret
= sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
1345 ret
= lbs_suspend(priv
);
1349 return sdio_set_host_pm_flags(func
, MMC_PM_WAKE_SDIO_IRQ
);
1352 static int if_sdio_resume(struct device
*dev
)
1354 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1355 struct if_sdio_card
*card
= sdio_get_drvdata(func
);
1358 dev_info(dev
, "%s: resume: we're back\n", sdio_func_id(func
));
1360 if (card
->priv
->power_up_on_resume
) {
1361 if_sdio_power_on(card
);
1362 wait_event(card
->pwron_waitq
, card
->priv
->fw_ready
);
1365 ret
= lbs_resume(card
->priv
);
1370 static const struct dev_pm_ops if_sdio_pm_ops
= {
1371 .suspend
= if_sdio_suspend
,
1372 .resume
= if_sdio_resume
,
1375 static struct sdio_driver if_sdio_driver
= {
1376 .name
= "libertas_sdio",
1377 .id_table
= if_sdio_ids
,
1378 .probe
= if_sdio_probe
,
1379 .remove
= if_sdio_remove
,
1381 .pm
= &if_sdio_pm_ops
,
1385 /*******************************************************************/
1386 /* Module functions */
1387 /*******************************************************************/
1389 static int __init
if_sdio_init_module(void)
1393 printk(KERN_INFO
"libertas_sdio: Libertas SDIO driver\n");
1394 printk(KERN_INFO
"libertas_sdio: Copyright Pierre Ossman\n");
1396 ret
= sdio_register_driver(&if_sdio_driver
);
1398 /* Clear the flag in case user removes the card. */
1404 static void __exit
if_sdio_exit_module(void)
1406 /* Set the flag as user is removing this module. */
1409 sdio_unregister_driver(&if_sdio_driver
);
1412 module_init(if_sdio_init_module
);
1413 module_exit(if_sdio_exit_module
);
1415 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1416 MODULE_AUTHOR("Pierre Ossman");
1417 MODULE_LICENSE("GPL");