rio-driver.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * RapidIO driver support
  3. *
  4. * Copyright 2005 MontaVista Software, Inc.
  5. * Matt Porter <mporter@kernel.crashing.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/rio.h>
  15. #include <linux/rio_ids.h>
  16. #include "rio.h"
  17. /**
  18. * rio_match_device - Tell if a RIO device has a matching RIO device id structure
  19. * @id: the RIO device id structure to match against
  20. * @rdev: the RIO device structure to match against
  21. *
  22. * Used from driver probe and bus matching to check whether a RIO device
  23. * matches a device id structure provided by a RIO driver. Returns the
  24. * matching &struct rio_device_id or %NULL if there is no match.
  25. */
  26. static const struct rio_device_id *rio_match_device(const struct rio_device_id
  27. *id,
  28. const struct rio_dev *rdev)
  29. {
  30. while (id->vid || id->asm_vid) {
  31. if (((id->vid == RIO_ANY_ID) || (id->vid == rdev->vid)) &&
  32. ((id->did == RIO_ANY_ID) || (id->did == rdev->did)) &&
  33. ((id->asm_vid == RIO_ANY_ID)
  34. || (id->asm_vid == rdev->asm_vid))
  35. && ((id->asm_did == RIO_ANY_ID)
  36. || (id->asm_did == rdev->asm_did)))
  37. return id;
  38. id++;
  39. }
  40. return NULL;
  41. }
  42. /**
  43. * rio_dev_get - Increments the reference count of the RIO device structure
  44. *
  45. * @rdev: RIO device being referenced
  46. *
  47. * Each live reference to a device should be refcounted.
  48. *
  49. * Drivers for RIO devices should normally record such references in
  50. * their probe() methods, when they bind to a device, and release
  51. * them by calling rio_dev_put(), in their disconnect() methods.
  52. */
  53. struct rio_dev *rio_dev_get(struct rio_dev *rdev)
  54. {
  55. if (rdev)
  56. get_device(&rdev->dev);
  57. return rdev;
  58. }
  59. /**
  60. * rio_dev_put - Release a use of the RIO device structure
  61. *
  62. * @rdev: RIO device being disconnected
  63. *
  64. * Must be called when a user of a device is finished with it.
  65. * When the last user of the device calls this function, the
  66. * memory of the device is freed.
  67. */
  68. void rio_dev_put(struct rio_dev *rdev)
  69. {
  70. if (rdev)
  71. put_device(&rdev->dev);
  72. }
  73. /**
  74. * rio_device_probe - Tell if a RIO device structure has a matching RIO
  75. * device id structure
  76. * @id: the RIO device id structure to match against
  77. * @dev: the RIO device structure to match against
  78. *
  79. * return 0 and set rio_dev->driver when drv claims rio_dev, else error
  80. */
  81. static int rio_device_probe(struct device *dev)
  82. {
  83. struct rio_driver *rdrv = to_rio_driver(dev->driver);
  84. struct rio_dev *rdev = to_rio_dev(dev);
  85. int error = -ENODEV;
  86. const struct rio_device_id *id;
  87. if (!rdev->driver && rdrv->probe) {
  88. if (!rdrv->id_table)
  89. return error;
  90. id = rio_match_device(rdrv->id_table, rdev);
  91. rio_dev_get(rdev);
  92. if (id)
  93. error = rdrv->probe(rdev, id);
  94. if (error >= 0) {
  95. rdev->driver = rdrv;
  96. error = 0;
  97. rio_dev_put(rdev);
  98. }
  99. }
  100. return error;
  101. }
  102. /**
  103. * rio_device_remove - Remove a RIO device from the system
  104. *
  105. * @dev: the RIO device structure to match against
  106. *
  107. * Remove a RIO device from the system. If it has an associated
  108. * driver, then run the driver remove() method. Then update
  109. * the reference count.
  110. */
  111. static int rio_device_remove(struct device *dev)
  112. {
  113. struct rio_dev *rdev = to_rio_dev(dev);
  114. struct rio_driver *rdrv = rdev->driver;
  115. if (rdrv) {
  116. if (rdrv->remove)
  117. rdrv->remove(rdev);
  118. rdev->driver = NULL;
  119. }
  120. rio_dev_put(rdev);
  121. return 0;
  122. }
  123. /**
  124. * rio_register_driver - register a new RIO driver
  125. * @rdrv: the RIO driver structure to register
  126. *
  127. * Adds a &struct rio_driver to the list of registered drivers
  128. * Returns a negative value on error, otherwise 0. If no error
  129. * occurred, the driver remains registered even if no device
  130. * was claimed during registration.
  131. */
  132. int rio_register_driver(struct rio_driver *rdrv)
  133. {
  134. /* initialize common driver fields */
  135. rdrv->driver.name = rdrv->name;
  136. rdrv->driver.bus = &rio_bus_type;
  137. /* register with core */
  138. return driver_register(&rdrv->driver);
  139. }
  140. /**
  141. * rio_unregister_driver - unregister a RIO driver
  142. * @rdrv: the RIO driver structure to unregister
  143. *
  144. * Deletes the &struct rio_driver from the list of registered RIO
  145. * drivers, gives it a chance to clean up by calling its remove()
  146. * function for each device it was responsible for, and marks those
  147. * devices as driverless.
  148. */
  149. void rio_unregister_driver(struct rio_driver *rdrv)
  150. {
  151. driver_unregister(&rdrv->driver);
  152. }
  153. /**
  154. * rio_match_bus - Tell if a RIO device structure has a matching RIO
  155. * driver device id structure
  156. * @dev: the standard device structure to match against
  157. * @drv: the standard driver structure containing the ids to match against
  158. *
  159. * Used by a driver to check whether a RIO device present in the
  160. * system is in its list of supported devices. Returns 1 if
  161. * there is a matching &struct rio_device_id or 0 if there is
  162. * no match.
  163. */
  164. static int rio_match_bus(struct device *dev, struct device_driver *drv)
  165. {
  166. struct rio_dev *rdev = to_rio_dev(dev);
  167. struct rio_driver *rdrv = to_rio_driver(drv);
  168. const struct rio_device_id *id = rdrv->id_table;
  169. const struct rio_device_id *found_id;
  170. if (!id)
  171. goto out;
  172. found_id = rio_match_device(id, rdev);
  173. if (found_id)
  174. return 1;
  175. out:return 0;
  176. }
  177. static struct device rio_bus = {
  178. .bus_id = "rapidio",
  179. };
  180. struct bus_type rio_bus_type = {
  181. .name = "rapidio",
  182. .match = rio_match_bus,
  183. .dev_attrs = rio_dev_attrs,
  184. .probe = rio_device_probe,
  185. .remove = rio_device_remove,
  186. };
  187. /**
  188. * rio_bus_init - Register the RapidIO bus with the device model
  189. *
  190. * Registers the RIO bus device and RIO bus type with the Linux
  191. * device model.
  192. */
  193. static int __init rio_bus_init(void)
  194. {
  195. if (device_register(&rio_bus) < 0)
  196. printk("RIO: failed to register RIO bus device\n");
  197. return bus_register(&rio_bus_type);
  198. }
  199. postcore_initcall(rio_bus_init);
  200. EXPORT_SYMBOL_GPL(rio_register_driver);
  201. EXPORT_SYMBOL_GPL(rio_unregister_driver);
  202. EXPORT_SYMBOL_GPL(rio_bus_type);
  203. EXPORT_SYMBOL_GPL(rio_dev_get);
  204. EXPORT_SYMBOL_GPL(rio_dev_put);