Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sdio_mmc.c
Go to the documentation of this file.
1 /*
2  * ---------------------------------------------------------------------------
3  *
4  * FILE: sdio_mmc.c
5  *
6  * PURPOSE: SDIO driver interface for generic MMC stack.
7  *
8  * Copyright (C) 2008-2009 by Cambridge Silicon Radio Ltd.
9  *
10  * ---------------------------------------------------------------------------
11  */
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>
25 
26 #include "unifi_priv.h"
27 
28 #ifdef ANDROID_BUILD
29 struct wake_lock unifi_sdio_wake_lock; /* wakelock to prevent suspend while resuming */
30 #endif
31 
32 static CsrSdioFunctionDriver *sdio_func_drv;
33 
34 #ifdef CONFIG_PM
35 static int uf_sdio_mmc_power_event(struct notifier_block *this, unsigned long event, void *ptr);
36 #endif
37 
38 /*
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).
45  */
46 static int card_is_powered = 1;
47 
48 /* MMC uses ENOMEDIUM to indicate card gone away */
49 
50 static CsrResult
51 ConvertSdioToCsrSdioResult(int r)
52 {
53  CsrResult csrResult = CSR_RESULT_FAILURE;
54 
55  switch (r) {
56  case 0:
57  csrResult = CSR_RESULT_SUCCESS;
58  break;
59  case -EIO:
60  case -EILSEQ:
61  csrResult = CSR_SDIO_RESULT_CRC_ERROR;
62  break;
63  /* Timeout errors */
64  case -ETIMEDOUT:
65  case -EBUSY:
66  csrResult = CSR_SDIO_RESULT_TIMEOUT;
67  break;
68  case -ENODEV:
69  case -ENOMEDIUM:
70  csrResult = CSR_SDIO_RESULT_NO_DEVICE;
71  break;
72  case -EINVAL:
74  break;
75  case -ENOMEM:
76  case -ENOSYS:
77  case -ERANGE:
78  case -ENXIO:
79  csrResult = CSR_RESULT_FAILURE;
80  break;
81  default:
82  unifi_warning(NULL, "Unrecognised SDIO error code: %d\n", r);
83  break;
84  }
85 
86  return csrResult;
87 }
88 
89 
90 static int
91 csr_io_rw_direct(struct mmc_card *card, int write, uint8_t fn,
93 {
94  struct mmc_command cmd;
95  int err;
96 
97  BUG_ON(!card);
98  BUG_ON(fn > 7);
99 
100  memset(&cmd, 0, sizeof(struct mmc_command));
101 
102  cmd.opcode = SD_IO_RW_DIRECT;
103  cmd.arg = write ? 0x80000000 : 0x00000000;
104  cmd.arg |= fn << 28;
105  cmd.arg |= (write && out) ? 0x08000000 : 0x00000000;
106  cmd.arg |= addr << 9;
107  cmd.arg |= in;
109 
110  err = mmc_wait_for_cmd(card->host, &cmd, 0);
111  if (err)
112  return err;
113 
114  /* this function is not exported, so we will need to sort it out here
115  * for now, lets hard code it to sdio */
116  if (0) {
117  /* old arg (mmc_host_is_spi(card->host)) { */
118  /* host driver already reported errors */
119  } else {
120  if (cmd.resp[0] & R5_ERROR) {
121  printk(KERN_ERR "%s: r5 error 0x%02x\n",
122  __FUNCTION__, cmd.resp[0]);
123  return -EIO;
124  }
125  if (cmd.resp[0] & R5_FUNCTION_NUMBER)
126  return -EINVAL;
127  if (cmd.resp[0] & R5_OUT_OF_RANGE)
128  return -ERANGE;
129  }
130 
131  if (out) {
132  if (0) { /* old argument (mmc_host_is_spi(card->host)) */
133  *out = (cmd.resp[0] >> 8) & 0xFF;
134  }
135  else {
136  *out = cmd.resp[0] & 0xFF;
137  }
138  }
139 
140  return CSR_RESULT_SUCCESS;
141 }
142 
143 
144 CsrResult
146 {
147  struct sdio_func *func = (struct sdio_func *)function->priv;
148  int err = 0;
149 
150  _sdio_claim_host(func);
151  *data = sdio_readb(func, address, &err);
152  _sdio_release_host(func);
153 
154  if (err) {
155  func_exit_r(err);
156  return ConvertSdioToCsrSdioResult(err);
157  }
158 
159  return CSR_RESULT_SUCCESS;
160 } /* CsrSdioRead8() */
161 
162 CsrResult
164 {
165  struct sdio_func *func = (struct sdio_func *)function->priv;
166  int err = 0;
167 
168  _sdio_claim_host(func);
169  sdio_writeb(func, data, address, &err);
170  _sdio_release_host(func);
171 
172  if (err) {
173  func_exit_r(err);
174  return ConvertSdioToCsrSdioResult(err);
175  }
176 
177  return CSR_RESULT_SUCCESS;
178 } /* CsrSdioWrite8() */
179 
180 CsrResult
182 {
183  struct sdio_func *func = (struct sdio_func *)function->priv;
184  int err;
185  uint8_t b0, b1;
186 
187  _sdio_claim_host(func);
188  b0 = sdio_readb(func, address, &err);
189  if (err) {
190  _sdio_release_host(func);
191  return ConvertSdioToCsrSdioResult(err);
192  }
193 
194  b1 = sdio_readb(func, address+1, &err);
195  if (err) {
196  _sdio_release_host(func);
197  return ConvertSdioToCsrSdioResult(err);
198  }
199  _sdio_release_host(func);
200 
201  *data = ((uint16_t)b1 << 8) | b0;
202 
203  return CSR_RESULT_SUCCESS;
204 } /* CsrSdioRead16() */
205 
206 
207 CsrResult
209 {
210  struct sdio_func *func = (struct sdio_func *)function->priv;
211  int err;
212  uint8_t b0, b1;
213 
214  _sdio_claim_host(func);
215  b1 = (data >> 8) & 0xFF;
216  sdio_writeb(func, b1, address+1, &err);
217  if (err) {
218  _sdio_release_host(func);
219  return ConvertSdioToCsrSdioResult(err);
220  }
221 
222  b0 = data & 0xFF;
223  sdio_writeb(func, b0, address, &err);
224  if (err) {
225  _sdio_release_host(func);
226  return ConvertSdioToCsrSdioResult(err);
227  }
228 
229  _sdio_release_host(func);
230  return CSR_RESULT_SUCCESS;
231 } /* CsrSdioWrite16() */
232 
233 
234 CsrResult
236 {
237  struct sdio_func *func = (struct sdio_func *)function->priv;
238  int err = 0;
239 
240  _sdio_claim_host(func);
241 #ifdef MMC_QUIRK_LENIENT_FN0
242  *data = sdio_f0_readb(func, address, &err);
243 #else
244  err = csr_io_rw_direct(func->card, 0, 0, address, 0, data);
245 #endif
246  _sdio_release_host(func);
247 
248  if (err) {
249  func_exit_r(err);
250  return ConvertSdioToCsrSdioResult(err);
251  }
252 
253  return CSR_RESULT_SUCCESS;
254 } /* CsrSdioF0Read8() */
255 
256 CsrResult
258 {
259  struct sdio_func *func = (struct sdio_func *)function->priv;
260  int err = 0;
261 
262  _sdio_claim_host(func);
263 #ifdef MMC_QUIRK_LENIENT_FN0
264  sdio_f0_writeb(func, data, address, &err);
265 #else
266  err = csr_io_rw_direct(func->card, 1, 0, address, data, NULL);
267 #endif
268  _sdio_release_host(func);
269 
270  if (err) {
271  func_exit_r(err);
272  return ConvertSdioToCsrSdioResult(err);
273  }
274 
275  return CSR_RESULT_SUCCESS;
276 } /* CsrSdioF0Write8() */
277 
278 
279 CsrResult
281 {
282  struct sdio_func *func = (struct sdio_func *)function->priv;
283  int err;
284 
285  _sdio_claim_host(func);
286  err = sdio_readsb(func, data, address, length);
287  _sdio_release_host(func);
288 
289  if (err) {
290  func_exit_r(err);
291  return ConvertSdioToCsrSdioResult(err);
292  }
293 
294  return CSR_RESULT_SUCCESS;
295 } /* CsrSdioRead() */
296 
297 CsrResult
299 {
300  struct sdio_func *func = (struct sdio_func *)function->priv;
301  int err;
302 
303  _sdio_claim_host(func);
304  err = sdio_writesb(func, address, (void*)data, length);
305  _sdio_release_host(func);
306 
307  if (err) {
308  func_exit_r(err);
309  return ConvertSdioToCsrSdioResult(err);
310  }
311 
312  return CSR_RESULT_SUCCESS;
313 } /* CsrSdioWrite() */
314 
315 
316 static int
317 csr_sdio_enable_hs(struct mmc_card *card)
318 {
319  int ret;
320  u8 speed;
321 
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.
326  */
327  printk(KERN_INFO "unifi: request HS but not MMC_CAP_SD_HIGHSPEED");
328  return 0;
329  }
330 
331  if (!card->cccr.high_speed)
332  return 0;
333 
334 #if 1
335  ret = csr_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
336  if (ret)
337  return ret;
338 
339  speed |= SDIO_SPEED_EHS;
340 #else
341  /* Optimisation: Eliminate read by always assuming SHS and that reserved bits can be zero */
342  speed = SDIO_SPEED_EHS | SDIO_SPEED_SHS;
343 #endif
344 
345  ret = csr_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
346  if (ret)
347  return ret;
348 
350  card->host->ios.timing = MMC_TIMING_SD_HS;
351  card->host->ops->set_ios(card->host, &card->host->ios);
352 
353  return 0;
354 }
355 
356 static int
357 csr_sdio_disable_hs(struct mmc_card *card)
358 {
359  int ret;
360  u8 speed;
361 
362  if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
363  return 0;
364 
365  if (!card->cccr.high_speed)
366  return 0;
367 #if 1
368  ret = csr_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
369  if (ret)
370  return ret;
371 
372  speed &= ~SDIO_SPEED_EHS;
373 #else
374  /* Optimisation: Eliminate read by always assuming SHS and that reserved bits can be zero */
375  speed = SDIO_SPEED_SHS; /* clear SDIO_SPEED_EHS */
376 #endif
377 
378  ret = csr_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
379  if (ret)
380  return ret;
381 
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);
385 
386  return 0;
387 }
388 
389 
390 /*
391  * ---------------------------------------------------------------------------
392  * CsrSdioMaxBusClockFrequencySet
393  *
394  * Set the maximum SDIO bus clock speed to use.
395  *
396  * Arguments:
397  * sdio SDIO context pointer
398  * maxFrequency maximum clock speed in Hz
399  *
400  * Returns:
401  * an error code.
402  * ---------------------------------------------------------------------------
403  */
404 CsrResult
406 {
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;
410  unsigned int max_hz;
411  int err;
412  u32 max_khz = maxFrequency/1000;
413 
414  if (!max_khz || max_khz > sdio_clock) {
415  max_khz = sdio_clock;
416  }
417 
418  _sdio_claim_host(func);
419  max_hz = 1000 * max_khz;
420  if (max_hz > host->f_max) {
421  max_hz = host->f_max;
422  }
423 
424  if (max_hz > 25000000) {
425  err = csr_sdio_enable_hs(func->card);
426  } else {
427  err = csr_sdio_disable_hs(func->card);
428  }
429  if (err) {
430  printk(KERN_ERR "SDIO warning: Failed to configure SDIO clock mode\n");
431  _sdio_release_host(func);
432  return CSR_RESULT_SUCCESS;
433  }
434 
435  ios->clock = max_hz;
436  host->ops->set_ios(host, ios);
437 
438  _sdio_release_host(func);
439 
440  return CSR_RESULT_SUCCESS;
441 } /* CsrSdioMaxBusClockFrequencySet() */
442 
443 
444 /*
445  * ---------------------------------------------------------------------------
446  * CsrSdioInterruptEnable
447  * CsrSdioInterruptDisable
448  *
449  * Enable or disable the SDIO interrupt.
450  * The driver disables the SDIO interrupt until the i/o thread can
451  * process it.
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.
456  *
457  * Arguments:
458  * sdio SDIO context pointer
459  *
460  * Returns:
461  * Zero on success or a UniFi driver error code.
462  *
463  * ---------------------------------------------------------------------------
464  */
465 CsrResult
467 {
468  struct sdio_func *func = (struct sdio_func *)function->priv;
469  int err = 0;
470 
471 #ifdef CSR_CONFIG_MMC_INT_BYPASS_KSOFTIRQD
472  sdio_unblock_card_irq(func);
473 #else
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);
478 #else
479  err = csr_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, 0x03, NULL);
480 #endif
481  _sdio_release_host(func);
482 
483  func_exit();
484  if (err) {
485  printk(KERN_ERR "unifi: %s: error %d writing IENx\n", __FUNCTION__, err);
486  return ConvertSdioToCsrSdioResult(err);
487  }
488 #endif
489  return CSR_RESULT_SUCCESS;
490 } /* CsrSdioInterruptEnable() */
491 
492 CsrResult
494 {
495  struct sdio_func *func = (struct sdio_func *)function->priv;
496  int err = 0;
497 
498 #ifdef CSR_CONFIG_MMC_INT_BYPASS_KSOFTIRQD
499  sdio_block_card_irq(func);
500 #else
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);
505 #else
506  err = csr_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, 0x00, NULL);
507 #endif
508  _sdio_release_host(func);
509 
510  func_exit();
511  if (err) {
512  printk(KERN_ERR "unifi: %s: error %d writing IENx\n", __FUNCTION__, err);
513  return ConvertSdioToCsrSdioResult(err);
514  }
515 #endif
516  return CSR_RESULT_SUCCESS;
517 } /* CsrSdioInterruptDisable() */
518 
519 
521 {
522 }
523 
524 
525 /*
526  * ---------------------------------------------------------------------------
527  * CsrSdioFunctionEnable
528  *
529  * Enable i/o on function 1.
530  *
531  * Arguments:
532  * sdio SDIO context pointer
533  *
534  * Returns:
535  * UniFi driver error code.
536  * ---------------------------------------------------------------------------
537  */
538 CsrResult
540 {
541  struct sdio_func *func = (struct sdio_func *)function->priv;
542  int err;
543 
544  func_enter();
545 
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);
550  if (err) {
551  unifi_error(NULL, "Failed to enable SDIO function %d\n", func->num);
552  }
553 
554  func_exit();
555  return ConvertSdioToCsrSdioResult(err);
556 } /* CsrSdioFunctionEnable() */
557 
558 
559 /*
560  * ---------------------------------------------------------------------------
561  * CsrSdioFunctionDisable
562  *
563  * Enable i/o on function 1.
564  *
565  * Arguments:
566  * sdio SDIO context pointer
567  *
568  * Returns:
569  * UniFi driver error code.
570  * ---------------------------------------------------------------------------
571  */
572 CsrResult
574 {
575  struct sdio_func *func = (struct sdio_func *)function->priv;
576  int err;
577 
578  func_enter();
579 
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);
584  if (err) {
585  unifi_error(NULL, "Failed to disable SDIO function %d\n", func->num);
586  }
587 
588  func_exit();
589  return ConvertSdioToCsrSdioResult(err);
590 } /* CsrSdioFunctionDisable() */
591 
592 
593 /*
594  * ---------------------------------------------------------------------------
595  * CsrSdioFunctionActive
596  *
597  * No-op as the bus goes to an active state at the start of every
598  * command.
599  *
600  * Arguments:
601  * sdio SDIO context pointer
602  * ---------------------------------------------------------------------------
603  */
604 void
606 {
607 } /* CsrSdioFunctionActive() */
608 
609 /*
610  * ---------------------------------------------------------------------------
611  * CsrSdioFunctionIdle
612  *
613  * Set the function as idle.
614  *
615  * Arguments:
616  * sdio SDIO context pointer
617  * ---------------------------------------------------------------------------
618  */
619 void
621 {
622 } /* CsrSdioFunctionIdle() */
623 
624 
625 /*
626  * ---------------------------------------------------------------------------
627  * CsrSdioPowerOn
628  *
629  * Power on UniFi.
630  *
631  * Arguments:
632  * sdio SDIO context pointer
633  * ---------------------------------------------------------------------------
634  */
635 CsrResult
637 {
638  struct sdio_func *func = (struct sdio_func *)function->priv;
639  struct mmc_host *host = func->card->host;
640 
641  _sdio_claim_host(func);
642  if (!card_is_powered) {
644  card_is_powered = 1;
645  } else {
646  printk(KERN_INFO "SDIO: Skip power on; card is already powered.\n");
647  }
648  _sdio_release_host(func);
649 
650  return CSR_RESULT_SUCCESS;
651 } /* CsrSdioPowerOn() */
652 
653 /*
654  * ---------------------------------------------------------------------------
655  * CsrSdioPowerOff
656  *
657  * Power off UniFi.
658  *
659  * Arguments:
660  * sdio SDIO context pointer
661  * ---------------------------------------------------------------------------
662  */
663 void
665 {
666  struct sdio_func *func = (struct sdio_func *)function->priv;
667  struct mmc_host *host = func->card->host;
668 
669  _sdio_claim_host(func);
670  if (card_is_powered) {
672  card_is_powered = 0;
673  } else {
674  printk(KERN_INFO "SDIO: Skip power off; card is already powered off.\n");
675  }
676  _sdio_release_host(func);
677 } /* CsrSdioPowerOff() */
678 
679 
680 static int
681 sdio_set_block_size_ignore_first_error(struct sdio_func *func, unsigned blksz)
682 {
683  int ret;
684 
685  if (blksz > func->card->host->max_blk_size)
686  return -EINVAL;
687 
688  if (blksz == 0) {
689  blksz = min(func->max_blksize, func->card->host->max_blk_size);
690  blksz = min(blksz, 512u);
691  }
692 
693  /*
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.
696  */
697  ret = csr_io_rw_direct(func->card, 1, 0,
699  blksz & 0xff, NULL);
700  if (ret && ret != -ERANGE)
701  return ret;
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);
705  if (ret)
706  return ret;
707  func->cur_blksize = blksz;
708 
709  return 0;
710 }
711 
712 CsrResult
714 {
715  struct sdio_func *func = (struct sdio_func *)function->priv;
716  int r = 0;
717 
718  /* Module parameter overrides */
719  if (sdio_block_size > -1) {
720  blockSize = sdio_block_size;
721  }
722 
723  unifi_trace(NULL, UDBG1, "Set SDIO function block size to %d\n",
724  blockSize);
725 
726  _sdio_claim_host(func);
727  r = sdio_set_block_size(func, blockSize);
728  _sdio_release_host(func);
729 
730  /*
731  * The MMC driver for kernels prior to 2.6.32 may fail this request
732  * with -ERANGE. In this case use our workaround.
733  */
734  if (r == -ERANGE) {
735  _sdio_claim_host(func);
736  r = sdio_set_block_size_ignore_first_error(func, blockSize);
737  _sdio_release_host(func);
738  }
739  if (r) {
740  unifi_error(NULL, "Error %d setting block size\n", r);
741  }
742 
743  /* Determine the achieved block size to pass to the core */
744  function->blockSize = func->cur_blksize;
745 
746  return ConvertSdioToCsrSdioResult(r);
747 } /* CsrSdioBlockSizeSet() */
748 
749 
750 /*
751  * ---------------------------------------------------------------------------
752  * CsrSdioHardReset
753  *
754  * Hard Resets UniFi is possible.
755  *
756  * Arguments:
757  * sdio SDIO context pointer
758  * ---------------------------------------------------------------------------
759  */
760 CsrResult
762 {
763  return CSR_RESULT_FAILURE;
764 } /* CsrSdioHardReset() */
765 
766 
767 
768 /*
769  * ---------------------------------------------------------------------------
770  * uf_glue_sdio_int_handler
771  *
772  * Interrupt callback function for SDIO interrupts.
773  * This is called in kernel context (i.e. not interrupt context).
774  *
775  * Arguments:
776  * func SDIO context pointer
777  *
778  * Returns:
779  * None.
780  *
781  * Note: Called with host already claimed.
782  * ---------------------------------------------------------------------------
783  */
784 static void
785 uf_glue_sdio_int_handler(struct sdio_func *func)
786 {
787  CsrSdioFunction *sdio_ctx;
788  CsrSdioInterruptDsrCallback func_dsr_callback;
789  int r;
790 
791  sdio_ctx = sdio_get_drvdata(func);
792  if (!sdio_ctx) {
793  return;
794  }
795 
796 #ifndef CSR_CONFIG_MMC_INT_BYPASS_KSOFTIRQD
797  /*
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.
802  */
803 #ifdef MMC_QUIRK_LENIENT_FN0
804  sdio_f0_writeb(func, 0, SDIO_CCCR_IENx, &r);
805 #else
806  r = csr_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, 0x00, NULL);
807 #endif
808  if (r) {
809  printk(KERN_ERR "UniFi MMC Int handler: Failed to disable interrupts %d\n", r);
810  }
811 #endif
812 
813  /* If the function driver has registered a handler, call it */
814  if (sdio_func_drv && sdio_func_drv->intr) {
815 
816  func_dsr_callback = sdio_func_drv->intr(sdio_ctx);
817 
818  /* If interrupt handle returns a DSR handle, call it */
819  if (func_dsr_callback) {
820  func_dsr_callback(sdio_ctx);
821  }
822  }
823 
824 } /* uf_glue_sdio_int_handler() */
825 
826 
827 
828 /*
829  * ---------------------------------------------------------------------------
830  * csr_sdio_linux_remove_irq
831  *
832  * Unregister the interrupt handler.
833  * This means that the linux layer can not process interrupts any more.
834  *
835  * Arguments:
836  * sdio SDIO context pointer
837  *
838  * Returns:
839  * Status of the removal.
840  * ---------------------------------------------------------------------------
841  */
843 {
844  struct sdio_func *func = (struct sdio_func *)function->priv;
845  int r;
846 
847  unifi_trace(NULL, UDBG1, "csr_sdio_linux_remove_irq\n");
848 
849  sdio_claim_host(func);
850  r = sdio_release_irq(func);
851  sdio_release_host(func);
852 
853  return r;
854 
855 } /* csr_sdio_linux_remove_irq() */
856 
857 
858 /*
859  * ---------------------------------------------------------------------------
860  * csr_sdio_linux_install_irq
861  *
862  * Register the interrupt handler.
863  * This means that the linux layer can process interrupts.
864  *
865  * Arguments:
866  * sdio SDIO context pointer
867  *
868  * Returns:
869  * Status of the removal.
870  * ---------------------------------------------------------------------------
871  */
873 {
874  struct sdio_func *func = (struct sdio_func *)function->priv;
875  int r;
876 
877  unifi_trace(NULL, UDBG1, "csr_sdio_linux_install_irq\n");
878 
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);
883 
884  /* If the interrupt was installed earlier, is fine */
885  if (r == -EBUSY)
886  r = 0;
887 
888  return r;
889 } /* csr_sdio_linux_install_irq() */
890 
891 #ifdef CONFIG_PM
892 
893 /*
894  * Power Management notifier
895  */
896 struct uf_sdio_mmc_pm_notifier
897 {
898  struct list_head list;
899 
900  CsrSdioFunction *sdio_ctx;
902 };
903 
904 /* PM notifier list head */
905 static struct uf_sdio_mmc_pm_notifier uf_sdio_mmc_pm_notifiers = {
906  .sdio_ctx = NULL,
907 };
908 
909 /*
910  * ---------------------------------------------------------------------------
911  * uf_sdio_mmc_register_pm_notifier
912  * uf_sdio_mmc_unregister_pm_notifier
913  *
914  * Register/unregister for power management events. A list is used to
915  * allow multiple card instances to be supported.
916  *
917  * Arguments:
918  * sdio_ctx - CSR SDIO context to associate PM notifier to
919  *
920  * Returns:
921  * Register function returns NULL on error
922  * ---------------------------------------------------------------------------
923  */
924 static struct uf_sdio_mmc_pm_notifier *
925 uf_sdio_mmc_register_pm_notifier(CsrSdioFunction *sdio_ctx)
926 {
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);
929 
930  if (notifier_ctx)
931  {
932  notifier_ctx->sdio_ctx = sdio_ctx;
933  notifier_ctx->pm_notifier.notifier_call = uf_sdio_mmc_power_event;
934 
935  list_add(&notifier_ctx->list, &uf_sdio_mmc_pm_notifiers.list);
936 
937  if (register_pm_notifier(&notifier_ctx->pm_notifier)) {
938  printk(KERN_ERR "unifi: register_pm_notifier failed\n");
939  }
940  }
941 
942  return notifier_ctx;
943 }
944 
945 static void
946 uf_sdio_mmc_unregister_pm_notifier(CsrSdioFunction *sdio_ctx)
947 {
948  struct uf_sdio_mmc_pm_notifier *notifier_ctx;
949  struct list_head *node, *q;
950 
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);
953 
954  /* If it matches, unregister and free the notifier context */
955  if (notifier_ctx && notifier_ctx->sdio_ctx == sdio_ctx)
956  {
957  if (unregister_pm_notifier(&notifier_ctx->pm_notifier)) {
958  printk(KERN_ERR "unifi: unregister_pm_notifier failed\n");
959  }
960 
961  /* Remove from list */
962  notifier_ctx->sdio_ctx = NULL;
963  list_del(node);
964  kfree(notifier_ctx);
965  }
966  }
967 }
968 
969 /*
970  * ---------------------------------------------------------------------------
971  * uf_sdio_mmc_power_event
972  *
973  * Handler for power management events.
974  *
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.
977  *
978  * Arguments:
979  * event event ID
980  *
981  * Returns:
982  * Status of the event handling
983  * ---------------------------------------------------------------------------
984  */
985 static int
986 uf_sdio_mmc_power_event(struct notifier_block *this, unsigned long event, void *ptr)
987 {
988  struct uf_sdio_mmc_pm_notifier *notifier_ctx = container_of(this,
989  struct uf_sdio_mmc_pm_notifier,
990  pm_notifier);
991 
992  /* Call the CSR SDIO function driver's suspend/resume method
993  * while the userspace is unsuspended.
994  */
995  switch (event) {
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);
1001  }
1002  break;
1003 
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);
1009  }
1010  break;
1011  }
1012  return NOTIFY_DONE;
1013 }
1014 
1015 #endif /* CONFIG_PM */
1016 
1017 /*
1018  * ---------------------------------------------------------------------------
1019  * uf_glue_sdio_probe
1020  *
1021  * Card insert callback.
1022  *
1023  * Arguments:
1024  * func Our (glue layer) context pointer.
1025  *
1026  * Returns:
1027  * UniFi driver error code.
1028  * ---------------------------------------------------------------------------
1029  */
1030 static int
1031 uf_glue_sdio_probe(struct sdio_func *func,
1032  const struct sdio_device_id *id)
1033 {
1034  int instance;
1035  CsrSdioFunction *sdio_ctx;
1036 
1037  func_enter();
1038 
1039  /* First of all claim the SDIO driver */
1040  sdio_claim_host(func);
1041 
1042  /* Assume that the card is already powered */
1043  card_is_powered = 1;
1044 
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);
1049 
1050  /* Allocate context */
1051  sdio_ctx = (CsrSdioFunction *)kmalloc(sizeof(CsrSdioFunction),
1052  GFP_KERNEL);
1053  if (sdio_ctx == NULL) {
1054  sdio_release_host(func);
1055  return -ENOMEM;
1056  }
1057 
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;
1066 
1067  /* Module parameter enables byte mode */
1068  if (sdio_byte_mode) {
1069  sdio_ctx->features |= CSR_SDIO_FEATURE_BYTE_MODE;
1070  }
1071 
1072  if (func->card->host->caps & MMC_CAP_SD_HIGHSPEED) {
1073  unifi_trace(NULL, UDBG1, "MMC_CAP_SD_HIGHSPEED is available\n");
1074  }
1075 
1076 #ifdef MMC_QUIRK_LENIENT_FN0
1077  func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
1078 #endif
1079 
1080  /* Pass context to the SDIO driver */
1081  sdio_set_drvdata(func, sdio_ctx);
1082 
1083 #ifdef CONFIG_PM
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__);
1087  }
1088 #endif
1089 
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);
1095  }
1096 
1097  /* We have finished, so release the SDIO driver */
1098  sdio_release_host(func);
1099 
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);
1104 #endif
1105 
1106  func_exit();
1107  return 0;
1108 } /* uf_glue_sdio_probe() */
1109 
1110 
1111 /*
1112  * ---------------------------------------------------------------------------
1113  * uf_glue_sdio_remove
1114  *
1115  * Card removal callback.
1116  *
1117  * Arguments:
1118  * func Our (glue layer) context pointer.
1119  *
1120  * Returns:
1121  * UniFi driver error code.
1122  * ---------------------------------------------------------------------------
1123  */
1124 static void
1125 uf_glue_sdio_remove(struct sdio_func *func)
1126 {
1127  CsrSdioFunction *sdio_ctx;
1128 
1129  sdio_ctx = sdio_get_drvdata(func);
1130  if (!sdio_ctx) {
1131  return;
1132  }
1133 
1134  func_enter();
1135 
1136  unifi_info(NULL, "UniFi card removed\n");
1137 
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);
1142  }
1143 
1144 #ifdef CONFIG_PM
1145  /* Unregister for PM events */
1146  uf_sdio_mmc_unregister_pm_notifier(sdio_ctx);
1147 #endif
1148 
1149  kfree(sdio_ctx);
1150 
1151  func_exit();
1152 
1153 } /* uf_glue_sdio_remove */
1154 
1155 
1156 /*
1157  * SDIO ids *must* be statically declared, so we can't take
1158  * them from the list passed in csr_sdio_register_driver().
1159  */
1160 static const struct sdio_device_id unifi_ids[] = {
1163  { /* end: all zeroes */ },
1164 };
1165 
1166 MODULE_DEVICE_TABLE(sdio, unifi_ids);
1167 
1168 #ifdef CONFIG_PM
1169 
1170 /*
1171  * ---------------------------------------------------------------------------
1172  * uf_glue_sdio_suspend
1173  *
1174  * Card suspend callback. The userspace will already be suspended.
1175  *
1176  * Arguments:
1177  * dev The struct device owned by the MMC driver
1178  *
1179  * Returns:
1180  * None
1181  * ---------------------------------------------------------------------------
1182  */
1183 static int
1184 uf_glue_sdio_suspend(struct device *dev)
1185 {
1186  func_enter();
1187 
1188  unifi_trace(NULL, UDBG1, "uf_glue_sdio_suspend");
1189 
1190  func_exit();
1191  return 0;
1192 } /* uf_glue_sdio_suspend */
1193 
1194 
1195 /*
1196  * ---------------------------------------------------------------------------
1197  * uf_glue_sdio_resume
1198  *
1199  * Card resume callback. The userspace will still be suspended.
1200  *
1201  * Arguments:
1202  * dev The struct device owned by the MMC driver
1203  *
1204  * Returns:
1205  * None
1206  * ---------------------------------------------------------------------------
1207  */
1208 static int
1209 uf_glue_sdio_resume(struct device *dev)
1210 {
1211  func_enter();
1212 
1213  unifi_trace(NULL, UDBG1, "uf_glue_sdio_resume");
1214 
1215 #ifdef ANDROID_BUILD
1216  unifi_trace(NULL, UDBG1, "resume: take wakelock\n");
1217  wake_lock(&unifi_sdio_wake_lock);
1218 #endif
1219 
1220  func_exit();
1221  return 0;
1222 
1223 } /* uf_glue_sdio_resume */
1224 
1225 static struct dev_pm_ops unifi_pm_ops = {
1226  .suspend = uf_glue_sdio_suspend,
1227  .resume = uf_glue_sdio_resume,
1228 };
1229 
1230 #define UNIFI_PM_OPS (&unifi_pm_ops)
1231 
1232 #else
1233 
1234 #define UNIFI_PM_OPS NULL
1235 
1236 #endif /* CONFIG_PM */
1237 
1238 static struct sdio_driver unifi_driver = {
1239  .probe = uf_glue_sdio_probe,
1240  .remove = uf_glue_sdio_remove,
1241  .name = "unifi",
1242  .id_table = unifi_ids,
1243  .drv.pm = UNIFI_PM_OPS,
1244 };
1245 
1246 
1247 /*
1248  * ---------------------------------------------------------------------------
1249  * CsrSdioFunctionDriverRegister
1250  * CsrSdioFunctionDriverUnregister
1251  *
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.
1255  *
1256  * Arguments:
1257  * sdio_drv Pointer to the function driver's SDIO structure.
1258  *
1259  * Returns:
1260  * None.
1261  * ---------------------------------------------------------------------------
1262  */
1263 CsrResult
1265 {
1266  int r;
1267 
1268  printk("UniFi: Using native Linux MMC driver for SDIO.\n");
1269 
1270  if (sdio_func_drv) {
1271  unifi_error(NULL, "sdio_mmc: UniFi driver already registered\n");
1273  }
1274 
1275 #ifdef ANDROID_BUILD
1276  wake_lock_init(&unifi_sdio_wake_lock, WAKE_LOCK_SUSPEND, "unifi_sdio_work");
1277 #endif
1278 
1279  /* Save the registered driver description */
1280  /*
1281  * FIXME:
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
1284  */
1285  sdio_func_drv = sdio_drv;
1286 
1287 #ifdef CONFIG_PM
1288  /* Initialise PM notifier list */
1289  INIT_LIST_HEAD(&uf_sdio_mmc_pm_notifiers.list);
1290 #endif
1291 
1292  /* Register ourself with mmc_core */
1293  r = sdio_register_driver(&unifi_driver);
1294  if (r) {
1295  printk(KERN_ERR "unifi_sdio: Failed to register UniFi SDIO driver: %d\n", r);
1296  return ConvertSdioToCsrSdioResult(r);
1297  }
1298 
1299  return CSR_RESULT_SUCCESS;
1300 } /* CsrSdioFunctionDriverRegister() */
1301 
1302 
1303 
1304 void
1306 {
1307  printk(KERN_INFO "UniFi: unregister from MMC sdio\n");
1308 
1309 #ifdef ANDROID_BUILD
1310  wake_lock_destroy(&unifi_sdio_wake_lock);
1311 #endif
1312  sdio_unregister_driver(&unifi_driver);
1313 
1314  sdio_func_drv = NULL;
1315 
1316 } /* CsrSdioFunctionDriverUnregister() */
1317