host.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/mmc/host.h>
  20. #include "core.h"
  21. #include "host.h"
  22. #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
  23. static void mmc_host_classdev_release(struct device *dev)
  24. {
  25. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  26. kfree(host);
  27. }
  28. static struct class mmc_host_class = {
  29. .name = "mmc_host",
  30. .dev_release = mmc_host_classdev_release,
  31. };
  32. int mmc_register_host_class(void)
  33. {
  34. return class_register(&mmc_host_class);
  35. }
  36. void mmc_unregister_host_class(void)
  37. {
  38. class_unregister(&mmc_host_class);
  39. }
  40. static DEFINE_IDR(mmc_host_idr);
  41. static DEFINE_SPINLOCK(mmc_host_lock);
  42. /**
  43. * mmc_alloc_host - initialise the per-host structure.
  44. * @extra: sizeof private data structure
  45. * @dev: pointer to host device model structure
  46. *
  47. * Initialise the per-host structure.
  48. */
  49. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
  50. {
  51. int err;
  52. struct mmc_host *host;
  53. if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
  54. return NULL;
  55. host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
  56. if (!host)
  57. return NULL;
  58. spin_lock(&mmc_host_lock);
  59. err = idr_get_new(&mmc_host_idr, host, &host->index);
  60. spin_unlock(&mmc_host_lock);
  61. if (err)
  62. goto free;
  63. dev_set_name(&host->class_dev, "mmc%d", host->index);
  64. host->parent = dev;
  65. host->class_dev.parent = dev;
  66. host->class_dev.class = &mmc_host_class;
  67. device_initialize(&host->class_dev);
  68. spin_lock_init(&host->lock);
  69. init_waitqueue_head(&host->wq);
  70. INIT_DELAYED_WORK(&host->detect, mmc_rescan);
  71. INIT_DELAYED_WORK_DEFERRABLE(&host->disable, mmc_host_deeper_disable);
  72. /*
  73. * By default, hosts do not support SGIO or large requests.
  74. * They have to set these according to their abilities.
  75. */
  76. host->max_hw_segs = 1;
  77. host->max_phys_segs = 1;
  78. host->max_seg_size = PAGE_CACHE_SIZE;
  79. host->max_req_size = PAGE_CACHE_SIZE;
  80. host->max_blk_size = 512;
  81. host->max_blk_count = PAGE_CACHE_SIZE / 512;
  82. return host;
  83. free:
  84. kfree(host);
  85. return NULL;
  86. }
  87. EXPORT_SYMBOL(mmc_alloc_host);
  88. /**
  89. * mmc_add_host - initialise host hardware
  90. * @host: mmc host
  91. *
  92. * Register the host with the driver model. The host must be
  93. * prepared to start servicing requests before this function
  94. * completes.
  95. */
  96. int mmc_add_host(struct mmc_host *host)
  97. {
  98. int err;
  99. WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
  100. !host->ops->enable_sdio_irq);
  101. led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
  102. err = device_add(&host->class_dev);
  103. if (err)
  104. return err;
  105. #ifdef CONFIG_DEBUG_FS
  106. mmc_add_host_debugfs(host);
  107. #endif
  108. mmc_start_host(host);
  109. return 0;
  110. }
  111. EXPORT_SYMBOL(mmc_add_host);
  112. /**
  113. * mmc_remove_host - remove host hardware
  114. * @host: mmc host
  115. *
  116. * Unregister and remove all cards associated with this host,
  117. * and power down the MMC bus. No new requests will be issued
  118. * after this function has returned.
  119. */
  120. void mmc_remove_host(struct mmc_host *host)
  121. {
  122. mmc_stop_host(host);
  123. #ifdef CONFIG_DEBUG_FS
  124. mmc_remove_host_debugfs(host);
  125. #endif
  126. device_del(&host->class_dev);
  127. led_trigger_unregister_simple(host->led);
  128. }
  129. EXPORT_SYMBOL(mmc_remove_host);
  130. /**
  131. * mmc_free_host - free the host structure
  132. * @host: mmc host
  133. *
  134. * Free the host once all references to it have been dropped.
  135. */
  136. void mmc_free_host(struct mmc_host *host)
  137. {
  138. spin_lock(&mmc_host_lock);
  139. idr_remove(&mmc_host_idr, host->index);
  140. spin_unlock(&mmc_host_lock);
  141. put_device(&host->class_dev);
  142. }
  143. EXPORT_SYMBOL(mmc_free_host);