sdio_bus.c 7.9 KB

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