sdio_bus.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. /* show configuration fields */
  20. #define sdio_config_attr(field, format_string) \
  21. static ssize_t \
  22. field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  23. { \
  24. struct sdio_func *func; \
  25. \
  26. func = dev_to_sdio_func (dev); \
  27. return sprintf (buf, format_string, func->field); \
  28. }
  29. sdio_config_attr(class, "0x%02x\n");
  30. sdio_config_attr(vendor, "0x%04x\n");
  31. sdio_config_attr(device, "0x%04x\n");
  32. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  33. {
  34. struct sdio_func *func = dev_to_sdio_func (dev);
  35. return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
  36. func->class, func->vendor, func->device);
  37. }
  38. static struct device_attribute sdio_dev_attrs[] = {
  39. __ATTR_RO(class),
  40. __ATTR_RO(vendor),
  41. __ATTR_RO(device),
  42. __ATTR_RO(modalias),
  43. __ATTR_NULL,
  44. };
  45. static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
  46. const struct sdio_device_id *id)
  47. {
  48. if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
  49. return NULL;
  50. if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
  51. return NULL;
  52. if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
  53. return NULL;
  54. return id;
  55. }
  56. static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
  57. struct sdio_driver *sdrv)
  58. {
  59. const struct sdio_device_id *ids;
  60. ids = sdrv->id_table;
  61. if (ids) {
  62. while (ids->class || ids->vendor || ids->device) {
  63. if (sdio_match_one(func, ids))
  64. return ids;
  65. ids++;
  66. }
  67. }
  68. return NULL;
  69. }
  70. static int sdio_bus_match(struct device *dev, struct device_driver *drv)
  71. {
  72. struct sdio_func *func = dev_to_sdio_func(dev);
  73. struct sdio_driver *sdrv = to_sdio_driver(drv);
  74. if (sdio_match_device(func, sdrv))
  75. return 1;
  76. return 0;
  77. }
  78. static int
  79. sdio_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  80. {
  81. struct sdio_func *func = dev_to_sdio_func(dev);
  82. if (add_uevent_var(env,
  83. "SDIO_CLASS=%02X", func->class))
  84. return -ENOMEM;
  85. if (add_uevent_var(env,
  86. "SDIO_ID=%04X:%04X", func->vendor, func->device))
  87. return -ENOMEM;
  88. if (add_uevent_var(env,
  89. "MODALIAS=sdio:c%02Xv%04Xd%04X",
  90. func->class, func->vendor, func->device))
  91. return -ENOMEM;
  92. return 0;
  93. }
  94. static int sdio_bus_probe(struct device *dev)
  95. {
  96. struct sdio_driver *drv = to_sdio_driver(dev->driver);
  97. struct sdio_func *func = dev_to_sdio_func(dev);
  98. const struct sdio_device_id *id;
  99. int ret;
  100. id = sdio_match_device(func, drv);
  101. if (!id)
  102. return -ENODEV;
  103. /* Set the default block size so the driver is sure it's something
  104. * sensible. */
  105. sdio_claim_host(func);
  106. ret = sdio_set_block_size(func, 0);
  107. sdio_release_host(func);
  108. if (ret)
  109. return ret;
  110. return drv->probe(func, id);
  111. }
  112. static int sdio_bus_remove(struct device *dev)
  113. {
  114. struct sdio_driver *drv = to_sdio_driver(dev->driver);
  115. struct sdio_func *func = dev_to_sdio_func(dev);
  116. drv->remove(func);
  117. if (func->irq_handler) {
  118. printk(KERN_WARNING "WARNING: driver %s did not remove "
  119. "its interrupt handler!\n", drv->name);
  120. sdio_claim_host(func);
  121. sdio_release_irq(func);
  122. sdio_release_host(func);
  123. }
  124. return 0;
  125. }
  126. static struct bus_type sdio_bus_type = {
  127. .name = "sdio",
  128. .dev_attrs = sdio_dev_attrs,
  129. .match = sdio_bus_match,
  130. .uevent = sdio_bus_uevent,
  131. .probe = sdio_bus_probe,
  132. .remove = sdio_bus_remove,
  133. };
  134. int sdio_register_bus(void)
  135. {
  136. return bus_register(&sdio_bus_type);
  137. }
  138. void sdio_unregister_bus(void)
  139. {
  140. bus_unregister(&sdio_bus_type);
  141. }
  142. /**
  143. * sdio_register_driver - register a function driver
  144. * @drv: SDIO function driver
  145. */
  146. int sdio_register_driver(struct sdio_driver *drv)
  147. {
  148. drv->drv.name = drv->name;
  149. drv->drv.bus = &sdio_bus_type;
  150. return driver_register(&drv->drv);
  151. }
  152. EXPORT_SYMBOL_GPL(sdio_register_driver);
  153. /**
  154. * sdio_unregister_driver - unregister a function driver
  155. * @drv: SDIO function driver
  156. */
  157. void sdio_unregister_driver(struct sdio_driver *drv)
  158. {
  159. drv->drv.bus = &sdio_bus_type;
  160. driver_unregister(&drv->drv);
  161. }
  162. EXPORT_SYMBOL_GPL(sdio_unregister_driver);
  163. static void sdio_release_func(struct device *dev)
  164. {
  165. struct sdio_func *func = dev_to_sdio_func(dev);
  166. sdio_free_func_cis(func);
  167. if (func->info)
  168. kfree(func->info);
  169. kfree(func);
  170. }
  171. /*
  172. * Allocate and initialise a new SDIO function structure.
  173. */
  174. struct sdio_func *sdio_alloc_func(struct mmc_card *card)
  175. {
  176. struct sdio_func *func;
  177. func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
  178. if (!func)
  179. return ERR_PTR(-ENOMEM);
  180. func->card = card;
  181. device_initialize(&func->dev);
  182. func->dev.parent = &card->dev;
  183. func->dev.bus = &sdio_bus_type;
  184. func->dev.release = sdio_release_func;
  185. return func;
  186. }
  187. /*
  188. * Register a new SDIO function with the driver model.
  189. */
  190. int sdio_add_func(struct sdio_func *func)
  191. {
  192. int ret;
  193. dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
  194. ret = device_add(&func->dev);
  195. if (ret == 0)
  196. sdio_func_set_present(func);
  197. return ret;
  198. }
  199. /*
  200. * Unregister a SDIO function with the driver model, and
  201. * (eventually) free it.
  202. * This function can be called through error paths where sdio_add_func() was
  203. * never executed (because a failure occurred at an earlier point).
  204. */
  205. void sdio_remove_func(struct sdio_func *func)
  206. {
  207. if (!sdio_func_present(func))
  208. return;
  209. device_del(&func->dev);
  210. put_device(&func->dev);
  211. }