bus.c 6.1 KB

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