core.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Greybus "Core"
  4. *
  5. * Copyright 2014-2015 Google Inc.
  6. * Copyright 2014-2015 Linaro Ltd.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #define CREATE_TRACE_POINTS
  10. #include <linux/greybus.h>
  11. #include "greybus_trace.h"
  12. #define GB_BUNDLE_AUTOSUSPEND_MS 3000
  13. /* Allow greybus to be disabled at boot if needed */
  14. static bool nogreybus;
  15. #ifdef MODULE
  16. module_param(nogreybus, bool, 0444);
  17. #else
  18. core_param(nogreybus, nogreybus, bool, 0444);
  19. #endif
  20. int greybus_disabled(void)
  21. {
  22. return nogreybus;
  23. }
  24. EXPORT_SYMBOL_GPL(greybus_disabled);
  25. static bool greybus_match_one_id(struct gb_bundle *bundle,
  26. const struct greybus_bundle_id *id)
  27. {
  28. if ((id->match_flags & GREYBUS_ID_MATCH_VENDOR) &&
  29. (id->vendor != bundle->intf->vendor_id))
  30. return false;
  31. if ((id->match_flags & GREYBUS_ID_MATCH_PRODUCT) &&
  32. (id->product != bundle->intf->product_id))
  33. return false;
  34. if ((id->match_flags & GREYBUS_ID_MATCH_CLASS) &&
  35. (id->class != bundle->class))
  36. return false;
  37. return true;
  38. }
  39. static const struct greybus_bundle_id *
  40. greybus_match_id(struct gb_bundle *bundle, const struct greybus_bundle_id *id)
  41. {
  42. if (!id)
  43. return NULL;
  44. for (; id->vendor || id->product || id->class || id->driver_info;
  45. id++) {
  46. if (greybus_match_one_id(bundle, id))
  47. return id;
  48. }
  49. return NULL;
  50. }
  51. static int greybus_match_device(struct device *dev, struct device_driver *drv)
  52. {
  53. struct greybus_driver *driver = to_greybus_driver(drv);
  54. struct gb_bundle *bundle;
  55. const struct greybus_bundle_id *id;
  56. if (!is_gb_bundle(dev))
  57. return 0;
  58. bundle = to_gb_bundle(dev);
  59. id = greybus_match_id(bundle, driver->id_table);
  60. if (id)
  61. return 1;
  62. /* FIXME - Dynamic ids? */
  63. return 0;
  64. }
  65. static int greybus_uevent(struct device *dev, struct kobj_uevent_env *env)
  66. {
  67. struct gb_host_device *hd;
  68. struct gb_module *module = NULL;
  69. struct gb_interface *intf = NULL;
  70. struct gb_control *control = NULL;
  71. struct gb_bundle *bundle = NULL;
  72. struct gb_svc *svc = NULL;
  73. if (is_gb_host_device(dev)) {
  74. hd = to_gb_host_device(dev);
  75. } else if (is_gb_module(dev)) {
  76. module = to_gb_module(dev);
  77. hd = module->hd;
  78. } else if (is_gb_interface(dev)) {
  79. intf = to_gb_interface(dev);
  80. module = intf->module;
  81. hd = intf->hd;
  82. } else if (is_gb_control(dev)) {
  83. control = to_gb_control(dev);
  84. intf = control->intf;
  85. module = intf->module;
  86. hd = intf->hd;
  87. } else if (is_gb_bundle(dev)) {
  88. bundle = to_gb_bundle(dev);
  89. intf = bundle->intf;
  90. module = intf->module;
  91. hd = intf->hd;
  92. } else if (is_gb_svc(dev)) {
  93. svc = to_gb_svc(dev);
  94. hd = svc->hd;
  95. } else {
  96. dev_WARN(dev, "uevent for unknown greybus device \"type\"!\n");
  97. return -EINVAL;
  98. }
  99. if (add_uevent_var(env, "BUS=%u", hd->bus_id))
  100. return -ENOMEM;
  101. if (module) {
  102. if (add_uevent_var(env, "MODULE=%u", module->module_id))
  103. return -ENOMEM;
  104. }
  105. if (intf) {
  106. if (add_uevent_var(env, "INTERFACE=%u", intf->interface_id))
  107. return -ENOMEM;
  108. if (add_uevent_var(env, "GREYBUS_ID=%08x/%08x",
  109. intf->vendor_id, intf->product_id))
  110. return -ENOMEM;
  111. }
  112. if (bundle) {
  113. // FIXME
  114. // add a uevent that can "load" a bundle type
  115. // This is what we need to bind a driver to so use the info
  116. // in gmod here as well
  117. if (add_uevent_var(env, "BUNDLE=%u", bundle->id))
  118. return -ENOMEM;
  119. if (add_uevent_var(env, "BUNDLE_CLASS=%02x", bundle->class))
  120. return -ENOMEM;
  121. }
  122. return 0;
  123. }
  124. static void greybus_shutdown(struct device *dev)
  125. {
  126. if (is_gb_host_device(dev)) {
  127. struct gb_host_device *hd;
  128. hd = to_gb_host_device(dev);
  129. gb_hd_shutdown(hd);
  130. }
  131. }
  132. struct bus_type greybus_bus_type = {
  133. .name = "greybus",
  134. .match = greybus_match_device,
  135. .uevent = greybus_uevent,
  136. .shutdown = greybus_shutdown,
  137. };
  138. static int greybus_probe(struct device *dev)
  139. {
  140. struct greybus_driver *driver = to_greybus_driver(dev->driver);
  141. struct gb_bundle *bundle = to_gb_bundle(dev);
  142. const struct greybus_bundle_id *id;
  143. int retval;
  144. /* match id */
  145. id = greybus_match_id(bundle, driver->id_table);
  146. if (!id)
  147. return -ENODEV;
  148. retval = pm_runtime_get_sync(&bundle->intf->dev);
  149. if (retval < 0) {
  150. pm_runtime_put_noidle(&bundle->intf->dev);
  151. return retval;
  152. }
  153. retval = gb_control_bundle_activate(bundle->intf->control, bundle->id);
  154. if (retval) {
  155. pm_runtime_put(&bundle->intf->dev);
  156. return retval;
  157. }
  158. /*
  159. * Unbound bundle devices are always deactivated. During probe, the
  160. * Runtime PM is set to enabled and active and the usage count is
  161. * incremented. If the driver supports runtime PM, it should call
  162. * pm_runtime_put() in its probe routine and pm_runtime_get_sync()
  163. * in remove routine.
  164. */
  165. pm_runtime_set_autosuspend_delay(dev, GB_BUNDLE_AUTOSUSPEND_MS);
  166. pm_runtime_use_autosuspend(dev);
  167. pm_runtime_get_noresume(dev);
  168. pm_runtime_set_active(dev);
  169. pm_runtime_enable(dev);
  170. retval = driver->probe(bundle, id);
  171. if (retval) {
  172. /*
  173. * Catch buggy drivers that fail to destroy their connections.
  174. */
  175. WARN_ON(!list_empty(&bundle->connections));
  176. gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
  177. pm_runtime_disable(dev);
  178. pm_runtime_set_suspended(dev);
  179. pm_runtime_put_noidle(dev);
  180. pm_runtime_dont_use_autosuspend(dev);
  181. pm_runtime_put(&bundle->intf->dev);
  182. return retval;
  183. }
  184. pm_runtime_put(&bundle->intf->dev);
  185. return 0;
  186. }
  187. static int greybus_remove(struct device *dev)
  188. {
  189. struct greybus_driver *driver = to_greybus_driver(dev->driver);
  190. struct gb_bundle *bundle = to_gb_bundle(dev);
  191. struct gb_connection *connection;
  192. int retval;
  193. retval = pm_runtime_get_sync(dev);
  194. if (retval < 0)
  195. dev_err(dev, "failed to resume bundle: %d\n", retval);
  196. /*
  197. * Disable (non-offloaded) connections early in case the interface is
  198. * already gone to avoid unceccessary operation timeouts during
  199. * driver disconnect. Otherwise, only disable incoming requests.
  200. */
  201. list_for_each_entry(connection, &bundle->connections, bundle_links) {
  202. if (gb_connection_is_offloaded(connection))
  203. continue;
  204. if (bundle->intf->disconnected)
  205. gb_connection_disable_forced(connection);
  206. else
  207. gb_connection_disable_rx(connection);
  208. }
  209. driver->disconnect(bundle);
  210. /* Catch buggy drivers that fail to destroy their connections. */
  211. WARN_ON(!list_empty(&bundle->connections));
  212. if (!bundle->intf->disconnected)
  213. gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
  214. pm_runtime_put_noidle(dev);
  215. pm_runtime_disable(dev);
  216. pm_runtime_set_suspended(dev);
  217. pm_runtime_dont_use_autosuspend(dev);
  218. pm_runtime_put_noidle(dev);
  219. return 0;
  220. }
  221. int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
  222. const char *mod_name)
  223. {
  224. int retval;
  225. if (greybus_disabled())
  226. return -ENODEV;
  227. driver->driver.bus = &greybus_bus_type;
  228. driver->driver.name = driver->name;
  229. driver->driver.probe = greybus_probe;
  230. driver->driver.remove = greybus_remove;
  231. driver->driver.owner = owner;
  232. driver->driver.mod_name = mod_name;
  233. retval = driver_register(&driver->driver);
  234. if (retval)
  235. return retval;
  236. pr_info("registered new driver %s\n", driver->name);
  237. return 0;
  238. }
  239. EXPORT_SYMBOL_GPL(greybus_register_driver);
  240. void greybus_deregister_driver(struct greybus_driver *driver)
  241. {
  242. driver_unregister(&driver->driver);
  243. }
  244. EXPORT_SYMBOL_GPL(greybus_deregister_driver);
  245. static int __init gb_init(void)
  246. {
  247. int retval;
  248. if (greybus_disabled())
  249. return -ENODEV;
  250. BUILD_BUG_ON(CPORT_ID_MAX >= (long)CPORT_ID_BAD);
  251. gb_debugfs_init();
  252. retval = bus_register(&greybus_bus_type);
  253. if (retval) {
  254. pr_err("bus_register failed (%d)\n", retval);
  255. goto error_bus;
  256. }
  257. retval = gb_hd_init();
  258. if (retval) {
  259. pr_err("gb_hd_init failed (%d)\n", retval);
  260. goto error_hd;
  261. }
  262. retval = gb_operation_init();
  263. if (retval) {
  264. pr_err("gb_operation_init failed (%d)\n", retval);
  265. goto error_operation;
  266. }
  267. return 0; /* Success */
  268. error_operation:
  269. gb_hd_exit();
  270. error_hd:
  271. bus_unregister(&greybus_bus_type);
  272. error_bus:
  273. gb_debugfs_cleanup();
  274. return retval;
  275. }
  276. module_init(gb_init);
  277. static void __exit gb_exit(void)
  278. {
  279. gb_operation_exit();
  280. gb_hd_exit();
  281. bus_unregister(&greybus_bus_type);
  282. gb_debugfs_cleanup();
  283. tracepoint_synchronize_unregister();
  284. }
  285. module_exit(gb_exit);
  286. MODULE_LICENSE("GPL v2");
  287. MODULE_AUTHOR("Greg Kroah-Hartman <[email protected]>");