core.c 8.6 KB

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