sdio_bus.c 7.6 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/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. #define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
  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. static DEVICE_ATTR_RO(field)
  37. sdio_config_attr(class, "0x%02x\n");
  38. sdio_config_attr(vendor, "0x%04x\n");
  39. sdio_config_attr(device, "0x%04x\n");
  40. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  41. {
  42. struct sdio_func *func = dev_to_sdio_func (dev);
  43. return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
  44. func->class, func->vendor, func->device);
  45. }
  46. static DEVICE_ATTR_RO(modalias);
  47. static struct attribute *sdio_dev_attrs[] = {
  48. &dev_attr_class.attr,
  49. &dev_attr_vendor.attr,
  50. &dev_attr_device.attr,
  51. &dev_attr_modalias.attr,
  52. NULL,
  53. };
  54. ATTRIBUTE_GROUPS(sdio_dev);
  55. static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
  56. const struct sdio_device_id *id)
  57. {
  58. if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
  59. return NULL;
  60. if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
  61. return NULL;
  62. if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
  63. return NULL;
  64. return id;
  65. }
  66. static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
  67. struct sdio_driver *sdrv)
  68. {
  69. const struct sdio_device_id *ids;
  70. ids = sdrv->id_table;
  71. if (ids) {
  72. while (ids->class || ids->vendor || ids->device) {
  73. if (sdio_match_one(func, ids))
  74. return ids;
  75. ids++;
  76. }
  77. }
  78. return NULL;
  79. }
  80. static int sdio_bus_match(struct device *dev, struct device_driver *drv)
  81. {
  82. struct sdio_func *func = dev_to_sdio_func(dev);
  83. struct sdio_driver *sdrv = to_sdio_driver(drv);
  84. if (sdio_match_device(func, sdrv))
  85. return 1;
  86. return 0;
  87. }
  88. static int
  89. sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  90. {
  91. struct sdio_func *func = dev_to_sdio_func(dev);
  92. if (add_uevent_var(env,
  93. "SDIO_CLASS=%02X", func->class))
  94. return -ENOMEM;
  95. if (add_uevent_var(env,
  96. "SDIO_ID=%04X:%04X", func->vendor, func->device))
  97. return -ENOMEM;
  98. if (add_uevent_var(env,
  99. "MODALIAS=sdio:c%02Xv%04Xd%04X",
  100. func->class, func->vendor, func->device))
  101. return -ENOMEM;
  102. return 0;
  103. }
  104. static int sdio_bus_probe(struct device *dev)
  105. {
  106. struct sdio_driver *drv = to_sdio_driver(dev->driver);
  107. struct sdio_func *func = dev_to_sdio_func(dev);
  108. const struct sdio_device_id *id;
  109. int ret;
  110. id = sdio_match_device(func, drv);
  111. if (!id)
  112. return -ENODEV;
  113. /* Unbound SDIO functions are always suspended.
  114. * During probe, the function is set active and the usage count
  115. * is incremented. If the driver supports runtime PM,
  116. * it should call pm_runtime_put_noidle() in its probe routine and
  117. * pm_runtime_get_noresume() in its remove routine.
  118. */
  119. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
  120. ret = pm_runtime_get_sync(dev);
  121. if (ret < 0)
  122. goto disable_runtimepm;
  123. }
  124. /* Set the default block size so the driver is sure it's something
  125. * sensible. */
  126. sdio_claim_host(func);
  127. ret = sdio_set_block_size(func, 0);
  128. sdio_release_host(func);
  129. if (ret)
  130. goto disable_runtimepm;
  131. ret = drv->probe(func, id);
  132. if (ret)
  133. goto disable_runtimepm;
  134. return 0;
  135. disable_runtimepm:
  136. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
  137. pm_runtime_put_noidle(dev);
  138. return ret;
  139. }
  140. static int sdio_bus_remove(struct device *dev)
  141. {
  142. struct sdio_driver *drv = to_sdio_driver(dev->driver);
  143. struct sdio_func *func = dev_to_sdio_func(dev);
  144. int ret = 0;
  145. /* Make sure card is powered before invoking ->remove() */
  146. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
  147. pm_runtime_get_sync(dev);
  148. drv->remove(func);
  149. if (func->irq_handler) {
  150. pr_warn("WARNING: driver %s did not remove its interrupt handler!\n",
  151. 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. return ret;
  163. }
  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. static struct bus_type sdio_bus_type = {
  173. .name = "sdio",
  174. .dev_groups = sdio_dev_groups,
  175. .match = sdio_bus_match,
  176. .uevent = sdio_bus_uevent,
  177. .probe = sdio_bus_probe,
  178. .remove = sdio_bus_remove,
  179. .pm = &sdio_bus_pm_ops,
  180. };
  181. int sdio_register_bus(void)
  182. {
  183. return bus_register(&sdio_bus_type);
  184. }
  185. void sdio_unregister_bus(void)
  186. {
  187. bus_unregister(&sdio_bus_type);
  188. }
  189. /**
  190. * sdio_register_driver - register a function driver
  191. * @drv: SDIO function driver
  192. */
  193. int sdio_register_driver(struct sdio_driver *drv)
  194. {
  195. drv->drv.name = drv->name;
  196. drv->drv.bus = &sdio_bus_type;
  197. return driver_register(&drv->drv);
  198. }
  199. EXPORT_SYMBOL_GPL(sdio_register_driver);
  200. /**
  201. * sdio_unregister_driver - unregister a function driver
  202. * @drv: SDIO function driver
  203. */
  204. void sdio_unregister_driver(struct sdio_driver *drv)
  205. {
  206. drv->drv.bus = &sdio_bus_type;
  207. driver_unregister(&drv->drv);
  208. }
  209. EXPORT_SYMBOL_GPL(sdio_unregister_driver);
  210. static void sdio_release_func(struct device *dev)
  211. {
  212. struct sdio_func *func = dev_to_sdio_func(dev);
  213. sdio_free_func_cis(func);
  214. kfree(func->info);
  215. kfree(func);
  216. }
  217. /*
  218. * Allocate and initialise a new SDIO function structure.
  219. */
  220. struct sdio_func *sdio_alloc_func(struct mmc_card *card)
  221. {
  222. struct sdio_func *func;
  223. func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
  224. if (!func)
  225. return ERR_PTR(-ENOMEM);
  226. func->card = card;
  227. device_initialize(&func->dev);
  228. func->dev.parent = &card->dev;
  229. func->dev.bus = &sdio_bus_type;
  230. func->dev.release = sdio_release_func;
  231. return func;
  232. }
  233. #ifdef CONFIG_ACPI
  234. static void sdio_acpi_set_handle(struct sdio_func *func)
  235. {
  236. struct mmc_host *host = func->card->host;
  237. u64 addr = ((u64)host->slotno << 16) | func->num;
  238. acpi_preset_companion(&func->dev, ACPI_COMPANION(host->parent), addr);
  239. }
  240. #else
  241. static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
  242. #endif
  243. /*
  244. * Register a new SDIO function with the driver model.
  245. */
  246. int sdio_add_func(struct sdio_func *func)
  247. {
  248. int ret;
  249. dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
  250. sdio_acpi_set_handle(func);
  251. ret = device_add(&func->dev);
  252. if (ret == 0) {
  253. sdio_func_set_present(func);
  254. dev_pm_domain_attach(&func->dev, false);
  255. }
  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. dev_pm_domain_detach(&func->dev, false);
  269. device_del(&func->dev);
  270. put_device(&func->dev);
  271. }