sdio_bus.c 7.4 KB

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