sdio_bus.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #define to_sdio_driver(d) container_of(d, struct sdio_driver, drv)
  21. static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
  22. const struct sdio_device_id *id)
  23. {
  24. if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
  25. return NULL;
  26. if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
  27. return NULL;
  28. if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
  29. return NULL;
  30. return id;
  31. }
  32. static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
  33. struct sdio_driver *sdrv)
  34. {
  35. const struct sdio_device_id *ids;
  36. ids = sdrv->id_table;
  37. if (ids) {
  38. while (ids->class || ids->vendor || ids->device) {
  39. if (sdio_match_one(func, ids))
  40. return ids;
  41. ids++;
  42. }
  43. }
  44. return NULL;
  45. }
  46. static int sdio_bus_match(struct device *dev, struct device_driver *drv)
  47. {
  48. struct sdio_func *func = dev_to_sdio_func(dev);
  49. struct sdio_driver *sdrv = to_sdio_driver(drv);
  50. if (sdio_match_device(func, sdrv))
  51. return 1;
  52. return 0;
  53. }
  54. static int
  55. sdio_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf,
  56. int buf_size)
  57. {
  58. envp[0] = NULL;
  59. return 0;
  60. }
  61. static int sdio_bus_probe(struct device *dev)
  62. {
  63. struct sdio_driver *drv = to_sdio_driver(dev->driver);
  64. struct sdio_func *func = dev_to_sdio_func(dev);
  65. const struct sdio_device_id *id;
  66. id = sdio_match_device(func, drv);
  67. if (!id)
  68. return -ENODEV;
  69. return drv->probe(func, id);
  70. }
  71. static int sdio_bus_remove(struct device *dev)
  72. {
  73. struct sdio_driver *drv = to_sdio_driver(dev->driver);
  74. struct sdio_func *func = dev_to_sdio_func(dev);
  75. drv->remove(func);
  76. return 0;
  77. }
  78. static struct bus_type sdio_bus_type = {
  79. .name = "sdio",
  80. .match = sdio_bus_match,
  81. .uevent = sdio_bus_uevent,
  82. .probe = sdio_bus_probe,
  83. .remove = sdio_bus_remove,
  84. };
  85. int sdio_register_bus(void)
  86. {
  87. return bus_register(&sdio_bus_type);
  88. }
  89. void sdio_unregister_bus(void)
  90. {
  91. bus_unregister(&sdio_bus_type);
  92. }
  93. /**
  94. * sdio_register_driver - register a function driver
  95. * @drv: SDIO function driver
  96. */
  97. int sdio_register_driver(struct sdio_driver *drv)
  98. {
  99. drv->drv.name = drv->name;
  100. drv->drv.bus = &sdio_bus_type;
  101. return driver_register(&drv->drv);
  102. }
  103. EXPORT_SYMBOL_GPL(sdio_register_driver);
  104. /**
  105. * sdio_unregister_driver - unregister a function driver
  106. * @drv: SDIO function driver
  107. */
  108. void sdio_unregister_driver(struct sdio_driver *drv)
  109. {
  110. drv->drv.bus = &sdio_bus_type;
  111. driver_unregister(&drv->drv);
  112. }
  113. EXPORT_SYMBOL_GPL(sdio_unregister_driver);
  114. static void sdio_release_func(struct device *dev)
  115. {
  116. struct sdio_func *func = dev_to_sdio_func(dev);
  117. sdio_free_func_cis(func);
  118. kfree(func);
  119. }
  120. /*
  121. * Allocate and initialise a new SDIO function structure.
  122. */
  123. struct sdio_func *sdio_alloc_func(struct mmc_card *card)
  124. {
  125. struct sdio_func *func;
  126. func = kmalloc(sizeof(struct sdio_func), GFP_KERNEL);
  127. if (!func)
  128. return ERR_PTR(-ENOMEM);
  129. memset(func, 0, sizeof(struct sdio_func));
  130. func->card = card;
  131. device_initialize(&func->dev);
  132. func->dev.parent = &card->dev;
  133. func->dev.bus = &sdio_bus_type;
  134. func->dev.release = sdio_release_func;
  135. return func;
  136. }
  137. /*
  138. * Register a new SDIO function with the driver model.
  139. */
  140. int sdio_add_func(struct sdio_func *func)
  141. {
  142. int ret;
  143. snprintf(func->dev.bus_id, sizeof(func->dev.bus_id),
  144. "%s:%d", mmc_card_id(func->card), func->num);
  145. ret = device_add(&func->dev);
  146. if (ret == 0)
  147. sdio_func_set_present(func);
  148. return ret;
  149. }
  150. /*
  151. * Unregister a SDIO function with the driver model, and
  152. * (eventually) free it.
  153. */
  154. void sdio_remove_func(struct sdio_func *func)
  155. {
  156. if (sdio_func_present(func))
  157. device_del(&func->dev);
  158. put_device(&func->dev);
  159. }