sdio_bus.c 8.2 KB

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