2 * ---------------------------------------------------------------------------
6 * PURPOSE: Driver instantiation and deletion for SDIO on Linux.
8 * This file brings together the SDIO bus interface, the UniFi
9 * driver core and the Linux net_device stack.
11 * Copyright (C) 2007-2009 by Cambridge Silicon Radio Ltd.
13 * Refer to LICENSE.txt included with this source code for details on
16 * ---------------------------------------------------------------------------
18 #include <linux/kmod.h>
19 #include <linux/init.h>
20 #include <linux/suspend.h>
21 #include "csr_wifi_hip_unifi.h"
22 #include "unifi_priv.h"
24 #include "sdioemb/sdio_api.h"
26 /* The function driver context, i.e the UniFi Driver */
27 static CsrSdioFunctionDriver *sdio_func_drv;
30 static int uf_sdio_emb_power_event(struct notifier_block *this, unsigned long event, void *ptr);
33 /* The Android wakelock is here for completeness. Typically the MMC driver is used
34 * instead of sdioemb, but sdioemb may be used for CSPI.
37 struct wake_lock unifi_sdio_wake_lock; /* wakelock to prevent suspend while resuming */
40 /* sdioemb driver uses POSIX error codes */
42 ConvertSdioToCsrSdioResult(int r)
44 CsrResult csrResult = CSR_RESULT_FAILURE;
48 csrResult = CSR_RESULT_SUCCESS;
51 csrResult = CSR_SDIO_RESULT_CRC_ERROR;
56 csrResult = CSR_SDIO_RESULT_TIMEOUT;
60 csrResult = CSR_SDIO_RESULT_NO_DEVICE;
63 csrResult = CSR_SDIO_RESULT_INVALID_VALUE;
70 csrResult = CSR_RESULT_FAILURE;
73 unifi_warning(NULL, "Unrecognised SDIO error code: %d\n", r);
82 CsrSdioRead8(CsrSdioFunction *function, CsrUint32 address, CsrUint8 *data)
84 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
86 err = sdioemb_read8(fdev, address, data);
88 return ConvertSdioToCsrSdioResult(err);
90 return CSR_RESULT_SUCCESS;
91 } /* CsrSdioRead8() */
94 CsrSdioWrite8(CsrSdioFunction *function, CsrUint32 address, CsrUint8 data)
96 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
98 err = sdioemb_write8(fdev, address, data);
100 return ConvertSdioToCsrSdioResult(err);
102 return CSR_RESULT_SUCCESS;
103 } /* CsrSdioWrite8() */
106 CsrSdioRead16(CsrSdioFunction *function, CsrUint32 address, CsrUint16 *data)
108 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
111 r = sdioemb_read16(fdev, address, data);
113 return ConvertSdioToCsrSdioResult(r);
116 return CSR_RESULT_SUCCESS;
117 } /* CsrSdioRead16() */
120 CsrSdioWrite16(CsrSdioFunction *function, CsrUint32 address, CsrUint16 data)
122 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
125 r = sdioemb_write16(fdev, address, data);
127 return ConvertSdioToCsrSdioResult(r);
130 return CSR_RESULT_SUCCESS;
131 } /* CsrSdioWrite16() */
135 CsrSdioF0Read8(CsrSdioFunction *function, CsrUint32 address, CsrUint8 *data)
137 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
139 err = sdioemb_f0_read8(fdev, address, data);
141 return ConvertSdioToCsrSdioResult(err);
143 return CSR_RESULT_SUCCESS;
144 } /* CsrSdioF0Read8() */
148 CsrSdioF0Write8(CsrSdioFunction *function, CsrUint32 address, CsrUint8 data)
150 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
152 err = sdioemb_f0_write8(fdev, address, data);
154 return ConvertSdioToCsrSdioResult(err);
156 return CSR_RESULT_SUCCESS;
157 } /* CsrSdioF0Write8() */
160 CsrSdioRead(CsrSdioFunction *function, CsrUint32 address, void *data, CsrUint32 length)
162 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
164 err = sdioemb_read(fdev, address, data, length);
166 return ConvertSdioToCsrSdioResult(err);
168 return CSR_RESULT_SUCCESS;
169 } /* CsrSdioRead() */
172 CsrSdioWrite(CsrSdioFunction *function, CsrUint32 address, const void *data, CsrUint32 length)
174 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
176 err = sdioemb_write(fdev, address, data, length);
178 return ConvertSdioToCsrSdioResult(err);
180 return CSR_RESULT_SUCCESS;
181 } /* CsrSdioWrite() */
185 CsrSdioBlockSizeSet(CsrSdioFunction *function, CsrUint16 blockSize)
187 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
190 /* Module parameter overrides */
191 if (sdio_block_size > -1) {
192 blockSize = sdio_block_size;
195 unifi_trace(NULL, UDBG1, "Set SDIO function block size to %d\n",
198 r = sdioemb_set_block_size(fdev, blockSize);
200 unifi_error(NULL, "Error %d setting block size\n", r);
203 /* Determine the achieved block size to report to the core */
204 function->blockSize = fdev->blocksize;
206 return ConvertSdioToCsrSdioResult(r);
207 } /* CsrSdioBlockSizeSet() */
211 * ---------------------------------------------------------------------------
212 * CsrSdioMaxBusClockFrequencySet
214 * Set the maximum SDIO bus clock speed to use.
217 * sdio SDIO context pointer
218 * maxFrequency maximum clock speed in Hz
222 * ---------------------------------------------------------------------------
225 CsrSdioMaxBusClockFrequencySet(CsrSdioFunction *function, CsrUint32 maxFrequency)
227 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
228 CsrUint32 max_khz = maxFrequency/1000;
230 if (!max_khz || max_khz > sdio_clock) {
231 max_khz = sdio_clock;
233 unifi_trace(NULL, UDBG1, "Setting SDIO bus clock to %d kHz\n", max_khz);
234 sdioemb_set_max_bus_freq(fdev, 1000 * max_khz);
236 return CSR_RESULT_SUCCESS;
237 } /* CsrSdioMaxBusClockFrequencySet() */
241 * ---------------------------------------------------------------------------
242 * CsrSdioInterruptEnable
243 * CsrSdioInterruptDisable
245 * Enable or disable the SDIO interrupt.
248 * sdio SDIO context pointer
251 * Zero on success or a UniFi driver error code.
252 * ---------------------------------------------------------------------------
255 CsrSdioInterruptEnable(CsrSdioFunction *function)
257 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
260 r = sdioemb_interrupt_enable(fdev);
262 return ConvertSdioToCsrSdioResult(r);
265 return CSR_RESULT_SUCCESS;
266 } /* CsrSdioInterruptEnable() */
269 CsrSdioInterruptDisable(CsrSdioFunction *function)
271 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
274 r = sdioemb_interrupt_disable(fdev);
276 return ConvertSdioToCsrSdioResult(r);
279 return CSR_RESULT_SUCCESS;
280 } /* CsrSdioInterruptDisable() */
284 * ---------------------------------------------------------------------------
285 * CsrSdioInterruptAcknowledge
287 * Acknowledge an SDIO interrupt.
290 * sdio SDIO context pointer
293 * Zero on success or a UniFi driver error code.
294 * ---------------------------------------------------------------------------
296 void CsrSdioInterruptAcknowledge(CsrSdioFunction *function)
298 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
300 sdioemb_interrupt_acknowledge(fdev);
301 } /* CsrSdioInterruptAcknowledge() */
305 * ---------------------------------------------------------------------------
306 * CsrSdioFunctionEnable
308 * Enable i/o on this function.
311 * sdio SDIO context pointer
314 * UniFi driver error code.
315 * ---------------------------------------------------------------------------
318 CsrSdioFunctionEnable(CsrSdioFunction *function)
320 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
323 /* Enable UniFi function (the 802.11 part). */
324 r = sdioemb_enable_function(fdev);
326 unifi_error(NULL, "Failed to enable SDIO function %d\n", fdev->function);
327 return ConvertSdioToCsrSdioResult(r);
329 return CSR_RESULT_SUCCESS;
330 } /* CsrSdioFunctionEnable() */
334 * ---------------------------------------------------------------------------
335 * CsrSdioFunctionDisable
337 * Disable i/o on this function.
340 * sdio SDIO context pointer
343 * UniFi driver error code.
344 * ---------------------------------------------------------------------------
347 CsrSdioFunctionDisable(CsrSdioFunction *function)
349 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
352 /* Disable UniFi function (the 802.11 part). */
353 r = sdioemb_disable_function(fdev);
355 unifi_error(NULL, "Failed to disable SDIO function %d\n", fdev->function);
356 return ConvertSdioToCsrSdioResult(r);
358 return CSR_RESULT_SUCCESS;
359 } /* CsrSdioFunctionDisable() */
363 * ---------------------------------------------------------------------------
364 * CsrSdioFunctionActive
366 * No-op as the bus goes to an active state at the start of every
370 * sdio SDIO context pointer
371 * ---------------------------------------------------------------------------
374 CsrSdioFunctionActive(CsrSdioFunction *function)
376 } /* CsrSdioFunctionActive() */
379 * ---------------------------------------------------------------------------
380 * CsrSdioFunctionIdle
382 * Set the function as idle.
385 * sdio SDIO context pointer
386 * ---------------------------------------------------------------------------
389 CsrSdioFunctionIdle(CsrSdioFunction *function)
391 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
393 sdioemb_idle_function(fdev);
394 } /* CsrSdioFunctionIdle() */
398 CsrSdioPowerOn(CsrSdioFunction *function)
400 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
402 if (disable_power_control != 1) {
403 sdioemb_power_on(fdev);
406 return CSR_RESULT_SUCCESS;
407 } /* CsrSdioPowerOn() */
410 CsrSdioPowerOff(CsrSdioFunction *function)
412 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
413 if (disable_power_control != 1) {
414 sdioemb_power_off(fdev);
416 } /* CsrSdioPowerOff() */
420 * ---------------------------------------------------------------------------
423 * Hard Resets UniFi is possible.
426 * sdio SDIO context pointer
429 * 1 if the SDIO driver is not capable of doing a hard reset.
430 * 0 if a hard reset was successfully performed.
431 * -CSR_EIO if an I/O error occured while re-initializing the card.
432 * This is a fatal, non-recoverable error.
433 * -CSR_ENODEV if the card is no longer present.
434 * ---------------------------------------------------------------------------
437 CsrSdioHardReset(CsrSdioFunction *function)
439 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
442 /* Hard reset can be disabled by a module parameter */
444 if (disable_hw_reset != 1) {
445 r = sdioemb_hard_reset(fdev); /* may return 1 if can't reset */
447 return ConvertSdioToCsrSdioResult(r); /* fatal error */
451 /* Set the SDIO bus width after a hard reset */
453 unifi_info(NULL, "Setting SDIO bus width to 1\n");
454 sdioemb_set_bus_width(fdev, buswidth);
455 } else if (buswidth == 4) {
456 unifi_info(NULL, "Setting SDIO bus width to 4\n");
457 sdioemb_set_bus_width(fdev, buswidth);
462 return CSR_SDIO_RESULT_NOT_RESET;
465 return ConvertSdioToCsrSdioResult(r);
467 } /* CsrSdioHardReset() */
470 int csr_sdio_linux_remove_irq(CsrSdioFunction *function)
472 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
474 return sdioemb_interrupt_disable(fdev);
477 int csr_sdio_linux_install_irq(CsrSdioFunction *function)
479 struct sdioemb_dev *fdev = (struct sdioemb_dev *)function->priv;
481 return sdioemb_interrupt_enable(fdev);
486 * ---------------------------------------------------------------------------
487 * uf_glue_sdio_int_handler
488 * Card interrupt callback.
491 * fdev SDIO context pointer
495 * ---------------------------------------------------------------------------
498 uf_glue_sdio_int_handler(struct sdioemb_dev *fdev)
500 CsrSdioFunction *sdio_ctx = fdev->drv_data;
501 CsrSdioInterruptDsrCallback func_dsr_callback;
503 /* If the function driver has registered a handler, call it */
504 if (sdio_func_drv && sdio_func_drv->intr) {
505 /* The function driver may return a DSR. */
506 func_dsr_callback = sdio_func_drv->intr(sdio_ctx);
507 /* If it did return a DSR handle, call it */
508 if (func_dsr_callback) {
509 func_dsr_callback(sdio_ctx);
517 * Power Management notifier
519 struct uf_sdio_emb_pm_notifier
521 struct list_head list;
523 CsrSdioFunction *sdio_ctx;
524 struct notifier_block pm_notifier;
527 /* PM notifier list head */
528 static struct uf_sdio_emb_pm_notifier uf_sdio_emb_pm_notifiers = {
533 * ---------------------------------------------------------------------------
534 * uf_sdio_emb_register_pm_notifier
535 * uf_sdio_emb_unregister_pm_notifier
537 * Register/unregister for power management events. A list is used to
538 * allow multiple card instances to be supported.
541 * sdio_ctx - CSR SDIO context to associate PM notifier to
544 * Register function returns NULL on error
545 * ---------------------------------------------------------------------------
547 static struct uf_sdio_emb_pm_notifier *
548 uf_sdio_emb_register_pm_notifier(CsrSdioFunction *sdio_ctx)
550 /* Allocate notifier context for this card instance */
551 struct uf_sdio_emb_pm_notifier *notifier_ctx = kmalloc(sizeof(struct uf_sdio_emb_pm_notifier), GFP_KERNEL);
555 notifier_ctx->sdio_ctx = sdio_ctx;
556 notifier_ctx->pm_notifier.notifier_call = uf_sdio_emb_power_event;
558 list_add(¬ifier_ctx->list, &uf_sdio_emb_pm_notifiers.list);
560 if (register_pm_notifier(¬ifier_ctx->pm_notifier)) {
561 printk(KERN_ERR "unifi: register_pm_notifier failed\n");
569 uf_sdio_emb_unregister_pm_notifier(CsrSdioFunction *sdio_ctx)
571 struct uf_sdio_emb_pm_notifier *notifier_ctx;
572 struct list_head *node, *q;
574 list_for_each_safe(node, q, &uf_sdio_emb_pm_notifiers.list) {
575 notifier_ctx = list_entry(node, struct uf_sdio_emb_pm_notifier, list);
577 /* If it matches, unregister and free the notifier context */
578 if (notifier_ctx && notifier_ctx->sdio_ctx == sdio_ctx)
580 if (unregister_pm_notifier(¬ifier_ctx->pm_notifier)) {
581 printk(KERN_ERR "unifi: unregister_pm_notifier failed\n");
584 /* Remove from list */
585 notifier_ctx->sdio_ctx = NULL;
593 * ---------------------------------------------------------------------------
594 * uf_sdio_emb_power_event
596 * Handler for power management events.
598 * We need to handle suspend/resume events while the userspace is unsuspended
599 * to allow the SME to run its suspend/resume state machines.
605 * Status of the event handling
606 * ---------------------------------------------------------------------------
609 uf_sdio_emb_power_event(struct notifier_block *this, unsigned long event, void *ptr)
611 struct uf_sdio_emb_pm_notifier *notifier_ctx = container_of(this,
612 struct uf_sdio_emb_pm_notifier,
615 /* Call the CSR SDIO function driver's suspend/resume method
616 * while the userspace is unsuspended.
619 case PM_POST_HIBERNATION:
620 case PM_POST_SUSPEND:
621 printk(KERN_INFO "%s:%d resume\n", __FUNCTION__, __LINE__ );
622 if (sdio_func_drv && sdio_func_drv->resume) {
623 sdio_func_drv->resume(notifier_ctx->sdio_ctx);
627 case PM_HIBERNATION_PREPARE:
628 case PM_SUSPEND_PREPARE:
629 printk(KERN_INFO "%s:%d suspend\n", __FUNCTION__, __LINE__ );
630 if (sdio_func_drv && sdio_func_drv->suspend) {
631 sdio_func_drv->suspend(notifier_ctx->sdio_ctx);
638 #endif /* CONFIG_PM */
641 * ---------------------------------------------------------------------------
644 * Card insert callback.
647 * fdev SDIO context pointer
650 * UniFi driver error code.
651 * ---------------------------------------------------------------------------
654 uf_glue_sdio_probe(struct sdioemb_dev *fdev)
656 CsrSdioFunction *sdio_ctx;
658 unifi_info(NULL, "UniFi card inserted\n");
660 /* Allocate context and private in one lump */
661 sdio_ctx = (CsrSdioFunction *)kmalloc(sizeof(CsrSdioFunction),
663 if (sdio_ctx == NULL) {
668 sdio_ctx->sdioId.manfId = fdev->vendor_id;
669 sdio_ctx->sdioId.cardId = fdev->device_id;
670 sdio_ctx->sdioId.sdioFunction = fdev->function;
671 sdio_ctx->sdioId.sdioInterface = 0;
672 sdio_ctx->blockSize = fdev->blocksize;
673 sdio_ctx->priv = (void *)fdev;
674 sdio_ctx->features = 0;
676 /* Module parameter enables byte mode */
677 if (sdio_byte_mode) {
678 sdio_ctx->features |= CSR_SDIO_FEATURE_BYTE_MODE;
681 /* Set up pointer to func_priv in middle of lump */
682 fdev->drv_data = sdio_ctx;
684 /* Always override default SDIO bus clock */
685 unifi_trace(NULL, UDBG1, "Setting SDIO bus clock to %d kHz\n", sdio_clock);
686 sdioemb_set_max_bus_freq(fdev, 1000 * sdio_clock);
689 /* Register to get PM events */
690 if (uf_sdio_emb_register_pm_notifier(sdio_ctx) == NULL) {
691 unifi_error(NULL, "%s: Failed to register for PM events\n", __FUNCTION__);
695 /* Call the main UniFi driver inserted handler */
696 if (sdio_func_drv && sdio_func_drv->inserted) {
697 uf_add_os_device(fdev->slot_id, fdev->os_device);
698 sdio_func_drv->inserted(sdio_ctx);
702 /* Take the wakelock */
703 unifi_trace(NULL, UDBG1, "emb probe: take wake lock\n");
704 wake_lock(&unifi_sdio_wake_lock);
708 } /* uf_glue_sdio_probe() */
713 * ---------------------------------------------------------------------------
716 * Card removal callback.
722 * UniFi driver error code.
723 * ---------------------------------------------------------------------------
726 uf_sdio_remove(struct sdioemb_dev *fdev)
728 CsrSdioFunction *sdio_ctx = fdev->drv_data;
730 unifi_info(NULL, "UniFi card removed\n");
732 /* Clean up the SDIO function driver */
733 if (sdio_func_drv && sdio_func_drv->removed) {
734 sdio_func_drv->removed(sdio_ctx);
738 /* Unregister for PM events */
739 uf_sdio_emb_unregister_pm_notifier(sdio_ctx);
744 } /* uf_sdio_remove */
748 * ---------------------------------------------------------------------------
749 * uf_glue_sdio_suspend
751 * System suspend callback.
758 * ---------------------------------------------------------------------------
761 uf_glue_sdio_suspend(struct sdioemb_dev *fdev)
763 unifi_info(NULL, "Suspending...\n");
765 } /* uf_glue_sdio_suspend() */
769 * ---------------------------------------------------------------------------
770 * uf_glue_sdio_resume
772 * System resume callback.
779 * ---------------------------------------------------------------------------
782 uf_glue_sdio_resume(struct sdioemb_dev *fdev)
784 unifi_info(NULL, "Resuming...\n");
787 unifi_trace(NULL, UDBG1, "emb resume: take wakelock\n");
788 wake_lock(&unifi_sdio_wake_lock);
791 } /* uf_glue_sdio_resume() */
796 static struct sdioemb_func_driver unifi_sdioemb = {
798 .id_table = NULL, /* Filled in when main driver registers */
800 .probe = uf_glue_sdio_probe,
801 .remove = uf_sdio_remove,
802 .card_int_handler = uf_glue_sdio_int_handler,
803 .suspend = uf_glue_sdio_suspend,
804 .resume = uf_glue_sdio_resume,
809 * ---------------------------------------------------------------------------
810 * CsrSdioFunctionDriverRegister
811 * CsrSdioFunctionDriverUnregister
813 * These functions are called from the main module load and unload
814 * functions. They perform the appropriate operations for the
822 * ---------------------------------------------------------------------------
825 CsrSdioFunctionDriverRegister(CsrSdioFunctionDriver *sdio_drv)
830 printk("Unifi: Using CSR embedded SDIO driver\n");
833 unifi_error(NULL, "sdio_emb: UniFi driver already registered\n");
834 return CSR_SDIO_RESULT_INVALID_VALUE;
837 /* Build ID table to pass to sdioemb */
838 unifi_sdioemb.id_table = CsrPmemAlloc(sizeof(struct sdioemb_id_table) * (sdio_drv->idsCount + 1));
839 if (unifi_sdioemb.id_table == NULL) {
840 unifi_error(NULL, "sdio_emb: Failed to allocate memory for ID table (%d IDs)\n", sdio_drv->idsCount);
841 return CSR_RESULT_FAILURE;
843 for (i = 0; i < sdio_drv->idsCount; i++) {
844 unifi_sdioemb.id_table[i].vendor_id = sdio_drv->ids[i].manfId;
845 unifi_sdioemb.id_table[i].device_id = sdio_drv->ids[i].cardId;
846 unifi_sdioemb.id_table[i].function = sdio_drv->ids[i].sdioFunction;
847 unifi_sdioemb.id_table[i].interface = sdio_drv->ids[i].sdioInterface;
849 unifi_sdioemb.id_table[i].vendor_id = 0;
850 unifi_sdioemb.id_table[i].device_id = 0;
851 unifi_sdioemb.id_table[i].function = 0;
852 unifi_sdioemb.id_table[i].interface = 0;
854 /* Save the registered driver description */
855 sdio_func_drv = sdio_drv;
858 /* Initialise PM notifier list */
859 INIT_LIST_HEAD(&uf_sdio_emb_pm_notifiers.list);
863 wake_lock_init(&unifi_sdio_wake_lock, WAKE_LOCK_SUSPEND, "unifi_sdio_work");
866 /* Register ourself with sdioemb */
867 r = sdioemb_driver_register(&unifi_sdioemb);
869 unifi_error(NULL, "Failed to register UniFi SDIO driver: %d\n", r);
870 return ConvertSdioToCsrSdioResult(r);
873 return CSR_RESULT_SUCCESS;
874 } /* CsrSdioFunctionDriverRegister() */
878 CsrSdioFunctionDriverUnregister(CsrSdioFunctionDriver *sdio_drv)
880 sdioemb_driver_unregister(&unifi_sdioemb);
883 wake_lock_destroy(&unifi_sdio_wake_lock);
886 sdio_func_drv = NULL;
888 CsrPmemFree(unifi_sdioemb.id_table);
889 unifi_sdioemb.id_table = NULL;
890 } /* CsrSdioFunctionDriverUnregister() */