rio-driver.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. void rio_attach_device(struct rio_dev *rdev)
  152. {
  153. rdev->dev.bus = &rio_bus_type;
  154. }
  155. EXPORT_SYMBOL_GPL(rio_attach_device);
  156. /**
  157. * rio_match_bus - Tell if a RIO device structure has a matching RIO driver device id structure
  158. * @dev: the standard device structure to match against
  159. * @drv: the standard driver structure containing the ids to match against
  160. *
  161. * Used by a driver to check whether a RIO device present in the
  162. * system is in its list of supported devices. Returns 1 if
  163. * there is a matching &struct rio_device_id or 0 if there is
  164. * no match.
  165. */
  166. static int rio_match_bus(struct device *dev, struct device_driver *drv)
  167. {
  168. struct rio_dev *rdev = to_rio_dev(dev);
  169. struct rio_driver *rdrv = to_rio_driver(drv);
  170. const struct rio_device_id *id = rdrv->id_table;
  171. const struct rio_device_id *found_id;
  172. if (!id)
  173. goto out;
  174. found_id = rio_match_device(id, rdev);
  175. if (found_id)
  176. return 1;
  177. out:return 0;
  178. }
  179. static int rio_uevent(struct device *dev, struct kobj_uevent_env *env)
  180. {
  181. struct rio_dev *rdev;
  182. if (!dev)
  183. return -ENODEV;
  184. rdev = to_rio_dev(dev);
  185. if (!rdev)
  186. return -ENODEV;
  187. if (add_uevent_var(env, "MODALIAS=rapidio:v%04Xd%04Xav%04Xad%04X",
  188. rdev->vid, rdev->did, rdev->asm_vid, rdev->asm_did))
  189. return -ENOMEM;
  190. return 0;
  191. }
  192. struct class rio_mport_class = {
  193. .name = "rapidio_port",
  194. .owner = THIS_MODULE,
  195. .dev_groups = rio_mport_groups,
  196. };
  197. EXPORT_SYMBOL_GPL(rio_mport_class);
  198. struct bus_type rio_bus_type = {
  199. .name = "rapidio",
  200. .match = rio_match_bus,
  201. .dev_groups = rio_dev_groups,
  202. .bus_groups = rio_bus_groups,
  203. .probe = rio_device_probe,
  204. .remove = rio_device_remove,
  205. .uevent = rio_uevent,
  206. };
  207. /**
  208. * rio_bus_init - Register the RapidIO bus with the device model
  209. *
  210. * Registers the RIO mport device class and RIO bus type with the Linux
  211. * device model.
  212. */
  213. static int __init rio_bus_init(void)
  214. {
  215. int ret;
  216. ret = class_register(&rio_mport_class);
  217. if (!ret) {
  218. ret = bus_register(&rio_bus_type);
  219. if (ret)
  220. class_unregister(&rio_mport_class);
  221. }
  222. return ret;
  223. }
  224. postcore_initcall(rio_bus_init);
  225. EXPORT_SYMBOL_GPL(rio_register_driver);
  226. EXPORT_SYMBOL_GPL(rio_unregister_driver);
  227. EXPORT_SYMBOL_GPL(rio_bus_type);
  228. EXPORT_SYMBOL_GPL(rio_dev_get);
  229. EXPORT_SYMBOL_GPL(rio_dev_put);