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