mmc_sysfs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * linux/drivers/mmc/mmc_sysfs.c
  3. *
  4. * Copyright (C) 2003 Russell King, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * MMC sysfs/driver model support.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/device.h>
  15. #include <linux/mmc/card.h>
  16. #include <linux/mmc/host.h>
  17. #include "mmc.h"
  18. #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
  19. #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
  20. #define MMC_ATTR(name, fmt, args...) \
  21. static ssize_t mmc_##name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  22. { \
  23. struct mmc_card *card = dev_to_mmc_card(dev); \
  24. return sprintf(buf, fmt, args); \
  25. }
  26. MMC_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  27. card->raw_cid[2], card->raw_cid[3]);
  28. MMC_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  29. card->raw_csd[2], card->raw_csd[3]);
  30. MMC_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  31. MMC_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  32. MMC_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  33. MMC_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  34. MMC_ATTR(name, "%s\n", card->cid.prod_name);
  35. MMC_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  36. MMC_ATTR(serial, "0x%08x\n", card->cid.serial);
  37. #define MMC_ATTR_RO(name) __ATTR(name, S_IRUGO, mmc_##name##_show, NULL)
  38. static struct device_attribute mmc_dev_attrs[] = {
  39. MMC_ATTR_RO(cid),
  40. MMC_ATTR_RO(csd),
  41. MMC_ATTR_RO(date),
  42. MMC_ATTR_RO(fwrev),
  43. MMC_ATTR_RO(hwrev),
  44. MMC_ATTR_RO(manfid),
  45. MMC_ATTR_RO(name),
  46. MMC_ATTR_RO(oemid),
  47. MMC_ATTR_RO(serial),
  48. __ATTR_NULL
  49. };
  50. static void mmc_release_card(struct device *dev)
  51. {
  52. struct mmc_card *card = dev_to_mmc_card(dev);
  53. kfree(card);
  54. }
  55. /*
  56. * This currently matches any MMC driver to any MMC card - drivers
  57. * themselves make the decision whether to drive this card in their
  58. * probe method. However, we force "bad" cards to fail.
  59. */
  60. static int mmc_bus_match(struct device *dev, struct device_driver *drv)
  61. {
  62. struct mmc_card *card = dev_to_mmc_card(dev);
  63. return !mmc_card_bad(card);
  64. }
  65. static int
  66. mmc_bus_hotplug(struct device *dev, char **envp, int num_envp, char *buf,
  67. int buf_size)
  68. {
  69. struct mmc_card *card = dev_to_mmc_card(dev);
  70. char ccc[13];
  71. int i = 0;
  72. #define add_env(fmt,val) \
  73. ({ \
  74. int len, ret = -ENOMEM; \
  75. if (i < num_envp) { \
  76. envp[i++] = buf; \
  77. len = snprintf(buf, buf_size, fmt, val) + 1; \
  78. buf_size -= len; \
  79. buf += len; \
  80. if (buf_size >= 0) \
  81. ret = 0; \
  82. } \
  83. ret; \
  84. })
  85. for (i = 0; i < 12; i++)
  86. ccc[i] = card->csd.cmdclass & (1 << i) ? '1' : '0';
  87. ccc[12] = '\0';
  88. i = 0;
  89. add_env("MMC_CCC=%s", ccc);
  90. add_env("MMC_MANFID=%06x", card->cid.manfid);
  91. add_env("MMC_NAME=%s", mmc_card_name(card));
  92. add_env("MMC_OEMID=%04x", card->cid.oemid);
  93. return 0;
  94. }
  95. static int mmc_bus_suspend(struct device *dev, pm_message_t state)
  96. {
  97. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  98. struct mmc_card *card = dev_to_mmc_card(dev);
  99. int ret = 0;
  100. if (dev->driver && drv->suspend)
  101. ret = drv->suspend(card, state);
  102. return ret;
  103. }
  104. static int mmc_bus_resume(struct device *dev)
  105. {
  106. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  107. struct mmc_card *card = dev_to_mmc_card(dev);
  108. int ret = 0;
  109. if (dev->driver && drv->resume)
  110. ret = drv->resume(card);
  111. return ret;
  112. }
  113. static struct bus_type mmc_bus_type = {
  114. .name = "mmc",
  115. .dev_attrs = mmc_dev_attrs,
  116. .match = mmc_bus_match,
  117. .hotplug = mmc_bus_hotplug,
  118. .suspend = mmc_bus_suspend,
  119. .resume = mmc_bus_resume,
  120. };
  121. static int mmc_drv_probe(struct device *dev)
  122. {
  123. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  124. struct mmc_card *card = dev_to_mmc_card(dev);
  125. return drv->probe(card);
  126. }
  127. static int mmc_drv_remove(struct device *dev)
  128. {
  129. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  130. struct mmc_card *card = dev_to_mmc_card(dev);
  131. drv->remove(card);
  132. return 0;
  133. }
  134. /**
  135. * mmc_register_driver - register a media driver
  136. * @drv: MMC media driver
  137. */
  138. int mmc_register_driver(struct mmc_driver *drv)
  139. {
  140. drv->drv.bus = &mmc_bus_type;
  141. drv->drv.probe = mmc_drv_probe;
  142. drv->drv.remove = mmc_drv_remove;
  143. return driver_register(&drv->drv);
  144. }
  145. EXPORT_SYMBOL(mmc_register_driver);
  146. /**
  147. * mmc_unregister_driver - unregister a media driver
  148. * @drv: MMC media driver
  149. */
  150. void mmc_unregister_driver(struct mmc_driver *drv)
  151. {
  152. drv->drv.bus = &mmc_bus_type;
  153. driver_unregister(&drv->drv);
  154. }
  155. EXPORT_SYMBOL(mmc_unregister_driver);
  156. /*
  157. * Internal function. Initialise a MMC card structure.
  158. */
  159. void mmc_init_card(struct mmc_card *card, struct mmc_host *host)
  160. {
  161. memset(card, 0, sizeof(struct mmc_card));
  162. card->host = host;
  163. device_initialize(&card->dev);
  164. card->dev.parent = card->host->dev;
  165. card->dev.bus = &mmc_bus_type;
  166. card->dev.release = mmc_release_card;
  167. }
  168. /*
  169. * Internal function. Register a new MMC card with the driver model.
  170. */
  171. int mmc_register_card(struct mmc_card *card)
  172. {
  173. snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
  174. "%s:%04x", card->host->host_name, card->rca);
  175. return device_add(&card->dev);
  176. }
  177. /*
  178. * Internal function. Unregister a new MMC card with the
  179. * driver model, and (eventually) free it.
  180. */
  181. void mmc_remove_card(struct mmc_card *card)
  182. {
  183. if (mmc_card_present(card))
  184. device_del(&card->dev);
  185. put_device(&card->dev);
  186. }
  187. static int __init mmc_init(void)
  188. {
  189. return bus_register(&mmc_bus_type);
  190. }
  191. static void __exit mmc_exit(void)
  192. {
  193. bus_unregister(&mmc_bus_type);
  194. }
  195. module_init(mmc_init);
  196. module_exit(mmc_exit);