bus.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/mmc/card.h>
  16. #include <linux/mmc/host.h>
  17. #include "core.h"
  18. #include "sdio_cis.h"
  19. #include "bus.h"
  20. #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
  21. #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
  22. static ssize_t mmc_type_show(struct device *dev,
  23. struct device_attribute *attr, char *buf)
  24. {
  25. struct mmc_card *card = dev_to_mmc_card(dev);
  26. switch (card->type) {
  27. case MMC_TYPE_MMC:
  28. return sprintf(buf, "MMC\n");
  29. case MMC_TYPE_SD:
  30. return sprintf(buf, "SD\n");
  31. case MMC_TYPE_SDIO:
  32. return sprintf(buf, "SDIO\n");
  33. default:
  34. return -EFAULT;
  35. }
  36. }
  37. static struct device_attribute mmc_dev_attrs[] = {
  38. __ATTR(type, S_IRUGO, mmc_type_show, NULL),
  39. __ATTR_NULL,
  40. };
  41. /*
  42. * This currently matches any MMC driver to any MMC card - drivers
  43. * themselves make the decision whether to drive this card in their
  44. * probe method.
  45. */
  46. static int mmc_bus_match(struct device *dev, struct device_driver *drv)
  47. {
  48. return 1;
  49. }
  50. static int
  51. mmc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  52. {
  53. struct mmc_card *card = dev_to_mmc_card(dev);
  54. const char *type;
  55. int retval = 0;
  56. switch (card->type) {
  57. case MMC_TYPE_MMC:
  58. type = "MMC";
  59. break;
  60. case MMC_TYPE_SD:
  61. type = "SD";
  62. break;
  63. case MMC_TYPE_SDIO:
  64. type = "SDIO";
  65. break;
  66. default:
  67. type = NULL;
  68. }
  69. if (type) {
  70. retval = add_uevent_var(env, "MMC_TYPE=%s", type);
  71. if (retval)
  72. return retval;
  73. }
  74. retval = add_uevent_var(env, "MMC_NAME=%s", mmc_card_name(card));
  75. return retval;
  76. }
  77. static int mmc_bus_probe(struct device *dev)
  78. {
  79. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  80. struct mmc_card *card = dev_to_mmc_card(dev);
  81. return drv->probe(card);
  82. }
  83. static int mmc_bus_remove(struct device *dev)
  84. {
  85. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  86. struct mmc_card *card = dev_to_mmc_card(dev);
  87. drv->remove(card);
  88. return 0;
  89. }
  90. static int mmc_bus_suspend(struct device *dev, pm_message_t state)
  91. {
  92. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  93. struct mmc_card *card = dev_to_mmc_card(dev);
  94. int ret = 0;
  95. if (dev->driver && drv->suspend)
  96. ret = drv->suspend(card, state);
  97. return ret;
  98. }
  99. static int mmc_bus_resume(struct device *dev)
  100. {
  101. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  102. struct mmc_card *card = dev_to_mmc_card(dev);
  103. int ret = 0;
  104. if (dev->driver && drv->resume)
  105. ret = drv->resume(card);
  106. return ret;
  107. }
  108. static struct bus_type mmc_bus_type = {
  109. .name = "mmc",
  110. .dev_attrs = mmc_dev_attrs,
  111. .match = mmc_bus_match,
  112. .uevent = mmc_bus_uevent,
  113. .probe = mmc_bus_probe,
  114. .remove = mmc_bus_remove,
  115. .suspend = mmc_bus_suspend,
  116. .resume = mmc_bus_resume,
  117. };
  118. int mmc_register_bus(void)
  119. {
  120. return bus_register(&mmc_bus_type);
  121. }
  122. void mmc_unregister_bus(void)
  123. {
  124. bus_unregister(&mmc_bus_type);
  125. }
  126. /**
  127. * mmc_register_driver - register a media driver
  128. * @drv: MMC media driver
  129. */
  130. int mmc_register_driver(struct mmc_driver *drv)
  131. {
  132. drv->drv.bus = &mmc_bus_type;
  133. return driver_register(&drv->drv);
  134. }
  135. EXPORT_SYMBOL(mmc_register_driver);
  136. /**
  137. * mmc_unregister_driver - unregister a media driver
  138. * @drv: MMC media driver
  139. */
  140. void mmc_unregister_driver(struct mmc_driver *drv)
  141. {
  142. drv->drv.bus = &mmc_bus_type;
  143. driver_unregister(&drv->drv);
  144. }
  145. EXPORT_SYMBOL(mmc_unregister_driver);
  146. static void mmc_release_card(struct device *dev)
  147. {
  148. struct mmc_card *card = dev_to_mmc_card(dev);
  149. sdio_free_common_cis(card);
  150. if (card->info)
  151. kfree(card->info);
  152. kfree(card);
  153. }
  154. /*
  155. * Allocate and initialise a new MMC card structure.
  156. */
  157. struct mmc_card *mmc_alloc_card(struct mmc_host *host, struct device_type *type)
  158. {
  159. struct mmc_card *card;
  160. card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
  161. if (!card)
  162. return ERR_PTR(-ENOMEM);
  163. card->host = host;
  164. device_initialize(&card->dev);
  165. card->dev.parent = mmc_classdev(host);
  166. card->dev.bus = &mmc_bus_type;
  167. card->dev.release = mmc_release_card;
  168. card->dev.type = type;
  169. return card;
  170. }
  171. /*
  172. * Register a new MMC card with the driver model.
  173. */
  174. int mmc_add_card(struct mmc_card *card)
  175. {
  176. int ret;
  177. const char *type;
  178. snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
  179. "%s:%04x", mmc_hostname(card->host), card->rca);
  180. switch (card->type) {
  181. case MMC_TYPE_MMC:
  182. type = "MMC";
  183. break;
  184. case MMC_TYPE_SD:
  185. type = "SD";
  186. if (mmc_card_blockaddr(card))
  187. type = "SDHC";
  188. break;
  189. case MMC_TYPE_SDIO:
  190. type = "SDIO";
  191. break;
  192. default:
  193. type = "?";
  194. break;
  195. }
  196. if (mmc_host_is_spi(card->host)) {
  197. printk(KERN_INFO "%s: new %s%s card on SPI\n",
  198. mmc_hostname(card->host),
  199. mmc_card_highspeed(card) ? "high speed " : "",
  200. type);
  201. } else {
  202. printk(KERN_INFO "%s: new %s%s card at address %04x\n",
  203. mmc_hostname(card->host),
  204. mmc_card_highspeed(card) ? "high speed " : "",
  205. type, card->rca);
  206. }
  207. ret = device_add(&card->dev);
  208. if (ret)
  209. return ret;
  210. #ifdef CONFIG_DEBUG_FS
  211. mmc_add_card_debugfs(card);
  212. #endif
  213. mmc_card_set_present(card);
  214. return 0;
  215. }
  216. /*
  217. * Unregister a new MMC card with the driver model, and
  218. * (eventually) free it.
  219. */
  220. void mmc_remove_card(struct mmc_card *card)
  221. {
  222. #ifdef CONFIG_DEBUG_FS
  223. mmc_remove_card_debugfs(card);
  224. #endif
  225. if (mmc_card_present(card)) {
  226. if (mmc_host_is_spi(card->host)) {
  227. printk(KERN_INFO "%s: SPI card removed\n",
  228. mmc_hostname(card->host));
  229. } else {
  230. printk(KERN_INFO "%s: card %04x removed\n",
  231. mmc_hostname(card->host), card->rca);
  232. }
  233. device_del(&card->dev);
  234. }
  235. put_device(&card->dev);
  236. }