bus.c 5.9 KB

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