bus.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * linux/drivers/mmc/core/bus.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 card bus driver model
  12. */
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/mmc/card.h>
  16. #include <linux/mmc/host.h>
  17. #include "core.h"
  18. #include "sdio_cis.h"
  19. #include "bus.h"
  20. #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
  21. #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
  22. static ssize_t mmc_type_show(struct device *dev,
  23. struct device_attribute *attr, char *buf)
  24. {
  25. struct mmc_card *card = dev_to_mmc_card(dev);
  26. switch (card->type) {
  27. case MMC_TYPE_MMC:
  28. return sprintf(buf, "MMC\n");
  29. case MMC_TYPE_SD:
  30. return sprintf(buf, "SD\n");
  31. case MMC_TYPE_SDIO:
  32. return sprintf(buf, "SDIO\n");
  33. default:
  34. return -EFAULT;
  35. }
  36. }
  37. static struct device_attribute mmc_dev_attrs[] = {
  38. __ATTR(type, S_IRUGO, mmc_type_show, NULL),
  39. __ATTR_NULL,
  40. };
  41. /*
  42. * This currently matches any MMC driver to any MMC card - drivers
  43. * themselves make the decision whether to drive this card in their
  44. * probe method.
  45. */
  46. static int mmc_bus_match(struct device *dev, struct device_driver *drv)
  47. {
  48. return 1;
  49. }
  50. static int
  51. mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  52. {
  53. struct mmc_card *card = dev_to_mmc_card(dev);
  54. const char *type;
  55. int retval = 0;
  56. switch (card->type) {
  57. case MMC_TYPE_MMC:
  58. type = "MMC";
  59. break;
  60. case MMC_TYPE_SD:
  61. type = "SD";
  62. break;
  63. case MMC_TYPE_SDIO:
  64. type = "SDIO";
  65. break;
  66. default:
  67. type = NULL;
  68. }
  69. if (type) {
  70. retval = add_uevent_var(env, "MMC_TYPE=%s", type);
  71. if (retval)
  72. return retval;
  73. }
  74. retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
  75. if (retval)
  76. return retval;
  77. /*
  78. * Request the mmc_block device. Note: that this is a direct request
  79. * for the module it carries no information as to what is inserted.
  80. */
  81. retval = add_uevent_var(env, "MODALIAS=mmc:block");
  82. return retval;
  83. }
  84. static int mmc_bus_probe(struct device *dev)
  85. {
  86. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  87. struct mmc_card *card = dev_to_mmc_card(dev);
  88. return drv->probe(card);
  89. }
  90. static int mmc_bus_remove(struct device *dev)
  91. {
  92. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  93. struct mmc_card *card = dev_to_mmc_card(dev);
  94. drv->remove(card);
  95. return 0;
  96. }
  97. static int mmc_bus_suspend(struct device *dev, pm_message_t state)
  98. {
  99. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  100. struct mmc_card *card = dev_to_mmc_card(dev);
  101. int ret = 0;
  102. if (dev->driver && drv->suspend)
  103. ret = drv->suspend(card, state);
  104. return ret;
  105. }
  106. static int mmc_bus_resume(struct device *dev)
  107. {
  108. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  109. struct mmc_card *card = dev_to_mmc_card(dev);
  110. int ret = 0;
  111. if (dev->driver && drv->resume)
  112. ret = drv->resume(card);
  113. return ret;
  114. }
  115. static struct bus_type mmc_bus_type = {
  116. .name = "mmc",
  117. .dev_attrs = mmc_dev_attrs,
  118. .match = mmc_bus_match,
  119. .uevent = mmc_bus_uevent,
  120. .probe = mmc_bus_probe,
  121. .remove = mmc_bus_remove,
  122. .suspend = mmc_bus_suspend,
  123. .resume = mmc_bus_resume,
  124. };
  125. int mmc_register_bus(void)
  126. {
  127. return bus_register(&mmc_bus_type);
  128. }
  129. void mmc_unregister_bus(void)
  130. {
  131. bus_unregister(&mmc_bus_type);
  132. }
  133. /**
  134. * mmc_register_driver - register a media driver
  135. * @drv: MMC media driver
  136. */
  137. int mmc_register_driver(struct mmc_driver *drv)
  138. {
  139. drv->drv.bus = &mmc_bus_type;
  140. return driver_register(&drv->drv);
  141. }
  142. EXPORT_SYMBOL(mmc_register_driver);
  143. /**
  144. * mmc_unregister_driver - unregister a media driver
  145. * @drv: MMC media driver
  146. */
  147. void mmc_unregister_driver(struct mmc_driver *drv)
  148. {
  149. drv->drv.bus = &mmc_bus_type;
  150. driver_unregister(&drv->drv);
  151. }
  152. EXPORT_SYMBOL(mmc_unregister_driver);
  153. static void mmc_release_card(struct device *dev)
  154. {
  155. struct mmc_card *card = dev_to_mmc_card(dev);
  156. sdio_free_common_cis(card);
  157. if (card->info)
  158. kfree(card->info);
  159. kfree(card);
  160. }
  161. /*
  162. * Allocate and initialise a new MMC card structure.
  163. */
  164. struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
  165. {
  166. struct mmc_card *card;
  167. card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
  168. if (!card)
  169. return ERR_PTR(-ENOMEM);
  170. card->host = host;
  171. device_initialize(&card->dev);
  172. card->dev.parent = mmc_classdev(host);
  173. card->dev.bus = &mmc_bus_type;
  174. card->dev.release = mmc_release_card;
  175. card->dev.type = type;
  176. return card;
  177. }
  178. /*
  179. * Register a new MMC card with the driver model.
  180. */
  181. int mmc_add_card(struct mmc_card *card)
  182. {
  183. int ret;
  184. const char *type;
  185. dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
  186. switch (card->type) {
  187. case MMC_TYPE_MMC:
  188. type = "MMC";
  189. break;
  190. case MMC_TYPE_SD:
  191. type = "SD";
  192. if (mmc_card_blockaddr(card))
  193. type = "SDHC";
  194. break;
  195. case MMC_TYPE_SDIO:
  196. type = "SDIO";
  197. break;
  198. default:
  199. type = "?";
  200. break;
  201. }
  202. if (mmc_host_is_spi(card->host)) {
  203. printk(KERN_INFO "%s: new %s%s card on SPI\n",
  204. mmc_hostname(card->host),
  205. mmc_card_highspeed(card) ? "high speed " : "",
  206. type);
  207. } else {
  208. printk(KERN_INFO "%s: new %s%s card at address %04x\n",
  209. mmc_hostname(card->host),
  210. mmc_card_highspeed(card) ? "high speed " : "",
  211. type, card->rca);
  212. }
  213. ret = device_add(&card->dev);
  214. if (ret)
  215. return ret;
  216. #ifdef CONFIG_DEBUG_FS
  217. mmc_add_card_debugfs(card);
  218. #endif
  219. mmc_card_set_present(card);
  220. return 0;
  221. }
  222. /*
  223. * Unregister a new MMC card with the driver model, and
  224. * (eventually) free it.
  225. */
  226. void mmc_remove_card(struct mmc_card *card)
  227. {
  228. #ifdef CONFIG_DEBUG_FS
  229. mmc_remove_card_debugfs(card);
  230. #endif
  231. if (mmc_card_present(card)) {
  232. if (mmc_host_is_spi(card->host)) {
  233. printk(KERN_INFO "%s: SPI card removed\n",
  234. mmc_hostname(card->host));
  235. } else {
  236. printk(KERN_INFO "%s: card %04x removed\n",
  237. mmc_hostname(card->host), card->rca);
  238. }
  239. device_del(&card->dev);
  240. }
  241. put_device(&card->dev);
  242. }