Merge tag 'input-for-v6.3-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor...
[linux-block.git] / drivers / greybus / core.c
CommitLineData
eb50fd3a 1// SPDX-License-Identifier: GPL-2.0
c8a797a9
GKH
2/*
3 * Greybus "Core"
4 *
4441f475
AE
5 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
c8a797a9
GKH
7 */
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
5c8ad599 11#define CREATE_TRACE_POINTS
ec0ad868 12#include <linux/greybus.h>
5c8ad599 13#include "greybus_trace.h"
c8a797a9 14
61e13db9
DL
15#define GB_BUNDLE_AUTOSUSPEND_MS 3000
16
c8a797a9
GKH
17/* Allow greybus to be disabled at boot if needed */
18static bool nogreybus;
19#ifdef MODULE
20module_param(nogreybus, bool, 0444);
21#else
8597e6b2 22core_param(nogreybus, nogreybus, bool, 0444);
c8a797a9
GKH
23#endif
24int greybus_disabled(void)
25{
26 return nogreybus;
27}
28EXPORT_SYMBOL_GPL(greybus_disabled);
29
129a6fbe 30static bool greybus_match_one_id(struct gb_bundle *bundle,
f17f5429 31 const struct greybus_bundle_id *id)
700001af
JH
32{
33 if ((id->match_flags & GREYBUS_ID_MATCH_VENDOR) &&
9f59263a 34 (id->vendor != bundle->intf->vendor_id))
129a6fbe 35 return false;
700001af
JH
36
37 if ((id->match_flags & GREYBUS_ID_MATCH_PRODUCT) &&
9f59263a 38 (id->product != bundle->intf->product_id))
129a6fbe 39 return false;
700001af
JH
40
41 if ((id->match_flags & GREYBUS_ID_MATCH_CLASS) &&
42 (id->class != bundle->class))
129a6fbe 43 return false;
700001af 44
129a6fbe 45 return true;
700001af
JH
46}
47
48static const struct greybus_bundle_id *
49greybus_match_id(struct gb_bundle *bundle, const struct greybus_bundle_id *id)
50{
9b76530d 51 if (!id)
700001af
JH
52 return NULL;
53
54 for (; id->vendor || id->product || id->class || id->driver_info;
55 id++) {
56 if (greybus_match_one_id(bundle, id))
57 return id;
58 }
59
60 return NULL;
61}
62
e4e55360 63static int greybus_match_device(struct device *dev, struct device_driver *drv)
c8a797a9 64{
95bd99de 65 struct greybus_driver *driver = to_greybus_driver(drv);
b77f9328 66 struct gb_bundle *bundle;
9f5f30e7 67 const struct greybus_bundle_id *id;
c8a797a9 68
b77f9328
JH
69 if (!is_gb_bundle(dev))
70 return 0;
71
72 bundle = to_gb_bundle(dev);
73
700001af 74 id = greybus_match_id(bundle, driver->id_table);
c8a797a9
GKH
75 if (id)
76 return 1;
696e0cca 77 /* FIXME - Dynamic ids? */
c8a797a9
GKH
78 return 0;
79}
80
2a81ada3 81static int greybus_uevent(const struct device *dev, struct kobj_uevent_env *env)
c8a797a9 82{
2a81ada3
GKH
83 const struct gb_host_device *hd;
84 const struct gb_module *module = NULL;
85 const struct gb_interface *intf = NULL;
86 const struct gb_control *control = NULL;
87 const struct gb_bundle *bundle = NULL;
88 const struct gb_svc *svc = NULL;
0ac5a838 89
2adaefb1
JH
90 if (is_gb_host_device(dev)) {
91 hd = to_gb_host_device(dev);
b15d97d7
JH
92 } else if (is_gb_module(dev)) {
93 module = to_gb_module(dev);
94 hd = module->hd;
df671553 95 } else if (is_gb_interface(dev)) {
4ab9b3c2 96 intf = to_gb_interface(dev);
b15d97d7 97 module = intf->module;
f0960d05 98 hd = intf->hd;
a6e5b014
JH
99 } else if (is_gb_control(dev)) {
100 control = to_gb_control(dev);
101 intf = control->intf;
3e29bcf4 102 module = intf->module;
a6e5b014 103 hd = intf->hd;
1db0a5ff
GKH
104 } else if (is_gb_bundle(dev)) {
105 bundle = to_gb_bundle(dev);
4ab9b3c2 106 intf = bundle->intf;
b15d97d7 107 module = intf->module;
f0960d05 108 hd = intf->hd;
88f7b96d
JH
109 } else if (is_gb_svc(dev)) {
110 svc = to_gb_svc(dev);
f0960d05 111 hd = svc->hd;
0ac5a838
GKH
112 } else {
113 dev_WARN(dev, "uevent for unknown greybus device \"type\"!\n");
114 return -EINVAL;
115 }
116
f0960d05
JH
117 if (add_uevent_var(env, "BUS=%u", hd->bus_id))
118 return -ENOMEM;
119
b15d97d7
JH
120 if (module) {
121 if (add_uevent_var(env, "MODULE=%u", module->module_id))
122 return -ENOMEM;
123 }
124
c5e6b05c
JH
125 if (intf) {
126 if (add_uevent_var(env, "INTERFACE=%u", intf->interface_id))
127 return -ENOMEM;
611924dd
GKH
128 if (add_uevent_var(env, "GREYBUS_ID=%08x/%08x",
129 intf->vendor_id, intf->product_id))
de141314 130 return -ENOMEM;
c5e6b05c
JH
131 }
132
1db0a5ff 133 if (bundle) {
0ac5a838 134 // FIXME
1db0a5ff 135 // add a uevent that can "load" a bundle type
0ac5a838
GKH
136 // This is what we need to bind a driver to so use the info
137 // in gmod here as well
c29c016f
JH
138
139 if (add_uevent_var(env, "BUNDLE=%u", bundle->id))
140 return -ENOMEM;
6ce4cc27
GKH
141 if (add_uevent_var(env, "BUNDLE_CLASS=%02x", bundle->class))
142 return -ENOMEM;
0ac5a838
GKH
143 }
144
c8a797a9
GKH
145 return 0;
146}
147
1f77b363
DL
148static void greybus_shutdown(struct device *dev)
149{
150 if (is_gb_host_device(dev)) {
151 struct gb_host_device *hd;
152
153 hd = to_gb_host_device(dev);
154 gb_hd_shutdown(hd);
155 }
156}
157
f0f61b90 158struct bus_type greybus_bus_type = {
c8a797a9 159 .name = "greybus",
e4e55360 160 .match = greybus_match_device,
c8a797a9 161 .uevent = greybus_uevent,
1f77b363 162 .shutdown = greybus_shutdown,
c8a797a9
GKH
163};
164
165static int greybus_probe(struct device *dev)
166{
167 struct greybus_driver *driver = to_greybus_driver(dev->driver);
9f5f30e7
VK
168 struct gb_bundle *bundle = to_gb_bundle(dev);
169 const struct greybus_bundle_id *id;
c8a797a9
GKH
170 int retval;
171
172 /* match id */
700001af 173 id = greybus_match_id(bundle, driver->id_table);
c8a797a9
GKH
174 if (!id)
175 return -ENODEV;
176
61e13db9
DL
177 retval = pm_runtime_get_sync(&bundle->intf->dev);
178 if (retval < 0) {
179 pm_runtime_put_noidle(&bundle->intf->dev);
180 return retval;
181 }
182
f5c93dea
BG
183 retval = gb_control_bundle_activate(bundle->intf->control, bundle->id);
184 if (retval) {
185 pm_runtime_put(&bundle->intf->dev);
186 return retval;
187 }
2d48b5b4 188
61e13db9
DL
189 /*
190 * Unbound bundle devices are always deactivated. During probe, the
191 * Runtime PM is set to enabled and active and the usage count is
192 * incremented. If the driver supports runtime PM, it should call
193 * pm_runtime_put() in its probe routine and pm_runtime_get_sync()
194 * in remove routine.
195 */
196 pm_runtime_set_autosuspend_delay(dev, GB_BUNDLE_AUTOSUSPEND_MS);
197 pm_runtime_use_autosuspend(dev);
198 pm_runtime_get_noresume(dev);
199 pm_runtime_set_active(dev);
200 pm_runtime_enable(dev);
201
9f5f30e7 202 retval = driver->probe(bundle, id);
98fdf5a0
JH
203 if (retval) {
204 /*
205 * Catch buggy drivers that fail to destroy their connections.
206 */
207 WARN_ON(!list_empty(&bundle->connections));
208
2d48b5b4
DL
209 gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
210
61e13db9
DL
211 pm_runtime_disable(dev);
212 pm_runtime_set_suspended(dev);
213 pm_runtime_put_noidle(dev);
214 pm_runtime_dont_use_autosuspend(dev);
215 pm_runtime_put(&bundle->intf->dev);
216
c8a797a9 217 return retval;
98fdf5a0 218 }
c8a797a9 219
61e13db9
DL
220 pm_runtime_put(&bundle->intf->dev);
221
c8a797a9
GKH
222 return 0;
223}
224
225static int greybus_remove(struct device *dev)
226{
227 struct greybus_driver *driver = to_greybus_driver(dev->driver);
9f5f30e7 228 struct gb_bundle *bundle = to_gb_bundle(dev);
fa8369c1 229 struct gb_connection *connection;
61e13db9
DL
230 int retval;
231
232 retval = pm_runtime_get_sync(dev);
233 if (retval < 0)
234 dev_err(dev, "failed to resume bundle: %d\n", retval);
fa8369c1 235
82278bfe
JH
236 /*
237 * Disable (non-offloaded) connections early in case the interface is
238 * already gone to avoid unceccessary operation timeouts during
239 * driver disconnect. Otherwise, only disable incoming requests.
240 */
47a2e676 241 list_for_each_entry(connection, &bundle->connections, bundle_links) {
82278bfe
JH
242 if (gb_connection_is_offloaded(connection))
243 continue;
244
47a2e676 245 if (bundle->intf->disconnected)
7aefe791 246 gb_connection_disable_forced(connection);
47a2e676
JH
247 else
248 gb_connection_disable_rx(connection);
249 }
c8a797a9 250
9f5f30e7 251 driver->disconnect(bundle);
02a54dd1 252
98fdf5a0
JH
253 /* Catch buggy drivers that fail to destroy their connections. */
254 WARN_ON(!list_empty(&bundle->connections));
02a54dd1 255
2d48b5b4
DL
256 if (!bundle->intf->disconnected)
257 gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
258
61e13db9
DL
259 pm_runtime_put_noidle(dev);
260 pm_runtime_disable(dev);
261 pm_runtime_set_suspended(dev);
262 pm_runtime_dont_use_autosuspend(dev);
263 pm_runtime_put_noidle(dev);
264
c8a797a9
GKH
265 return 0;
266}
267
268int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
b7417e3c 269 const char *mod_name)
c8a797a9
GKH
270{
271 int retval;
272
273 if (greybus_disabled())
274 return -ENODEV;
275
3c48d1b8 276 driver->driver.bus = &greybus_bus_type;
c8a797a9
GKH
277 driver->driver.name = driver->name;
278 driver->driver.probe = greybus_probe;
279 driver->driver.remove = greybus_remove;
280 driver->driver.owner = owner;
281 driver->driver.mod_name = mod_name;
282
283 retval = driver_register(&driver->driver);
284 if (retval)
285 return retval;
286
287 pr_info("registered new driver %s\n", driver->name);
288 return 0;
289}
290EXPORT_SYMBOL_GPL(greybus_register_driver);
291
fd1c2e54 292void greybus_deregister_driver(struct greybus_driver *driver)
c8a797a9
GKH
293{
294 driver_unregister(&driver->driver);
295}
fd1c2e54 296EXPORT_SYMBOL_GPL(greybus_deregister_driver);
c8a797a9 297
503c1cdb 298static int __init gb_init(void)
199d68d4 299{
db6e1fd2
GKH
300 int retval;
301
337b0687
VK
302 if (greybus_disabled())
303 return -ENODEV;
304
fb690ca9 305 BUILD_BUG_ON(CPORT_ID_MAX >= (long)CPORT_ID_BAD);
1bb3c724 306
48f70474 307 gb_debugfs_init();
db6e1fd2 308
27fb8310 309 retval = bus_register(&greybus_bus_type);
168db1cd 310 if (retval) {
f35ab903 311 pr_err("bus_register failed (%d)\n", retval);
27fb8310 312 goto error_bus;
168db1cd 313 }
27fb8310 314
2adaefb1
JH
315 retval = gb_hd_init();
316 if (retval) {
317 pr_err("gb_hd_init failed (%d)\n", retval);
318 goto error_hd;
319 }
320
2eb585f8
AE
321 retval = gb_operation_init();
322 if (retval) {
f35ab903 323 pr_err("gb_operation_init failed (%d)\n", retval);
2eb585f8
AE
324 goto error_operation;
325 }
19d03dec
AE
326 return 0; /* Success */
327
2eb585f8 328error_operation:
2adaefb1
JH
329 gb_hd_exit();
330error_hd:
27fb8310 331 bus_unregister(&greybus_bus_type);
27fb8310 332error_bus:
de536e30 333 gb_debugfs_cleanup();
27fb8310
GKH
334
335 return retval;
199d68d4 336}
d71aaf28 337module_init(gb_init);
199d68d4 338
503c1cdb 339static void __exit gb_exit(void)
199d68d4 340{
2eb585f8 341 gb_operation_exit();
2adaefb1 342 gb_hd_exit();
27fb8310 343 bus_unregister(&greybus_bus_type);
de536e30 344 gb_debugfs_cleanup();
5c8ad599 345 tracepoint_synchronize_unregister();
199d68d4 346}
199d68d4 347module_exit(gb_exit);
6cf42a44 348MODULE_LICENSE("GPL v2");
c8a797a9 349MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");