sdio_bus.c 7.9 KB

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