sysfs.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * linux/drivers/mmc/core/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 "sysfs.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.
  64. */
  65. static int mmc_bus_match(struct device *dev, struct device_driver *drv)
  66. {
  67. return 1;
  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 retval = 0, i = 0, length = 0;
  76. #define add_env(fmt,val) do { \
  77. retval = add_uevent_var(envp, num_envp, &i, \
  78. buf, buf_size, &length, \
  79. fmt, val); \
  80. if (retval) \
  81. return retval; \
  82. } while (0);
  83. for (i = 0; i < 12; i++)
  84. ccc[i] = card->csd.cmdclass & (1 << i) ? '1' : '0';
  85. ccc[12] = '\0';
  86. add_env("MMC_CCC=%s", ccc);
  87. add_env("MMC_MANFID=%06x", card->cid.manfid);
  88. add_env("MMC_NAME=%s", mmc_card_name(card));
  89. add_env("MMC_OEMID=%04x", card->cid.oemid);
  90. #undef add_env
  91. envp[i] = NULL;
  92. return 0;
  93. }
  94. static int mmc_bus_suspend(struct device *dev, pm_message_t state)
  95. {
  96. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  97. struct mmc_card *card = dev_to_mmc_card(dev);
  98. int ret = 0;
  99. if (dev->driver && drv->suspend)
  100. ret = drv->suspend(card, state);
  101. return ret;
  102. }
  103. static int mmc_bus_resume(struct device *dev)
  104. {
  105. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  106. struct mmc_card *card = dev_to_mmc_card(dev);
  107. int ret = 0;
  108. if (dev->driver && drv->resume)
  109. ret = drv->resume(card);
  110. return ret;
  111. }
  112. static int mmc_bus_probe(struct device *dev)
  113. {
  114. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  115. struct mmc_card *card = dev_to_mmc_card(dev);
  116. return drv->probe(card);
  117. }
  118. static int mmc_bus_remove(struct device *dev)
  119. {
  120. struct mmc_driver *drv = to_mmc_driver(dev->driver);
  121. struct mmc_card *card = dev_to_mmc_card(dev);
  122. drv->remove(card);
  123. return 0;
  124. }
  125. static struct bus_type mmc_bus_type = {
  126. .name = "mmc",
  127. .dev_attrs = mmc_dev_attrs,
  128. .match = mmc_bus_match,
  129. .uevent = mmc_bus_uevent,
  130. .probe = mmc_bus_probe,
  131. .remove = mmc_bus_remove,
  132. .suspend = mmc_bus_suspend,
  133. .resume = mmc_bus_resume,
  134. };
  135. /**
  136. * mmc_register_driver - register a media driver
  137. * @drv: MMC media driver
  138. */
  139. int mmc_register_driver(struct mmc_driver *drv)
  140. {
  141. drv->drv.bus = &mmc_bus_type;
  142. return driver_register(&drv->drv);
  143. }
  144. EXPORT_SYMBOL(mmc_register_driver);
  145. /**
  146. * mmc_unregister_driver - unregister a media driver
  147. * @drv: MMC media driver
  148. */
  149. void mmc_unregister_driver(struct mmc_driver *drv)
  150. {
  151. drv->drv.bus = &mmc_bus_type;
  152. driver_unregister(&drv->drv);
  153. }
  154. EXPORT_SYMBOL(mmc_unregister_driver);
  155. /*
  156. * Internal function. Initialise a MMC card structure.
  157. */
  158. void mmc_init_card(struct mmc_card *card, struct mmc_host *host)
  159. {
  160. memset(card, 0, sizeof(struct mmc_card));
  161. card->host = host;
  162. device_initialize(&card->dev);
  163. card->dev.parent = mmc_classdev(host);
  164. card->dev.bus = &mmc_bus_type;
  165. card->dev.release = mmc_release_card;
  166. }
  167. /*
  168. * Internal function. Register a new MMC card with the driver model.
  169. */
  170. int mmc_register_card(struct mmc_card *card)
  171. {
  172. int ret;
  173. snprintf(card->dev.bus_id, sizeof(card->dev.bus_id),
  174. "%s:%04x", mmc_hostname(card->host), card->rca);
  175. ret = device_add(&card->dev);
  176. if (ret == 0) {
  177. if (mmc_card_sd(card)) {
  178. ret = device_create_file(&card->dev, &mmc_dev_attr_scr);
  179. if (ret)
  180. device_del(&card->dev);
  181. }
  182. }
  183. if (ret == 0)
  184. mmc_card_set_present(card);
  185. return ret;
  186. }
  187. /*
  188. * Internal function. Unregister a new MMC card with the
  189. * driver model, and (eventually) free it.
  190. */
  191. void mmc_remove_card(struct mmc_card *card)
  192. {
  193. if (mmc_card_present(card)) {
  194. if (mmc_card_sd(card))
  195. device_remove_file(&card->dev, &mmc_dev_attr_scr);
  196. device_del(&card->dev);
  197. }
  198. put_device(&card->dev);
  199. }
  200. static void mmc_host_classdev_release(struct device *dev)
  201. {
  202. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  203. kfree(host);
  204. }
  205. static struct class mmc_host_class = {
  206. .name = "mmc_host",
  207. .dev_release = mmc_host_classdev_release,
  208. };
  209. static DEFINE_IDR(mmc_host_idr);
  210. static DEFINE_SPINLOCK(mmc_host_lock);
  211. /*
  212. * Internal function. Allocate a new MMC host.
  213. */
  214. struct mmc_host *mmc_alloc_host_sysfs(int extra, struct device *dev)
  215. {
  216. struct mmc_host *host;
  217. host = kmalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
  218. if (host) {
  219. memset(host, 0, sizeof(struct mmc_host) + extra);
  220. host->parent = dev;
  221. host->class_dev.parent = dev;
  222. host->class_dev.class = &mmc_host_class;
  223. device_initialize(&host->class_dev);
  224. }
  225. return host;
  226. }
  227. /*
  228. * Internal function. Register a new MMC host with the MMC class.
  229. */
  230. int mmc_add_host_sysfs(struct mmc_host *host)
  231. {
  232. int err;
  233. if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
  234. return -ENOMEM;
  235. spin_lock(&mmc_host_lock);
  236. err = idr_get_new(&mmc_host_idr, host, &host->index);
  237. spin_unlock(&mmc_host_lock);
  238. if (err)
  239. return err;
  240. snprintf(host->class_dev.bus_id, BUS_ID_SIZE,
  241. "mmc%d", host->index);
  242. return device_add(&host->class_dev);
  243. }
  244. /*
  245. * Internal function. Unregister a MMC host with the MMC class.
  246. */
  247. void mmc_remove_host_sysfs(struct mmc_host *host)
  248. {
  249. device_del(&host->class_dev);
  250. spin_lock(&mmc_host_lock);
  251. idr_remove(&mmc_host_idr, host->index);
  252. spin_unlock(&mmc_host_lock);
  253. }
  254. /*
  255. * Internal function. Free a MMC host.
  256. */
  257. void mmc_free_host_sysfs(struct mmc_host *host)
  258. {
  259. put_device(&host->class_dev);
  260. }
  261. static struct workqueue_struct *workqueue;
  262. /*
  263. * Internal function. Schedule delayed work in the MMC work queue.
  264. */
  265. int mmc_schedule_delayed_work(struct delayed_work *work, unsigned long delay)
  266. {
  267. return queue_delayed_work(workqueue, work, delay);
  268. }
  269. /*
  270. * Internal function. Flush all scheduled work from the MMC work queue.
  271. */
  272. void mmc_flush_scheduled_work(void)
  273. {
  274. flush_workqueue(workqueue);
  275. }
  276. static int __init mmc_init(void)
  277. {
  278. int ret;
  279. workqueue = create_singlethread_workqueue("kmmcd");
  280. if (!workqueue)
  281. return -ENOMEM;
  282. ret = bus_register(&mmc_bus_type);
  283. if (ret == 0) {
  284. ret = class_register(&mmc_host_class);
  285. if (ret)
  286. bus_unregister(&mmc_bus_type);
  287. }
  288. return ret;
  289. }
  290. static void __exit mmc_exit(void)
  291. {
  292. class_unregister(&mmc_host_class);
  293. bus_unregister(&mmc_bus_type);
  294. destroy_workqueue(workqueue);
  295. }
  296. module_init(mmc_init);
  297. module_exit(mmc_exit);