sdio_bus.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * linux/drivers/mmc/core/sdio_bus.c
  3. *
  4. * Copyright 2007 Pierre Ossman
  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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * SDIO function driver model
  12. */
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/mmc/card.h>
  18. #include <linux/mmc/host.h>
  19. #include <linux/mmc/sdio_func.h>
  20. #include "sdio_cis.h"
  21. #include "sdio_bus.h"
  22. /* show configuration fields */
  23. #define sdio_config_attr(field, format_string) \
  24. static ssize_t \
  25. field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  26. { \
  27. struct sdio_func *func; \
  28. \
  29. func = dev_to_sdio_func (dev); \
  30. return sprintf (buf, format_string, func->field); \
  31. }
  32. sdio_config_attr(class, "0x%02x\n");
  33. sdio_config_attr(vendor, "0x%04x\n");
  34. sdio_config_attr(device, "0x%04x\n");
  35. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  36. {
  37. struct sdio_func *func = dev_to_sdio_func (dev);
  38. return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
  39. func->class, func->vendor, func->device);
  40. }
  41. static struct device_attribute sdio_dev_attrs[] = {
  42. __ATTR_RO(class),
  43. __ATTR_RO(vendor),
  44. __ATTR_RO(device),
  45. __ATTR_RO(modalias),
  46. __ATTR_NULL,
  47. };
  48. static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
  49. const struct sdio_device_id *id)
  50. {
  51. if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
  52. return NULL;
  53. if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
  54. return NULL;
  55. if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
  56. return NULL;
  57. return id;
  58. }
  59. static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
  60. struct sdio_driver *sdrv)
  61. {
  62. const struct sdio_device_id *ids;
  63. ids = sdrv->id_table;
  64. if (ids) {
  65. while (ids->class || ids->vendor || ids->device) {
  66. if (sdio_match_one(func, ids))
  67. return ids;
  68. ids++;
  69. }
  70. }
  71. return NULL;
  72. }
  73. static int sdio_bus_match(struct device *dev, struct device_driver *drv)
  74. {
  75. struct sdio_func *func = dev_to_sdio_func(dev);
  76. struct sdio_driver *sdrv = to_sdio_driver(drv);
  77. if (sdio_match_device(func, sdrv))
  78. return 1;
  79. return 0;
  80. }
  81. static int
  82. sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  83. {
  84. struct sdio_func *func = dev_to_sdio_func(dev);
  85. if (add_uevent_var(env,
  86. "SDIO_CLASS=%02X", func->class))
  87. return -ENOMEM;
  88. if (add_uevent_var(env,
  89. "SDIO_ID=%04X:%04X", func->vendor, func->device))
  90. return -ENOMEM;
  91. if (add_uevent_var(env,
  92. "MODALIAS=sdio:c%02Xv%04Xd%04X",
  93. func->class, func->vendor, func->device))
  94. return -ENOMEM;
  95. return 0;
  96. }
  97. static int sdio_bus_probe(struct device *dev)
  98. {
  99. struct sdio_driver *drv = to_sdio_driver(dev->driver);
  100. struct sdio_func *func = dev_to_sdio_func(dev);
  101. const struct sdio_device_id *id;
  102. int ret;
  103. id = sdio_match_device(func, drv);
  104. if (!id)
  105. return -ENODEV;
  106. /* Unbound SDIO functions are always suspended.
  107. * During probe, the function is set active and the usage count
  108. * is incremented. If the driver supports runtime PM,
  109. * it should call pm_runtime_put_noidle() in its probe routine and
  110. * pm_runtime_get_noresume() in its remove routine.
  111. */
  112. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
  113. ret = pm_runtime_get_sync(dev);
  114. if (ret < 0)
  115. goto out;
  116. }
  117. /* Set the default block size so the driver is sure it's something
  118. * sensible. */
  119. sdio_claim_host(func);
  120. ret = sdio_set_block_size(func, 0);
  121. sdio_release_host(func);
  122. if (ret)
  123. goto disable_runtimepm;
  124. ret = drv->probe(func, id);
  125. if (ret)
  126. goto disable_runtimepm;
  127. return 0;
  128. disable_runtimepm:
  129. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
  130. pm_runtime_put_noidle(dev);
  131. out:
  132. return ret;
  133. }
  134. static int sdio_bus_remove(struct device *dev)
  135. {
  136. struct sdio_driver *drv = to_sdio_driver(dev->driver);
  137. struct sdio_func *func = dev_to_sdio_func(dev);
  138. int ret = 0;
  139. /* Make sure card is powered before invoking ->remove() */
  140. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
  141. ret = pm_runtime_get_sync(dev);
  142. if (ret < 0)
  143. goto out;
  144. }
  145. drv->remove(func);
  146. if (func->irq_handler) {
  147. printk(KERN_WARNING "WARNING: driver %s did not remove "
  148. "its interrupt handler!\n", drv->name);
  149. sdio_claim_host(func);
  150. sdio_release_irq(func);
  151. sdio_release_host(func);
  152. }
  153. /* First, undo the increment made directly above */
  154. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
  155. pm_runtime_put_noidle(dev);
  156. /* Then undo the runtime PM settings in sdio_bus_probe() */
  157. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
  158. pm_runtime_put_noidle(dev);
  159. out:
  160. return ret;
  161. }
  162. #ifdef CONFIG_PM_RUNTIME
  163. static int sdio_bus_pm_prepare(struct device *dev)
  164. {
  165. struct sdio_func *func = dev_to_sdio_func(dev);
  166. /*
  167. * Resume an SDIO device which was suspended at run time at this
  168. * point, in order to allow standard SDIO suspend/resume paths
  169. * to keep working as usual.
  170. *
  171. * Ultimately, the SDIO driver itself will decide (in its
  172. * suspend handler, or lack thereof) whether the card should be
  173. * removed or kept, and if kept, at what power state.
  174. *
  175. * At this point, PM core have increased our use count, so it's
  176. * safe to directly resume the device. After system is resumed
  177. * again, PM core will drop back its runtime PM use count, and if
  178. * needed device will be suspended again.
  179. *
  180. * The end result is guaranteed to be a power state that is
  181. * coherent with the device's runtime PM use count.
  182. *
  183. * The return value of pm_runtime_resume is deliberately unchecked
  184. * since there is little point in failing system suspend if a
  185. * device can't be resumed.
  186. */
  187. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
  188. pm_runtime_resume(dev);
  189. return 0;
  190. }
  191. static const struct dev_pm_ops sdio_bus_pm_ops = {
  192. SET_RUNTIME_PM_OPS(
  193. pm_generic_runtime_suspend,
  194. pm_generic_runtime_resume,
  195. pm_generic_runtime_idle
  196. )
  197. .prepare = sdio_bus_pm_prepare,
  198. };
  199. #define SDIO_PM_OPS_PTR (&sdio_bus_pm_ops)
  200. #else /* !CONFIG_PM_RUNTIME */
  201. #define SDIO_PM_OPS_PTR NULL
  202. #endif /* !CONFIG_PM_RUNTIME */
  203. static struct bus_type sdio_bus_type = {
  204. .name = "sdio",
  205. .dev_attrs = sdio_dev_attrs,
  206. .match = sdio_bus_match,
  207. .uevent = sdio_bus_uevent,
  208. .probe = sdio_bus_probe,
  209. .remove = sdio_bus_remove,
  210. .pm = SDIO_PM_OPS_PTR,
  211. };
  212. int sdio_register_bus(void)
  213. {
  214. return bus_register(&sdio_bus_type);
  215. }
  216. void sdio_unregister_bus(void)
  217. {
  218. bus_unregister(&sdio_bus_type);
  219. }
  220. /**
  221. * sdio_register_driver - register a function driver
  222. * @drv: SDIO function driver
  223. */
  224. int sdio_register_driver(struct sdio_driver *drv)
  225. {
  226. drv->drv.name = drv->name;
  227. drv->drv.bus = &sdio_bus_type;
  228. return driver_register(&drv->drv);
  229. }
  230. EXPORT_SYMBOL_GPL(sdio_register_driver);
  231. /**
  232. * sdio_unregister_driver - unregister a function driver
  233. * @drv: SDIO function driver
  234. */
  235. void sdio_unregister_driver(struct sdio_driver *drv)
  236. {
  237. drv->drv.bus = &sdio_bus_type;
  238. driver_unregister(&drv->drv);
  239. }
  240. EXPORT_SYMBOL_GPL(sdio_unregister_driver);
  241. static void sdio_release_func(struct device *dev)
  242. {
  243. struct sdio_func *func = dev_to_sdio_func(dev);
  244. sdio_free_func_cis(func);
  245. if (func->info)
  246. kfree(func->info);
  247. kfree(func);
  248. }
  249. /*
  250. * Allocate and initialise a new SDIO function structure.
  251. */
  252. struct sdio_func *sdio_alloc_func(struct mmc_card *card)
  253. {
  254. struct sdio_func *func;
  255. func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
  256. if (!func)
  257. return ERR_PTR(-ENOMEM);
  258. func->card = card;
  259. device_initialize(&func->dev);
  260. func->dev.parent = &card->dev;
  261. func->dev.bus = &sdio_bus_type;
  262. func->dev.release = sdio_release_func;
  263. return func;
  264. }
  265. /*
  266. * Register a new SDIO function with the driver model.
  267. */
  268. int sdio_add_func(struct sdio_func *func)
  269. {
  270. int ret;
  271. dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
  272. ret = device_add(&func->dev);
  273. if (ret == 0)
  274. sdio_func_set_present(func);
  275. return ret;
  276. }
  277. /*
  278. * Unregister a SDIO function with the driver model, and
  279. * (eventually) free it.
  280. * This function can be called through error paths where sdio_add_func() was
  281. * never executed (because a failure occurred at an earlier point).
  282. */
  283. void sdio_remove_func(struct sdio_func *func)
  284. {
  285. if (!sdio_func_present(func))
  286. return;
  287. device_del(&func->dev);
  288. put_device(&func->dev);
  289. }