blob: 3c89cb368f6d23be72c17c7c824fc085ad598103 [file] [log] [blame]
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +08001/*
2 * Greybus "Core"
3 *
Alex Elder4441f472015-05-22 12:59:16 -05004 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 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
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080012#include "greybus.h"
13
14/* Allow greybus to be disabled at boot if needed */
15static bool nogreybus;
16#ifdef MODULE
17module_param(nogreybus, bool, 0444);
18#else
Viresh Kumar8597e6b2014-10-20 16:45:50 +053019core_param(nogreybus, nogreybus, bool, 0444);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080020#endif
21int greybus_disabled(void)
22{
23 return nogreybus;
24}
25EXPORT_SYMBOL_GPL(greybus_disabled);
26
Alex Elder778c69c2014-09-22 19:19:03 -050027static int greybus_module_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080028{
Viresh Kumar95bd99d2014-11-14 17:24:59 +053029 struct greybus_driver *driver = to_greybus_driver(drv);
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +053030 struct gb_bundle *bundle = to_gb_bundle(dev);
31 const struct greybus_bundle_id *id;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080032
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +053033 id = gb_bundle_match_id(bundle, driver->id_table);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080034 if (id)
35 return 1;
Viresh Kumar696e0cc2014-11-21 11:26:30 +053036 /* FIXME - Dynamic ids? */
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080037 return 0;
38}
39
40static int greybus_uevent(struct device *dev, struct kobj_uevent_env *env)
41{
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -080042 struct gb_module *module = NULL;
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -080043 struct gb_interface *intf = NULL;
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -050044 struct gb_bundle *bundle = NULL;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -080045 struct gb_connection *connection = NULL;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080046
Greg Kroah-Hartman0f035ac2015-04-07 20:26:53 +020047 if (is_gb_endo(dev)) {
48 /*
49 * Not much to do for an endo, just fall through, as the
50 * "default" attributes are good enough for us.
51 */
52 return 0;
53 }
54
Greg Kroah-Hartmandf671552014-12-21 14:10:26 -080055 if (is_gb_module(dev)) {
56 module = to_gb_module(dev);
57 } else if (is_gb_interface(dev)) {
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -080058 intf = to_gb_interface(dev);
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -050059 } else if (is_gb_bundle(dev)) {
60 bundle = to_gb_bundle(dev);
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -080061 intf = bundle->intf;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -080062 } else if (is_gb_connection(dev)) {
63 connection = to_gb_connection(dev);
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -050064 bundle = connection->bundle;
Greg Kroah-Hartman4ab9b3c2014-12-19 14:56:31 -080065 intf = bundle->intf;
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -080066 } else {
67 dev_WARN(dev, "uevent for unknown greybus device \"type\"!\n");
68 return -EINVAL;
69 }
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +080070
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -080071 if (connection) {
72 // FIXME
73 // add a uevent that can "load" a connection type
74 return 0;
75 }
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +080076
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -050077 if (bundle) {
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -080078 // FIXME
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -050079 // add a uevent that can "load" a bundle type
Greg Kroah-Hartman0ac5a832014-11-15 12:12:16 -080080 // This is what we need to bind a driver to so use the info
81 // in gmod here as well
82 return 0;
83 }
84
85 // FIXME
86 // "just" a module, be vague here, nothing binds to a module except
87 // the greybus core, so there's not much, if anything, we need to
88 // advertise.
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080089 return 0;
90}
91
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +080092struct bus_type greybus_bus_type = {
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080093 .name = "greybus",
Alex Elder778c69c2014-09-22 19:19:03 -050094 .match = greybus_module_match,
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +080095 .uevent = greybus_uevent,
96};
97
98static int greybus_probe(struct device *dev)
99{
100 struct greybus_driver *driver = to_greybus_driver(dev->driver);
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530101 struct gb_bundle *bundle = to_gb_bundle(dev);
102 const struct greybus_bundle_id *id;
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800103 int retval;
104
105 /* match id */
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530106 id = gb_bundle_match_id(bundle, driver->id_table);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800107 if (!id)
108 return -ENODEV;
109
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530110 retval = driver->probe(bundle, id);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800111 if (retval)
112 return retval;
113
114 return 0;
115}
116
117static int greybus_remove(struct device *dev)
118{
119 struct greybus_driver *driver = to_greybus_driver(dev->driver);
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530120 struct gb_bundle *bundle = to_gb_bundle(dev);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800121
Viresh Kumar9f5f30e7122015-04-01 20:32:04 +0530122 driver->disconnect(bundle);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800123 return 0;
124}
125
126int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
127 const char *mod_name)
128{
129 int retval;
130
131 if (greybus_disabled())
132 return -ENODEV;
133
134 driver->driver.name = driver->name;
135 driver->driver.probe = greybus_probe;
136 driver->driver.remove = greybus_remove;
137 driver->driver.owner = owner;
138 driver->driver.mod_name = mod_name;
139
140 retval = driver_register(&driver->driver);
141 if (retval)
142 return retval;
143
144 pr_info("registered new driver %s\n", driver->name);
145 return 0;
146}
147EXPORT_SYMBOL_GPL(greybus_register_driver);
148
Alex Elderfd1c2e52015-06-08 12:05:13 -0500149void greybus_deregister_driver(struct greybus_driver *driver)
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800150{
151 driver_unregister(&driver->driver);
152}
Alex Elderfd1c2e52015-06-08 12:05:13 -0500153EXPORT_SYMBOL_GPL(greybus_deregister_driver);
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800154
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700155
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700156static DEFINE_MUTEX(hd_mutex);
157
158static void free_hd(struct kref *kref)
159{
160 struct greybus_host_device *hd;
161
162 hd = container_of(kref, struct greybus_host_device, kref);
163
Greg Kroah-Hartmanfe166c62015-07-27 14:23:44 -0700164 ida_destroy(&hd->cport_id_map);
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700165 kfree(hd);
Alex Eldera06df4b2014-10-16 06:35:26 -0500166 mutex_unlock(&hd_mutex);
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700167}
168
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700169struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *driver,
Johan Hovoldd9336672015-05-19 11:22:43 +0200170 struct device *parent,
Fabien Parent144670c2015-09-02 15:50:35 +0200171 size_t buffer_size_max,
172 size_t num_cports)
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700173{
174 struct greybus_host_device *hd;
175
Greg Kroah-Hartman724b6192014-10-27 13:32:27 +0800176 /*
177 * Validate that the driver implements all of the callbacks
178 * so that we don't have to every time we make them.
179 */
Greg Kroah-Hartman260998e2015-07-24 17:19:21 -0700180 if ((!driver->message_send) || (!driver->message_cancel)) {
Greg Kroah-Hartman724b6192014-10-27 13:32:27 +0800181 pr_err("Must implement all greybus_host_driver callbacks!\n");
Alex Elder8ea70fe2015-05-22 09:52:45 -0500182 return ERR_PTR(-EINVAL);
Greg Kroah-Hartman724b6192014-10-27 13:32:27 +0800183 }
184
Johan Hovold8e929a82015-05-19 11:22:45 +0200185 if (buffer_size_max < GB_OPERATION_MESSAGE_SIZE_MIN) {
186 dev_err(parent, "greybus host-device buffers too small\n");
Johan Hovold1a58a3b2015-09-02 17:37:38 +0200187 return ERR_PTR(-EINVAL);
Johan Hovold8e929a82015-05-19 11:22:45 +0200188 }
189
Fabien Parent144670c2015-09-02 15:50:35 +0200190 if (num_cports == 0 || num_cports > CPORT_ID_MAX) {
191 dev_err(parent, "Invalid number of CPorts: %zu\n", num_cports);
192 return ERR_PTR(-EINVAL);
193 }
194
Johan Hovoldd9336672015-05-19 11:22:43 +0200195 /*
196 * Make sure to never allocate messages larger than what the Greybus
197 * protocol supports.
198 */
199 if (buffer_size_max > GB_OPERATION_MESSAGE_SIZE_MAX) {
200 dev_warn(parent, "limiting buffer size to %u\n",
201 GB_OPERATION_MESSAGE_SIZE_MAX);
202 buffer_size_max = GB_OPERATION_MESSAGE_SIZE_MAX;
203 }
204
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700205 hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
206 if (!hd)
Alex Elder8ea70fe2015-05-22 09:52:45 -0500207 return ERR_PTR(-ENOMEM);
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700208
209 kref_init(&hd->kref);
Greg Kroah-Hartman772149b2014-09-14 12:27:28 -0700210 hd->parent = parent;
211 hd->driver = driver;
Greg Kroah-Hartman1cd56a82014-12-19 14:56:36 -0800212 INIT_LIST_HEAD(&hd->interfaces);
Alex Elder2c43ce42014-11-17 08:08:44 -0600213 INIT_LIST_HEAD(&hd->connections);
Greg Kroah-Hartmanfd7b4352015-06-16 19:43:05 -0700214 ida_init(&hd->cport_id_map);
Johan Hovoldd9336672015-05-19 11:22:43 +0200215 hd->buffer_size_max = buffer_size_max;
Fabien Parent144670c2015-09-02 15:50:35 +0200216 hd->num_cports = num_cports;
Alex Elder1bb3c722014-10-02 12:30:03 -0500217
Viresh Kumare75b82b2015-07-28 07:28:42 +0530218 /*
219 * Initialize AP's SVC protocol connection:
220 *
221 * This is required as part of early initialization of the host device
222 * as we need this connection in order to start any kind of message
223 * exchange between the AP and the SVC. SVC will start with a
224 * 'get-version' request followed by a 'svc-hello' message and at that
225 * time we will create a fully initialized svc-connection, as we need
226 * endo-id and AP's interface id for that.
227 */
228 if (!gb_ap_svc_connection_create(hd)) {
229 kref_put_mutex(&hd->kref, free_hd, &hd_mutex);
230 return ERR_PTR(-ENOMEM);
231 }
232
Greg Kroah-Hartmana39879f2014-09-06 16:57:36 -0700233 return hd;
234}
235EXPORT_SYMBOL_GPL(greybus_create_hd);
236
Alex Eldereb765e42015-05-22 12:56:49 -0500237int greybus_endo_setup(struct greybus_host_device *hd, u16 endo_id,
238 u8 ap_intf_id)
239{
240 struct gb_endo *endo;
241
242 endo = gb_endo_create(hd, endo_id, ap_intf_id);
243 if (IS_ERR(endo))
244 return PTR_ERR(endo);
245 hd->endo = endo;
246
247 return 0;
248}
249EXPORT_SYMBOL_GPL(greybus_endo_setup);
250
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700251void greybus_remove_hd(struct greybus_host_device *hd)
252{
Greg Kroah-Hartmana4d91502015-04-07 20:27:15 +0200253 /*
254 * Tear down all interfaces, modules, and the endo that is associated
255 * with this host controller before freeing the memory associated with
256 * the host controller.
257 */
Viresh Kumar51b5d8d2015-05-20 17:33:51 +0530258 gb_interfaces_remove(hd);
Greg Kroah-Hartmana4d91502015-04-07 20:27:15 +0200259 gb_endo_remove(hd->endo);
Viresh Kumare99f3052015-07-09 10:56:30 +0530260
Viresh Kumardcd05002015-07-24 15:32:20 +0530261 /* Is the SVC still using the partially uninitialized connection ? */
Viresh Kumarfda23812015-08-31 17:21:13 +0530262 if (hd->initial_svc_connection)
Viresh Kumardcd05002015-07-24 15:32:20 +0530263 gb_connection_destroy(hd->initial_svc_connection);
Viresh Kumardcd05002015-07-24 15:32:20 +0530264
Viresh Kumare99f3052015-07-09 10:56:30 +0530265 /*
266 * Make sure there are no leftovers that can potentially corrupt sysfs.
267 */
268 if (WARN_ON(!list_empty(&hd->connections)))
269 gb_hd_connections_exit(hd);
270
Greg Kroah-Hartman68f1fc42014-09-07 13:12:11 -0700271 kref_put_mutex(&hd->kref, free_hd, &hd_mutex);
272}
273EXPORT_SYMBOL_GPL(greybus_remove_hd);
274
Greg Kroah-Hartman503c1cd2014-08-30 16:21:03 -0700275static int __init gb_init(void)
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700276{
Greg Kroah-Hartmandb6e1fd2014-08-30 16:47:26 -0700277 int retval;
278
Viresh Kumar337b0682015-03-20 20:29:13 +0530279 if (greybus_disabled())
280 return -ENODEV;
281
Alex Elderfb690ca2015-06-13 11:02:09 -0500282 BUILD_BUG_ON(CPORT_ID_MAX >= (long)CPORT_ID_BAD);
Alex Elder1bb3c722014-10-02 12:30:03 -0500283
Greg Kroah-Hartman48f70472015-03-27 11:38:06 +0100284 gb_debugfs_init();
Greg Kroah-Hartmandb6e1fd2014-08-30 16:47:26 -0700285
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700286 retval = bus_register(&greybus_bus_type);
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700287 if (retval) {
Alex Elderf35ab902015-06-09 17:42:51 -0500288 pr_err("bus_register failed (%d)\n", retval);
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700289 goto error_bus;
Greg Kroah-Hartman168db1c2014-09-13 16:15:52 -0700290 }
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700291
Alex Elder2eb585f2014-10-16 06:35:34 -0500292 retval = gb_operation_init();
293 if (retval) {
Alex Elderf35ab902015-06-09 17:42:51 -0500294 pr_err("gb_operation_init failed (%d)\n", retval);
Alex Elder2eb585f2014-10-16 06:35:34 -0500295 goto error_operation;
296 }
297
Alex Elderf35ab902015-06-09 17:42:51 -0500298 retval = gb_endo_init();
299 if (retval) {
300 pr_err("gb_endo_init failed (%d)\n", retval);
301 goto error_endo;
302 }
303
Viresh Kumarcdee4f72015-06-22 16:42:26 +0530304 retval = gb_control_protocol_init();
305 if (retval) {
306 pr_err("gb_control_protocol_init failed\n");
307 goto error_control;
308 }
309
Viresh Kumarab69c4c2015-07-03 17:00:29 +0530310 retval = gb_svc_protocol_init();
311 if (retval) {
312 pr_err("gb_svc_protocol_init failed\n");
313 goto error_svc;
314 }
315
Viresh Kumar90f1b612015-08-12 09:19:33 +0530316 retval = gb_firmware_protocol_init();
317 if (retval) {
318 pr_err("gb_firmware_protocol_init failed\n");
319 goto error_firmware;
320 }
321
Alex Elder19d03de2014-11-05 16:12:53 -0600322 return 0; /* Success */
323
Viresh Kumar90f1b612015-08-12 09:19:33 +0530324error_firmware:
325 gb_svc_protocol_exit();
Viresh Kumarab69c4c2015-07-03 17:00:29 +0530326error_svc:
327 gb_control_protocol_exit();
Viresh Kumarcdee4f72015-06-22 16:42:26 +0530328error_control:
329 gb_endo_exit();
Alex Elderf35ab902015-06-09 17:42:51 -0500330error_endo:
331 gb_operation_exit();
Alex Elder2eb585f2014-10-16 06:35:34 -0500332error_operation:
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700333 bus_unregister(&greybus_bus_type);
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700334error_bus:
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700335 gb_debugfs_cleanup();
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700336
337 return retval;
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700338}
Viresh Kumard71aaf22015-03-19 17:02:49 +0530339module_init(gb_init);
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700340
Greg Kroah-Hartman503c1cd2014-08-30 16:21:03 -0700341static void __exit gb_exit(void)
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700342{
Viresh Kumar90f1b612015-08-12 09:19:33 +0530343 gb_firmware_protocol_exit();
Viresh Kumarab69c4c2015-07-03 17:00:29 +0530344 gb_svc_protocol_exit();
Viresh Kumarcdee4f72015-06-22 16:42:26 +0530345 gb_control_protocol_exit();
Alex Elderf35ab902015-06-09 17:42:51 -0500346 gb_endo_exit();
Alex Elder2eb585f2014-10-16 06:35:34 -0500347 gb_operation_exit();
Greg Kroah-Hartman27fb8312014-08-31 13:54:59 -0700348 bus_unregister(&greybus_bus_type);
Greg Kroah-Hartmande536e32014-08-31 16:17:04 -0700349 gb_debugfs_cleanup();
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700350}
Greg Kroah-Hartman199d68d2014-08-30 16:20:22 -0700351module_exit(gb_exit);
Greg Kroah-Hartman6cf42a42015-04-13 19:51:33 +0200352MODULE_LICENSE("GPL v2");
Greg Kroah-Hartmanc8a797a2014-08-11 15:30:45 +0800353MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");