sdio_bus.c 7.7 KB

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