sdio_bus.c 5.9 KB

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