bus.c 5.9 KB

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