2 * ---------------------------------------------------------------------------
6 * PURPOSE: SDIO driver interface for generic MMC stack.
8 * Copyright (C) 2008-2009 by Cambridge Silicon Radio Ltd.
10 * ---------------------------------------------------------------------------
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/mutex.h>
16 #include <linux/gfp.h>
17 #include <linux/mmc/core.h>
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/host.h>
20 #include <linux/mmc/sdio_func.h>
21 #include <linux/mmc/sdio_ids.h>
22 #include <linux/mmc/sdio.h>
23 #include <linux/suspend.h>
25 #include "unifi_priv.h"
28 struct wake_lock unifi_sdio_wake_lock
; /* wakelock to prevent suspend while resuming */
31 static CsrSdioFunctionDriver
*sdio_func_drv
;
34 static int uf_sdio_mmc_power_event(struct notifier_block
*this, unsigned long event
, void *ptr
);
38 * We need to keep track of the power on/off because we can not call
39 * mmc_power_restore_host() when the card is already powered.
40 * Even then, we need to patch the MMC driver to add a power_restore handler
41 * in the mmc_sdio_ops structure. If the MMC driver before 2.6.37 is not patched,
42 * mmc_power_save_host() and mmc_power_restore_host() are no-ops in the kernel,
43 * returning immediately (at least on x86).
45 static int card_is_powered
= 1;
47 /* MMC uses ENOMEDIUM to indicate card gone away */
50 ConvertSdioToCsrSdioResult(int r
)
52 CsrResult csrResult
= CSR_RESULT_FAILURE
;
56 csrResult
= CSR_RESULT_SUCCESS
;
60 csrResult
= CSR_SDIO_RESULT_CRC_ERROR
;
65 csrResult
= CSR_SDIO_RESULT_TIMEOUT
;
69 csrResult
= CSR_SDIO_RESULT_NO_DEVICE
;
72 csrResult
= CSR_SDIO_RESULT_INVALID_VALUE
;
78 csrResult
= CSR_RESULT_FAILURE
;
81 unifi_warning(NULL
, "Unrecognised SDIO error code: %d\n", r
);
90 csr_io_rw_direct(struct mmc_card
*card
, int write
, uint8_t fn
,
91 uint32_t addr
, uint8_t in
, uint8_t* out
)
93 struct mmc_command cmd
;
99 memset(&cmd
, 0, sizeof(struct mmc_command
));
101 cmd
.opcode
= SD_IO_RW_DIRECT
;
102 cmd
.arg
= write
? 0x80000000 : 0x00000000;
104 cmd
.arg
|= (write
&& out
) ? 0x08000000 : 0x00000000;
105 cmd
.arg
|= addr
<< 9;
107 cmd
.flags
= MMC_RSP_SPI_R5
| MMC_RSP_R5
| MMC_CMD_AC
;
109 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
113 /* this function is not exported, so we will need to sort it out here
114 * for now, lets hard code it to sdio */
116 /* old arg (mmc_host_is_spi(card->host)) { */
117 /* host driver already reported errors */
119 if (cmd
.resp
[0] & R5_ERROR
) {
120 printk(KERN_ERR
"%s: r5 error 0x%02x\n",
121 __FUNCTION__
, cmd
.resp
[0]);
124 if (cmd
.resp
[0] & R5_FUNCTION_NUMBER
)
126 if (cmd
.resp
[0] & R5_OUT_OF_RANGE
)
131 if (0) { /* old argument (mmc_host_is_spi(card->host)) */
132 *out
= (cmd
.resp
[0] >> 8) & 0xFF;
135 *out
= cmd
.resp
[0] & 0xFF;
139 return CSR_RESULT_SUCCESS
;
144 CsrSdioRead8(CsrSdioFunction
*function
, u32 address
, u8
*data
)
146 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
149 _sdio_claim_host(func
);
150 *data
= sdio_readb(func
, address
, &err
);
151 _sdio_release_host(func
);
154 return ConvertSdioToCsrSdioResult(err
);
157 return CSR_RESULT_SUCCESS
;
158 } /* CsrSdioRead8() */
161 CsrSdioWrite8(CsrSdioFunction
*function
, u32 address
, u8 data
)
163 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
166 _sdio_claim_host(func
);
167 sdio_writeb(func
, data
, address
, &err
);
168 _sdio_release_host(func
);
171 return ConvertSdioToCsrSdioResult(err
);
174 return CSR_RESULT_SUCCESS
;
175 } /* CsrSdioWrite8() */
178 CsrSdioRead16(CsrSdioFunction
*function
, u32 address
, u16
*data
)
180 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
184 _sdio_claim_host(func
);
185 b0
= sdio_readb(func
, address
, &err
);
187 _sdio_release_host(func
);
188 return ConvertSdioToCsrSdioResult(err
);
191 b1
= sdio_readb(func
, address
+1, &err
);
193 _sdio_release_host(func
);
194 return ConvertSdioToCsrSdioResult(err
);
196 _sdio_release_host(func
);
198 *data
= ((uint16_t)b1
<< 8) | b0
;
200 return CSR_RESULT_SUCCESS
;
201 } /* CsrSdioRead16() */
205 CsrSdioWrite16(CsrSdioFunction
*function
, u32 address
, u16 data
)
207 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
211 _sdio_claim_host(func
);
212 b1
= (data
>> 8) & 0xFF;
213 sdio_writeb(func
, b1
, address
+1, &err
);
215 _sdio_release_host(func
);
216 return ConvertSdioToCsrSdioResult(err
);
220 sdio_writeb(func
, b0
, address
, &err
);
222 _sdio_release_host(func
);
223 return ConvertSdioToCsrSdioResult(err
);
226 _sdio_release_host(func
);
227 return CSR_RESULT_SUCCESS
;
228 } /* CsrSdioWrite16() */
232 CsrSdioF0Read8(CsrSdioFunction
*function
, u32 address
, u8
*data
)
234 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
237 _sdio_claim_host(func
);
238 #ifdef MMC_QUIRK_LENIENT_FN0
239 *data
= sdio_f0_readb(func
, address
, &err
);
241 err
= csr_io_rw_direct(func
->card
, 0, 0, address
, 0, data
);
243 _sdio_release_host(func
);
246 return ConvertSdioToCsrSdioResult(err
);
249 return CSR_RESULT_SUCCESS
;
250 } /* CsrSdioF0Read8() */
253 CsrSdioF0Write8(CsrSdioFunction
*function
, u32 address
, u8 data
)
255 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
258 _sdio_claim_host(func
);
259 #ifdef MMC_QUIRK_LENIENT_FN0
260 sdio_f0_writeb(func
, data
, address
, &err
);
262 err
= csr_io_rw_direct(func
->card
, 1, 0, address
, data
, NULL
);
264 _sdio_release_host(func
);
267 return ConvertSdioToCsrSdioResult(err
);
270 return CSR_RESULT_SUCCESS
;
271 } /* CsrSdioF0Write8() */
275 CsrSdioRead(CsrSdioFunction
*function
, u32 address
, void *data
, u32 length
)
277 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
280 _sdio_claim_host(func
);
281 err
= sdio_readsb(func
, data
, address
, length
);
282 _sdio_release_host(func
);
285 return ConvertSdioToCsrSdioResult(err
);
288 return CSR_RESULT_SUCCESS
;
289 } /* CsrSdioRead() */
292 CsrSdioWrite(CsrSdioFunction
*function
, u32 address
, const void *data
, u32 length
)
294 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
297 _sdio_claim_host(func
);
298 err
= sdio_writesb(func
, address
, (void*)data
, length
);
299 _sdio_release_host(func
);
302 return ConvertSdioToCsrSdioResult(err
);
305 return CSR_RESULT_SUCCESS
;
306 } /* CsrSdioWrite() */
310 csr_sdio_enable_hs(struct mmc_card
*card
)
315 if (!(card
->host
->caps
& MMC_CAP_SD_HIGHSPEED
)) {
316 /* We've asked for HS clock rates, but controller doesn't
317 * claim to support it. We should limit the clock
318 * to 25MHz via module parameter.
320 printk(KERN_INFO
"unifi: request HS but not MMC_CAP_SD_HIGHSPEED");
324 if (!card
->cccr
.high_speed
)
328 ret
= csr_io_rw_direct(card
, 0, 0, SDIO_CCCR_SPEED
, 0, &speed
);
332 speed
|= SDIO_SPEED_EHS
;
334 /* Optimisation: Eliminate read by always assuming SHS and that reserved bits can be zero */
335 speed
= SDIO_SPEED_EHS
| SDIO_SPEED_SHS
;
338 ret
= csr_io_rw_direct(card
, 1, 0, SDIO_CCCR_SPEED
, speed
, NULL
);
342 mmc_card_set_highspeed(card
);
343 card
->host
->ios
.timing
= MMC_TIMING_SD_HS
;
344 card
->host
->ops
->set_ios(card
->host
, &card
->host
->ios
);
350 csr_sdio_disable_hs(struct mmc_card
*card
)
355 if (!(card
->host
->caps
& MMC_CAP_SD_HIGHSPEED
))
358 if (!card
->cccr
.high_speed
)
361 ret
= csr_io_rw_direct(card
, 0, 0, SDIO_CCCR_SPEED
, 0, &speed
);
365 speed
&= ~SDIO_SPEED_EHS
;
367 /* Optimisation: Eliminate read by always assuming SHS and that reserved bits can be zero */
368 speed
= SDIO_SPEED_SHS
; /* clear SDIO_SPEED_EHS */
371 ret
= csr_io_rw_direct(card
, 1, 0, SDIO_CCCR_SPEED
, speed
, NULL
);
375 card
->state
&= ~MMC_STATE_HIGHSPEED
;
376 card
->host
->ios
.timing
= MMC_TIMING_LEGACY
;
377 card
->host
->ops
->set_ios(card
->host
, &card
->host
->ios
);
384 * ---------------------------------------------------------------------------
385 * CsrSdioMaxBusClockFrequencySet
387 * Set the maximum SDIO bus clock speed to use.
390 * sdio SDIO context pointer
391 * maxFrequency maximum clock speed in Hz
395 * ---------------------------------------------------------------------------
398 CsrSdioMaxBusClockFrequencySet(CsrSdioFunction
*function
, u32 maxFrequency
)
400 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
401 struct mmc_host
*host
= func
->card
->host
;
402 struct mmc_ios
*ios
= &host
->ios
;
405 u32 max_khz
= maxFrequency
/1000;
407 if (!max_khz
|| max_khz
> sdio_clock
) {
408 max_khz
= sdio_clock
;
411 _sdio_claim_host(func
);
412 max_hz
= 1000 * max_khz
;
413 if (max_hz
> host
->f_max
) {
414 max_hz
= host
->f_max
;
417 if (max_hz
> 25000000) {
418 err
= csr_sdio_enable_hs(func
->card
);
420 err
= csr_sdio_disable_hs(func
->card
);
423 printk(KERN_ERR
"SDIO warning: Failed to configure SDIO clock mode\n");
424 _sdio_release_host(func
);
425 return CSR_RESULT_SUCCESS
;
429 host
->ops
->set_ios(host
, ios
);
431 _sdio_release_host(func
);
433 return CSR_RESULT_SUCCESS
;
434 } /* CsrSdioMaxBusClockFrequencySet() */
438 * ---------------------------------------------------------------------------
439 * CsrSdioInterruptEnable
440 * CsrSdioInterruptDisable
442 * Enable or disable the SDIO interrupt.
443 * The driver disables the SDIO interrupt until the i/o thread can
445 * The SDIO interrupt can be disabled by modifying the SDIO_INT_ENABLE
446 * register in the Card Common Control Register block, but this requires
447 * two CMD52 operations. A better solution is to mask the interrupt at
448 * the host controller.
451 * sdio SDIO context pointer
454 * Zero on success or a UniFi driver error code.
456 * ---------------------------------------------------------------------------
459 CsrSdioInterruptEnable(CsrSdioFunction
*function
)
461 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
464 #ifdef CSR_CONFIG_MMC_INT_BYPASS_KSOFTIRQD
465 sdio_unblock_card_irq(func
);
467 _sdio_claim_host(func
);
468 /* Write the Int Enable in CCCR block */
469 #ifdef MMC_QUIRK_LENIENT_FN0
470 sdio_f0_writeb(func
, 0x3, SDIO_CCCR_IENx
, &err
);
472 err
= csr_io_rw_direct(func
->card
, 1, 0, SDIO_CCCR_IENx
, 0x03, NULL
);
474 _sdio_release_host(func
);
477 printk(KERN_ERR
"unifi: %s: error %d writing IENx\n", __FUNCTION__
, err
);
478 return ConvertSdioToCsrSdioResult(err
);
481 return CSR_RESULT_SUCCESS
;
482 } /* CsrSdioInterruptEnable() */
485 CsrSdioInterruptDisable(CsrSdioFunction
*function
)
487 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
490 #ifdef CSR_CONFIG_MMC_INT_BYPASS_KSOFTIRQD
491 sdio_block_card_irq(func
);
493 _sdio_claim_host(func
);
494 /* Write the Int Enable in CCCR block */
495 #ifdef MMC_QUIRK_LENIENT_FN0
496 sdio_f0_writeb(func
, 0, SDIO_CCCR_IENx
, &err
);
498 err
= csr_io_rw_direct(func
->card
, 1, 0, SDIO_CCCR_IENx
, 0x00, NULL
);
500 _sdio_release_host(func
);
503 printk(KERN_ERR
"unifi: %s: error %d writing IENx\n", __FUNCTION__
, err
);
504 return ConvertSdioToCsrSdioResult(err
);
507 return CSR_RESULT_SUCCESS
;
508 } /* CsrSdioInterruptDisable() */
511 void CsrSdioInterruptAcknowledge(CsrSdioFunction
*function
)
517 * ---------------------------------------------------------------------------
518 * CsrSdioFunctionEnable
520 * Enable i/o on function 1.
523 * sdio SDIO context pointer
526 * UniFi driver error code.
527 * ---------------------------------------------------------------------------
530 CsrSdioFunctionEnable(CsrSdioFunction
*function
)
532 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
535 /* Enable UniFi function 1 (the 802.11 part). */
536 _sdio_claim_host(func
);
537 err
= sdio_enable_func(func
);
538 _sdio_release_host(func
);
540 unifi_error(NULL
, "Failed to enable SDIO function %d\n", func
->num
);
543 return ConvertSdioToCsrSdioResult(err
);
544 } /* CsrSdioFunctionEnable() */
548 * ---------------------------------------------------------------------------
549 * CsrSdioFunctionDisable
551 * Enable i/o on function 1.
554 * sdio SDIO context pointer
557 * UniFi driver error code.
558 * ---------------------------------------------------------------------------
561 CsrSdioFunctionDisable(CsrSdioFunction
*function
)
563 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
566 /* Disable UniFi function 1 (the 802.11 part). */
567 _sdio_claim_host(func
);
568 err
= sdio_disable_func(func
);
569 _sdio_release_host(func
);
571 unifi_error(NULL
, "Failed to disable SDIO function %d\n", func
->num
);
574 return ConvertSdioToCsrSdioResult(err
);
575 } /* CsrSdioFunctionDisable() */
579 * ---------------------------------------------------------------------------
580 * CsrSdioFunctionActive
582 * No-op as the bus goes to an active state at the start of every
586 * sdio SDIO context pointer
587 * ---------------------------------------------------------------------------
590 CsrSdioFunctionActive(CsrSdioFunction
*function
)
592 } /* CsrSdioFunctionActive() */
595 * ---------------------------------------------------------------------------
596 * CsrSdioFunctionIdle
598 * Set the function as idle.
601 * sdio SDIO context pointer
602 * ---------------------------------------------------------------------------
605 CsrSdioFunctionIdle(CsrSdioFunction
*function
)
607 } /* CsrSdioFunctionIdle() */
611 * ---------------------------------------------------------------------------
617 * sdio SDIO context pointer
618 * ---------------------------------------------------------------------------
621 CsrSdioPowerOn(CsrSdioFunction
*function
)
623 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
624 struct mmc_host
*host
= func
->card
->host
;
626 _sdio_claim_host(func
);
627 if (!card_is_powered
) {
628 mmc_power_restore_host(host
);
631 printk(KERN_INFO
"SDIO: Skip power on; card is already powered.\n");
633 _sdio_release_host(func
);
635 return CSR_RESULT_SUCCESS
;
636 } /* CsrSdioPowerOn() */
639 * ---------------------------------------------------------------------------
645 * sdio SDIO context pointer
646 * ---------------------------------------------------------------------------
649 CsrSdioPowerOff(CsrSdioFunction
*function
)
651 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
652 struct mmc_host
*host
= func
->card
->host
;
654 _sdio_claim_host(func
);
655 if (card_is_powered
) {
656 mmc_power_save_host(host
);
659 printk(KERN_INFO
"SDIO: Skip power off; card is already powered off.\n");
661 _sdio_release_host(func
);
662 } /* CsrSdioPowerOff() */
666 sdio_set_block_size_ignore_first_error(struct sdio_func
*func
, unsigned blksz
)
670 if (blksz
> func
->card
->host
->max_blk_size
)
674 blksz
= min(func
->max_blksize
, func
->card
->host
->max_blk_size
);
675 blksz
= min(blksz
, 512u);
679 * Ignore -ERANGE (OUT_OF_RANGE in R5) on the first byte as
680 * the block size may be invalid until both bytes are written.
682 ret
= csr_io_rw_direct(func
->card
, 1, 0,
683 SDIO_FBR_BASE(func
->num
) + SDIO_FBR_BLKSIZE
,
685 if (ret
&& ret
!= -ERANGE
)
687 ret
= csr_io_rw_direct(func
->card
, 1, 0,
688 SDIO_FBR_BASE(func
->num
) + SDIO_FBR_BLKSIZE
+ 1,
689 (blksz
>> 8) & 0xff, NULL
);
692 func
->cur_blksize
= blksz
;
698 CsrSdioBlockSizeSet(CsrSdioFunction
*function
, u16 blockSize
)
700 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
703 /* Module parameter overrides */
704 if (sdio_block_size
> -1) {
705 blockSize
= sdio_block_size
;
708 unifi_trace(NULL
, UDBG1
, "Set SDIO function block size to %d\n",
711 _sdio_claim_host(func
);
712 r
= sdio_set_block_size(func
, blockSize
);
713 _sdio_release_host(func
);
716 * The MMC driver for kernels prior to 2.6.32 may fail this request
717 * with -ERANGE. In this case use our workaround.
720 _sdio_claim_host(func
);
721 r
= sdio_set_block_size_ignore_first_error(func
, blockSize
);
722 _sdio_release_host(func
);
725 unifi_error(NULL
, "Error %d setting block size\n", r
);
728 /* Determine the achieved block size to pass to the core */
729 function
->blockSize
= func
->cur_blksize
;
731 return ConvertSdioToCsrSdioResult(r
);
732 } /* CsrSdioBlockSizeSet() */
736 * ---------------------------------------------------------------------------
739 * Hard Resets UniFi is possible.
742 * sdio SDIO context pointer
743 * ---------------------------------------------------------------------------
746 CsrSdioHardReset(CsrSdioFunction
*function
)
748 return CSR_RESULT_FAILURE
;
749 } /* CsrSdioHardReset() */
754 * ---------------------------------------------------------------------------
755 * uf_glue_sdio_int_handler
757 * Interrupt callback function for SDIO interrupts.
758 * This is called in kernel context (i.e. not interrupt context).
761 * func SDIO context pointer
766 * Note: Called with host already claimed.
767 * ---------------------------------------------------------------------------
770 uf_glue_sdio_int_handler(struct sdio_func
*func
)
772 CsrSdioFunction
*sdio_ctx
;
773 CsrSdioInterruptDsrCallback func_dsr_callback
;
776 sdio_ctx
= sdio_get_drvdata(func
);
781 #ifndef CSR_CONFIG_MMC_INT_BYPASS_KSOFTIRQD
783 * Normally, we are not allowed to do any SDIO commands here.
784 * However, this is called in a thread context and with the SDIO lock
785 * so we disable the interrupts here instead of trying to do complicated
786 * things with the SDIO lock.
788 #ifdef MMC_QUIRK_LENIENT_FN0
789 sdio_f0_writeb(func
, 0, SDIO_CCCR_IENx
, &r
);
791 r
= csr_io_rw_direct(func
->card
, 1, 0, SDIO_CCCR_IENx
, 0x00, NULL
);
794 printk(KERN_ERR
"UniFi MMC Int handler: Failed to disable interrupts %d\n", r
);
798 /* If the function driver has registered a handler, call it */
799 if (sdio_func_drv
&& sdio_func_drv
->intr
) {
801 func_dsr_callback
= sdio_func_drv
->intr(sdio_ctx
);
803 /* If interrupt handle returns a DSR handle, call it */
804 if (func_dsr_callback
) {
805 func_dsr_callback(sdio_ctx
);
809 } /* uf_glue_sdio_int_handler() */
814 * ---------------------------------------------------------------------------
815 * csr_sdio_linux_remove_irq
817 * Unregister the interrupt handler.
818 * This means that the linux layer can not process interrupts any more.
821 * sdio SDIO context pointer
824 * Status of the removal.
825 * ---------------------------------------------------------------------------
827 int csr_sdio_linux_remove_irq(CsrSdioFunction
*function
)
829 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
832 unifi_trace(NULL
, UDBG1
, "csr_sdio_linux_remove_irq\n");
834 sdio_claim_host(func
);
835 r
= sdio_release_irq(func
);
836 sdio_release_host(func
);
840 } /* csr_sdio_linux_remove_irq() */
844 * ---------------------------------------------------------------------------
845 * csr_sdio_linux_install_irq
847 * Register the interrupt handler.
848 * This means that the linux layer can process interrupts.
851 * sdio SDIO context pointer
854 * Status of the removal.
855 * ---------------------------------------------------------------------------
857 int csr_sdio_linux_install_irq(CsrSdioFunction
*function
)
859 struct sdio_func
*func
= (struct sdio_func
*)function
->priv
;
862 unifi_trace(NULL
, UDBG1
, "csr_sdio_linux_install_irq\n");
864 /* Register our interrupt handle */
865 sdio_claim_host(func
);
866 r
= sdio_claim_irq(func
, uf_glue_sdio_int_handler
);
867 sdio_release_host(func
);
869 /* If the interrupt was installed earlier, is fine */
874 } /* csr_sdio_linux_install_irq() */
879 * Power Management notifier
881 struct uf_sdio_mmc_pm_notifier
883 struct list_head list
;
885 CsrSdioFunction
*sdio_ctx
;
886 struct notifier_block pm_notifier
;
889 /* PM notifier list head */
890 static struct uf_sdio_mmc_pm_notifier uf_sdio_mmc_pm_notifiers
= {
895 * ---------------------------------------------------------------------------
896 * uf_sdio_mmc_register_pm_notifier
897 * uf_sdio_mmc_unregister_pm_notifier
899 * Register/unregister for power management events. A list is used to
900 * allow multiple card instances to be supported.
903 * sdio_ctx - CSR SDIO context to associate PM notifier to
906 * Register function returns NULL on error
907 * ---------------------------------------------------------------------------
909 static struct uf_sdio_mmc_pm_notifier
*
910 uf_sdio_mmc_register_pm_notifier(CsrSdioFunction
*sdio_ctx
)
912 /* Allocate notifier context for this card instance */
913 struct uf_sdio_mmc_pm_notifier
*notifier_ctx
= kmalloc(sizeof(struct uf_sdio_mmc_pm_notifier
), GFP_KERNEL
);
917 notifier_ctx
->sdio_ctx
= sdio_ctx
;
918 notifier_ctx
->pm_notifier
.notifier_call
= uf_sdio_mmc_power_event
;
920 list_add(¬ifier_ctx
->list
, &uf_sdio_mmc_pm_notifiers
.list
);
922 if (register_pm_notifier(¬ifier_ctx
->pm_notifier
)) {
923 printk(KERN_ERR
"unifi: register_pm_notifier failed\n");
931 uf_sdio_mmc_unregister_pm_notifier(CsrSdioFunction
*sdio_ctx
)
933 struct uf_sdio_mmc_pm_notifier
*notifier_ctx
;
934 struct list_head
*node
, *q
;
936 list_for_each_safe(node
, q
, &uf_sdio_mmc_pm_notifiers
.list
) {
937 notifier_ctx
= list_entry(node
, struct uf_sdio_mmc_pm_notifier
, list
);
939 /* If it matches, unregister and free the notifier context */
940 if (notifier_ctx
&& notifier_ctx
->sdio_ctx
== sdio_ctx
)
942 if (unregister_pm_notifier(¬ifier_ctx
->pm_notifier
)) {
943 printk(KERN_ERR
"unifi: unregister_pm_notifier failed\n");
946 /* Remove from list */
947 notifier_ctx
->sdio_ctx
= NULL
;
955 * ---------------------------------------------------------------------------
956 * uf_sdio_mmc_power_event
958 * Handler for power management events.
960 * We need to handle suspend/resume events while the userspace is unsuspended
961 * to allow the SME to run its suspend/resume state machines.
967 * Status of the event handling
968 * ---------------------------------------------------------------------------
971 uf_sdio_mmc_power_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
973 struct uf_sdio_mmc_pm_notifier
*notifier_ctx
= container_of(this,
974 struct uf_sdio_mmc_pm_notifier
,
977 /* Call the CSR SDIO function driver's suspend/resume method
978 * while the userspace is unsuspended.
981 case PM_POST_HIBERNATION
:
982 case PM_POST_SUSPEND
:
983 printk(KERN_INFO
"%s:%d resume\n", __FUNCTION__
, __LINE__
);
984 if (sdio_func_drv
&& sdio_func_drv
->resume
) {
985 sdio_func_drv
->resume(notifier_ctx
->sdio_ctx
);
989 case PM_HIBERNATION_PREPARE
:
990 case PM_SUSPEND_PREPARE
:
991 printk(KERN_INFO
"%s:%d suspend\n", __FUNCTION__
, __LINE__
);
992 if (sdio_func_drv
&& sdio_func_drv
->suspend
) {
993 sdio_func_drv
->suspend(notifier_ctx
->sdio_ctx
);
1000 #endif /* CONFIG_PM */
1003 * ---------------------------------------------------------------------------
1004 * uf_glue_sdio_probe
1006 * Card insert callback.
1009 * func Our (glue layer) context pointer.
1012 * UniFi driver error code.
1013 * ---------------------------------------------------------------------------
1016 uf_glue_sdio_probe(struct sdio_func
*func
,
1017 const struct sdio_device_id
*id
)
1020 CsrSdioFunction
*sdio_ctx
;
1022 /* First of all claim the SDIO driver */
1023 sdio_claim_host(func
);
1025 /* Assume that the card is already powered */
1026 card_is_powered
= 1;
1028 /* Assumes one card per host, which is true for SDIO */
1029 instance
= func
->card
->host
->index
;
1030 printk("sdio bus_id: %16s - UniFi card 0x%X inserted\n",
1031 sdio_func_id(func
), instance
);
1033 /* Allocate context */
1034 sdio_ctx
= kmalloc(sizeof(CsrSdioFunction
), GFP_KERNEL
);
1035 if (sdio_ctx
== NULL
) {
1036 sdio_release_host(func
);
1040 /* Initialise the context */
1041 sdio_ctx
->sdioId
.manfId
= func
->vendor
;
1042 sdio_ctx
->sdioId
.cardId
= func
->device
;
1043 sdio_ctx
->sdioId
.sdioFunction
= func
->num
;
1044 sdio_ctx
->sdioId
.sdioInterface
= func
->class;
1045 sdio_ctx
->blockSize
= func
->cur_blksize
;
1046 sdio_ctx
->priv
= (void *)func
;
1047 sdio_ctx
->features
= 0;
1049 /* Module parameter enables byte mode */
1050 if (sdio_byte_mode
) {
1051 sdio_ctx
->features
|= CSR_SDIO_FEATURE_BYTE_MODE
;
1054 if (func
->card
->host
->caps
& MMC_CAP_SD_HIGHSPEED
) {
1055 unifi_trace(NULL
, UDBG1
, "MMC_CAP_SD_HIGHSPEED is available\n");
1058 #ifdef MMC_QUIRK_LENIENT_FN0
1059 func
->card
->quirks
|= MMC_QUIRK_LENIENT_FN0
;
1062 /* Pass context to the SDIO driver */
1063 sdio_set_drvdata(func
, sdio_ctx
);
1066 /* Register to get PM events */
1067 if (uf_sdio_mmc_register_pm_notifier(sdio_ctx
) == NULL
) {
1068 unifi_error(NULL
, "%s: Failed to register for PM events\n", __FUNCTION__
);
1072 /* Register this device with the SDIO function driver */
1073 /* Call the main UniFi driver inserted handler */
1074 if (sdio_func_drv
&& sdio_func_drv
->inserted
) {
1075 uf_add_os_device(instance
, &func
->dev
);
1076 sdio_func_drv
->inserted(sdio_ctx
);
1079 /* We have finished, so release the SDIO driver */
1080 sdio_release_host(func
);
1082 #ifdef ANDROID_BUILD
1083 /* Take the wakelock */
1084 unifi_trace(NULL
, UDBG1
, "probe: take wake lock\n");
1085 wake_lock(&unifi_sdio_wake_lock
);
1089 } /* uf_glue_sdio_probe() */
1093 * ---------------------------------------------------------------------------
1094 * uf_glue_sdio_remove
1096 * Card removal callback.
1099 * func Our (glue layer) context pointer.
1102 * UniFi driver error code.
1103 * ---------------------------------------------------------------------------
1106 uf_glue_sdio_remove(struct sdio_func
*func
)
1108 CsrSdioFunction
*sdio_ctx
;
1110 sdio_ctx
= sdio_get_drvdata(func
);
1115 unifi_info(NULL
, "UniFi card removed\n");
1117 /* Clean up the SDIO function driver */
1118 if (sdio_func_drv
&& sdio_func_drv
->removed
) {
1119 uf_remove_os_device(func
->card
->host
->index
);
1120 sdio_func_drv
->removed(sdio_ctx
);
1124 /* Unregister for PM events */
1125 uf_sdio_mmc_unregister_pm_notifier(sdio_ctx
);
1130 } /* uf_glue_sdio_remove */
1134 * SDIO ids *must* be statically declared, so we can't take
1135 * them from the list passed in csr_sdio_register_driver().
1137 static const struct sdio_device_id unifi_ids
[] = {
1138 { SDIO_DEVICE(SDIO_MANF_ID_CSR
, SDIO_CARD_ID_UNIFI_3
) },
1139 { SDIO_DEVICE(SDIO_MANF_ID_CSR
, SDIO_CARD_ID_UNIFI_4
) },
1140 { /* end: all zeroes */ },
1143 MODULE_DEVICE_TABLE(sdio
, unifi_ids
);
1148 * ---------------------------------------------------------------------------
1149 * uf_glue_sdio_suspend
1151 * Card suspend callback. The userspace will already be suspended.
1154 * dev The struct device owned by the MMC driver
1158 * ---------------------------------------------------------------------------
1161 uf_glue_sdio_suspend(struct device
*dev
)
1163 unifi_trace(NULL
, UDBG1
, "uf_glue_sdio_suspend");
1166 } /* uf_glue_sdio_suspend */
1170 * ---------------------------------------------------------------------------
1171 * uf_glue_sdio_resume
1173 * Card resume callback. The userspace will still be suspended.
1176 * dev The struct device owned by the MMC driver
1180 * ---------------------------------------------------------------------------
1183 uf_glue_sdio_resume(struct device
*dev
)
1185 unifi_trace(NULL
, UDBG1
, "uf_glue_sdio_resume");
1187 #ifdef ANDROID_BUILD
1188 unifi_trace(NULL
, UDBG1
, "resume: take wakelock\n");
1189 wake_lock(&unifi_sdio_wake_lock
);
1194 } /* uf_glue_sdio_resume */
1196 static struct dev_pm_ops unifi_pm_ops
= {
1197 .suspend
= uf_glue_sdio_suspend
,
1198 .resume
= uf_glue_sdio_resume
,
1201 #define UNIFI_PM_OPS (&unifi_pm_ops)
1205 #define UNIFI_PM_OPS NULL
1207 #endif /* CONFIG_PM */
1209 static struct sdio_driver unifi_driver
= {
1210 .probe
= uf_glue_sdio_probe
,
1211 .remove
= uf_glue_sdio_remove
,
1213 .id_table
= unifi_ids
,
1214 .drv
.pm
= UNIFI_PM_OPS
,
1219 * ---------------------------------------------------------------------------
1220 * CsrSdioFunctionDriverRegister
1221 * CsrSdioFunctionDriverUnregister
1223 * These functions are called from the main module load and unload
1224 * functions. They perform the appropriate operations for the
1225 * linux MMC/SDIO driver.
1228 * sdio_drv Pointer to the function driver's SDIO structure.
1232 * ---------------------------------------------------------------------------
1235 CsrSdioFunctionDriverRegister(CsrSdioFunctionDriver
*sdio_drv
)
1239 printk("UniFi: Using native Linux MMC driver for SDIO.\n");
1241 if (sdio_func_drv
) {
1242 unifi_error(NULL
, "sdio_mmc: UniFi driver already registered\n");
1243 return CSR_SDIO_RESULT_INVALID_VALUE
;
1246 #ifdef ANDROID_BUILD
1247 wake_lock_init(&unifi_sdio_wake_lock
, WAKE_LOCK_SUSPEND
, "unifi_sdio_work");
1250 /* Save the registered driver description */
1253 * Need a table here to handle a call to register for just one function.
1254 * mmc only allows us to register for the whole device
1256 sdio_func_drv
= sdio_drv
;
1259 /* Initialise PM notifier list */
1260 INIT_LIST_HEAD(&uf_sdio_mmc_pm_notifiers
.list
);
1263 /* Register ourself with mmc_core */
1264 r
= sdio_register_driver(&unifi_driver
);
1266 printk(KERN_ERR
"unifi_sdio: Failed to register UniFi SDIO driver: %d\n", r
);
1267 return ConvertSdioToCsrSdioResult(r
);
1270 return CSR_RESULT_SUCCESS
;
1271 } /* CsrSdioFunctionDriverRegister() */
1276 CsrSdioFunctionDriverUnregister(CsrSdioFunctionDriver
*sdio_drv
)
1278 printk(KERN_INFO
"UniFi: unregister from MMC sdio\n");
1280 #ifdef ANDROID_BUILD
1281 wake_lock_destroy(&unifi_sdio_wake_lock
);
1283 sdio_unregister_driver(&unifi_driver
);
1285 sdio_func_drv
= NULL
;
1287 } /* CsrSdioFunctionDriverUnregister() */