greybus: pm: add error handling to bundle activation
[linux-2.6/btrfs-unstable.git] / drivers / staging / greybus / core.c
blob1049e9c0edb07e0922de238bd26c6c11cfa2d757
1 /*
2 * Greybus "Core"
4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
8 */
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #define CREATE_TRACE_POINTS
13 #include "greybus.h"
14 #include "greybus_trace.h"
16 #define GB_BUNDLE_AUTOSUSPEND_MS 3000
18 /* Allow greybus to be disabled at boot if needed */
19 static bool nogreybus;
20 #ifdef MODULE
21 module_param(nogreybus, bool, 0444);
22 #else
23 core_param(nogreybus, nogreybus, bool, 0444);
24 #endif
25 int greybus_disabled(void)
27 return nogreybus;
29 EXPORT_SYMBOL_GPL(greybus_disabled);
31 static bool greybus_match_one_id(struct gb_bundle *bundle,
32 const struct greybus_bundle_id *id)
34 if ((id->match_flags & GREYBUS_ID_MATCH_VENDOR) &&
35 (id->vendor != bundle->intf->vendor_id))
36 return false;
38 if ((id->match_flags & GREYBUS_ID_MATCH_PRODUCT) &&
39 (id->product != bundle->intf->product_id))
40 return false;
42 if ((id->match_flags & GREYBUS_ID_MATCH_CLASS) &&
43 (id->class != bundle->class))
44 return false;
46 return true;
49 static const struct greybus_bundle_id *
50 greybus_match_id(struct gb_bundle *bundle, const struct greybus_bundle_id *id)
52 if (id == NULL)
53 return NULL;
55 for (; id->vendor || id->product || id->class || id->driver_info;
56 id++) {
57 if (greybus_match_one_id(bundle, id))
58 return id;
61 return NULL;
64 static int greybus_match_device(struct device *dev, struct device_driver *drv)
66 struct greybus_driver *driver = to_greybus_driver(drv);
67 struct gb_bundle *bundle;
68 const struct greybus_bundle_id *id;
70 if (!is_gb_bundle(dev))
71 return 0;
73 bundle = to_gb_bundle(dev);
75 id = greybus_match_id(bundle, driver->id_table);
76 if (id)
77 return 1;
78 /* FIXME - Dynamic ids? */
79 return 0;
82 static int greybus_uevent(struct device *dev, struct kobj_uevent_env *env)
84 struct gb_host_device *hd;
85 struct gb_module *module = NULL;
86 struct gb_interface *intf = NULL;
87 struct gb_control *control = NULL;
88 struct gb_bundle *bundle = NULL;
89 struct gb_svc *svc = NULL;
91 if (is_gb_host_device(dev)) {
92 hd = to_gb_host_device(dev);
93 } else if (is_gb_module(dev)) {
94 module = to_gb_module(dev);
95 hd = module->hd;
96 } else if (is_gb_interface(dev)) {
97 intf = to_gb_interface(dev);
98 module = intf->module;
99 hd = intf->hd;
100 } else if (is_gb_control(dev)) {
101 control = to_gb_control(dev);
102 intf = control->intf;
103 module = intf->module;
104 hd = intf->hd;
105 } else if (is_gb_bundle(dev)) {
106 bundle = to_gb_bundle(dev);
107 intf = bundle->intf;
108 module = intf->module;
109 hd = intf->hd;
110 } else if (is_gb_svc(dev)) {
111 svc = to_gb_svc(dev);
112 hd = svc->hd;
113 } else {
114 dev_WARN(dev, "uevent for unknown greybus device \"type\"!\n");
115 return -EINVAL;
118 if (add_uevent_var(env, "BUS=%u", hd->bus_id))
119 return -ENOMEM;
121 if (module) {
122 if (add_uevent_var(env, "MODULE=%u", module->module_id))
123 return -ENOMEM;
126 if (intf) {
127 if (add_uevent_var(env, "INTERFACE=%u", intf->interface_id))
128 return -ENOMEM;
129 if (add_uevent_var(env, "GREYBUS_ID=%08x/%08x",
130 intf->vendor_id, intf->product_id))
131 return -ENOMEM;
134 if (bundle) {
135 // FIXME
136 // add a uevent that can "load" a bundle type
137 // This is what we need to bind a driver to so use the info
138 // in gmod here as well
140 if (add_uevent_var(env, "BUNDLE=%u", bundle->id))
141 return -ENOMEM;
142 if (add_uevent_var(env, "BUNDLE_CLASS=%02x", bundle->class))
143 return -ENOMEM;
146 return 0;
149 static void greybus_shutdown(struct device *dev)
151 if (is_gb_host_device(dev)) {
152 struct gb_host_device *hd;
154 hd = to_gb_host_device(dev);
155 gb_hd_shutdown(hd);
159 struct bus_type greybus_bus_type = {
160 .name = "greybus",
161 .match = greybus_match_device,
162 .uevent = greybus_uevent,
163 .shutdown = greybus_shutdown,
166 static int greybus_probe(struct device *dev)
168 struct greybus_driver *driver = to_greybus_driver(dev->driver);
169 struct gb_bundle *bundle = to_gb_bundle(dev);
170 const struct greybus_bundle_id *id;
171 int retval;
173 /* match id */
174 id = greybus_match_id(bundle, driver->id_table);
175 if (!id)
176 return -ENODEV;
178 retval = pm_runtime_get_sync(&bundle->intf->dev);
179 if (retval < 0) {
180 pm_runtime_put_noidle(&bundle->intf->dev);
181 return retval;
184 retval = gb_control_bundle_activate(bundle->intf->control, bundle->id);
185 if (retval) {
186 pm_runtime_put(&bundle->intf->dev);
187 return retval;
191 * Unbound bundle devices are always deactivated. During probe, the
192 * Runtime PM is set to enabled and active and the usage count is
193 * incremented. If the driver supports runtime PM, it should call
194 * pm_runtime_put() in its probe routine and pm_runtime_get_sync()
195 * in remove routine.
197 pm_runtime_set_autosuspend_delay(dev, GB_BUNDLE_AUTOSUSPEND_MS);
198 pm_runtime_use_autosuspend(dev);
199 pm_runtime_get_noresume(dev);
200 pm_runtime_set_active(dev);
201 pm_runtime_enable(dev);
203 retval = driver->probe(bundle, id);
204 if (retval) {
206 * Catch buggy drivers that fail to destroy their connections.
208 WARN_ON(!list_empty(&bundle->connections));
210 gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
212 pm_runtime_disable(dev);
213 pm_runtime_set_suspended(dev);
214 pm_runtime_put_noidle(dev);
215 pm_runtime_dont_use_autosuspend(dev);
216 pm_runtime_put(&bundle->intf->dev);
218 return retval;
221 gb_timesync_schedule_synchronous(bundle->intf);
223 pm_runtime_put(&bundle->intf->dev);
225 return 0;
228 static int greybus_remove(struct device *dev)
230 struct greybus_driver *driver = to_greybus_driver(dev->driver);
231 struct gb_bundle *bundle = to_gb_bundle(dev);
232 struct gb_connection *connection;
233 int retval;
235 retval = pm_runtime_get_sync(dev);
236 if (retval < 0)
237 dev_err(dev, "failed to resume bundle: %d\n", retval);
240 * Disable (non-offloaded) connections early in case the interface is
241 * already gone to avoid unceccessary operation timeouts during
242 * driver disconnect. Otherwise, only disable incoming requests.
244 list_for_each_entry(connection, &bundle->connections, bundle_links) {
245 if (gb_connection_is_offloaded(connection))
246 continue;
248 if (bundle->intf->disconnected)
249 gb_connection_disable_forced(connection);
250 else
251 gb_connection_disable_rx(connection);
254 driver->disconnect(bundle);
256 /* Catch buggy drivers that fail to destroy their connections. */
257 WARN_ON(!list_empty(&bundle->connections));
259 if (!bundle->intf->disconnected)
260 gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
262 pm_runtime_put_noidle(dev);
263 pm_runtime_disable(dev);
264 pm_runtime_set_suspended(dev);
265 pm_runtime_dont_use_autosuspend(dev);
266 pm_runtime_put_noidle(dev);
268 return 0;
271 int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
272 const char *mod_name)
274 int retval;
276 if (greybus_disabled())
277 return -ENODEV;
279 driver->driver.bus = &greybus_bus_type;
280 driver->driver.name = driver->name;
281 driver->driver.probe = greybus_probe;
282 driver->driver.remove = greybus_remove;
283 driver->driver.owner = owner;
284 driver->driver.mod_name = mod_name;
286 retval = driver_register(&driver->driver);
287 if (retval)
288 return retval;
290 pr_info("registered new driver %s\n", driver->name);
291 return 0;
293 EXPORT_SYMBOL_GPL(greybus_register_driver);
295 void greybus_deregister_driver(struct greybus_driver *driver)
297 driver_unregister(&driver->driver);
299 EXPORT_SYMBOL_GPL(greybus_deregister_driver);
301 static int __init gb_init(void)
303 int retval;
305 if (greybus_disabled())
306 return -ENODEV;
308 BUILD_BUG_ON(CPORT_ID_MAX >= (long)CPORT_ID_BAD);
310 gb_debugfs_init();
312 retval = bus_register(&greybus_bus_type);
313 if (retval) {
314 pr_err("bus_register failed (%d)\n", retval);
315 goto error_bus;
318 retval = gb_hd_init();
319 if (retval) {
320 pr_err("gb_hd_init failed (%d)\n", retval);
321 goto error_hd;
324 retval = gb_operation_init();
325 if (retval) {
326 pr_err("gb_operation_init failed (%d)\n", retval);
327 goto error_operation;
330 retval = gb_timesync_init();
331 if (retval) {
332 pr_err("gb_timesync_init failed\n");
333 goto error_timesync;
335 return 0; /* Success */
337 error_timesync:
338 gb_operation_exit();
339 error_operation:
340 gb_hd_exit();
341 error_hd:
342 bus_unregister(&greybus_bus_type);
343 error_bus:
344 gb_debugfs_cleanup();
346 return retval;
348 module_init(gb_init);
350 static void __exit gb_exit(void)
352 gb_timesync_exit();
353 gb_operation_exit();
354 gb_hd_exit();
355 bus_unregister(&greybus_bus_type);
356 gb_debugfs_cleanup();
357 tracepoint_synchronize_unregister();
359 module_exit(gb_exit);
360 MODULE_LICENSE("GPL v2");
361 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");