sdio_bus.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. #ifdef CONFIG_PM_SLEEP
  164. static int pm_no_operation(struct device *dev)
  165. {
  166. /*
  167. * Prevent the PM core from calling SDIO device drivers' suspend
  168. * callback routines, which it is not supposed to do, by using this
  169. * empty function as the bus type suspend callaback for SDIO.
  170. */
  171. return 0;
  172. }
  173. #endif
  174. static const struct dev_pm_ops sdio_bus_pm_ops = {
  175. SET_SYSTEM_SLEEP_PM_OPS(pm_no_operation, pm_no_operation)
  176. SET_RUNTIME_PM_OPS(
  177. pm_generic_runtime_suspend,
  178. pm_generic_runtime_resume,
  179. NULL
  180. )
  181. };
  182. #define SDIO_PM_OPS_PTR (&sdio_bus_pm_ops)
  183. #else /* !CONFIG_PM */
  184. #define SDIO_PM_OPS_PTR NULL
  185. #endif /* !CONFIG_PM */
  186. static struct bus_type sdio_bus_type = {
  187. .name = "sdio",
  188. .dev_groups = sdio_dev_groups,
  189. .match = sdio_bus_match,
  190. .uevent = sdio_bus_uevent,
  191. .probe = sdio_bus_probe,
  192. .remove = sdio_bus_remove,
  193. .pm = SDIO_PM_OPS_PTR,
  194. };
  195. int sdio_register_bus(void)
  196. {
  197. return bus_register(&sdio_bus_type);
  198. }
  199. void sdio_unregister_bus(void)
  200. {
  201. bus_unregister(&sdio_bus_type);
  202. }
  203. /**
  204. * sdio_register_driver - register a function driver
  205. * @drv: SDIO function driver
  206. */
  207. int sdio_register_driver(struct sdio_driver *drv)
  208. {
  209. drv->drv.name = drv->name;
  210. drv->drv.bus = &sdio_bus_type;
  211. return driver_register(&drv->drv);
  212. }
  213. EXPORT_SYMBOL_GPL(sdio_register_driver);
  214. /**
  215. * sdio_unregister_driver - unregister a function driver
  216. * @drv: SDIO function driver
  217. */
  218. void sdio_unregister_driver(struct sdio_driver *drv)
  219. {
  220. drv->drv.bus = &sdio_bus_type;
  221. driver_unregister(&drv->drv);
  222. }
  223. EXPORT_SYMBOL_GPL(sdio_unregister_driver);
  224. static void sdio_release_func(struct device *dev)
  225. {
  226. struct sdio_func *func = dev_to_sdio_func(dev);
  227. sdio_free_func_cis(func);
  228. kfree(func->info);
  229. kfree(func);
  230. }
  231. /*
  232. * Allocate and initialise a new SDIO function structure.
  233. */
  234. struct sdio_func *sdio_alloc_func(struct mmc_card *card)
  235. {
  236. struct sdio_func *func;
  237. func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
  238. if (!func)
  239. return ERR_PTR(-ENOMEM);
  240. func->card = card;
  241. device_initialize(&func->dev);
  242. func->dev.parent = &card->dev;
  243. func->dev.bus = &sdio_bus_type;
  244. func->dev.release = sdio_release_func;
  245. return func;
  246. }
  247. #ifdef CONFIG_ACPI
  248. static void sdio_acpi_set_handle(struct sdio_func *func)
  249. {
  250. struct mmc_host *host = func->card->host;
  251. u64 addr = (host->slotno << 16) | func->num;
  252. acpi_preset_companion(&func->dev, ACPI_HANDLE(host->parent), addr);
  253. }
  254. #else
  255. static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
  256. #endif
  257. /*
  258. * Register a new SDIO function with the driver model.
  259. */
  260. int sdio_add_func(struct sdio_func *func)
  261. {
  262. int ret;
  263. dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
  264. sdio_acpi_set_handle(func);
  265. ret = device_add(&func->dev);
  266. if (ret == 0) {
  267. sdio_func_set_present(func);
  268. acpi_dev_pm_attach(&func->dev, false);
  269. }
  270. return ret;
  271. }
  272. /*
  273. * Unregister a SDIO function with the driver model, and
  274. * (eventually) free it.
  275. * This function can be called through error paths where sdio_add_func() was
  276. * never executed (because a failure occurred at an earlier point).
  277. */
  278. void sdio_remove_func(struct sdio_func *func)
  279. {
  280. if (!sdio_func_present(func))
  281. return;
  282. acpi_dev_pm_detach(&func->dev, false);
  283. device_del(&func->dev);
  284. put_device(&func->dev);
  285. }