bus.c 7.0 KB

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