mmc_sysfs.c 7.7 KB

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