bus.c 6.1 KB

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