sdio_bus.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. struct sdio_func *func = dev_to_sdio_func(dev);
  59. int i = 0, length = 0;
  60. if (add_uevent_var(envp, num_envp, &i,
  61. buf, buf_size, &length,
  62. "SDIO_CLASS=%02X", func->class))
  63. return -ENOMEM;
  64. if (add_uevent_var(envp, num_envp, &i,
  65. buf, buf_size, &length,
  66. "SDIO_ID=%04X:%04X", func->vendor, func->device))
  67. return -ENOMEM;
  68. if (add_uevent_var(envp, num_envp, &i,
  69. buf, buf_size, &length,
  70. "MODALIAS=sdio:c%02Xv%04Xd%04X",
  71. func->class, func->vendor, func->device))
  72. return -ENOMEM;
  73. envp[i] = NULL;
  74. return 0;
  75. }
  76. static int sdio_bus_probe(struct device *dev)
  77. {
  78. struct sdio_driver *drv = to_sdio_driver(dev->driver);
  79. struct sdio_func *func = dev_to_sdio_func(dev);
  80. const struct sdio_device_id *id;
  81. id = sdio_match_device(func, drv);
  82. if (!id)
  83. return -ENODEV;
  84. return drv->probe(func, id);
  85. }
  86. static int sdio_bus_remove(struct device *dev)
  87. {
  88. struct sdio_driver *drv = to_sdio_driver(dev->driver);
  89. struct sdio_func *func = dev_to_sdio_func(dev);
  90. drv->remove(func);
  91. return 0;
  92. }
  93. static struct bus_type sdio_bus_type = {
  94. .name = "sdio",
  95. .match = sdio_bus_match,
  96. .uevent = sdio_bus_uevent,
  97. .probe = sdio_bus_probe,
  98. .remove = sdio_bus_remove,
  99. };
  100. int sdio_register_bus(void)
  101. {
  102. return bus_register(&sdio_bus_type);
  103. }
  104. void sdio_unregister_bus(void)
  105. {
  106. bus_unregister(&sdio_bus_type);
  107. }
  108. /**
  109. * sdio_register_driver - register a function driver
  110. * @drv: SDIO function driver
  111. */
  112. int sdio_register_driver(struct sdio_driver *drv)
  113. {
  114. drv->drv.name = drv->name;
  115. drv->drv.bus = &sdio_bus_type;
  116. return driver_register(&drv->drv);
  117. }
  118. EXPORT_SYMBOL_GPL(sdio_register_driver);
  119. /**
  120. * sdio_unregister_driver - unregister a function driver
  121. * @drv: SDIO function driver
  122. */
  123. void sdio_unregister_driver(struct sdio_driver *drv)
  124. {
  125. drv->drv.bus = &sdio_bus_type;
  126. driver_unregister(&drv->drv);
  127. }
  128. EXPORT_SYMBOL_GPL(sdio_unregister_driver);
  129. static void sdio_release_func(struct device *dev)
  130. {
  131. struct sdio_func *func = dev_to_sdio_func(dev);
  132. sdio_free_func_cis(func);
  133. kfree(func);
  134. }
  135. /*
  136. * Allocate and initialise a new SDIO function structure.
  137. */
  138. struct sdio_func *sdio_alloc_func(struct mmc_card *card)
  139. {
  140. struct sdio_func *func;
  141. func = kmalloc(sizeof(struct sdio_func), GFP_KERNEL);
  142. if (!func)
  143. return ERR_PTR(-ENOMEM);
  144. memset(func, 0, sizeof(struct sdio_func));
  145. func->card = card;
  146. device_initialize(&func->dev);
  147. func->dev.parent = &card->dev;
  148. func->dev.bus = &sdio_bus_type;
  149. func->dev.release = sdio_release_func;
  150. return func;
  151. }
  152. /*
  153. * Register a new SDIO function with the driver model.
  154. */
  155. int sdio_add_func(struct sdio_func *func)
  156. {
  157. int ret;
  158. snprintf(func->dev.bus_id, sizeof(func->dev.bus_id),
  159. "%s:%d", mmc_card_id(func->card), func->num);
  160. ret = device_add(&func->dev);
  161. if (ret == 0)
  162. sdio_func_set_present(func);
  163. return ret;
  164. }
  165. /*
  166. * Unregister a SDIO function with the driver model, and
  167. * (eventually) free it.
  168. */
  169. void sdio_remove_func(struct sdio_func *func)
  170. {
  171. if (sdio_func_present(func))
  172. device_del(&func->dev);
  173. put_device(&func->dev);
  174. }