bus.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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/pm_runtime.h>
  17. #include <linux/mmc/card.h>
  18. #include <linux/mmc/host.h>
  19. #include "core.h"
  20. #include "sdio_cis.h"
  21. #include "bus.h"
  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 = mmc_dev_to_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 = mmc_dev_to_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 = mmc_dev_to_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 = mmc_dev_to_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 = mmc_dev_to_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 = mmc_dev_to_card(dev);
  116. int ret = 0;
  117. if (dev->driver && drv->resume)
  118. ret = drv->resume(card);
  119. return ret;
  120. }
  121. #ifdef CONFIG_PM_RUNTIME
  122. static int mmc_runtime_suspend(struct device *dev)
  123. {
  124. struct mmc_card *card = mmc_dev_to_card(dev);
  125. return mmc_power_save_host(card->host);
  126. }
  127. static int mmc_runtime_resume(struct device *dev)
  128. {
  129. struct mmc_card *card = mmc_dev_to_card(dev);
  130. return mmc_power_restore_host(card->host);
  131. }
  132. static int mmc_runtime_idle(struct device *dev)
  133. {
  134. return pm_runtime_suspend(dev);
  135. }
  136. static const struct dev_pm_ops mmc_bus_pm_ops = {
  137. .runtime_suspend = mmc_runtime_suspend,
  138. .runtime_resume = mmc_runtime_resume,
  139. .runtime_idle = mmc_runtime_idle,
  140. };
  141. #define MMC_PM_OPS_PTR (&mmc_bus_pm_ops)
  142. #else /* !CONFIG_PM_RUNTIME */
  143. #define MMC_PM_OPS_PTR NULL
  144. #endif /* !CONFIG_PM_RUNTIME */
  145. static struct bus_type mmc_bus_type = {
  146. .name = "mmc",
  147. .dev_attrs = mmc_dev_attrs,
  148. .match = mmc_bus_match,
  149. .uevent = mmc_bus_uevent,
  150. .probe = mmc_bus_probe,
  151. .remove = mmc_bus_remove,
  152. .suspend = mmc_bus_suspend,
  153. .resume = mmc_bus_resume,
  154. .pm = MMC_PM_OPS_PTR,
  155. };
  156. int mmc_register_bus(void)
  157. {
  158. return bus_register(&mmc_bus_type);
  159. }
  160. void mmc_unregister_bus(void)
  161. {
  162. bus_unregister(&mmc_bus_type);
  163. }
  164. /**
  165. * mmc_register_driver - register a media driver
  166. * @drv: MMC media driver
  167. */
  168. int mmc_register_driver(struct mmc_driver *drv)
  169. {
  170. drv->drv.bus = &mmc_bus_type;
  171. return driver_register(&drv->drv);
  172. }
  173. EXPORT_SYMBOL(mmc_register_driver);
  174. /**
  175. * mmc_unregister_driver - unregister a media driver
  176. * @drv: MMC media driver
  177. */
  178. void mmc_unregister_driver(struct mmc_driver *drv)
  179. {
  180. drv->drv.bus = &mmc_bus_type;
  181. driver_unregister(&drv->drv);
  182. }
  183. EXPORT_SYMBOL(mmc_unregister_driver);
  184. static void mmc_release_card(struct device *dev)
  185. {
  186. struct mmc_card *card = mmc_dev_to_card(dev);
  187. sdio_free_common_cis(card);
  188. if (card->info)
  189. kfree(card->info);
  190. kfree(card);
  191. }
  192. /*
  193. * Allocate and initialise a new MMC card structure.
  194. */
  195. struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
  196. {
  197. struct mmc_card *card;
  198. card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
  199. if (!card)
  200. return ERR_PTR(-ENOMEM);
  201. card->host = host;
  202. device_initialize(&card->dev);
  203. card->dev.parent = mmc_classdev(host);
  204. card->dev.bus = &mmc_bus_type;
  205. card->dev.release = mmc_release_card;
  206. card->dev.type = type;
  207. return card;
  208. }
  209. /*
  210. * Register a new MMC card with the driver model.
  211. */
  212. int mmc_add_card(struct mmc_card *card)
  213. {
  214. int ret;
  215. const char *type;
  216. dev_set_name(&card->dev, "%s:%04x", mmc_hostname(card->host), card->rca);
  217. switch (card->type) {
  218. case MMC_TYPE_MMC:
  219. type = "MMC";
  220. break;
  221. case MMC_TYPE_SD:
  222. type = "SD";
  223. if (mmc_card_blockaddr(card)) {
  224. if (mmc_card_ext_capacity(card))
  225. type = "SDXC";
  226. else
  227. type = "SDHC";
  228. }
  229. break;
  230. case MMC_TYPE_SDIO:
  231. type = "SDIO";
  232. break;
  233. case MMC_TYPE_SD_COMBO:
  234. type = "SD-combo";
  235. if (mmc_card_blockaddr(card))
  236. type = "SDHC-combo";
  237. break;
  238. default:
  239. type = "?";
  240. break;
  241. }
  242. if (mmc_host_is_spi(card->host)) {
  243. printk(KERN_INFO "%s: new %s%s%s card on SPI\n",
  244. mmc_hostname(card->host),
  245. mmc_card_highspeed(card) ? "high speed " : "",
  246. mmc_card_ddr_mode(card) ? "DDR " : "",
  247. type);
  248. } else {
  249. printk(KERN_INFO "%s: new %s%s%s card at address %04x\n",
  250. mmc_hostname(card->host),
  251. mmc_sd_card_uhs(card) ? "ultra high speed " :
  252. (mmc_card_highspeed(card) ? "high speed " : ""),
  253. mmc_card_ddr_mode(card) ? "DDR " : "",
  254. type, card->rca);
  255. }
  256. #ifdef CONFIG_DEBUG_FS
  257. mmc_add_card_debugfs(card);
  258. #endif
  259. ret = device_add(&card->dev);
  260. if (ret)
  261. return ret;
  262. mmc_card_set_present(card);
  263. return 0;
  264. }
  265. /*
  266. * Unregister a new MMC card with the driver model, and
  267. * (eventually) free it.
  268. */
  269. void mmc_remove_card(struct mmc_card *card)
  270. {
  271. #ifdef CONFIG_DEBUG_FS
  272. mmc_remove_card_debugfs(card);
  273. #endif
  274. if (mmc_card_present(card)) {
  275. if (mmc_host_is_spi(card->host)) {
  276. printk(KERN_INFO "%s: SPI card removed\n",
  277. mmc_hostname(card->host));
  278. } else {
  279. printk(KERN_INFO "%s: card %04x removed\n",
  280. mmc_hostname(card->host), card->rca);
  281. }
  282. device_del(&card->dev);
  283. }
  284. put_device(&card->dev);
  285. }