min/max: remove sparse warnings when they're nested
[cascardo/linux.git] / drivers / staging / greybus / core.c
1 /*
2  * Greybus "Core"
3  *
4  * Copyright 2014-2015 Google Inc.
5  * Copyright 2014-2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #define CREATE_TRACE_POINTS
13 #include "greybus.h"
14 #include "greybus_trace.h"
15
16 #define GB_BUNDLE_AUTOSUSPEND_MS        3000
17
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)
26 {
27         return nogreybus;
28 }
29 EXPORT_SYMBOL_GPL(greybus_disabled);
30
31 static bool greybus_match_one_id(struct gb_bundle *bundle,
32                                      const struct greybus_bundle_id *id)
33 {
34         if ((id->match_flags & GREYBUS_ID_MATCH_VENDOR) &&
35             (id->vendor != bundle->intf->vendor_id))
36                 return false;
37
38         if ((id->match_flags & GREYBUS_ID_MATCH_PRODUCT) &&
39             (id->product != bundle->intf->product_id))
40                 return false;
41
42         if ((id->match_flags & GREYBUS_ID_MATCH_CLASS) &&
43             (id->class != bundle->class))
44                 return false;
45
46         return true;
47 }
48
49 static const struct greybus_bundle_id *
50 greybus_match_id(struct gb_bundle *bundle, const struct greybus_bundle_id *id)
51 {
52         if (id == NULL)
53                 return NULL;
54
55         for (; id->vendor || id->product || id->class || id->driver_info;
56                                                                         id++) {
57                 if (greybus_match_one_id(bundle, id))
58                         return id;
59         }
60
61         return NULL;
62 }
63
64 static int greybus_match_device(struct device *dev, struct device_driver *drv)
65 {
66         struct greybus_driver *driver = to_greybus_driver(drv);
67         struct gb_bundle *bundle;
68         const struct greybus_bundle_id *id;
69
70         if (!is_gb_bundle(dev))
71                 return 0;
72
73         bundle = to_gb_bundle(dev);
74
75         id = greybus_match_id(bundle, driver->id_table);
76         if (id)
77                 return 1;
78         /* FIXME - Dynamic ids? */
79         return 0;
80 }
81
82 static int greybus_uevent(struct device *dev, struct kobj_uevent_env *env)
83 {
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;
90
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;
116         }
117
118         if (add_uevent_var(env, "BUS=%u", hd->bus_id))
119                 return -ENOMEM;
120
121         if (module) {
122                 if (add_uevent_var(env, "MODULE=%u", module->module_id))
123                         return -ENOMEM;
124         }
125
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;
132         }
133
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
139
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;
144         }
145
146         return 0;
147 }
148
149 static void greybus_shutdown(struct device *dev)
150 {
151         if (is_gb_host_device(dev)) {
152                 struct gb_host_device *hd;
153
154                 hd = to_gb_host_device(dev);
155                 gb_hd_shutdown(hd);
156         }
157 }
158
159 struct bus_type greybus_bus_type = {
160         .name =         "greybus",
161         .match =        greybus_match_device,
162         .uevent =       greybus_uevent,
163         .shutdown =     greybus_shutdown,
164 };
165
166 static int greybus_probe(struct device *dev)
167 {
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;
172
173         /* match id */
174         id = greybus_match_id(bundle, driver->id_table);
175         if (!id)
176                 return -ENODEV;
177
178         retval = pm_runtime_get_sync(&bundle->intf->dev);
179         if (retval < 0) {
180                 pm_runtime_put_noidle(&bundle->intf->dev);
181                 return retval;
182         }
183
184         retval = gb_control_bundle_activate(bundle->intf->control, bundle->id);
185         if (retval) {
186                 pm_runtime_put(&bundle->intf->dev);
187                 return retval;
188         }
189
190         /*
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.
196          */
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);
202
203         retval = driver->probe(bundle, id);
204         if (retval) {
205                 /*
206                  * Catch buggy drivers that fail to destroy their connections.
207                  */
208                 WARN_ON(!list_empty(&bundle->connections));
209
210                 gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
211
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);
217
218                 return retval;
219         }
220
221         gb_timesync_schedule_synchronous(bundle->intf);
222
223         pm_runtime_put(&bundle->intf->dev);
224
225         return 0;
226 }
227
228 static int greybus_remove(struct device *dev)
229 {
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;
234
235         retval = pm_runtime_get_sync(dev);
236         if (retval < 0)
237                 dev_err(dev, "failed to resume bundle: %d\n", retval);
238
239         /*
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.
243          */
244         list_for_each_entry(connection, &bundle->connections, bundle_links) {
245                 if (gb_connection_is_offloaded(connection))
246                         continue;
247
248                 if (bundle->intf->disconnected)
249                         gb_connection_disable_forced(connection);
250                 else
251                         gb_connection_disable_rx(connection);
252         }
253
254         driver->disconnect(bundle);
255
256         /* Catch buggy drivers that fail to destroy their connections. */
257         WARN_ON(!list_empty(&bundle->connections));
258
259         if (!bundle->intf->disconnected)
260                 gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
261
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);
267
268         return 0;
269 }
270
271 int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
272                 const char *mod_name)
273 {
274         int retval;
275
276         if (greybus_disabled())
277                 return -ENODEV;
278
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;
285
286         retval = driver_register(&driver->driver);
287         if (retval)
288                 return retval;
289
290         pr_info("registered new driver %s\n", driver->name);
291         return 0;
292 }
293 EXPORT_SYMBOL_GPL(greybus_register_driver);
294
295 void greybus_deregister_driver(struct greybus_driver *driver)
296 {
297         driver_unregister(&driver->driver);
298 }
299 EXPORT_SYMBOL_GPL(greybus_deregister_driver);
300
301 static int __init gb_init(void)
302 {
303         int retval;
304
305         if (greybus_disabled())
306                 return -ENODEV;
307
308         BUILD_BUG_ON(CPORT_ID_MAX >= (long)CPORT_ID_BAD);
309
310         gb_debugfs_init();
311
312         retval = bus_register(&greybus_bus_type);
313         if (retval) {
314                 pr_err("bus_register failed (%d)\n", retval);
315                 goto error_bus;
316         }
317
318         retval = gb_hd_init();
319         if (retval) {
320                 pr_err("gb_hd_init failed (%d)\n", retval);
321                 goto error_hd;
322         }
323
324         retval = gb_operation_init();
325         if (retval) {
326                 pr_err("gb_operation_init failed (%d)\n", retval);
327                 goto error_operation;
328         }
329
330         retval = gb_timesync_init();
331         if (retval) {
332                 pr_err("gb_timesync_init failed\n");
333                 goto error_timesync;
334         }
335         return 0;       /* Success */
336
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();
345
346         return retval;
347 }
348 module_init(gb_init);
349
350 static void __exit gb_exit(void)
351 {
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();
358 }
359 module_exit(gb_exit);
360 MODULE_LICENSE("GPL v2");
361 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");