host.c 3.7 KB

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