bus.c 7.4 KB

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