rio-driver.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. static void rio_device_shutdown(struct device *dev)
  122. {
  123. struct rio_dev *rdev = to_rio_dev(dev);
  124. struct rio_driver *rdrv = rdev->driver;
  125. dev_dbg(dev, "RIO: %s\n", __func__);
  126. if (rdrv && rdrv->shutdown)
  127. rdrv->shutdown(rdev);
  128. }
  129. /**
  130. * rio_register_driver - register a new RIO driver
  131. * @rdrv: the RIO driver structure to register
  132. *
  133. * Adds a &struct rio_driver to the list of registered drivers.
  134. * Returns a negative value on error, otherwise 0. If no error
  135. * occurred, the driver remains registered even if no device
  136. * was claimed during registration.
  137. */
  138. int rio_register_driver(struct rio_driver *rdrv)
  139. {
  140. /* initialize common driver fields */
  141. rdrv->driver.name = rdrv->name;
  142. rdrv->driver.bus = &rio_bus_type;
  143. /* register with core */
  144. return driver_register(&rdrv->driver);
  145. }
  146. /**
  147. * rio_unregister_driver - unregister a RIO driver
  148. * @rdrv: the RIO driver structure to unregister
  149. *
  150. * Deletes the &struct rio_driver from the list of registered RIO
  151. * drivers, gives it a chance to clean up by calling its remove()
  152. * function for each device it was responsible for, and marks those
  153. * devices as driverless.
  154. */
  155. void rio_unregister_driver(struct rio_driver *rdrv)
  156. {
  157. driver_unregister(&rdrv->driver);
  158. }
  159. void rio_attach_device(struct rio_dev *rdev)
  160. {
  161. rdev->dev.bus = &rio_bus_type;
  162. }
  163. EXPORT_SYMBOL_GPL(rio_attach_device);
  164. /**
  165. * rio_match_bus - Tell if a RIO device structure has a matching RIO driver device id structure
  166. * @dev: the standard device structure to match against
  167. * @drv: the standard driver structure containing the ids to match against
  168. *
  169. * Used by a driver to check whether a RIO device present in the
  170. * system is in its list of supported devices. Returns 1 if
  171. * there is a matching &struct rio_device_id or 0 if there is
  172. * no match.
  173. */
  174. static int rio_match_bus(struct device *dev, struct device_driver *drv)
  175. {
  176. struct rio_dev *rdev = to_rio_dev(dev);
  177. struct rio_driver *rdrv = to_rio_driver(drv);
  178. const struct rio_device_id *id = rdrv->id_table;
  179. const struct rio_device_id *found_id;
  180. if (!id)
  181. goto out;
  182. found_id = rio_match_device(id, rdev);
  183. if (found_id)
  184. return 1;
  185. out:return 0;
  186. }
  187. static int rio_uevent(struct device *dev, struct kobj_uevent_env *env)
  188. {
  189. struct rio_dev *rdev;
  190. if (!dev)
  191. return -ENODEV;
  192. rdev = to_rio_dev(dev);
  193. if (!rdev)
  194. return -ENODEV;
  195. if (add_uevent_var(env, "MODALIAS=rapidio:v%04Xd%04Xav%04Xad%04X",
  196. rdev->vid, rdev->did, rdev->asm_vid, rdev->asm_did))
  197. return -ENOMEM;
  198. return 0;
  199. }
  200. struct class rio_mport_class = {
  201. .name = "rapidio_port",
  202. .owner = THIS_MODULE,
  203. .dev_groups = rio_mport_groups,
  204. };
  205. EXPORT_SYMBOL_GPL(rio_mport_class);
  206. struct bus_type rio_bus_type = {
  207. .name = "rapidio",
  208. .match = rio_match_bus,
  209. .dev_groups = rio_dev_groups,
  210. .bus_groups = rio_bus_groups,
  211. .probe = rio_device_probe,
  212. .remove = rio_device_remove,
  213. .shutdown = rio_device_shutdown,
  214. .uevent = rio_uevent,
  215. };
  216. /**
  217. * rio_bus_init - Register the RapidIO bus with the device model
  218. *
  219. * Registers the RIO mport device class and RIO bus type with the Linux
  220. * device model.
  221. */
  222. static int __init rio_bus_init(void)
  223. {
  224. int ret;
  225. ret = class_register(&rio_mport_class);
  226. if (!ret) {
  227. ret = bus_register(&rio_bus_type);
  228. if (ret)
  229. class_unregister(&rio_mport_class);
  230. }
  231. return ret;
  232. }
  233. postcore_initcall(rio_bus_init);
  234. EXPORT_SYMBOL_GPL(rio_register_driver);
  235. EXPORT_SYMBOL_GPL(rio_unregister_driver);
  236. EXPORT_SYMBOL_GPL(rio_bus_type);
  237. EXPORT_SYMBOL_GPL(rio_dev_get);
  238. EXPORT_SYMBOL_GPL(rio_dev_put);