core.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. const struct bus_type greybus_bus_type = {
  157. .name = "greybus",
  158. .match = greybus_match_device,
  159. .uevent = greybus_uevent,
  160. .shutdown = greybus_shutdown,
  161. };
  162. static int greybus_probe(struct device *dev)
  163. {
  164. struct greybus_driver *driver = to_greybus_driver(dev->driver);
  165. struct gb_bundle *bundle = to_gb_bundle(dev);
  166. const struct greybus_bundle_id *id;
  167. int retval;
  168. /* match id */
  169. id = greybus_match_id(bundle, driver->id_table);
  170. if (!id)
  171. return -ENODEV;
  172. retval = pm_runtime_get_sync(&bundle->intf->dev);
  173. if (retval < 0) {
  174. pm_runtime_put_noidle(&bundle->intf->dev);
  175. return retval;
  176. }
  177. retval = gb_control_bundle_activate(bundle->intf->control, bundle->id);
  178. if (retval) {
  179. pm_runtime_put(&bundle->intf->dev);
  180. return retval;
  181. }
  182. /*
  183. * Unbound bundle devices are always deactivated. During probe, the
  184. * Runtime PM is set to enabled and active and the usage count is
  185. * incremented. If the driver supports runtime PM, it should call
  186. * pm_runtime_put() in its probe routine and pm_runtime_get_sync()
  187. * in remove routine.
  188. */
  189. pm_runtime_set_autosuspend_delay(dev, GB_BUNDLE_AUTOSUSPEND_MS);
  190. pm_runtime_use_autosuspend(dev);
  191. pm_runtime_get_noresume(dev);
  192. pm_runtime_set_active(dev);
  193. pm_runtime_enable(dev);
  194. retval = driver->probe(bundle, id);
  195. if (retval) {
  196. /*
  197. * Catch buggy drivers that fail to destroy their connections.
  198. */
  199. WARN_ON(!list_empty(&bundle->connections));
  200. gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
  201. pm_runtime_disable(dev);
  202. pm_runtime_set_suspended(dev);
  203. pm_runtime_put_noidle(dev);
  204. pm_runtime_dont_use_autosuspend(dev);
  205. pm_runtime_put(&bundle->intf->dev);
  206. return retval;
  207. }
  208. pm_runtime_put(&bundle->intf->dev);
  209. return 0;
  210. }
  211. static int greybus_remove(struct device *dev)
  212. {
  213. struct greybus_driver *driver = to_greybus_driver(dev->driver);
  214. struct gb_bundle *bundle = to_gb_bundle(dev);
  215. struct gb_connection *connection;
  216. int retval;
  217. retval = pm_runtime_get_sync(dev);
  218. if (retval < 0)
  219. dev_err(dev, "failed to resume bundle: %d\n", retval);
  220. /*
  221. * Disable (non-offloaded) connections early in case the interface is
  222. * already gone to avoid unceccessary operation timeouts during
  223. * driver disconnect. Otherwise, only disable incoming requests.
  224. */
  225. list_for_each_entry(connection, &bundle->connections, bundle_links) {
  226. if (gb_connection_is_offloaded(connection))
  227. continue;
  228. if (bundle->intf->disconnected)
  229. gb_connection_disable_forced(connection);
  230. else
  231. gb_connection_disable_rx(connection);
  232. }
  233. driver->disconnect(bundle);
  234. /* Catch buggy drivers that fail to destroy their connections. */
  235. WARN_ON(!list_empty(&bundle->connections));
  236. if (!bundle->intf->disconnected)
  237. gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
  238. pm_runtime_put_noidle(dev);
  239. pm_runtime_disable(dev);
  240. pm_runtime_set_suspended(dev);
  241. pm_runtime_dont_use_autosuspend(dev);
  242. pm_runtime_put_noidle(dev);
  243. return 0;
  244. }
  245. int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
  246. const char *mod_name)
  247. {
  248. int retval;
  249. if (greybus_disabled())
  250. return -ENODEV;
  251. driver->driver.bus = &greybus_bus_type;
  252. driver->driver.name = driver->name;
  253. driver->driver.probe = greybus_probe;
  254. driver->driver.remove = greybus_remove;
  255. driver->driver.owner = owner;
  256. driver->driver.mod_name = mod_name;
  257. retval = driver_register(&driver->driver);
  258. if (retval)
  259. return retval;
  260. pr_info("registered new driver %s\n", driver->name);
  261. return 0;
  262. }
  263. EXPORT_SYMBOL_GPL(greybus_register_driver);
  264. void greybus_deregister_driver(struct greybus_driver *driver)
  265. {
  266. driver_unregister(&driver->driver);
  267. }
  268. EXPORT_SYMBOL_GPL(greybus_deregister_driver);
  269. static int __init gb_init(void)
  270. {
  271. int retval;
  272. if (greybus_disabled())
  273. return -ENODEV;
  274. BUILD_BUG_ON(CPORT_ID_MAX >= (long)CPORT_ID_BAD);
  275. gb_debugfs_init();
  276. retval = bus_register(&greybus_bus_type);
  277. if (retval) {
  278. pr_err("bus_register failed (%d)\n", retval);
  279. goto error_bus;
  280. }
  281. retval = gb_hd_init();
  282. if (retval) {
  283. pr_err("gb_hd_init failed (%d)\n", retval);
  284. goto error_hd;
  285. }
  286. retval = gb_operation_init();
  287. if (retval) {
  288. pr_err("gb_operation_init failed (%d)\n", retval);
  289. goto error_operation;
  290. }
  291. return 0; /* Success */
  292. error_operation:
  293. gb_hd_exit();
  294. error_hd:
  295. bus_unregister(&greybus_bus_type);
  296. error_bus:
  297. gb_debugfs_cleanup();
  298. return retval;
  299. }
  300. module_init(gb_init);
  301. static void __exit gb_exit(void)
  302. {
  303. gb_operation_exit();
  304. gb_hd_exit();
  305. bus_unregister(&greybus_bus_type);
  306. gb_debugfs_cleanup();
  307. tracepoint_synchronize_unregister();
  308. }
  309. module_exit(gb_exit);
  310. MODULE_DESCRIPTION("Greybus core driver");
  311. MODULE_LICENSE("GPL v2");
  312. MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");