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/moduleparam.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 char *lbs_helper_name
= NULL
;
69 module_param_named(helper_name
, lbs_helper_name
, charp
, 0644);
71 static char *lbs_fw_name
= NULL
;
72 module_param_named(fw_name
, lbs_fw_name
, charp
, 0644);
74 static const struct sdio_device_id if_sdio_ids
[] = {
75 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
,
76 SDIO_DEVICE_ID_MARVELL_LIBERTAS
) },
77 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL
,
78 SDIO_DEVICE_ID_MARVELL_8688WLAN
) },
79 { /* end: all zeroes */ },
82 MODULE_DEVICE_TABLE(sdio
, if_sdio_ids
);
84 #define MODEL_8385 0x04
85 #define MODEL_8686 0x0b
86 #define MODEL_8688 0x10
88 static const struct lbs_fw_table fw_table
[] = {
89 { MODEL_8385
, "libertas/sd8385_helper.bin", "libertas/sd8385.bin" },
90 { MODEL_8385
, "sd8385_helper.bin", "sd8385.bin" },
91 { MODEL_8686
, "libertas/sd8686_v9_helper.bin", "libertas/sd8686_v9.bin" },
92 { MODEL_8686
, "libertas/sd8686_v8_helper.bin", "libertas/sd8686_v8.bin" },
93 { MODEL_8686
, "sd8686_helper.bin", "sd8686.bin" },
94 { MODEL_8688
, "libertas/sd8688_helper.bin", "libertas/sd8688.bin" },
95 { MODEL_8688
, "sd8688_helper.bin", "sd8688.bin" },
98 MODULE_FIRMWARE("libertas/sd8385_helper.bin");
99 MODULE_FIRMWARE("libertas/sd8385.bin");
100 MODULE_FIRMWARE("sd8385_helper.bin");
101 MODULE_FIRMWARE("sd8385.bin");
102 MODULE_FIRMWARE("libertas/sd8686_v9_helper.bin");
103 MODULE_FIRMWARE("libertas/sd8686_v9.bin");
104 MODULE_FIRMWARE("libertas/sd8686_v8_helper.bin");
105 MODULE_FIRMWARE("libertas/sd8686_v8.bin");
106 MODULE_FIRMWARE("sd8686_helper.bin");
107 MODULE_FIRMWARE("sd8686.bin");
108 MODULE_FIRMWARE("libertas/sd8688_helper.bin");
109 MODULE_FIRMWARE("libertas/sd8688.bin");
110 MODULE_FIRMWARE("sd8688_helper.bin");
111 MODULE_FIRMWARE("sd8688.bin");
113 struct if_sdio_packet
{
114 struct if_sdio_packet
*next
;
116 u8 buffer
[0] __attribute__((aligned(4)));
119 struct if_sdio_card
{
120 struct sdio_func
*func
;
121 struct lbs_private
*priv
;
124 unsigned long ioport
;
125 unsigned int scratch_reg
;
128 const char *firmware
;
129 bool helper_allocated
;
130 bool firmware_allocated
;
132 u8 buffer
[65536] __attribute__((aligned(4)));
135 struct if_sdio_packet
*packets
;
137 struct workqueue_struct
*workqueue
;
138 struct work_struct packet_worker
;
143 /********************************************************************/
145 /********************************************************************/
148 * For SD8385/SD8686, this function reads firmware status after
149 * the image is downloaded, or reads RX packet length when
150 * interrupt (with IF_SDIO_H_INT_UPLD bit set) is received.
151 * For SD8688, this function reads firmware status only.
153 static u16
if_sdio_read_scratch(struct if_sdio_card
*card
, int *err
)
158 scratch
= sdio_readb(card
->func
, card
->scratch_reg
, &ret
);
160 scratch
|= sdio_readb(card
->func
, card
->scratch_reg
+ 1,
172 static u8
if_sdio_read_rx_unit(struct if_sdio_card
*card
)
177 rx_unit
= sdio_readb(card
->func
, IF_SDIO_RX_UNIT
, &ret
);
185 static u16
if_sdio_read_rx_len(struct if_sdio_card
*card
, int *err
)
190 switch (card
->model
) {
193 rx_len
= if_sdio_read_scratch(card
, &ret
);
196 default: /* for newer chipsets */
197 rx_len
= sdio_readb(card
->func
, IF_SDIO_RX_LEN
, &ret
);
199 rx_len
<<= card
->rx_unit
;
201 rx_len
= 0xffff; /* invalid length */
212 static int if_sdio_handle_cmd(struct if_sdio_card
*card
,
213 u8
*buffer
, unsigned size
)
215 struct lbs_private
*priv
= card
->priv
;
220 lbs_deb_enter(LBS_DEB_SDIO
);
222 if (size
> LBS_CMD_BUFFER_SIZE
) {
223 lbs_deb_sdio("response packet too large (%d bytes)\n",
229 spin_lock_irqsave(&priv
->driver_lock
, flags
);
231 i
= (priv
->resp_idx
== 0) ? 1 : 0;
232 BUG_ON(priv
->resp_len
[i
]);
233 priv
->resp_len
[i
] = size
;
234 memcpy(priv
->resp_buf
[i
], buffer
, size
);
235 lbs_notify_command_response(priv
, i
);
237 spin_unlock_irqrestore(&card
->priv
->driver_lock
, flags
);
242 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
246 static int if_sdio_handle_data(struct if_sdio_card
*card
,
247 u8
*buffer
, unsigned size
)
253 lbs_deb_enter(LBS_DEB_SDIO
);
255 if (size
> MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
) {
256 lbs_deb_sdio("response packet too large (%d bytes)\n",
262 skb
= dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE
+ NET_IP_ALIGN
);
268 skb_reserve(skb
, NET_IP_ALIGN
);
270 data
= skb_put(skb
, size
);
272 memcpy(data
, buffer
, size
);
274 lbs_process_rxed_packet(card
->priv
, skb
);
279 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
284 static int if_sdio_handle_event(struct if_sdio_card
*card
,
285 u8
*buffer
, unsigned size
)
290 lbs_deb_enter(LBS_DEB_SDIO
);
292 if (card
->model
== MODEL_8385
) {
293 event
= sdio_readb(card
->func
, IF_SDIO_EVENT
, &ret
);
297 /* right shift 3 bits to get the event id */
301 lbs_deb_sdio("event packet too small (%d bytes)\n",
306 event
= buffer
[3] << 24;
307 event
|= buffer
[2] << 16;
308 event
|= buffer
[1] << 8;
309 event
|= buffer
[0] << 0;
312 lbs_queue_event(card
->priv
, event
& 0xFF);
316 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
321 static int if_sdio_wait_status(struct if_sdio_card
*card
, const u8 condition
)
324 unsigned long timeout
;
327 timeout
= jiffies
+ HZ
;
329 status
= sdio_readb(card
->func
, IF_SDIO_STATUS
, &ret
);
332 if ((status
& condition
) == condition
)
334 if (time_after(jiffies
, timeout
))
341 static int if_sdio_card_to_host(struct if_sdio_card
*card
)
344 u16 size
, type
, chunk
;
346 lbs_deb_enter(LBS_DEB_SDIO
);
348 size
= if_sdio_read_rx_len(card
, &ret
);
353 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
359 ret
= if_sdio_wait_status(card
, IF_SDIO_IO_RDY
);
364 * The transfer must be in one transaction or the firmware
365 * goes suicidal. There's no way to guarantee that for all
366 * controllers, but we can at least try.
368 chunk
= sdio_align_size(card
->func
, size
);
370 ret
= sdio_readsb(card
->func
, card
->buffer
, card
->ioport
, chunk
);
374 chunk
= card
->buffer
[0] | (card
->buffer
[1] << 8);
375 type
= card
->buffer
[2] | (card
->buffer
[3] << 8);
377 lbs_deb_sdio("packet of type %d and size %d bytes\n",
378 (int)type
, (int)chunk
);
381 lbs_deb_sdio("packet fragment (%d > %d)\n",
382 (int)chunk
, (int)size
);
388 lbs_deb_sdio("packet fragment (%d < %d)\n",
389 (int)chunk
, (int)size
);
394 ret
= if_sdio_handle_cmd(card
, card
->buffer
+ 4, chunk
- 4);
399 ret
= if_sdio_handle_data(card
, card
->buffer
+ 4, chunk
- 4);
404 ret
= if_sdio_handle_event(card
, card
->buffer
+ 4, chunk
- 4);
409 lbs_deb_sdio("invalid type (%d) from firmware\n",
417 pr_err("problem fetching packet from firmware\n");
419 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
424 static void if_sdio_host_to_card_worker(struct work_struct
*work
)
426 struct if_sdio_card
*card
;
427 struct if_sdio_packet
*packet
;
431 lbs_deb_enter(LBS_DEB_SDIO
);
433 card
= container_of(work
, struct if_sdio_card
, packet_worker
);
436 spin_lock_irqsave(&card
->lock
, flags
);
437 packet
= card
->packets
;
439 card
->packets
= packet
->next
;
440 spin_unlock_irqrestore(&card
->lock
, flags
);
445 sdio_claim_host(card
->func
);
447 ret
= if_sdio_wait_status(card
, IF_SDIO_IO_RDY
);
449 ret
= sdio_writesb(card
->func
, card
->ioport
,
450 packet
->buffer
, packet
->nb
);
454 pr_err("error %d sending packet to firmware\n", ret
);
456 sdio_release_host(card
->func
);
461 lbs_deb_leave(LBS_DEB_SDIO
);
464 /********************************************************************/
466 /********************************************************************/
468 #define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY)
470 static int if_sdio_prog_helper(struct if_sdio_card
*card
,
471 const struct firmware
*fw
)
474 unsigned long timeout
;
480 lbs_deb_enter(LBS_DEB_SDIO
);
482 chunk_buffer
= kzalloc(64, GFP_KERNEL
);
488 sdio_claim_host(card
->func
);
490 ret
= sdio_set_block_size(card
->func
, 32);
498 ret
= if_sdio_wait_status(card
, FW_DL_READY_STATUS
);
502 /* On some platforms (like Davinci) the chip needs more time
503 * between helper blocks.
507 chunk_size
= min(size
, (size_t)60);
509 *((__le32
*)chunk_buffer
) = cpu_to_le32(chunk_size
);
510 memcpy(chunk_buffer
+ 4, firmware
, chunk_size
);
512 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
514 ret
= sdio_writesb(card
->func
, card
->ioport
,
519 firmware
+= chunk_size
;
523 /* an empty block marks the end of the transfer */
524 memset(chunk_buffer
, 0, 4);
525 ret
= sdio_writesb(card
->func
, card
->ioport
, chunk_buffer
, 64);
529 lbs_deb_sdio("waiting for helper to boot...\n");
531 /* wait for the helper to boot by looking at the size register */
532 timeout
= jiffies
+ HZ
;
536 req_size
= sdio_readb(card
->func
, IF_SDIO_RD_BASE
, &ret
);
540 req_size
|= sdio_readb(card
->func
, IF_SDIO_RD_BASE
+ 1, &ret
) << 8;
547 if (time_after(jiffies
, timeout
)) {
558 sdio_release_host(card
->func
);
563 pr_err("failed to load helper firmware\n");
565 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
569 static int if_sdio_prog_real(struct if_sdio_card
*card
,
570 const struct firmware
*fw
)
573 unsigned long timeout
;
577 size_t size
, req_size
;
579 lbs_deb_enter(LBS_DEB_SDIO
);
581 chunk_buffer
= kzalloc(512, GFP_KERNEL
);
587 sdio_claim_host(card
->func
);
589 ret
= sdio_set_block_size(card
->func
, 32);
597 ret
= if_sdio_wait_status(card
, FW_DL_READY_STATUS
);
601 req_size
= sdio_readb(card
->func
, IF_SDIO_RD_BASE
, &ret
);
605 req_size
|= sdio_readb(card
->func
, IF_SDIO_RD_BASE
+ 1, &ret
) << 8;
609 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
612 lbs_deb_sdio("firmware helper gave up early\n");
617 if (req_size
& 0x01) {
618 lbs_deb_sdio("firmware helper signalled error\n");
627 chunk_size
= min(req_size
, (size_t)512);
629 memcpy(chunk_buffer
, firmware
, chunk_size
);
631 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
632 chunk_size, (chunk_size + 31) / 32 * 32);
634 ret
= sdio_writesb(card
->func
, card
->ioport
,
635 chunk_buffer
, roundup(chunk_size
, 32));
639 firmware
+= chunk_size
;
641 req_size
-= chunk_size
;
647 lbs_deb_sdio("waiting for firmware to boot...\n");
649 /* wait for the firmware to boot */
650 timeout
= jiffies
+ HZ
;
654 scratch
= if_sdio_read_scratch(card
, &ret
);
658 if (scratch
== IF_SDIO_FIRMWARE_OK
)
661 if (time_after(jiffies
, timeout
)) {
672 sdio_release_host(card
->func
);
677 pr_err("failed to load firmware\n");
679 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
683 static int if_sdio_prog_firmware(struct if_sdio_card
*card
)
687 const struct firmware
*helper
= NULL
;
688 const struct firmware
*mainfw
= NULL
;
690 lbs_deb_enter(LBS_DEB_SDIO
);
695 sdio_claim_host(card
->func
);
696 sdio_writeb(card
->func
, 0x00, IF_SDIO_H_INT_MASK
, &ret
);
697 sdio_release_host(card
->func
);
699 sdio_claim_host(card
->func
);
700 scratch
= if_sdio_read_scratch(card
, &ret
);
701 sdio_release_host(card
->func
);
703 lbs_deb_sdio("firmware status = %#x\n", scratch
);
704 lbs_deb_sdio("scratch ret = %d\n", ret
);
711 * The manual clearly describes that FEDC is the right code to use
712 * to detect firmware presence, but for SD8686 it is not that simple.
713 * Scratch is also used to store the RX packet length, so we lose
714 * the FEDC value early on. So we use a non-zero check in order
715 * to validate firmware presence.
716 * Additionally, the SD8686 in the Gumstix always has the high scratch
717 * bit set, even when the firmware is not loaded. So we have to
718 * exclude that from the test.
720 if (scratch
== IF_SDIO_FIRMWARE_OK
) {
721 lbs_deb_sdio("firmware already loaded\n");
723 } else if ((card
->model
== MODEL_8686
) && (scratch
& 0x7fff)) {
724 lbs_deb_sdio("firmware may be running\n");
728 ret
= lbs_get_firmware(&card
->func
->dev
, lbs_helper_name
, lbs_fw_name
,
729 card
->model
, &fw_table
[0], &helper
, &mainfw
);
731 pr_err("failed to find firmware (%d)\n", ret
);
735 ret
= if_sdio_prog_helper(card
, helper
);
739 lbs_deb_sdio("Helper firmware loaded\n");
741 ret
= if_sdio_prog_real(card
, mainfw
);
745 lbs_deb_sdio("Firmware loaded\n");
748 sdio_claim_host(card
->func
);
749 sdio_set_block_size(card
->func
, IF_SDIO_BLOCK_SIZE
);
750 sdio_release_host(card
->func
);
755 release_firmware(helper
);
757 release_firmware(mainfw
);
759 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
763 /********************************************************************/
764 /* Power management */
765 /********************************************************************/
767 static int if_sdio_power_on(struct if_sdio_card
*card
)
769 struct sdio_func
*func
= card
->func
;
770 struct lbs_private
*priv
= card
->priv
;
771 struct mmc_host
*host
= func
->card
->host
;
774 sdio_claim_host(func
);
776 ret
= sdio_enable_func(func
);
780 /* For 1-bit transfers to the 8686 model, we need to enable the
781 * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0
782 * bit to allow access to non-vendor registers. */
783 if ((card
->model
== MODEL_8686
) &&
784 (host
->caps
& MMC_CAP_SDIO_IRQ
) &&
785 (host
->ios
.bus_width
== MMC_BUS_WIDTH_1
)) {
788 func
->card
->quirks
|= MMC_QUIRK_LENIENT_FN0
;
789 reg
= sdio_f0_readb(func
, SDIO_CCCR_IF
, &ret
);
793 reg
|= SDIO_BUS_ECSI
;
794 sdio_f0_writeb(func
, reg
, SDIO_CCCR_IF
, &ret
);
799 card
->ioport
= sdio_readb(func
, IF_SDIO_IOPORT
, &ret
);
803 card
->ioport
|= sdio_readb(func
, IF_SDIO_IOPORT
+ 1, &ret
) << 8;
807 card
->ioport
|= sdio_readb(func
, IF_SDIO_IOPORT
+ 2, &ret
) << 16;
811 sdio_release_host(func
);
812 ret
= if_sdio_prog_firmware(card
);
813 sdio_claim_host(func
);
818 * Get rx_unit if the chip is SD8688 or newer.
819 * SD8385 & SD8686 do not have rx_unit.
821 if ((card
->model
!= MODEL_8385
)
822 && (card
->model
!= MODEL_8686
))
823 card
->rx_unit
= if_sdio_read_rx_unit(card
);
828 * Set up the interrupt handler late.
830 * If we set it up earlier, the (buggy) hardware generates a spurious
831 * interrupt, even before the interrupt has been enabled, with
834 * We register the interrupt handler late so that we can handle any
835 * spurious interrupts, and also to avoid generation of that known
836 * spurious interrupt in the first place.
838 ret
= sdio_claim_irq(func
, if_sdio_interrupt
);
843 * Enable interrupts now that everything is set up
845 sdio_writeb(func
, 0x0f, IF_SDIO_H_INT_MASK
, &ret
);
849 sdio_release_host(func
);
852 * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
854 if (card
->model
== MODEL_8688
) {
855 struct cmd_header cmd
;
857 memset(&cmd
, 0, sizeof(cmd
));
859 lbs_deb_sdio("send function INIT command\n");
860 if (__lbs_cmd(priv
, CMD_FUNC_INIT
, &cmd
, sizeof(cmd
),
861 lbs_cmd_copyback
, (unsigned long) &cmd
))
862 netdev_alert(priv
->dev
, "CMD_FUNC_INIT cmd failed\n");
870 sdio_release_irq(func
);
872 sdio_disable_func(func
);
874 sdio_release_host(func
);
878 static int if_sdio_power_off(struct if_sdio_card
*card
)
880 struct sdio_func
*func
= card
->func
;
881 struct lbs_private
*priv
= card
->priv
;
885 sdio_claim_host(func
);
886 sdio_release_irq(func
);
887 sdio_disable_func(func
);
888 sdio_release_host(func
);
893 /*******************************************************************/
894 /* Libertas callbacks */
895 /*******************************************************************/
897 static int if_sdio_host_to_card(struct lbs_private
*priv
,
898 u8 type
, u8
*buf
, u16 nb
)
901 struct if_sdio_card
*card
;
902 struct if_sdio_packet
*packet
, *cur
;
906 lbs_deb_enter_args(LBS_DEB_SDIO
, "type %d, bytes %d", type
, nb
);
910 if (nb
> (65536 - sizeof(struct if_sdio_packet
) - 4)) {
916 * The transfer must be in one transaction or the firmware
917 * goes suicidal. There's no way to guarantee that for all
918 * controllers, but we can at least try.
920 size
= sdio_align_size(card
->func
, nb
+ 4);
922 packet
= kzalloc(sizeof(struct if_sdio_packet
) + size
,
933 * SDIO specific header.
935 packet
->buffer
[0] = (nb
+ 4) & 0xff;
936 packet
->buffer
[1] = ((nb
+ 4) >> 8) & 0xff;
937 packet
->buffer
[2] = type
;
938 packet
->buffer
[3] = 0;
940 memcpy(packet
->buffer
+ 4, buf
, nb
);
942 spin_lock_irqsave(&card
->lock
, flags
);
945 card
->packets
= packet
;
955 priv
->dnld_sent
= DNLD_CMD_SENT
;
958 priv
->dnld_sent
= DNLD_DATA_SENT
;
961 lbs_deb_sdio("unknown packet type %d\n", (int)type
);
964 spin_unlock_irqrestore(&card
->lock
, flags
);
966 queue_work(card
->workqueue
, &card
->packet_worker
);
971 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
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 lbs_deb_enter(LBS_DEB_SDIO
);
999 sdio_claim_host(card
->func
);
1001 sdio_writeb(card
->func
, HOST_POWER_UP
, CONFIGURATION_REG
, &ret
);
1003 netdev_err(priv
->dev
, "sdio_writeb failed!\n");
1005 sdio_release_host(card
->func
);
1006 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1010 static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private
*priv
)
1012 struct if_sdio_card
*card
= priv
->card
;
1015 lbs_deb_enter(LBS_DEB_SDIO
);
1016 sdio_claim_host(card
->func
);
1018 sdio_writeb(card
->func
, 0, CONFIGURATION_REG
, &ret
);
1020 netdev_err(priv
->dev
, "sdio_writeb failed!\n");
1022 sdio_release_host(card
->func
);
1023 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1028 static struct mmc_host
*reset_host
;
1030 static void if_sdio_reset_card_worker(struct work_struct
*work
)
1033 * The actual reset operation must be run outside of lbs_thread. This
1034 * is because mmc_remove_host() will cause the device to be instantly
1035 * destroyed, and the libertas driver then needs to end lbs_thread,
1036 * leading to a deadlock.
1038 * We run it in a workqueue totally independent from the if_sdio_card
1039 * instance for that reason.
1042 pr_info("Resetting card...");
1043 mmc_remove_host(reset_host
);
1044 mmc_add_host(reset_host
);
1046 static DECLARE_WORK(card_reset_work
, if_sdio_reset_card_worker
);
1048 static void if_sdio_reset_card(struct lbs_private
*priv
)
1050 struct if_sdio_card
*card
= priv
->card
;
1052 if (work_pending(&card_reset_work
))
1055 reset_host
= card
->func
->card
->host
;
1056 schedule_work(&card_reset_work
);
1059 static int if_sdio_power_save(struct lbs_private
*priv
)
1061 struct if_sdio_card
*card
= priv
->card
;
1064 flush_workqueue(card
->workqueue
);
1066 ret
= if_sdio_power_off(card
);
1068 /* Let runtime PM know the card is powered off */
1069 pm_runtime_put_sync(&card
->func
->dev
);
1074 static int if_sdio_power_restore(struct lbs_private
*priv
)
1076 struct if_sdio_card
*card
= priv
->card
;
1078 /* Make sure the card will not be powered off by runtime PM */
1079 pm_runtime_get_sync(&card
->func
->dev
);
1081 return if_sdio_power_on(card
);
1085 /*******************************************************************/
1086 /* SDIO callbacks */
1087 /*******************************************************************/
1089 static void if_sdio_interrupt(struct sdio_func
*func
)
1092 struct if_sdio_card
*card
;
1095 lbs_deb_enter(LBS_DEB_SDIO
);
1097 card
= sdio_get_drvdata(func
);
1099 cause
= sdio_readb(card
->func
, IF_SDIO_H_INT_STATUS
, &ret
);
1103 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause
);
1105 sdio_writeb(card
->func
, ~cause
, IF_SDIO_H_INT_STATUS
, &ret
);
1110 * Ignore the define name, this really means the card has
1111 * successfully received the command.
1113 card
->priv
->is_activity_detected
= 1;
1114 if (cause
& IF_SDIO_H_INT_DNLD
)
1115 lbs_host_to_card_done(card
->priv
);
1118 if (cause
& IF_SDIO_H_INT_UPLD
) {
1119 ret
= if_sdio_card_to_host(card
);
1127 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1130 static int if_sdio_probe(struct sdio_func
*func
,
1131 const struct sdio_device_id
*id
)
1133 struct if_sdio_card
*card
;
1134 struct lbs_private
*priv
;
1137 struct if_sdio_packet
*packet
;
1139 lbs_deb_enter(LBS_DEB_SDIO
);
1141 for (i
= 0;i
< func
->card
->num_info
;i
++) {
1142 if (sscanf(func
->card
->info
[i
],
1143 "802.11 SDIO ID: %x", &model
) == 1)
1145 if (sscanf(func
->card
->info
[i
],
1146 "ID: %x", &model
) == 1)
1148 if (!strcmp(func
->card
->info
[i
], "IBIS Wireless SDIO Card")) {
1154 if (i
== func
->card
->num_info
) {
1155 pr_err("unable to identify card model\n");
1159 card
= kzalloc(sizeof(struct if_sdio_card
), GFP_KERNEL
);
1164 card
->model
= model
;
1166 switch (card
->model
) {
1168 card
->scratch_reg
= IF_SDIO_SCRATCH_OLD
;
1171 card
->scratch_reg
= IF_SDIO_SCRATCH
;
1174 default: /* for newer chipsets */
1175 card
->scratch_reg
= IF_SDIO_FW_STATUS
;
1179 spin_lock_init(&card
->lock
);
1180 card
->workqueue
= create_workqueue("libertas_sdio");
1181 INIT_WORK(&card
->packet_worker
, if_sdio_host_to_card_worker
);
1183 /* Check if we support this card */
1184 for (i
= 0; i
< ARRAY_SIZE(fw_table
); i
++) {
1185 if (card
->model
== fw_table
[i
].model
)
1188 if (i
== ARRAY_SIZE(fw_table
)) {
1189 pr_err("unknown card model 0x%x\n", card
->model
);
1194 sdio_set_drvdata(func
, card
);
1196 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
1197 "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
1198 func
->class, func
->vendor
, func
->device
,
1199 model
, (unsigned)card
->ioport
);
1202 priv
= lbs_add_card(card
, &func
->dev
);
1211 priv
->hw_host_to_card
= if_sdio_host_to_card
;
1212 priv
->enter_deep_sleep
= if_sdio_enter_deep_sleep
;
1213 priv
->exit_deep_sleep
= if_sdio_exit_deep_sleep
;
1214 priv
->reset_deep_sleep_wakeup
= if_sdio_reset_deep_sleep_wakeup
;
1215 priv
->reset_card
= if_sdio_reset_card
;
1216 priv
->power_save
= if_sdio_power_save
;
1217 priv
->power_restore
= if_sdio_power_restore
;
1219 ret
= if_sdio_power_on(card
);
1221 goto err_activate_card
;
1223 ret
= lbs_start_card(priv
);
1224 if_sdio_power_off(card
);
1226 goto err_activate_card
;
1228 /* Tell PM core that we don't need the card to be powered now */
1229 pm_runtime_put_noidle(&func
->dev
);
1232 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1237 flush_workqueue(card
->workqueue
);
1238 lbs_remove_card(priv
);
1240 destroy_workqueue(card
->workqueue
);
1241 while (card
->packets
) {
1242 packet
= card
->packets
;
1243 card
->packets
= card
->packets
->next
;
1247 if (card
->helper_allocated
)
1248 kfree(card
->helper
);
1249 if (card
->firmware_allocated
)
1250 kfree(card
->firmware
);
1256 static void if_sdio_remove(struct sdio_func
*func
)
1258 struct if_sdio_card
*card
;
1259 struct if_sdio_packet
*packet
;
1261 lbs_deb_enter(LBS_DEB_SDIO
);
1263 card
= sdio_get_drvdata(func
);
1265 /* Undo decrement done above in if_sdio_probe */
1266 pm_runtime_get_noresume(&func
->dev
);
1268 if (user_rmmod
&& (card
->model
== MODEL_8688
)) {
1270 * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
1271 * multiple functions
1273 struct cmd_header cmd
;
1275 memset(&cmd
, 0, sizeof(cmd
));
1277 lbs_deb_sdio("send function SHUTDOWN command\n");
1278 if (__lbs_cmd(card
->priv
, CMD_FUNC_SHUTDOWN
,
1279 &cmd
, sizeof(cmd
), lbs_cmd_copyback
,
1280 (unsigned long) &cmd
))
1281 pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1285 lbs_deb_sdio("call remove card\n");
1286 lbs_stop_card(card
->priv
);
1287 lbs_remove_card(card
->priv
);
1289 flush_workqueue(card
->workqueue
);
1290 destroy_workqueue(card
->workqueue
);
1292 while (card
->packets
) {
1293 packet
= card
->packets
;
1294 card
->packets
= card
->packets
->next
;
1298 if (card
->helper_allocated
)
1299 kfree(card
->helper
);
1300 if (card
->firmware_allocated
)
1301 kfree(card
->firmware
);
1304 lbs_deb_leave(LBS_DEB_SDIO
);
1307 static int if_sdio_suspend(struct device
*dev
)
1309 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1311 struct if_sdio_card
*card
= sdio_get_drvdata(func
);
1313 mmc_pm_flag_t flags
= sdio_get_host_pm_caps(func
);
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 (card
->priv
->wol_criteria
== EHS_REMOVE_WAKEUP
) {
1322 dev_info(dev
, "Suspend without wake params -- powering down card\n");
1326 if (!(flags
& MMC_PM_KEEP_POWER
)) {
1327 dev_err(dev
, "%s: cannot remain alive while host is suspended\n",
1328 sdio_func_id(func
));
1332 ret
= sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
1336 ret
= lbs_suspend(card
->priv
);
1340 return sdio_set_host_pm_flags(func
, MMC_PM_WAKE_SDIO_IRQ
);
1343 static int if_sdio_resume(struct device
*dev
)
1345 struct sdio_func
*func
= dev_to_sdio_func(dev
);
1346 struct if_sdio_card
*card
= sdio_get_drvdata(func
);
1349 dev_info(dev
, "%s: resume: we're back\n", sdio_func_id(func
));
1351 ret
= lbs_resume(card
->priv
);
1356 static const struct dev_pm_ops if_sdio_pm_ops
= {
1357 .suspend
= if_sdio_suspend
,
1358 .resume
= if_sdio_resume
,
1361 static struct sdio_driver if_sdio_driver
= {
1362 .name
= "libertas_sdio",
1363 .id_table
= if_sdio_ids
,
1364 .probe
= if_sdio_probe
,
1365 .remove
= if_sdio_remove
,
1367 .pm
= &if_sdio_pm_ops
,
1371 /*******************************************************************/
1372 /* Module functions */
1373 /*******************************************************************/
1375 static int __init
if_sdio_init_module(void)
1379 lbs_deb_enter(LBS_DEB_SDIO
);
1381 printk(KERN_INFO
"libertas_sdio: Libertas SDIO driver\n");
1382 printk(KERN_INFO
"libertas_sdio: Copyright Pierre Ossman\n");
1384 ret
= sdio_register_driver(&if_sdio_driver
);
1386 /* Clear the flag in case user removes the card. */
1389 lbs_deb_leave_args(LBS_DEB_SDIO
, "ret %d", ret
);
1394 static void __exit
if_sdio_exit_module(void)
1396 lbs_deb_enter(LBS_DEB_SDIO
);
1398 /* Set the flag as user is removing this module. */
1401 cancel_work_sync(&card_reset_work
);
1403 sdio_unregister_driver(&if_sdio_driver
);
1405 lbs_deb_leave(LBS_DEB_SDIO
);
1408 module_init(if_sdio_init_module
);
1409 module_exit(if_sdio_exit_module
);
1411 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1412 MODULE_AUTHOR("Pierre Ossman");
1413 MODULE_LICENSE("GPL");