bus.c 7.6 KB

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