sdio_bus.c 7.8 KB

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