host.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * linux/drivers/mmc/core/host.c
  3. *
  4. * Copyright (C) 2003 Russell King, All Rights Reserved.
  5. * Copyright (C) 2007-2008 Pierre Ossman
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * MMC host class device management
  12. */
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/idr.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/leds.h>
  18. #include <linux/slab.h>
  19. #include <linux/suspend.h>
  20. #include <linux/mmc/host.h>
  21. #include "core.h"
  22. #include "host.h"
  23. #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
  24. static void mmc_host_classdev_release(struct device *dev)
  25. {
  26. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  27. kfree(host);
  28. }
  29. static struct class mmc_host_class = {
  30. .name = "mmc_host",
  31. .dev_release = mmc_host_classdev_release,
  32. };
  33. int mmc_register_host_class(void)
  34. {
  35. return class_register(&mmc_host_class);
  36. }
  37. void mmc_unregister_host_class(void)
  38. {
  39. class_unregister(&mmc_host_class);
  40. }
  41. static DEFINE_IDR(mmc_host_idr);
  42. static DEFINE_SPINLOCK(mmc_host_lock);
  43. /**
  44. * mmc_alloc_host - initialise the per-host structure.
  45. * @extra: sizeof private data structure
  46. * @dev: pointer to host device model structure
  47. *
  48. * Initialise the per-host structure.
  49. */
  50. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
  51. {
  52. int err;
  53. struct mmc_host *host;
  54. if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
  55. return NULL;
  56. host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
  57. if (!host)
  58. return NULL;
  59. spin_lock(&mmc_host_lock);
  60. err = idr_get_new(&mmc_host_idr, host, &host->index);
  61. spin_unlock(&mmc_host_lock);
  62. if (err)
  63. goto free;
  64. dev_set_name(&host->class_dev, "mmc%d", host->index);
  65. host->parent = dev;
  66. host->class_dev.parent = dev;
  67. host->class_dev.class = &mmc_host_class;
  68. device_initialize(&host->class_dev);
  69. spin_lock_init(&host->lock);
  70. init_waitqueue_head(&host->wq);
  71. INIT_DELAYED_WORK(&host->detect, mmc_rescan);
  72. INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable);
  73. #ifdef CONFIG_PM
  74. host->pm_notify.notifier_call = mmc_pm_notify;
  75. #endif
  76. /*
  77. * By default, hosts do not support SGIO or large requests.
  78. * They have to set these according to their abilities.
  79. */
  80. host->max_hw_segs = 1;
  81. host->max_phys_segs = 1;
  82. host->max_seg_size = PAGE_CACHE_SIZE;
  83. host->max_req_size = PAGE_CACHE_SIZE;
  84. host->max_blk_size = 512;
  85. host->max_blk_count = PAGE_CACHE_SIZE / 512;
  86. return host;
  87. free:
  88. kfree(host);
  89. return NULL;
  90. }
  91. EXPORT_SYMBOL(mmc_alloc_host);
  92. /**
  93. * mmc_add_host - initialise host hardware
  94. * @host: mmc host
  95. *
  96. * Register the host with the driver model. The host must be
  97. * prepared to start servicing requests before this function
  98. * completes.
  99. */
  100. int mmc_add_host(struct mmc_host *host)
  101. {
  102. int err;
  103. WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
  104. !host->ops->enable_sdio_irq);
  105. led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
  106. err = device_add(&host->class_dev);
  107. if (err)
  108. return err;
  109. #ifdef CONFIG_DEBUG_FS
  110. mmc_add_host_debugfs(host);
  111. #endif
  112. mmc_start_host(host);
  113. register_pm_notifier(&host->pm_notify);
  114. return 0;
  115. }
  116. EXPORT_SYMBOL(mmc_add_host);
  117. /**
  118. * mmc_remove_host - remove host hardware
  119. * @host: mmc host
  120. *
  121. * Unregister and remove all cards associated with this host,
  122. * and power down the MMC bus. No new requests will be issued
  123. * after this function has returned.
  124. */
  125. void mmc_remove_host(struct mmc_host *host)
  126. {
  127. unregister_pm_notifier(&host->pm_notify);
  128. mmc_stop_host(host);
  129. #ifdef CONFIG_DEBUG_FS
  130. mmc_remove_host_debugfs(host);
  131. #endif
  132. device_del(&host->class_dev);
  133. led_trigger_unregister_simple(host->led);
  134. }
  135. EXPORT_SYMBOL(mmc_remove_host);
  136. /**
  137. * mmc_free_host - free the host structure
  138. * @host: mmc host
  139. *
  140. * Free the host once all references to it have been dropped.
  141. */
  142. void mmc_free_host(struct mmc_host *host)
  143. {
  144. spin_lock(&mmc_host_lock);
  145. idr_remove(&mmc_host_idr, host->index);
  146. spin_unlock(&mmc_host_lock);
  147. put_device(&host->class_dev);
  148. }
  149. EXPORT_SYMBOL(mmc_free_host);