sdio_bus.c 5.7 KB

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