blob: da62c5496e50bc79b61e4a93ce3eead5f25525d9 [file] [log] [blame]
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +08001/*
2 * Greybus "Core"
3 *
4 * Copyright 2014 Google Inc.
Alex Eldera46e9672014-12-12 12:08:42 -06005 * Copyright 2014 Linaro Ltd.
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +08006 *
7 * Released under the GPLv2 only.
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/types.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
Greg Kroah-Hartmana239f672014-09-01 14:39:49 -070016#include <linux/slab.h>
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080017#include <linux/device.h>
18
19#include "greybus.h"
20
21/* Allow greybus to be disabled at boot if needed */
22static bool nogreybus;
23#ifdef MODULE
24module_param(nogreybus, bool, 0444);
25#else
Viresh Kumar8597e6b2014-10-20 16:45:50 +053026core_param(nogreybus, nogreybus, bool, 0444);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080027#endif
28int greybus_disabled(void)
29{
30 return nogreybus;
31}
32EXPORT_SYMBOL_GPL(greybus_disabled);
33
Alex Elder778c69c2014-09-22 19:19:03 -050034static int greybus_module_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080035{
Viresh Kumar95bd99d2014-11-14 17:24:59 +053036 struct greybus_driver *driver = to_greybus_driver(drv);
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +053037 struct gb_bundle *bundle = to_gb_bundle(dev);
38 const struct greybus_bundle_id *id;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080039
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +053040 id = gb_bundle_match_id(bundle, driver->id_table);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080041 if (id)
42 return 1;
Viresh Kumar696e0cc2014-11-21 11:26:30 +053043 /* FIXME - Dynamic ids? */
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080044 return 0;
45}
46
47static int greybus_uevent(struct device *dev, struct kobj_uevent_env *env)
48{
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -080049 struct gb_module *module = NULL;
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -080050 struct gb_interface *intf = NULL;
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -050051 struct gb_bundle *bundle = NULL;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -080052 struct gb_connection *connection = NULL;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080053
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -080054 if (is_gb_module(dev)) {
55 module = to_gb_module(dev);
56 } else if (is_gb_interface(dev)) {
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -080057 intf = to_gb_interface(dev);
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -050058 } else if (is_gb_bundle(dev)) {
59 bundle = to_gb_bundle(dev);
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -080060 intf = bundle->intf;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -080061 } else if (is_gb_connection(dev)) {
62 connection = to_gb_connection(dev);
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -050063 bundle = connection->bundle;
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -080064 intf = bundle->intf;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -080065 } else {
66 dev_WARN(dev, "uevent for unknown greybus device \"type\"!\n");
67 return -EINVAL;
68 }
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +080069
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -080070 if (connection) {
71 // FIXME
72 // add a uevent that can "load" a connection type
73 return 0;
74 }
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +080075
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -050076 if (bundle) {
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -080077 // FIXME
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -050078 // add a uevent that can "load" a bundle type
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -080079 // This is what we need to bind a driver to so use the info
80 // in gmod here as well
81 return 0;
82 }
83
84 // FIXME
85 // "just" a module, be vague here, nothing binds to a module except
86 // the greybus core, so there's not much, if anything, we need to
87 // advertise.
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080088 return 0;
89}
90
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +080091struct bus_type greybus_bus_type = {
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080092 .name = "greybus",
Alex Elder778c69c2014-09-22 19:19:03 -050093 .match = greybus_module_match,
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080094 .uevent = greybus_uevent,
95};
96
97static int greybus_probe(struct device *dev)
98{
99 struct greybus_driver *driver = to_greybus_driver(dev->driver);
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530100 struct gb_bundle *bundle = to_gb_bundle(dev);
101 const struct greybus_bundle_id *id;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800102 int retval;
103
104 /* match id */
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530105 id = gb_bundle_match_id(bundle, driver->id_table);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800106 if (!id)
107 return -ENODEV;
108
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530109 retval = driver->probe(bundle, id);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800110 if (retval)
111 return retval;
112
113 return 0;
114}
115
116static int greybus_remove(struct device *dev)
117{
118 struct greybus_driver *driver = to_greybus_driver(dev->driver);
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530119 struct gb_bundle *bundle = to_gb_bundle(dev);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800120
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530121 driver->disconnect(bundle);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800122 return 0;
123}
124
125int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
126 const char *mod_name)
127{
128 int retval;
129
130 if (greybus_disabled())
131 return -ENODEV;
132
133 driver->driver.name = driver->name;
134 driver->driver.probe = greybus_probe;
135 driver->driver.remove = greybus_remove;
136 driver->driver.owner = owner;
137 driver->driver.mod_name = mod_name;
138
139 retval = driver_register(&driver->driver);
140 if (retval)
141 return retval;
142
143 pr_info("registered new driver %s\n", driver->name);
144 return 0;
145}
146EXPORT_SYMBOL_GPL(greybus_register_driver);
147
148void greybus_deregister(struct greybus_driver *driver)
149{
150 driver_unregister(&driver->driver);
151}
152EXPORT_SYMBOL_GPL(greybus_deregister);
153
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700154
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700155static DEFINE_MUTEX(hd_mutex);
156
157static void free_hd(struct kref *kref)
158{
159 struct greybus_host_device *hd;
160
161 hd = container_of(kref, struct greybus_host_device, kref);
162
163 kfree(hd);
Alex Eldera06df4b2014-10-16 06:35:26 -0500164 mutex_unlock(&hd_mutex);
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700165}
166
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700167struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *driver,
168 struct device *parent)
169{
170 struct greybus_host_device *hd;
171
Greg Kroah-Hartman724b6192014-10-27 13:32:27 +0800172 /*
173 * Validate that the driver implements all of the callbacks
174 * so that we don't have to every time we make them.
175 */
Johan Hovold7cf7bca2015-04-07 11:27:16 +0200176 if ((!driver->message_send) || (!driver->message_cancel) ||
Alex Eldera9163b22014-11-18 13:26:44 -0600177 (!driver->submit_svc)) {
Greg Kroah-Hartman724b6192014-10-27 13:32:27 +0800178 pr_err("Must implement all greybus_host_driver callbacks!\n");
179 return NULL;
180 }
181
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700182 hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
183 if (!hd)
184 return NULL;
185
186 kref_init(&hd->kref);
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700187 hd->parent = parent;
188 hd->driver = driver;
Greg Kroah-Hartman1cd56a82014-12-19 14:56:36 -0800189 INIT_LIST_HEAD(&hd->interfaces);
Alex Elder2c43ce42014-11-17 08:08:44 -0600190 INIT_LIST_HEAD(&hd->connections);
Alex Elder177404b2014-10-03 14:14:24 -0500191 ida_init(&hd->cport_id_map);
Alex Elder1bb3c722014-10-02 12:30:03 -0500192
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700193 return hd;
194}
195EXPORT_SYMBOL_GPL(greybus_create_hd);
196
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700197void greybus_remove_hd(struct greybus_host_device *hd)
198{
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +0800199 /* Tear down all modules that happen to be associated with this host
200 * controller */
Greg Kroah-Hartman13e6aac2014-12-19 14:56:35 -0800201 gb_remove_interfaces(hd);
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700202 kref_put_mutex(&hd->kref, free_hd, &hd_mutex);
203}
204EXPORT_SYMBOL_GPL(greybus_remove_hd);
205
Greg Kroah-Hartman503c1cd2014-08-30 16:21:03 -0700206static int __init gb_init(void)
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700207{
Greg Kroah-Hartmandb6e1fd2014-08-30 16:47:26 -0700208 int retval;
209
Viresh Kumar337b0682015-03-20 20:29:13 +0530210 if (greybus_disabled())
211 return -ENODEV;
212
Alex Elder1bb3c722014-10-02 12:30:03 -0500213 BUILD_BUG_ON(HOST_DEV_CPORT_ID_MAX >= (long)CPORT_ID_BAD);
Alex Elder1bb3c722014-10-02 12:30:03 -0500214
Greg Kroah-Hartman48f70472015-03-27 11:38:06 +0100215 gb_debugfs_init();
Greg Kroah-Hartmandb6e1fd2014-08-30 16:47:26 -0700216
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700217 retval = bus_register(&greybus_bus_type);
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700218 if (retval) {
219 pr_err("bus_register failed\n");
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700220 goto error_bus;
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700221 }
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700222
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700223 retval = gb_ap_init();
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700224 if (retval) {
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700225 pr_err("gb_ap_init failed\n");
226 goto error_ap;
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700227 }
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700228
Alex Elder2eb585f2014-10-16 06:35:34 -0500229 retval = gb_operation_init();
230 if (retval) {
231 pr_err("gb_operation_init failed\n");
232 goto error_operation;
233 }
234
Alex Elder19d03de2014-11-05 16:12:53 -0600235 return 0; /* Success */
236
Alex Elder2eb585f2014-10-16 06:35:34 -0500237error_operation:
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700238 gb_ap_exit();
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700239error_ap:
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700240 bus_unregister(&greybus_bus_type);
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700241error_bus:
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700242 gb_debugfs_cleanup();
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700243
244 return retval;
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700245}
Viresh Kumard71aaf22015-03-19 17:02:49 +0530246module_init(gb_init);
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700247
Greg Kroah-Hartman503c1cd2014-08-30 16:21:03 -0700248static void __exit gb_exit(void)
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700249{
Alex Elder2eb585f2014-10-16 06:35:34 -0500250 gb_operation_exit();
Greg Kroah-Hartman45f36782014-09-14 11:40:35 -0700251 gb_ap_exit();
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700252 bus_unregister(&greybus_bus_type);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700253 gb_debugfs_cleanup();
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700254}
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700255module_exit(gb_exit);
256
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800257MODULE_LICENSE("GPL");
258MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");