mmc_sysfs.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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/idr.h>
  16. #include <linux/mmc/card.h>
  17. #include <linux/mmc/host.h>
  18. #include "mmc.h"
  19. #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
  20. #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
  21. #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
  22. #define MMC_ATTR(name, fmt, args...) \
  23. static ssize_t mmc_##name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  24. { \
  25. struct mmc_card *card = dev_to_mmc_card(dev); \
  26. return sprintf(buf, fmt, args); \
  27. }
  28. MMC_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  29. card->raw_cid[2], card->raw_cid[3]);
  30. MMC_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  31. card->raw_csd[2], card->raw_csd[3]);
  32. MMC_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  33. MMC_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  34. MMC_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  35. MMC_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  36. MMC_ATTR(name, "%s\n", card->cid.prod_name);
  37. MMC_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  38. MMC_ATTR(serial, "0x%08x\n", card->cid.serial);
  39. #define MMC_ATTR_RO(name) __ATTR(name, S_IRUGO, mmc_##name##_show, NULL)
  40. static struct device_attribute mmc_dev_attrs[] = {
  41. MMC_ATTR_RO(cid),
  42. MMC_ATTR_RO(csd),
  43. MMC_ATTR_RO(date),
  44. MMC_ATTR_RO(fwrev),
  45. MMC_ATTR_RO(hwrev),
  46. MMC_ATTR_RO(manfid),
  47. MMC_ATTR_RO(name),
  48. MMC_ATTR_RO(oemid),
  49. MMC_ATTR_RO(serial),
  50. __ATTR_NULL
  51. };
  52. static void mmc_release_card(struct device *dev)
  53. {
  54. struct mmc_card *card = dev_to_mmc_card(dev);
  55. kfree(card);
  56. }
  57. /*
  58. * This currently matches any MMC driver to any MMC card - drivers
  59. * themselves make the decision whether to drive this card in their
  60. * probe method. However, we force "bad" cards to fail.
  61. */
  62. static int mmc_bus_match(struct device *dev, struct device_driver *drv)
  63. {
  64. struct mmc_card *card = dev_to_mmc_card(dev);
  65. return !mmc_card_bad(card);
  66. }
  67. static int
  68. mmc_bus_hotplug(struct device *dev, char **envp, int num_envp, char *buf,
  69. int buf_size)
  70. {
  71. struct mmc_card *card = dev_to_mmc_card(dev);
  72. char ccc[13];
  73. int i = 0;
  74. #define add_env(fmt,val) \
  75. ({ \
  76. int len, ret = -ENOMEM; \
  77. if (i < num_envp) { \
  78. envp[i++] = buf; \
  79. len = snprintf(buf, buf_size, fmt, val) + 1; \
  80. buf_size -= len; \
  81. buf += len; \
  82. if (buf_size >= 0) \
  83. ret = 0; \
  84. } \
  85. ret; \
  86. })
  87. for (i = 0; i < 12; i++)
  88. ccc[i] = card->csd.cmdclass & (1 << i) ? '1' : '0';
  89. ccc[12] = '\0';
  90. i = 0;
  91. add_env("MMC_CCC=%s", ccc);
  92. add_env("MMC_MANFID=%06x", card->cid.manfid);
  93. add_env("MMC_NAME=%s", mmc_card_name(card));
  94. add_env("MMC_OEMID=%04x", card->cid.oemid);
  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. .hotplug = mmc_bus_hotplug,
  120. .suspend = mmc_bus_suspend,
  121. .resume = mmc_bus_resume,
  122. };
  123. static int mmc_drv_probe(struct device *dev)
  124. {
  125. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  126. struct mmc_card *card = dev_to_mmc_card(dev);
  127. return drv->probe(card);
  128. }
  129. static int mmc_drv_remove(struct device *dev)
  130. {
  131. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  132. struct mmc_card *card = dev_to_mmc_card(dev);
  133. drv->remove(card);
  134. return 0;
  135. }
  136. /**
  137. * mmc_register_driver - register a media driver
  138. * @drv: MMC media driver
  139. */
  140. int mmc_register_driver(struct mmc_driver *drv)
  141. {
  142. drv->drv.bus = &mmc_bus_type;
  143. drv->drv.probe = mmc_drv_probe;
  144. drv->drv.remove = mmc_drv_remove;
  145. return driver_register(&drv->drv);
  146. }
  147. EXPORT_SYMBOL(mmc_register_driver);
  148. /**
  149. * mmc_unregister_driver - unregister a media driver
  150. * @drv: MMC media driver
  151. */
  152. void mmc_unregister_driver(struct mmc_driver *drv)
  153. {
  154. drv->drv.bus = &mmc_bus_type;
  155. driver_unregister(&drv->drv);
  156. }
  157. EXPORT_SYMBOL(mmc_unregister_driver);
  158. /*
  159. * Internal function. Initialise a MMC card structure.
  160. */
  161. void mmc_init_card(struct mmc_card *card, struct mmc_host *host)
  162. {
  163. memset(card, 0, sizeof(struct mmc_card));
  164. card->host = host;
  165. device_initialize(&card->dev);
  166. card->dev.parent = card->host->dev;
  167. card->dev.bus = &mmc_bus_type;
  168. card->dev.release = mmc_release_card;
  169. }
  170. /*
  171. * Internal function. Register a new MMC card with the driver model.
  172. */
  173. int mmc_register_card(struct mmc_card *card)
  174. {
  175. snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
  176. "%s:%04x", mmc_hostname(card->host), card->rca);
  177. return device_add(&card->dev);
  178. }
  179. /*
  180. * Internal function. Unregister a new MMC card with the
  181. * driver model, and (eventually) free it.
  182. */
  183. void mmc_remove_card(struct mmc_card *card)
  184. {
  185. if (mmc_card_present(card))
  186. device_del(&card->dev);
  187. put_device(&card->dev);
  188. }
  189. static void mmc_host_classdev_release(struct class_device *dev)
  190. {
  191. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  192. kfree(host);
  193. }
  194. static struct class mmc_host_class = {
  195. .name = "mmc_host",
  196. .release = mmc_host_classdev_release,
  197. };
  198. static DEFINE_IDR(mmc_host_idr);
  199. static DEFINE_SPINLOCK(mmc_host_lock);
  200. /*
  201. * Internal function. Allocate a new MMC host.
  202. */
  203. struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev)
  204. {
  205. struct mmc_host *host;
  206. host = kmalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
  207. if (host) {
  208. memset(host, 0, sizeof(struct mmc_host) + extra);
  209. host->dev = dev;
  210. host->class_dev.dev = host->dev;
  211. host->class_dev.class = &mmc_host_class;
  212. class_device_initialize(&host->class_dev);
  213. }
  214. return host;
  215. }
  216. /*
  217. * Internal function. Register a new MMC host with the MMC class.
  218. */
  219. int mmc_add_host_sysfs(struct mmc_host *host)
  220. {
  221. int err;
  222. if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
  223. return -ENOMEM;
  224. spin_lock(&mmc_host_lock);
  225. err = idr_get_new(&mmc_host_idr, host, &host->index);
  226. spin_unlock(&mmc_host_lock);
  227. if (err)
  228. return err;
  229. snprintf(host->class_dev.class_id, BUS_ID_SIZE,
  230. "mmc%d", host->index);
  231. return class_device_add(&host->class_dev);
  232. }
  233. /*
  234. * Internal function. Unregister a MMC host with the MMC class.
  235. */
  236. void mmc_remove_host_sysfs(struct mmc_host *host)
  237. {
  238. class_device_del(&host->class_dev);
  239. spin_lock(&mmc_host_lock);
  240. idr_remove(&mmc_host_idr, host->index);
  241. spin_unlock(&mmc_host_lock);
  242. }
  243. /*
  244. * Internal function. Free a MMC host.
  245. */
  246. void mmc_free_host_sysfs(struct mmc_host *host)
  247. {
  248. class_device_put(&host->class_dev);
  249. }
  250. static int __init mmc_init(void)
  251. {
  252. int ret = bus_register(&mmc_bus_type);
  253. if (ret == 0) {
  254. ret = class_register(&mmc_host_class);
  255. if (ret)
  256. bus_unregister(&mmc_bus_type);
  257. }
  258. return ret;
  259. }
  260. static void __exit mmc_exit(void)
  261. {
  262. class_unregister(&mmc_host_class);
  263. bus_unregister(&mmc_bus_type);
  264. }
  265. module_init(mmc_init);
  266. module_exit(mmc_exit);