rio-driver.c 6.7 KB

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