sdio_sys.c 6.0 KB

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