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