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/version.h>
18 #include <linux/mmc/core.h>
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/host.h>
21 #include <linux/mmc/sdio_func.h>
22 #include <linux/mmc/sdio_ids.h>
23 #include <linux/mmc/sdio.h>
24 #include <linux/suspend.h>
26 #include "unifi_priv.h"
29 struct wake_lock unifi_sdio_wake_lock; /* wakelock to prevent suspend while resuming */
32 static CsrSdioFunctionDriver *sdio_func_drv;
35 static int uf_sdio_mmc_power_event(struct notifier_block *this, unsigned long event, void *ptr);
39 * We need to keep track of the power on/off because we can not call
40 * mmc_power_restore_host() when the card is already powered.
41 * Even then, we need to patch the MMC driver to add a power_restore handler
42 * in the mmc_sdio_ops structure. If the MMC driver before 2.6.37 is not patched,
43 * mmc_power_save_host() and mmc_power_restore_host() are no-ops in the kernel,
44 * returning immediately (at least on x86).
46 static int card_is_powered = 1;
48 /* MMC uses ENOMEDIUM to indicate card gone away */
51 ConvertSdioToCsrSdioResult(int r)
53 CsrResult csrResult = CSR_RESULT_FAILURE;
57 csrResult = CSR_RESULT_SUCCESS;
61 csrResult = CSR_SDIO_RESULT_CRC_ERROR;
66 csrResult = CSR_SDIO_RESULT_TIMEOUT;
70 csrResult = CSR_SDIO_RESULT_NO_DEVICE;
73 csrResult = CSR_SDIO_RESULT_INVALID_VALUE;
79 csrResult = CSR_RESULT_FAILURE;
82 unifi_warning(NULL, "Unrecognised SDIO error code: %d\n", r);
91 csr_io_rw_direct(struct mmc_card *card, int write, uint8_t fn,
92 uint32_t addr, uint8_t in, uint8_t* out)
94 struct mmc_command cmd;
100 memset(&cmd, 0, sizeof(struct mmc_command));
102 cmd.opcode = SD_IO_RW_DIRECT;
103 cmd.arg = write ? 0x80000000 : 0x00000000;
105 cmd.arg |= (write && out) ? 0x08000000 : 0x00000000;
106 cmd.arg |= addr << 9;
108 cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
110 err = mmc_wait_for_cmd(card->host, &cmd, 0);
114 /* this function is not exported, so we will need to sort it out here
115 * for now, lets hard code it to sdio */
117 /* old arg (mmc_host_is_spi(card->host)) { */
118 /* host driver already reported errors */
120 if (cmd.resp[0] & R5_ERROR) {
121 printk(KERN_ERR "%s: r5 error 0x%02x\n",
122 __FUNCTION__, cmd.resp[0]);
125 if (cmd.resp[0] & R5_FUNCTION_NUMBER)
127 if (cmd.resp[0] & R5_OUT_OF_RANGE)
132 if (0) { /* old argument (mmc_host_is_spi(card->host)) */
133 *out = (cmd.resp[0] >> 8) & 0xFF;
136 *out = cmd.resp[0] & 0xFF;
140 return CSR_RESULT_SUCCESS;
145 CsrSdioRead8(CsrSdioFunction *function, u32 address, u8 *data)
147 struct sdio_func *func = (struct sdio_func *)function->priv;
150 _sdio_claim_host(func);
151 *data = sdio_readb(func, address, &err);
152 _sdio_release_host(func);
156 return ConvertSdioToCsrSdioResult(err);
159 return CSR_RESULT_SUCCESS;
160 } /* CsrSdioRead8() */
163 CsrSdioWrite8(CsrSdioFunction *function, u32 address, u8 data)
165 struct sdio_func *func = (struct sdio_func *)function->priv;
168 _sdio_claim_host(func);
169 sdio_writeb(func, data, address, &err);
170 _sdio_release_host(func);
174 return ConvertSdioToCsrSdioResult(err);
177 return CSR_RESULT_SUCCESS;
178 } /* CsrSdioWrite8() */
181 CsrSdioRead16(CsrSdioFunction *function, u32 address, u16 *data)
183 struct sdio_func *func = (struct sdio_func *)function->priv;
187 _sdio_claim_host(func);
188 b0 = sdio_readb(func, address, &err);
190 _sdio_release_host(func);
191 return ConvertSdioToCsrSdioResult(err);
194 b1 = sdio_readb(func, address+1, &err);
196 _sdio_release_host(func);
197 return ConvertSdioToCsrSdioResult(err);
199 _sdio_release_host(func);
201 *data = ((uint16_t)b1 << 8) | b0;
203 return CSR_RESULT_SUCCESS;
204 } /* CsrSdioRead16() */
208 CsrSdioWrite16(CsrSdioFunction *function, u32 address, u16 data)
210 struct sdio_func *func = (struct sdio_func *)function->priv;
214 _sdio_claim_host(func);
215 b1 = (data >> 8) & 0xFF;
216 sdio_writeb(func, b1, address+1, &err);
218 _sdio_release_host(func);
219 return ConvertSdioToCsrSdioResult(err);
223 sdio_writeb(func, b0, address, &err);
225 _sdio_release_host(func);
226 return ConvertSdioToCsrSdioResult(err);
229 _sdio_release_host(func);
230 return CSR_RESULT_SUCCESS;
231 } /* CsrSdioWrite16() */
235 CsrSdioF0Read8(CsrSdioFunction *function, u32 address, u8 *data)
237 struct sdio_func *func = (struct sdio_func *)function->priv;
240 _sdio_claim_host(func);
241 #ifdef MMC_QUIRK_LENIENT_FN0
242 *data = sdio_f0_readb(func, address, &err);
244 err = csr_io_rw_direct(func->card, 0, 0, address, 0, data);
246 _sdio_release_host(func);
250 return ConvertSdioToCsrSdioResult(err);
253 return CSR_RESULT_SUCCESS;
254 } /* CsrSdioF0Read8() */
257 CsrSdioF0Write8(CsrSdioFunction *function, u32 address, u8 data)
259 struct sdio_func *func = (struct sdio_func *)function->priv;
262 _sdio_claim_host(func);
263 #ifdef MMC_QUIRK_LENIENT_FN0
264 sdio_f0_writeb(func, data, address, &err);
266 err = csr_io_rw_direct(func->card, 1, 0, address, data, NULL);
268 _sdio_release_host(func);
272 return ConvertSdioToCsrSdioResult(err);
275 return CSR_RESULT_SUCCESS;
276 } /* CsrSdioF0Write8() */
280 CsrSdioRead(CsrSdioFunction *function, u32 address, void *data, u32 length)
282 struct sdio_func *func = (struct sdio_func *)function->priv;
285 _sdio_claim_host(func);
286 err = sdio_readsb(func, data, address, length);
287 _sdio_release_host(func);
291 return ConvertSdioToCsrSdioResult(err);
294 return CSR_RESULT_SUCCESS;
295 } /* CsrSdioRead() */
298 CsrSdioWrite(CsrSdioFunction *function, u32 address, const void *data, u32 length)
300 struct sdio_func *func = (struct sdio_func *)function->priv;
303 _sdio_claim_host(func);
304 err = sdio_writesb(func, address, (void*)data, length);
305 _sdio_release_host(func);
309 return ConvertSdioToCsrSdioResult(err);
312 return CSR_RESULT_SUCCESS;
313 } /* CsrSdioWrite() */
317 csr_sdio_enable_hs(struct mmc_card *card)
322 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) {
323 /* We've asked for HS clock rates, but controller doesn't
324 * claim to support it. We should limit the clock
325 * to 25MHz via module parameter.
327 printk(KERN_INFO "unifi: request HS but not MMC_CAP_SD_HIGHSPEED");
331 if (!card->cccr.high_speed)
335 ret = csr_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
339 speed |= SDIO_SPEED_EHS;
341 /* Optimisation: Eliminate read by always assuming SHS and that reserved bits can be zero */
342 speed = SDIO_SPEED_EHS | SDIO_SPEED_SHS;
345 ret = csr_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
349 mmc_card_set_highspeed(card);
350 card->host->ios.timing = MMC_TIMING_SD_HS;
351 card->host->ops->set_ios(card->host, &card->host->ios);
357 csr_sdio_disable_hs(struct mmc_card *card)
362 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
365 if (!card->cccr.high_speed)
368 ret = csr_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
372 speed &= ~SDIO_SPEED_EHS;
374 /* Optimisation: Eliminate read by always assuming SHS and that reserved bits can be zero */
375 speed = SDIO_SPEED_SHS; /* clear SDIO_SPEED_EHS */
378 ret = csr_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
382 card->state &= ~MMC_STATE_HIGHSPEED;
383 card->host->ios.timing = MMC_TIMING_LEGACY;
384 card->host->ops->set_ios(card->host, &card->host->ios);
391 * ---------------------------------------------------------------------------
392 * CsrSdioMaxBusClockFrequencySet
394 * Set the maximum SDIO bus clock speed to use.
397 * sdio SDIO context pointer
398 * maxFrequency maximum clock speed in Hz
402 * ---------------------------------------------------------------------------
405 CsrSdioMaxBusClockFrequencySet(CsrSdioFunction *function, u32 maxFrequency)
407 struct sdio_func *func = (struct sdio_func *)function->priv;
408 struct mmc_host *host = func->card->host;
409 struct mmc_ios *ios = &host->ios;
412 u32 max_khz = maxFrequency/1000;
414 if (!max_khz || max_khz > sdio_clock) {
415 max_khz = sdio_clock;
418 _sdio_claim_host(func);
419 max_hz = 1000 * max_khz;
420 if (max_hz > host->f_max) {
421 max_hz = host->f_max;
424 if (max_hz > 25000000) {
425 err = csr_sdio_enable_hs(func->card);
427 err = csr_sdio_disable_hs(func->card);
430 printk(KERN_ERR "SDIO warning: Failed to configure SDIO clock mode\n");
431 _sdio_release_host(func);
432 return CSR_RESULT_SUCCESS;
436 host->ops->set_ios(host, ios);
438 _sdio_release_host(func);
440 return CSR_RESULT_SUCCESS;
441 } /* CsrSdioMaxBusClockFrequencySet() */
445 * ---------------------------------------------------------------------------
446 * CsrSdioInterruptEnable
447 * CsrSdioInterruptDisable
449 * Enable or disable the SDIO interrupt.
450 * The driver disables the SDIO interrupt until the i/o thread can
452 * The SDIO interrupt can be disabled by modifying the SDIO_INT_ENABLE
453 * register in the Card Common Control Register block, but this requires
454 * two CMD52 operations. A better solution is to mask the interrupt at
455 * the host controller.
458 * sdio SDIO context pointer
461 * Zero on success or a UniFi driver error code.
463 * ---------------------------------------------------------------------------
466 CsrSdioInterruptEnable(CsrSdioFunction *function)
468 struct sdio_func *func = (struct sdio_func *)function->priv;
471 #ifdef CSR_CONFIG_MMC_INT_BYPASS_KSOFTIRQD
472 sdio_unblock_card_irq(func);
474 _sdio_claim_host(func);
475 /* Write the Int Enable in CCCR block */
476 #ifdef MMC_QUIRK_LENIENT_FN0
477 sdio_f0_writeb(func, 0x3, SDIO_CCCR_IENx, &err);
479 err = csr_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, 0x03, NULL);
481 _sdio_release_host(func);
485 printk(KERN_ERR "unifi: %s: error %d writing IENx\n", __FUNCTION__, err);
486 return ConvertSdioToCsrSdioResult(err);
489 return CSR_RESULT_SUCCESS;
490 } /* CsrSdioInterruptEnable() */
493 CsrSdioInterruptDisable(CsrSdioFunction *function)
495 struct sdio_func *func = (struct sdio_func *)function->priv;
498 #ifdef CSR_CONFIG_MMC_INT_BYPASS_KSOFTIRQD
499 sdio_block_card_irq(func);
501 _sdio_claim_host(func);
502 /* Write the Int Enable in CCCR block */
503 #ifdef MMC_QUIRK_LENIENT_FN0
504 sdio_f0_writeb(func, 0, SDIO_CCCR_IENx, &err);
506 err = csr_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, 0x00, NULL);
508 _sdio_release_host(func);
512 printk(KERN_ERR "unifi: %s: error %d writing IENx\n", __FUNCTION__, err);
513 return ConvertSdioToCsrSdioResult(err);
516 return CSR_RESULT_SUCCESS;
517 } /* CsrSdioInterruptDisable() */
520 void CsrSdioInterruptAcknowledge(CsrSdioFunction *function)
526 * ---------------------------------------------------------------------------
527 * CsrSdioFunctionEnable
529 * Enable i/o on function 1.
532 * sdio SDIO context pointer
535 * UniFi driver error code.
536 * ---------------------------------------------------------------------------
539 CsrSdioFunctionEnable(CsrSdioFunction *function)
541 struct sdio_func *func = (struct sdio_func *)function->priv;
546 /* Enable UniFi function 1 (the 802.11 part). */
547 _sdio_claim_host(func);
548 err = sdio_enable_func(func);
549 _sdio_release_host(func);
551 unifi_error(NULL, "Failed to enable SDIO function %d\n", func->num);
555 return ConvertSdioToCsrSdioResult(err);
556 } /* CsrSdioFunctionEnable() */
560 * ---------------------------------------------------------------------------
561 * CsrSdioFunctionDisable
563 * Enable i/o on function 1.
566 * sdio SDIO context pointer
569 * UniFi driver error code.
570 * ---------------------------------------------------------------------------
573 CsrSdioFunctionDisable(CsrSdioFunction *function)
575 struct sdio_func *func = (struct sdio_func *)function->priv;
580 /* Disable UniFi function 1 (the 802.11 part). */
581 _sdio_claim_host(func);
582 err = sdio_disable_func(func);
583 _sdio_release_host(func);
585 unifi_error(NULL, "Failed to disable SDIO function %d\n", func->num);
589 return ConvertSdioToCsrSdioResult(err);
590 } /* CsrSdioFunctionDisable() */
594 * ---------------------------------------------------------------------------
595 * CsrSdioFunctionActive
597 * No-op as the bus goes to an active state at the start of every
601 * sdio SDIO context pointer
602 * ---------------------------------------------------------------------------
605 CsrSdioFunctionActive(CsrSdioFunction *function)
607 } /* CsrSdioFunctionActive() */
610 * ---------------------------------------------------------------------------
611 * CsrSdioFunctionIdle
613 * Set the function as idle.
616 * sdio SDIO context pointer
617 * ---------------------------------------------------------------------------
620 CsrSdioFunctionIdle(CsrSdioFunction *function)
622 } /* CsrSdioFunctionIdle() */
626 * ---------------------------------------------------------------------------
632 * sdio SDIO context pointer
633 * ---------------------------------------------------------------------------
636 CsrSdioPowerOn(CsrSdioFunction *function)
638 struct sdio_func *func = (struct sdio_func *)function->priv;
639 struct mmc_host *host = func->card->host;
641 _sdio_claim_host(func);
642 if (!card_is_powered) {
643 mmc_power_restore_host(host);
646 printk(KERN_INFO "SDIO: Skip power on; card is already powered.\n");
648 _sdio_release_host(func);
650 return CSR_RESULT_SUCCESS;
651 } /* CsrSdioPowerOn() */
654 * ---------------------------------------------------------------------------
660 * sdio SDIO context pointer
661 * ---------------------------------------------------------------------------
664 CsrSdioPowerOff(CsrSdioFunction *function)
666 struct sdio_func *func = (struct sdio_func *)function->priv;
667 struct mmc_host *host = func->card->host;
669 _sdio_claim_host(func);
670 if (card_is_powered) {
671 mmc_power_save_host(host);
674 printk(KERN_INFO "SDIO: Skip power off; card is already powered off.\n");
676 _sdio_release_host(func);
677 } /* CsrSdioPowerOff() */
681 sdio_set_block_size_ignore_first_error(struct sdio_func *func, unsigned blksz)
685 if (blksz > func->card->host->max_blk_size)
689 blksz = min(func->max_blksize, func->card->host->max_blk_size);
690 blksz = min(blksz, 512u);
694 * Ignore -ERANGE (OUT_OF_RANGE in R5) on the first byte as
695 * the block size may be invalid until both bytes are written.
697 ret = csr_io_rw_direct(func->card, 1, 0,
698 SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
700 if (ret && ret != -ERANGE)
702 ret = csr_io_rw_direct(func->card, 1, 0,
703 SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,
704 (blksz >> 8) & 0xff, NULL);
707 func->cur_blksize = blksz;
713 CsrSdioBlockSizeSet(CsrSdioFunction *function, u16 blockSize)
715 struct sdio_func *func = (struct sdio_func *)function->priv;
718 /* Module parameter overrides */
719 if (sdio_block_size > -1) {
720 blockSize = sdio_block_size;
723 unifi_trace(NULL, UDBG1, "Set SDIO function block size to %d\n",
726 _sdio_claim_host(func);
727 r = sdio_set_block_size(func, blockSize);
728 _sdio_release_host(func);
731 * The MMC driver for kernels prior to 2.6.32 may fail this request
732 * with -ERANGE. In this case use our workaround.
735 _sdio_claim_host(func);
736 r = sdio_set_block_size_ignore_first_error(func, blockSize);
737 _sdio_release_host(func);
740 unifi_error(NULL, "Error %d setting block size\n", r);
743 /* Determine the achieved block size to pass to the core */
744 function->blockSize = func->cur_blksize;
746 return ConvertSdioToCsrSdioResult(r);
747 } /* CsrSdioBlockSizeSet() */
751 * ---------------------------------------------------------------------------
754 * Hard Resets UniFi is possible.
757 * sdio SDIO context pointer
758 * ---------------------------------------------------------------------------
761 CsrSdioHardReset(CsrSdioFunction *function)
763 return CSR_RESULT_FAILURE;
764 } /* CsrSdioHardReset() */
769 * ---------------------------------------------------------------------------
770 * uf_glue_sdio_int_handler
772 * Interrupt callback function for SDIO interrupts.
773 * This is called in kernel context (i.e. not interrupt context).
776 * func SDIO context pointer
781 * Note: Called with host already claimed.
782 * ---------------------------------------------------------------------------
785 uf_glue_sdio_int_handler(struct sdio_func *func)
787 CsrSdioFunction *sdio_ctx;
788 CsrSdioInterruptDsrCallback func_dsr_callback;
791 sdio_ctx = sdio_get_drvdata(func);
796 #ifndef CSR_CONFIG_MMC_INT_BYPASS_KSOFTIRQD
798 * Normally, we are not allowed to do any SDIO commands here.
799 * However, this is called in a thread context and with the SDIO lock
800 * so we disable the interrupts here instead of trying to do complicated
801 * things with the SDIO lock.
803 #ifdef MMC_QUIRK_LENIENT_FN0
804 sdio_f0_writeb(func, 0, SDIO_CCCR_IENx, &r);
806 r = csr_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, 0x00, NULL);
809 printk(KERN_ERR "UniFi MMC Int handler: Failed to disable interrupts %d\n", r);
813 /* If the function driver has registered a handler, call it */
814 if (sdio_func_drv && sdio_func_drv->intr) {
816 func_dsr_callback = sdio_func_drv->intr(sdio_ctx);
818 /* If interrupt handle returns a DSR handle, call it */
819 if (func_dsr_callback) {
820 func_dsr_callback(sdio_ctx);
824 } /* uf_glue_sdio_int_handler() */
829 * ---------------------------------------------------------------------------
830 * csr_sdio_linux_remove_irq
832 * Unregister the interrupt handler.
833 * This means that the linux layer can not process interrupts any more.
836 * sdio SDIO context pointer
839 * Status of the removal.
840 * ---------------------------------------------------------------------------
842 int csr_sdio_linux_remove_irq(CsrSdioFunction *function)
844 struct sdio_func *func = (struct sdio_func *)function->priv;
847 unifi_trace(NULL, UDBG1, "csr_sdio_linux_remove_irq\n");
849 sdio_claim_host(func);
850 r = sdio_release_irq(func);
851 sdio_release_host(func);
855 } /* csr_sdio_linux_remove_irq() */
859 * ---------------------------------------------------------------------------
860 * csr_sdio_linux_install_irq
862 * Register the interrupt handler.
863 * This means that the linux layer can process interrupts.
866 * sdio SDIO context pointer
869 * Status of the removal.
870 * ---------------------------------------------------------------------------
872 int csr_sdio_linux_install_irq(CsrSdioFunction *function)
874 struct sdio_func *func = (struct sdio_func *)function->priv;
877 unifi_trace(NULL, UDBG1, "csr_sdio_linux_install_irq\n");
879 /* Register our interrupt handle */
880 sdio_claim_host(func);
881 r = sdio_claim_irq(func, uf_glue_sdio_int_handler);
882 sdio_release_host(func);
884 /* If the interrupt was installed earlier, is fine */
889 } /* csr_sdio_linux_install_irq() */
894 * Power Management notifier
896 struct uf_sdio_mmc_pm_notifier
898 struct list_head list;
900 CsrSdioFunction *sdio_ctx;
901 struct notifier_block pm_notifier;
904 /* PM notifier list head */
905 static struct uf_sdio_mmc_pm_notifier uf_sdio_mmc_pm_notifiers = {
910 * ---------------------------------------------------------------------------
911 * uf_sdio_mmc_register_pm_notifier
912 * uf_sdio_mmc_unregister_pm_notifier
914 * Register/unregister for power management events. A list is used to
915 * allow multiple card instances to be supported.
918 * sdio_ctx - CSR SDIO context to associate PM notifier to
921 * Register function returns NULL on error
922 * ---------------------------------------------------------------------------
924 static struct uf_sdio_mmc_pm_notifier *
925 uf_sdio_mmc_register_pm_notifier(CsrSdioFunction *sdio_ctx)
927 /* Allocate notifier context for this card instance */
928 struct uf_sdio_mmc_pm_notifier *notifier_ctx = kmalloc(sizeof(struct uf_sdio_mmc_pm_notifier), GFP_KERNEL);
932 notifier_ctx->sdio_ctx = sdio_ctx;
933 notifier_ctx->pm_notifier.notifier_call = uf_sdio_mmc_power_event;
935 list_add(¬ifier_ctx->list, &uf_sdio_mmc_pm_notifiers.list);
937 if (register_pm_notifier(¬ifier_ctx->pm_notifier)) {
938 printk(KERN_ERR "unifi: register_pm_notifier failed\n");
946 uf_sdio_mmc_unregister_pm_notifier(CsrSdioFunction *sdio_ctx)
948 struct uf_sdio_mmc_pm_notifier *notifier_ctx;
949 struct list_head *node, *q;
951 list_for_each_safe(node, q, &uf_sdio_mmc_pm_notifiers.list) {
952 notifier_ctx = list_entry(node, struct uf_sdio_mmc_pm_notifier, list);
954 /* If it matches, unregister and free the notifier context */
955 if (notifier_ctx && notifier_ctx->sdio_ctx == sdio_ctx)
957 if (unregister_pm_notifier(¬ifier_ctx->pm_notifier)) {
958 printk(KERN_ERR "unifi: unregister_pm_notifier failed\n");
961 /* Remove from list */
962 notifier_ctx->sdio_ctx = NULL;
970 * ---------------------------------------------------------------------------
971 * uf_sdio_mmc_power_event
973 * Handler for power management events.
975 * We need to handle suspend/resume events while the userspace is unsuspended
976 * to allow the SME to run its suspend/resume state machines.
982 * Status of the event handling
983 * ---------------------------------------------------------------------------
986 uf_sdio_mmc_power_event(struct notifier_block *this, unsigned long event, void *ptr)
988 struct uf_sdio_mmc_pm_notifier *notifier_ctx = container_of(this,
989 struct uf_sdio_mmc_pm_notifier,
992 /* Call the CSR SDIO function driver's suspend/resume method
993 * while the userspace is unsuspended.
996 case PM_POST_HIBERNATION:
997 case PM_POST_SUSPEND:
998 printk(KERN_INFO "%s:%d resume\n", __FUNCTION__, __LINE__ );
999 if (sdio_func_drv && sdio_func_drv->resume) {
1000 sdio_func_drv->resume(notifier_ctx->sdio_ctx);
1004 case PM_HIBERNATION_PREPARE:
1005 case PM_SUSPEND_PREPARE:
1006 printk(KERN_INFO "%s:%d suspend\n", __FUNCTION__, __LINE__ );
1007 if (sdio_func_drv && sdio_func_drv->suspend) {
1008 sdio_func_drv->suspend(notifier_ctx->sdio_ctx);
1015 #endif /* CONFIG_PM */
1018 * ---------------------------------------------------------------------------
1019 * uf_glue_sdio_probe
1021 * Card insert callback.
1024 * func Our (glue layer) context pointer.
1027 * UniFi driver error code.
1028 * ---------------------------------------------------------------------------
1031 uf_glue_sdio_probe(struct sdio_func *func,
1032 const struct sdio_device_id *id)
1035 CsrSdioFunction *sdio_ctx;
1039 /* First of all claim the SDIO driver */
1040 sdio_claim_host(func);
1042 /* Assume that the card is already powered */
1043 card_is_powered = 1;
1045 /* Assumes one card per host, which is true for SDIO */
1046 instance = func->card->host->index;
1047 printk("sdio bus_id: %16s - UniFi card 0x%X inserted\n",
1048 sdio_func_id(func), instance);
1050 /* Allocate context */
1051 sdio_ctx = (CsrSdioFunction *)kmalloc(sizeof(CsrSdioFunction),
1053 if (sdio_ctx == NULL) {
1054 sdio_release_host(func);
1058 /* Initialise the context */
1059 sdio_ctx->sdioId.manfId = func->vendor;
1060 sdio_ctx->sdioId.cardId = func->device;
1061 sdio_ctx->sdioId.sdioFunction = func->num;
1062 sdio_ctx->sdioId.sdioInterface = func->class;
1063 sdio_ctx->blockSize = func->cur_blksize;
1064 sdio_ctx->priv = (void *)func;
1065 sdio_ctx->features = 0;
1067 /* Module parameter enables byte mode */
1068 if (sdio_byte_mode) {
1069 sdio_ctx->features |= CSR_SDIO_FEATURE_BYTE_MODE;
1072 if (func->card->host->caps & MMC_CAP_SD_HIGHSPEED) {
1073 unifi_trace(NULL, UDBG1, "MMC_CAP_SD_HIGHSPEED is available\n");
1076 #ifdef MMC_QUIRK_LENIENT_FN0
1077 func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
1080 /* Pass context to the SDIO driver */
1081 sdio_set_drvdata(func, sdio_ctx);
1084 /* Register to get PM events */
1085 if (uf_sdio_mmc_register_pm_notifier(sdio_ctx) == NULL) {
1086 unifi_error(NULL, "%s: Failed to register for PM events\n", __FUNCTION__);
1090 /* Register this device with the SDIO function driver */
1091 /* Call the main UniFi driver inserted handler */
1092 if (sdio_func_drv && sdio_func_drv->inserted) {
1093 uf_add_os_device(instance, &func->dev);
1094 sdio_func_drv->inserted(sdio_ctx);
1097 /* We have finished, so release the SDIO driver */
1098 sdio_release_host(func);
1100 #ifdef ANDROID_BUILD
1101 /* Take the wakelock */
1102 unifi_trace(NULL, UDBG1, "probe: take wake lock\n");
1103 wake_lock(&unifi_sdio_wake_lock);
1108 } /* uf_glue_sdio_probe() */
1112 * ---------------------------------------------------------------------------
1113 * uf_glue_sdio_remove
1115 * Card removal callback.
1118 * func Our (glue layer) context pointer.
1121 * UniFi driver error code.
1122 * ---------------------------------------------------------------------------
1125 uf_glue_sdio_remove(struct sdio_func *func)
1127 CsrSdioFunction *sdio_ctx;
1129 sdio_ctx = sdio_get_drvdata(func);
1136 unifi_info(NULL, "UniFi card removed\n");
1138 /* Clean up the SDIO function driver */
1139 if (sdio_func_drv && sdio_func_drv->removed) {
1140 uf_remove_os_device(func->card->host->index);
1141 sdio_func_drv->removed(sdio_ctx);
1145 /* Unregister for PM events */
1146 uf_sdio_mmc_unregister_pm_notifier(sdio_ctx);
1153 } /* uf_glue_sdio_remove */
1157 * SDIO ids *must* be statically declared, so we can't take
1158 * them from the list passed in csr_sdio_register_driver().
1160 static const struct sdio_device_id unifi_ids[] = {
1161 { SDIO_DEVICE(SDIO_MANF_ID_CSR,SDIO_CARD_ID_UNIFI_3) },
1162 { SDIO_DEVICE(SDIO_MANF_ID_CSR,SDIO_CARD_ID_UNIFI_4) },
1163 { /* end: all zeroes */ },
1166 MODULE_DEVICE_TABLE(sdio, unifi_ids);
1171 * ---------------------------------------------------------------------------
1172 * uf_glue_sdio_suspend
1174 * Card suspend callback. The userspace will already be suspended.
1177 * dev The struct device owned by the MMC driver
1181 * ---------------------------------------------------------------------------
1184 uf_glue_sdio_suspend(struct device *dev)
1188 unifi_trace(NULL, UDBG1, "uf_glue_sdio_suspend");
1192 } /* uf_glue_sdio_suspend */
1196 * ---------------------------------------------------------------------------
1197 * uf_glue_sdio_resume
1199 * Card resume callback. The userspace will still be suspended.
1202 * dev The struct device owned by the MMC driver
1206 * ---------------------------------------------------------------------------
1209 uf_glue_sdio_resume(struct device *dev)
1213 unifi_trace(NULL, UDBG1, "uf_glue_sdio_resume");
1215 #ifdef ANDROID_BUILD
1216 unifi_trace(NULL, UDBG1, "resume: take wakelock\n");
1217 wake_lock(&unifi_sdio_wake_lock);
1223 } /* uf_glue_sdio_resume */
1225 static struct dev_pm_ops unifi_pm_ops = {
1226 .suspend = uf_glue_sdio_suspend,
1227 .resume = uf_glue_sdio_resume,
1230 #define UNIFI_PM_OPS (&unifi_pm_ops)
1234 #define UNIFI_PM_OPS NULL
1236 #endif /* CONFIG_PM */
1238 static struct sdio_driver unifi_driver = {
1239 .probe = uf_glue_sdio_probe,
1240 .remove = uf_glue_sdio_remove,
1242 .id_table = unifi_ids,
1243 .drv.pm = UNIFI_PM_OPS,
1248 * ---------------------------------------------------------------------------
1249 * CsrSdioFunctionDriverRegister
1250 * CsrSdioFunctionDriverUnregister
1252 * These functions are called from the main module load and unload
1253 * functions. They perform the appropriate operations for the
1254 * linux MMC/SDIO driver.
1257 * sdio_drv Pointer to the function driver's SDIO structure.
1261 * ---------------------------------------------------------------------------
1264 CsrSdioFunctionDriverRegister(CsrSdioFunctionDriver *sdio_drv)
1268 printk("UniFi: Using native Linux MMC driver for SDIO.\n");
1270 if (sdio_func_drv) {
1271 unifi_error(NULL, "sdio_mmc: UniFi driver already registered\n");
1272 return CSR_SDIO_RESULT_INVALID_VALUE;
1275 #ifdef ANDROID_BUILD
1276 wake_lock_init(&unifi_sdio_wake_lock, WAKE_LOCK_SUSPEND, "unifi_sdio_work");
1279 /* Save the registered driver description */
1282 * Need a table here to handle a call to register for just one function.
1283 * mmc only allows us to register for the whole device
1285 sdio_func_drv = sdio_drv;
1288 /* Initialise PM notifier list */
1289 INIT_LIST_HEAD(&uf_sdio_mmc_pm_notifiers.list);
1292 /* Register ourself with mmc_core */
1293 r = sdio_register_driver(&unifi_driver);
1295 printk(KERN_ERR "unifi_sdio: Failed to register UniFi SDIO driver: %d\n", r);
1296 return ConvertSdioToCsrSdioResult(r);
1299 return CSR_RESULT_SUCCESS;
1300 } /* CsrSdioFunctionDriverRegister() */
1305 CsrSdioFunctionDriverUnregister(CsrSdioFunctionDriver *sdio_drv)
1307 printk(KERN_INFO "UniFi: unregister from MMC sdio\n");
1309 #ifdef ANDROID_BUILD
1310 wake_lock_destroy(&unifi_sdio_wake_lock);
1312 sdio_unregister_driver(&unifi_driver);
1314 sdio_func_drv = NULL;
1316 } /* CsrSdioFunctionDriverUnregister() */