rio-driver.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 device id structure
  75. * @id: the RIO device id structure to match against
  76. * @dev: the RIO device structure to match against
  77. *
  78. * return 0 and set rio_dev->driver when drv claims rio_dev, else error
  79. */
  80. static int rio_device_probe(struct device *dev)
  81. {
  82. struct rio_driver *rdrv = to_rio_driver(dev->driver);
  83. struct rio_dev *rdev = to_rio_dev(dev);
  84. int error = -ENODEV;
  85. const struct rio_device_id *id;
  86. if (!rdev->driver && rdrv->probe) {
  87. if (!rdrv->id_table)
  88. return error;
  89. id = rio_match_device(rdrv->id_table, rdev);
  90. rio_dev_get(rdev);
  91. if (id)
  92. error = rdrv->probe(rdev, id);
  93. if (error >= 0) {
  94. rdev->driver = rdrv;
  95. error = 0;
  96. } else
  97. rio_dev_put(rdev);
  98. }
  99. return error;
  100. }
  101. /**
  102. * rio_device_remove - Remove a RIO device from the system
  103. *
  104. * @dev: the RIO device structure to match against
  105. *
  106. * Remove a RIO device from the system. If it has an associated
  107. * driver, then run the driver remove() method. Then update
  108. * the reference count.
  109. */
  110. static int rio_device_remove(struct device *dev)
  111. {
  112. struct rio_dev *rdev = to_rio_dev(dev);
  113. struct rio_driver *rdrv = rdev->driver;
  114. if (rdrv) {
  115. if (rdrv->remove)
  116. rdrv->remove(rdev);
  117. rdev->driver = NULL;
  118. }
  119. rio_dev_put(rdev);
  120. return 0;
  121. }
  122. /**
  123. * rio_register_driver - register a new RIO driver
  124. * @rdrv: the RIO driver structure to register
  125. *
  126. * Adds a &struct rio_driver to the list of registered drivers.
  127. * Returns a negative value on error, otherwise 0. If no error
  128. * occurred, the driver remains registered even if no device
  129. * was claimed during registration.
  130. */
  131. int rio_register_driver(struct rio_driver *rdrv)
  132. {
  133. /* initialize common driver fields */
  134. rdrv->driver.name = rdrv->name;
  135. rdrv->driver.bus = &rio_bus_type;
  136. /* register with core */
  137. return driver_register(&rdrv->driver);
  138. }
  139. /**
  140. * rio_unregister_driver - unregister a RIO driver
  141. * @rdrv: the RIO driver structure to unregister
  142. *
  143. * Deletes the &struct rio_driver from the list of registered RIO
  144. * drivers, gives it a chance to clean up by calling its remove()
  145. * function for each device it was responsible for, and marks those
  146. * devices as driverless.
  147. */
  148. void rio_unregister_driver(struct rio_driver *rdrv)
  149. {
  150. driver_unregister(&rdrv->driver);
  151. }
  152. /**
  153. * rio_match_bus - Tell if a RIO device structure has a matching RIO driver device id structure
  154. * @dev: the standard device structure to match against
  155. * @drv: the standard driver structure containing the ids to match against
  156. *
  157. * Used by a driver to check whether a RIO device present in the
  158. * system is in its list of supported devices. Returns 1 if
  159. * there is a matching &struct rio_device_id or 0 if there is
  160. * no match.
  161. */
  162. static int rio_match_bus(struct device *dev, struct device_driver *drv)
  163. {
  164. struct rio_dev *rdev = to_rio_dev(dev);
  165. struct rio_driver *rdrv = to_rio_driver(drv);
  166. const struct rio_device_id *id = rdrv->id_table;
  167. const struct rio_device_id *found_id;
  168. if (!id)
  169. goto out;
  170. found_id = rio_match_device(id, rdev);
  171. if (found_id)
  172. return 1;
  173. out:return 0;
  174. }
  175. static struct device rio_bus = {
  176. .bus_id = "rapidio",
  177. };
  178. struct bus_type rio_bus_type = {
  179. .name = "rapidio",
  180. .match = rio_match_bus,
  181. .dev_attrs = rio_dev_attrs,
  182. .probe = rio_device_probe,
  183. .remove = rio_device_remove,
  184. };
  185. /**
  186. * rio_bus_init - Register the RapidIO bus with the device model
  187. *
  188. * Registers the RIO bus device and RIO bus type with the Linux
  189. * device model.
  190. */
  191. static int __init rio_bus_init(void)
  192. {
  193. if (device_register(&rio_bus) < 0)
  194. printk("RIO: failed to register RIO bus device\n");
  195. return bus_register(&rio_bus_type);
  196. }
  197. postcore_initcall(rio_bus_init);
  198. EXPORT_SYMBOL_GPL(rio_register_driver);
  199. EXPORT_SYMBOL_GPL(rio_unregister_driver);
  200. EXPORT_SYMBOL_GPL(rio_bus_type);
  201. EXPORT_SYMBOL_GPL(rio_dev_get);
  202. EXPORT_SYMBOL_GPL(rio_dev_put);