sdio_bus.c 7.7 KB

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