sdio_bus.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/mmc/card.h>
  16. #include <linux/mmc/sdio_func.h>
  17. #include "sdio_cis.h"
  18. #include "sdio_bus.h"
  19. #define dev_to_sdio_func(d) container_of(d, struct sdio_func, dev)
  20. /*
  21. * This currently matches any SDIO function to any driver in order
  22. * to help initial development and testing.
  23. */
  24. static int sdio_bus_match(struct device *dev, struct device_driver *drv)
  25. {
  26. return 1;
  27. }
  28. static int
  29. sdio_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf,
  30. int buf_size)
  31. {
  32. envp[0] = NULL;
  33. return 0;
  34. }
  35. static int sdio_bus_probe(struct device *dev)
  36. {
  37. return -ENODEV;
  38. }
  39. static int sdio_bus_remove(struct device *dev)
  40. {
  41. return 0;
  42. }
  43. static struct bus_type sdio_bus_type = {
  44. .name = "sdio",
  45. .match = sdio_bus_match,
  46. .uevent = sdio_bus_uevent,
  47. .probe = sdio_bus_probe,
  48. .remove = sdio_bus_remove,
  49. };
  50. int sdio_register_bus(void)
  51. {
  52. return bus_register(&sdio_bus_type);
  53. }
  54. void sdio_unregister_bus(void)
  55. {
  56. bus_unregister(&sdio_bus_type);
  57. }
  58. /**
  59. * sdio_register_driver - register a function driver
  60. * @drv: SDIO function driver
  61. */
  62. int sdio_register_driver(struct sdio_driver *drv)
  63. {
  64. drv->drv.name = drv->name;
  65. drv->drv.bus = &sdio_bus_type;
  66. return driver_register(&drv->drv);
  67. }
  68. EXPORT_SYMBOL_GPL(sdio_register_driver);
  69. /**
  70. * sdio_unregister_driver - unregister a function driver
  71. * @drv: SDIO function driver
  72. */
  73. void sdio_unregister_driver(struct sdio_driver *drv)
  74. {
  75. drv->drv.bus = &sdio_bus_type;
  76. driver_unregister(&drv->drv);
  77. }
  78. EXPORT_SYMBOL_GPL(sdio_unregister_driver);
  79. static void sdio_release_func(struct device *dev)
  80. {
  81. struct sdio_func *func = dev_to_sdio_func(dev);
  82. sdio_free_func_cis(func);
  83. kfree(func);
  84. }
  85. /*
  86. * Allocate and initialise a new SDIO function structure.
  87. */
  88. struct sdio_func *sdio_alloc_func(struct mmc_card *card)
  89. {
  90. struct sdio_func *func;
  91. func = kmalloc(sizeof(struct sdio_func), GFP_KERNEL);
  92. if (!func)
  93. return ERR_PTR(-ENOMEM);
  94. memset(func, 0, sizeof(struct sdio_func));
  95. func->card = card;
  96. device_initialize(&func->dev);
  97. func->dev.parent = &card->dev;
  98. func->dev.bus = &sdio_bus_type;
  99. func->dev.release = sdio_release_func;
  100. return func;
  101. }
  102. /*
  103. * Register a new SDIO function with the driver model.
  104. */
  105. int sdio_add_func(struct sdio_func *func)
  106. {
  107. int ret;
  108. snprintf(func->dev.bus_id, sizeof(func->dev.bus_id),
  109. "%s:%d", mmc_card_id(func->card), func->num);
  110. ret = device_add(&func->dev);
  111. if (ret == 0)
  112. sdio_func_set_present(func);
  113. return ret;
  114. }
  115. /*
  116. * Unregister a SDIO function with the driver model, and
  117. * (eventually) free it.
  118. */
  119. void sdio_remove_func(struct sdio_func *func)
  120. {
  121. if (sdio_func_present(func))
  122. device_del(&func->dev);
  123. put_device(&func->dev);
  124. }