mmc_sysfs.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/workqueue.h>
  17. #include <linux/mmc/card.h>
  18. #include <linux/mmc/host.h>
  19. #include "mmc.h"
  20. #define dev_to_mmc_card(d) container_of(d, struct mmc_card, dev)
  21. #define to_mmc_driver(d) container_of(d, struct mmc_driver, drv)
  22. #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
  23. #define MMC_ATTR(name, fmt, args...) \
  24. static ssize_t mmc_##name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  25. { \
  26. struct mmc_card *card = dev_to_mmc_card(dev); \
  27. return sprintf(buf, fmt, args); \
  28. }
  29. MMC_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
  30. card->raw_cid[2], card->raw_cid[3]);
  31. MMC_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
  32. card->raw_csd[2], card->raw_csd[3]);
  33. MMC_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
  34. MMC_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
  35. MMC_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
  36. MMC_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
  37. MMC_ATTR(manfid, "0x%06x\n", card->cid.manfid);
  38. MMC_ATTR(name, "%s\n", card->cid.prod_name);
  39. MMC_ATTR(oemid, "0x%04x\n", card->cid.oemid);
  40. MMC_ATTR(serial, "0x%08x\n", card->cid.serial);
  41. #define MMC_ATTR_RO(name) __ATTR(name, S_IRUGO, mmc_##name##_show, NULL)
  42. static struct device_attribute mmc_dev_attrs[] = {
  43. MMC_ATTR_RO(cid),
  44. MMC_ATTR_RO(csd),
  45. MMC_ATTR_RO(date),
  46. MMC_ATTR_RO(fwrev),
  47. MMC_ATTR_RO(hwrev),
  48. MMC_ATTR_RO(manfid),
  49. MMC_ATTR_RO(name),
  50. MMC_ATTR_RO(oemid),
  51. MMC_ATTR_RO(serial),
  52. __ATTR_NULL
  53. };
  54. static struct device_attribute mmc_dev_attr_scr = MMC_ATTR_RO(scr);
  55. static void mmc_release_card(struct device *dev)
  56. {
  57. struct mmc_card *card = dev_to_mmc_card(dev);
  58. kfree(card);
  59. }
  60. /*
  61. * This currently matches any MMC driver to any MMC card - drivers
  62. * themselves make the decision whether to drive this card in their
  63. * probe method. However, we force "bad" cards to fail.
  64. */
  65. static int mmc_bus_match(struct device *dev, struct device_driver *drv)
  66. {
  67. struct mmc_card *card = dev_to_mmc_card(dev);
  68. return !mmc_card_bad(card);
  69. }
  70. static int
  71. mmc_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf,
  72. int buf_size)
  73. {
  74. struct mmc_card *card = dev_to_mmc_card(dev);
  75. char ccc[13];
  76. int retval = 0, i = 0, length = 0;
  77. #define add_env(fmt,val) do { \
  78. retval = add_uevent_var(envp, num_envp, &i, \
  79. buf, buf_size, &length, \
  80. fmt, val); \
  81. if (retval) \
  82. return retval; \
  83. } while (0);
  84. for (i = 0; i < 12; i++)
  85. ccc[i] = card->csd.cmdclass & (1 << i) ? '1' : '0';
  86. ccc[12] = '\0';
  87. add_env("MMC_CCC=%s", ccc);
  88. add_env("MMC_MANFID=%06x", card->cid.manfid);
  89. add_env("MMC_NAME=%s", mmc_card_name(card));
  90. add_env("MMC_OEMID=%04x", card->cid.oemid);
  91. #undef add_env
  92. envp[i] = NULL;
  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 int mmc_bus_probe(struct device *dev)
  114. {
  115. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  116. struct mmc_card *card = dev_to_mmc_card(dev);
  117. return drv->probe(card);
  118. }
  119. static int mmc_bus_remove(struct device *dev)
  120. {
  121. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  122. struct mmc_card *card = dev_to_mmc_card(dev);
  123. drv->remove(card);
  124. return 0;
  125. }
  126. static struct bus_type mmc_bus_type = {
  127. .name = "mmc",
  128. .dev_attrs = mmc_dev_attrs,
  129. .match = mmc_bus_match,
  130. .uevent = mmc_bus_uevent,
  131. .probe = mmc_bus_probe,
  132. .remove = mmc_bus_remove,
  133. .suspend = mmc_bus_suspend,
  134. .resume = mmc_bus_resume,
  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. 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 = mmc_classdev(host);
  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. int ret;
  174. snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
  175. "%s:%04x", mmc_hostname(card->host), card->rca);
  176. ret = device_add(&card->dev);
  177. if (ret == 0) {
  178. if (mmc_card_sd(card)) {
  179. ret = device_create_file(&card->dev, &mmc_dev_attr_scr);
  180. if (ret)
  181. device_del(&card->dev);
  182. }
  183. }
  184. if (ret == 0)
  185. mmc_card_set_present(card);
  186. return ret;
  187. }
  188. /*
  189. * Internal function. Unregister a new MMC card with the
  190. * driver model, and (eventually) free it.
  191. */
  192. void mmc_remove_card(struct mmc_card *card)
  193. {
  194. if (mmc_card_present(card)) {
  195. if (mmc_card_sd(card))
  196. device_remove_file(&card->dev, &mmc_dev_attr_scr);
  197. device_del(&card->dev);
  198. }
  199. put_device(&card->dev);
  200. }
  201. static void mmc_host_classdev_release(struct device *dev)
  202. {
  203. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  204. kfree(host);
  205. }
  206. static struct class mmc_host_class = {
  207. .name = "mmc_host",
  208. .dev_release = mmc_host_classdev_release,
  209. };
  210. static DEFINE_IDR(mmc_host_idr);
  211. static DEFINE_SPINLOCK(mmc_host_lock);
  212. /*
  213. * Internal function. Allocate a new MMC host.
  214. */
  215. struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev)
  216. {
  217. struct mmc_host *host;
  218. host = kmalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
  219. if (host) {
  220. memset(host, 0, sizeof(struct mmc_host) + extra);
  221. host->parent = dev;
  222. host->class_dev.parent = dev;
  223. host->class_dev.class = &mmc_host_class;
  224. device_initialize(&host->class_dev);
  225. }
  226. return host;
  227. }
  228. /*
  229. * Internal function. Register a new MMC host with the MMC class.
  230. */
  231. int mmc_add_host_sysfs(struct mmc_host *host)
  232. {
  233. int err;
  234. if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
  235. return -ENOMEM;
  236. spin_lock(&mmc_host_lock);
  237. err = idr_get_new(&mmc_host_idr, host, &host->index);
  238. spin_unlock(&mmc_host_lock);
  239. if (err)
  240. return err;
  241. snprintf(host->class_dev.bus_id, BUS_ID_SIZE,
  242. "mmc%d", host->index);
  243. return device_add(&host->class_dev);
  244. }
  245. /*
  246. * Internal function. Unregister a MMC host with the MMC class.
  247. */
  248. void mmc_remove_host_sysfs(struct mmc_host *host)
  249. {
  250. device_del(&host->class_dev);
  251. spin_lock(&mmc_host_lock);
  252. idr_remove(&mmc_host_idr, host->index);
  253. spin_unlock(&mmc_host_lock);
  254. }
  255. /*
  256. * Internal function. Free a MMC host.
  257. */
  258. void mmc_free_host_sysfs(struct mmc_host *host)
  259. {
  260. put_device(&host->class_dev);
  261. }
  262. static struct workqueue_struct *workqueue;
  263. /*
  264. * Internal function. Schedule delayed work in the MMC work queue.
  265. */
  266. int mmc_schedule_delayed_work(struct delayed_work *work, unsigned long delay)
  267. {
  268. return queue_delayed_work(workqueue, work, delay);
  269. }
  270. /*
  271. * Internal function. Flush all scheduled work from the MMC work queue.
  272. */
  273. void mmc_flush_scheduled_work(void)
  274. {
  275. flush_workqueue(workqueue);
  276. }
  277. static int __init mmc_init(void)
  278. {
  279. int ret;
  280. workqueue = create_singlethread_workqueue("kmmcd");
  281. if (!workqueue)
  282. return -ENOMEM;
  283. ret = bus_register(&mmc_bus_type);
  284. if (ret == 0) {
  285. ret = class_register(&mmc_host_class);
  286. if (ret)
  287. bus_unregister(&mmc_bus_type);
  288. }
  289. return ret;
  290. }
  291. static void __exit mmc_exit(void)
  292. {
  293. class_unregister(&mmc_host_class);
  294. bus_unregister(&mmc_bus_type);
  295. destroy_workqueue(workqueue);
  296. }
  297. module_init(mmc_init);
  298. module_exit(mmc_exit);