sdio_bus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * linux/drivers/mmc/core/sdio_bus.c
  4. *
  5. * Copyright 2007 Pierre Ossman
  6. *
  7. * SDIO function driver model
  8. */
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include <linux/export.h>
  12. #include <linux/slab.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/pm_domain.h>
  15. #include <linux/acpi.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/mmc/card.h>
  18. #include <linux/mmc/host.h>
  19. #include <linux/mmc/sdio_func.h>
  20. #include <linux/of.h>
  21. #include "core.h"
  22. #include "card.h"
  23. #include "sdio_cis.h"
  24. #include "sdio_bus.h"
  25. #define to_sdio_driver(d) container_of_const(d, struct sdio_driver, drv)
  26. /* show configuration fields */
  27. #define sdio_config_attr(field, format_string, args...) \
  28. static ssize_t \
  29. field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  30. { \
  31. struct sdio_func *func; \
  32. \
  33. func = dev_to_sdio_func (dev); \
  34. return sysfs_emit(buf, format_string, args); \
  35. } \
  36. static DEVICE_ATTR_RO(field)
  37. sdio_config_attr(class, "0x%02x\n", func->class);
  38. sdio_config_attr(vendor, "0x%04x\n", func->vendor);
  39. sdio_config_attr(device, "0x%04x\n", func->device);
  40. sdio_config_attr(revision, "%u.%u\n", func->major_rev, func->minor_rev);
  41. sdio_config_attr(modalias, "sdio:c%02Xv%04Xd%04X\n", func->class, func->vendor, func->device);
  42. #define sdio_info_attr(num) \
  43. static ssize_t info##num##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  44. { \
  45. struct sdio_func *func = dev_to_sdio_func(dev); \
  46. \
  47. if (num > func->num_info) \
  48. return -ENODATA; \
  49. if (!func->info[num - 1][0]) \
  50. return 0; \
  51. return sysfs_emit(buf, "%s\n", func->info[num - 1]); \
  52. } \
  53. static DEVICE_ATTR_RO(info##num)
  54. sdio_info_attr(1);
  55. sdio_info_attr(2);
  56. sdio_info_attr(3);
  57. sdio_info_attr(4);
  58. static struct attribute *sdio_dev_attrs[] = {
  59. &dev_attr_class.attr,
  60. &dev_attr_vendor.attr,
  61. &dev_attr_device.attr,
  62. &dev_attr_revision.attr,
  63. &dev_attr_info1.attr,
  64. &dev_attr_info2.attr,
  65. &dev_attr_info3.attr,
  66. &dev_attr_info4.attr,
  67. &dev_attr_modalias.attr,
  68. NULL,
  69. };
  70. ATTRIBUTE_GROUPS(sdio_dev);
  71. static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
  72. const struct sdio_device_id *id)
  73. {
  74. if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
  75. return NULL;
  76. if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
  77. return NULL;
  78. if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
  79. return NULL;
  80. return id;
  81. }
  82. static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
  83. const struct sdio_driver *sdrv)
  84. {
  85. const struct sdio_device_id *ids;
  86. ids = sdrv->id_table;
  87. if (ids) {
  88. while (ids->class || ids->vendor || ids->device) {
  89. if (sdio_match_one(func, ids))
  90. return ids;
  91. ids++;
  92. }
  93. }
  94. return NULL;
  95. }
  96. static int sdio_bus_match(struct device *dev, const struct device_driver *drv)
  97. {
  98. struct sdio_func *func = dev_to_sdio_func(dev);
  99. const struct sdio_driver *sdrv = to_sdio_driver(drv);
  100. if (sdio_match_device(func, sdrv))
  101. return 1;
  102. return 0;
  103. }
  104. static int
  105. sdio_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
  106. {
  107. const struct sdio_func *func = dev_to_sdio_func(dev);
  108. unsigned int i;
  109. if (add_uevent_var(env,
  110. "SDIO_CLASS=%02X", func->class))
  111. return -ENOMEM;
  112. if (add_uevent_var(env,
  113. "SDIO_ID=%04X:%04X", func->vendor, func->device))
  114. return -ENOMEM;
  115. if (add_uevent_var(env,
  116. "SDIO_REVISION=%u.%u", func->major_rev, func->minor_rev))
  117. return -ENOMEM;
  118. for (i = 0; i < func->num_info; i++) {
  119. if (add_uevent_var(env, "SDIO_INFO%u=%s", i+1, func->info[i]))
  120. return -ENOMEM;
  121. }
  122. if (add_uevent_var(env,
  123. "MODALIAS=sdio:c%02Xv%04Xd%04X",
  124. func->class, func->vendor, func->device))
  125. return -ENOMEM;
  126. return 0;
  127. }
  128. static int sdio_bus_probe(struct device *dev)
  129. {
  130. struct sdio_driver *drv = to_sdio_driver(dev->driver);
  131. struct sdio_func *func = dev_to_sdio_func(dev);
  132. const struct sdio_device_id *id;
  133. int ret;
  134. id = sdio_match_device(func, drv);
  135. if (!id)
  136. return -ENODEV;
  137. ret = dev_pm_domain_attach(dev, 0);
  138. if (ret)
  139. return ret;
  140. atomic_inc(&func->card->sdio_funcs_probed);
  141. /* Unbound SDIO functions are always suspended.
  142. * During probe, the function is set active and the usage count
  143. * is incremented. If the driver supports runtime PM,
  144. * it should call pm_runtime_put_noidle() in its probe routine and
  145. * pm_runtime_get_noresume() in its remove routine.
  146. */
  147. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
  148. ret = pm_runtime_get_sync(dev);
  149. if (ret < 0)
  150. goto disable_runtimepm;
  151. }
  152. /* Set the default block size so the driver is sure it's something
  153. * sensible. */
  154. sdio_claim_host(func);
  155. if (mmc_card_removed(func->card))
  156. ret = -ENOMEDIUM;
  157. else
  158. ret = sdio_set_block_size(func, 0);
  159. sdio_release_host(func);
  160. if (ret)
  161. goto disable_runtimepm;
  162. ret = drv->probe(func, id);
  163. if (ret)
  164. goto disable_runtimepm;
  165. return 0;
  166. disable_runtimepm:
  167. atomic_dec(&func->card->sdio_funcs_probed);
  168. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
  169. pm_runtime_put_noidle(dev);
  170. return ret;
  171. }
  172. static void sdio_bus_remove(struct device *dev)
  173. {
  174. struct sdio_driver *drv = to_sdio_driver(dev->driver);
  175. struct sdio_func *func = dev_to_sdio_func(dev);
  176. /* Make sure card is powered before invoking ->remove() */
  177. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
  178. pm_runtime_get_sync(dev);
  179. drv->remove(func);
  180. atomic_dec(&func->card->sdio_funcs_probed);
  181. if (func->irq_handler) {
  182. pr_warn("WARNING: driver %s did not remove its interrupt handler!\n",
  183. drv->name);
  184. sdio_claim_host(func);
  185. sdio_release_irq(func);
  186. sdio_release_host(func);
  187. }
  188. /* First, undo the increment made directly above */
  189. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
  190. pm_runtime_put_noidle(dev);
  191. /* Then undo the runtime PM settings in sdio_bus_probe() */
  192. if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
  193. pm_runtime_put_sync(dev);
  194. }
  195. static void sdio_bus_shutdown(struct device *dev)
  196. {
  197. struct sdio_driver *drv = to_sdio_driver(dev->driver);
  198. struct sdio_func *func = dev_to_sdio_func(dev);
  199. if (dev->driver && drv->shutdown)
  200. drv->shutdown(func);
  201. }
  202. static const struct dev_pm_ops sdio_bus_pm_ops = {
  203. SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume)
  204. SET_RUNTIME_PM_OPS(
  205. pm_generic_runtime_suspend,
  206. pm_generic_runtime_resume,
  207. NULL
  208. )
  209. };
  210. static const struct bus_type sdio_bus_type = {
  211. .name = "sdio",
  212. .dev_groups = sdio_dev_groups,
  213. .match = sdio_bus_match,
  214. .uevent = sdio_bus_uevent,
  215. .probe = sdio_bus_probe,
  216. .remove = sdio_bus_remove,
  217. .shutdown = sdio_bus_shutdown,
  218. .pm = &sdio_bus_pm_ops,
  219. };
  220. int sdio_register_bus(void)
  221. {
  222. return bus_register(&sdio_bus_type);
  223. }
  224. void sdio_unregister_bus(void)
  225. {
  226. bus_unregister(&sdio_bus_type);
  227. }
  228. static void sdio_legacy_shutdown(struct sdio_func *func)
  229. {
  230. struct device *dev = &func->dev;
  231. struct device_driver *driver = dev->driver;
  232. driver->shutdown(dev);
  233. }
  234. /**
  235. * __sdio_register_driver - register a function driver
  236. * @drv: SDIO function driver
  237. * @owner: owning module/driver
  238. */
  239. int __sdio_register_driver(struct sdio_driver *drv, struct module *owner)
  240. {
  241. drv->drv.name = drv->name;
  242. drv->drv.bus = &sdio_bus_type;
  243. drv->drv.owner = owner;
  244. /*
  245. * This driver needs updating. Note that driver_register() warns about
  246. * this, so we're not adding another warning here.
  247. */
  248. if (!drv->shutdown && drv->drv.shutdown)
  249. drv->shutdown = sdio_legacy_shutdown;
  250. return driver_register(&drv->drv);
  251. }
  252. EXPORT_SYMBOL_GPL(__sdio_register_driver);
  253. /**
  254. * sdio_unregister_driver - unregister a function driver
  255. * @drv: SDIO function driver
  256. */
  257. void sdio_unregister_driver(struct sdio_driver *drv)
  258. {
  259. drv->drv.bus = &sdio_bus_type;
  260. driver_unregister(&drv->drv);
  261. }
  262. EXPORT_SYMBOL_GPL(sdio_unregister_driver);
  263. static void sdio_release_func(struct device *dev)
  264. {
  265. struct sdio_func *func = dev_to_sdio_func(dev);
  266. if (!(func->card->quirks & MMC_QUIRK_NONSTD_SDIO))
  267. sdio_free_func_cis(func);
  268. /*
  269. * We have now removed the link to the tuples in the
  270. * card structure, so remove the reference.
  271. */
  272. put_device(&func->card->dev);
  273. kfree(func->info);
  274. kfree(func->tmpbuf);
  275. kfree(func);
  276. }
  277. /*
  278. * Allocate and initialise a new SDIO function structure.
  279. */
  280. struct sdio_func *sdio_alloc_func(struct mmc_card *card)
  281. {
  282. struct sdio_func *func;
  283. func = kzalloc_obj(struct sdio_func);
  284. if (!func)
  285. return ERR_PTR(-ENOMEM);
  286. /*
  287. * allocate buffer separately to make sure it's properly aligned for
  288. * DMA usage (incl. 64 bit DMA)
  289. */
  290. func->tmpbuf = kmalloc(4, GFP_KERNEL);
  291. if (!func->tmpbuf) {
  292. kfree(func);
  293. return ERR_PTR(-ENOMEM);
  294. }
  295. func->card = card;
  296. device_initialize(&func->dev);
  297. /*
  298. * We may link to tuples in the card structure,
  299. * we need make sure we have a reference to it.
  300. */
  301. get_device(&func->card->dev);
  302. func->dev.parent = &card->dev;
  303. func->dev.bus = &sdio_bus_type;
  304. func->dev.release = sdio_release_func;
  305. return func;
  306. }
  307. #ifdef CONFIG_ACPI
  308. static void sdio_acpi_set_handle(struct sdio_func *func)
  309. {
  310. struct mmc_host *host = func->card->host;
  311. u64 addr = ((u64)host->slotno << 16) | func->num;
  312. acpi_preset_companion(&func->dev, ACPI_COMPANION(host->parent), addr);
  313. }
  314. #else
  315. static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
  316. #endif
  317. static void sdio_set_of_node(struct sdio_func *func)
  318. {
  319. struct mmc_host *host = func->card->host;
  320. func->dev.of_node = mmc_of_find_child_device(host, func->num);
  321. }
  322. /*
  323. * Register a new SDIO function with the driver model.
  324. */
  325. int sdio_add_func(struct sdio_func *func)
  326. {
  327. int ret;
  328. dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
  329. sdio_set_of_node(func);
  330. sdio_acpi_set_handle(func);
  331. device_enable_async_suspend(&func->dev);
  332. ret = device_add(&func->dev);
  333. if (ret == 0)
  334. sdio_func_set_present(func);
  335. return ret;
  336. }
  337. /*
  338. * Unregister a SDIO function with the driver model, and
  339. * (eventually) free it.
  340. * This function can be called through error paths where sdio_add_func() was
  341. * never executed (because a failure occurred at an earlier point).
  342. */
  343. void sdio_remove_func(struct sdio_func *func)
  344. {
  345. if (sdio_func_present(func))
  346. device_del(&func->dev);
  347. of_node_put(func->dev.of_node);
  348. put_device(&func->dev);
  349. }