sdio_bus.c 7.5 KB

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