bus.c 7.8 KB

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