bus.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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, char **envp, int num_envp, char *buf,
  53. int buf_size)
  54. {
  55. struct mmc_card *card = dev_to_mmc_card(dev);
  56. const char *type;
  57. int i = 0, length = 0;
  58. switch (card->type) {
  59. case MMC_TYPE_MMC:
  60. type = "MMC";
  61. break;
  62. case MMC_TYPE_SD:
  63. type = "SD";
  64. break;
  65. case MMC_TYPE_SDIO:
  66. type = "SDIO";
  67. break;
  68. default:
  69. type = NULL;
  70. }
  71. if (type) {
  72. if (add_uevent_var(envp, num_envp, &i,
  73. buf, buf_size, &length,
  74. "MMC_TYPE=%s", type))
  75. return -ENOMEM;
  76. }
  77. if (add_uevent_var(envp, num_envp, &i,
  78. buf, buf_size, &length,
  79. "MMC_NAME=%s", mmc_card_name(card)))
  80. return -ENOMEM;
  81. envp[i] = NULL;
  82. return 0;
  83. }
  84. static int mmc_bus_probe(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. return drv->probe(card);
  89. }
  90. static int mmc_bus_remove(struct device *dev)
  91. {
  92. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  93. struct mmc_card *card = dev_to_mmc_card(dev);
  94. drv->remove(card);
  95. return 0;
  96. }
  97. static int mmc_bus_suspend(struct device *dev, pm_message_t state)
  98. {
  99. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  100. struct mmc_card *card = dev_to_mmc_card(dev);
  101. int ret = 0;
  102. if (dev->driver && drv->suspend)
  103. ret = drv->suspend(card, state);
  104. return ret;
  105. }
  106. static int mmc_bus_resume(struct device *dev)
  107. {
  108. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  109. struct mmc_card *card = dev_to_mmc_card(dev);
  110. int ret = 0;
  111. if (dev->driver && drv->resume)
  112. ret = drv->resume(card);
  113. return ret;
  114. }
  115. static struct bus_type mmc_bus_type = {
  116. .name = "mmc",
  117. .dev_attrs = mmc_dev_attrs,
  118. .match = mmc_bus_match,
  119. .uevent = mmc_bus_uevent,
  120. .probe = mmc_bus_probe,
  121. .remove = mmc_bus_remove,
  122. .suspend = mmc_bus_suspend,
  123. .resume = mmc_bus_resume,
  124. };
  125. int mmc_register_bus(void)
  126. {
  127. return bus_register(&mmc_bus_type);
  128. }
  129. void mmc_unregister_bus(void)
  130. {
  131. bus_unregister(&mmc_bus_type);
  132. }
  133. /**
  134. * mmc_register_driver - register a media driver
  135. * @drv: MMC media driver
  136. */
  137. int mmc_register_driver(struct mmc_driver *drv)
  138. {
  139. drv->drv.bus = &mmc_bus_type;
  140. return driver_register(&drv->drv);
  141. }
  142. EXPORT_SYMBOL(mmc_register_driver);
  143. /**
  144. * mmc_unregister_driver - unregister a media driver
  145. * @drv: MMC media driver
  146. */
  147. void mmc_unregister_driver(struct mmc_driver *drv)
  148. {
  149. drv->drv.bus = &mmc_bus_type;
  150. driver_unregister(&drv->drv);
  151. }
  152. EXPORT_SYMBOL(mmc_unregister_driver);
  153. static void mmc_release_card(struct device *dev)
  154. {
  155. struct mmc_card *card = dev_to_mmc_card(dev);
  156. sdio_free_common_cis(card);
  157. if (card->info)
  158. kfree(card->info);
  159. kfree(card);
  160. }
  161. /*
  162. * Allocate and initialise a new MMC card structure.
  163. */
  164. struct mmc_card *mmc_alloc_card(struct mmc_host *host)
  165. {
  166. struct mmc_card *card;
  167. card = kzalloc(sizeof(struct mmc_card), GFP_KERNEL);
  168. if (!card)
  169. return ERR_PTR(-ENOMEM);
  170. card->host = host;
  171. device_initialize(&card->dev);
  172. card->dev.parent = mmc_classdev(host);
  173. card->dev.bus = &mmc_bus_type;
  174. card->dev.release = mmc_release_card;
  175. return card;
  176. }
  177. /*
  178. * Register a new MMC card with the driver model.
  179. */
  180. int mmc_add_card(struct mmc_card *card)
  181. {
  182. int ret;
  183. const char *type;
  184. snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
  185. "%s:%04x", mmc_hostname(card->host), card->rca);
  186. switch (card->type) {
  187. case MMC_TYPE_MMC:
  188. type = "MMC";
  189. break;
  190. case MMC_TYPE_SD:
  191. type = "SD";
  192. if (mmc_card_blockaddr(card))
  193. type = "SDHC";
  194. break;
  195. case MMC_TYPE_SDIO:
  196. type = "SDIO";
  197. break;
  198. default:
  199. type = "?";
  200. break;
  201. }
  202. if (mmc_host_is_spi(card->host)) {
  203. printk(KERN_INFO "%s: new %s%s card on SPI\n",
  204. mmc_hostname(card->host),
  205. mmc_card_highspeed(card) ? "high speed " : "",
  206. type);
  207. } else {
  208. printk(KERN_INFO "%s: new %s%s card at address %04x\n",
  209. mmc_hostname(card->host),
  210. mmc_card_highspeed(card) ? "high speed " : "",
  211. type, card->rca);
  212. }
  213. card->dev.uevent_suppress = 1;
  214. ret = device_add(&card->dev);
  215. if (ret)
  216. return ret;
  217. if (card->host->bus_ops->sysfs_add) {
  218. ret = card->host->bus_ops->sysfs_add(card->host, card);
  219. if (ret) {
  220. device_del(&card->dev);
  221. return ret;
  222. }
  223. }
  224. card->dev.uevent_suppress = 0;
  225. kobject_uevent(&card->dev.kobj, KOBJ_ADD);
  226. mmc_card_set_present(card);
  227. return 0;
  228. }
  229. /*
  230. * Unregister a new MMC card with the driver model, and
  231. * (eventually) free it.
  232. */
  233. void mmc_remove_card(struct mmc_card *card)
  234. {
  235. if (mmc_card_present(card)) {
  236. if (mmc_host_is_spi(card->host)) {
  237. printk(KERN_INFO "%s: SPI card removed\n",
  238. mmc_hostname(card->host));
  239. } else {
  240. printk(KERN_INFO "%s: card %04x removed\n",
  241. mmc_hostname(card->host), card->rca);
  242. }
  243. if (card->host->bus_ops->sysfs_remove)
  244. card->host->bus_ops->sysfs_remove(card->host, card);
  245. device_del(&card->dev);
  246. }
  247. put_device(&card->dev);
  248. }