host.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * linux/drivers/mmc/core/host.c
  3. *
  4. * Copyright (C) 2003 Russell King, All Rights Reserved.
  5. * Copyright (C) 2007 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/mmc/host.h>
  18. #include "core.h"
  19. #include "host.h"
  20. #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
  21. static void mmc_host_classdev_release(struct device *dev)
  22. {
  23. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  24. kfree(host);
  25. }
  26. static struct class mmc_host_class = {
  27. .name = "mmc_host",
  28. .dev_release = mmc_host_classdev_release,
  29. };
  30. int mmc_register_host_class(void)
  31. {
  32. return class_register(&mmc_host_class);
  33. }
  34. void mmc_unregister_host_class(void)
  35. {
  36. class_unregister(&mmc_host_class);
  37. }
  38. static DEFINE_IDR(mmc_host_idr);
  39. static DEFINE_SPINLOCK(mmc_host_lock);
  40. /**
  41. * mmc_alloc_host - initialise the per-host structure.
  42. * @extra: sizeof private data structure
  43. * @dev: pointer to host device model structure
  44. *
  45. * Initialise the per-host structure.
  46. */
  47. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
  48. {
  49. struct mmc_host *host;
  50. host = kmalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
  51. if (!host)
  52. return NULL;
  53. memset(host, 0, sizeof(struct mmc_host) + extra);
  54. host->parent = dev;
  55. host->class_dev.parent = dev;
  56. host->class_dev.class = &mmc_host_class;
  57. device_initialize(&host->class_dev);
  58. spin_lock_init(&host->lock);
  59. init_waitqueue_head(&host->wq);
  60. INIT_DELAYED_WORK(&host->detect, mmc_rescan);
  61. /*
  62. * By default, hosts do not support SGIO or large requests.
  63. * They have to set these according to their abilities.
  64. */
  65. host->max_hw_segs = 1;
  66. host->max_phys_segs = 1;
  67. host->max_seg_size = PAGE_CACHE_SIZE;
  68. host->max_req_size = PAGE_CACHE_SIZE;
  69. host->max_blk_size = 512;
  70. host->max_blk_count = PAGE_CACHE_SIZE / 512;
  71. return host;
  72. }
  73. EXPORT_SYMBOL(mmc_alloc_host);
  74. /**
  75. * mmc_add_host - initialise host hardware
  76. * @host: mmc host
  77. */
  78. int mmc_add_host(struct mmc_host *host)
  79. {
  80. int err;
  81. if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
  82. return -ENOMEM;
  83. spin_lock(&mmc_host_lock);
  84. err = idr_get_new(&mmc_host_idr, host, &host->index);
  85. spin_unlock(&mmc_host_lock);
  86. if (err)
  87. return err;
  88. snprintf(host->class_dev.bus_id, BUS_ID_SIZE,
  89. "mmc%d", host->index);
  90. err = device_add(&host->class_dev);
  91. if (err)
  92. return err;
  93. mmc_start_host(host);
  94. return 0;
  95. }
  96. EXPORT_SYMBOL(mmc_add_host);
  97. /**
  98. * mmc_remove_host - remove host hardware
  99. * @host: mmc host
  100. *
  101. * Unregister and remove all cards associated with this host,
  102. * and power down the MMC bus.
  103. */
  104. void mmc_remove_host(struct mmc_host *host)
  105. {
  106. mmc_stop_host(host);
  107. device_del(&host->class_dev);
  108. spin_lock(&mmc_host_lock);
  109. idr_remove(&mmc_host_idr, host->index);
  110. spin_unlock(&mmc_host_lock);
  111. }
  112. EXPORT_SYMBOL(mmc_remove_host);
  113. /**
  114. * mmc_free_host - free the host structure
  115. * @host: mmc host
  116. *
  117. * Free the host once all references to it have been dropped.
  118. */
  119. void mmc_free_host(struct mmc_host *host)
  120. {
  121. put_device(&host->class_dev);
  122. }
  123. EXPORT_SYMBOL(mmc_free_host);