sdio_bus.c 7.4 KB

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