mmc_sysfs.c 8.4 KB

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