]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/core.c
greybus: move receive handling to operation layer
[karo-tx-linux.git] / drivers / staging / greybus / core.c
1 /*
2  * Greybus "Core"
3  *
4  * Copyright 2014 Google Inc.
5  *
6  * Released under the GPLv2 only.
7  */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/types.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
17
18 #include "greybus.h"
19
20 /* Allow greybus to be disabled at boot if needed */
21 static bool nogreybus;
22 #ifdef MODULE
23 module_param(nogreybus, bool, 0444);
24 #else
25 core_param(nogreybus, bool, 0444);
26 #endif
27 int greybus_disabled(void)
28 {
29         return nogreybus;
30 }
31 EXPORT_SYMBOL_GPL(greybus_disabled);
32
33 static int greybus_module_match(struct device *dev, struct device_driver *drv)
34 {
35         struct greybus_driver *driver = to_greybus_driver(dev->driver);
36         struct gb_module *gmod = to_gb_module(dev);
37         const struct greybus_module_id *id;
38
39         id = gb_module_match_id(gmod, driver->id_table);
40         if (id)
41                 return 1;
42         /* FIXME - Dyanmic ids? */
43         return 0;
44 }
45
46 static int greybus_uevent(struct device *dev, struct kobj_uevent_env *env)
47 {
48         /* struct gb_module *gmod = to_gb_module(dev); */
49
50         /* FIXME - add some uevents here... */
51         return 0;
52 }
53
54 static struct bus_type greybus_bus_type = {
55         .name =         "greybus",
56         .match =        greybus_module_match,
57         .uevent =       greybus_uevent,
58 };
59
60 static int greybus_probe(struct device *dev)
61 {
62         struct greybus_driver *driver = to_greybus_driver(dev->driver);
63         struct gb_module *gmod = to_gb_module(dev);
64         const struct greybus_module_id *id;
65         int retval;
66
67         /* match id */
68         id = gb_module_match_id(gmod, driver->id_table);
69         if (!id)
70                 return -ENODEV;
71
72         retval = driver->probe(gmod, id);
73         if (retval)
74                 return retval;
75
76         return 0;
77 }
78
79 static int greybus_remove(struct device *dev)
80 {
81         struct greybus_driver *driver = to_greybus_driver(dev->driver);
82         struct gb_module *gmod = to_gb_module(dev);
83
84         driver->disconnect(gmod);
85         return 0;
86 }
87
88 int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
89                 const char *mod_name)
90 {
91         int retval;
92
93         if (greybus_disabled())
94                 return -ENODEV;
95
96         driver->driver.name = driver->name;
97         driver->driver.probe = greybus_probe;
98         driver->driver.remove = greybus_remove;
99         driver->driver.owner = owner;
100         driver->driver.mod_name = mod_name;
101
102         retval = driver_register(&driver->driver);
103         if (retval)
104                 return retval;
105
106         pr_info("registered new driver %s\n", driver->name);
107         return 0;
108 }
109 EXPORT_SYMBOL_GPL(greybus_register_driver);
110
111 void greybus_deregister(struct greybus_driver *driver)
112 {
113         driver_unregister(&driver->driver);
114 }
115 EXPORT_SYMBOL_GPL(greybus_deregister);
116
117
118 static void greybus_module_release(struct device *dev)
119 {
120         struct gb_module *gmod = to_gb_module(dev);
121         int i;
122
123         for (i = 0; i < gmod->num_strings; ++i)
124                 kfree(gmod->string[i]);
125         kfree(gmod);
126 }
127
128
129 static struct device_type greybus_module_type = {
130         .name =         "greybus_module",
131         .release =      greybus_module_release,
132 };
133
134 static const struct greybus_module_id fake_greybus_module_id = {
135         GREYBUS_DEVICE(0x42, 0x42)
136 };
137
138
139 /**
140  * gb_add_module
141  *
142  * Pass in a buffer that _should_ contain a Greybus module manifest
143  * and register a greybus device structure with the kernel core.
144  */
145 void gb_add_module(struct greybus_host_device *hd, u8 module_id,
146                    u8 *data, int size)
147 {
148         struct gb_module *gmod;
149         int retval;
150
151         gmod = gb_module_create(hd, module_id);
152         if (!gmod) {
153                 dev_err(hd->parent, "failed to create module\n");
154                 return;
155         }
156
157         /*
158          * Parse the manifest and build up our data structures
159          * representing what's in it.
160          */
161         if (!gb_manifest_parse(gmod, data, size)) {
162                 dev_err(hd->parent, "manifest error\n");
163                 goto error;
164         }
165
166         /*
167          * XXX
168          * We've successfully parsed the manifest.  Now we need to
169          * allocate CPort Id's for connecting to the CPorts found on
170          * other modules.  For each of these, establish a connection
171          * between the local and remote CPorts (including
172          * configuring the switch to allow them to communicate).
173          */
174
175         gmod->dev.parent = hd->parent;
176         gmod->dev.driver = NULL;
177         gmod->dev.bus = &greybus_bus_type;
178         gmod->dev.type = &greybus_module_type;
179         gmod->dev.groups = greybus_module_groups;
180         gmod->dev.dma_mask = hd->parent->dma_mask;
181         device_initialize(&gmod->dev);
182         dev_set_name(&gmod->dev, "%d", module_id);
183
184         retval = device_add(&gmod->dev);
185         if (!retval)
186                 return;         /* Success */
187 error:
188         gb_module_destroy(gmod);
189
190         put_device(&gmod->dev);
191         greybus_module_release(&gmod->dev);
192 }
193
194 void gb_remove_module(struct greybus_host_device *hd, u8 module_id)
195 {
196         struct gb_module *gmod;
197         bool found = false;
198
199         list_for_each_entry(gmod, &hd->modules, links)
200                 if (gmod->module_id == module_id) {
201                         found = true;
202                         break;
203                 }
204
205         if (found)
206                 greybus_remove_device(gmod);
207         else
208                 dev_err(hd->parent, "module id %d remove error\n", module_id);
209 }
210
211 void greybus_remove_device(struct gb_module *gmod)
212 {
213         device_del(&gmod->dev);
214         put_device(&gmod->dev);
215 }
216
217 static DEFINE_MUTEX(hd_mutex);
218
219 static void free_hd(struct kref *kref)
220 {
221         struct greybus_host_device *hd;
222
223         hd = container_of(kref, struct greybus_host_device, kref);
224
225         kfree(hd);
226         mutex_unlock(&hd_mutex);
227 }
228
229 struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *driver,
230                                               struct device *parent)
231 {
232         struct greybus_host_device *hd;
233
234         hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
235         if (!hd)
236                 return NULL;
237
238         kref_init(&hd->kref);
239         hd->parent = parent;
240         hd->driver = driver;
241         INIT_LIST_HEAD(&hd->modules);
242         hd->connections = RB_ROOT;
243         ida_init(&hd->cport_id_map);
244         spin_lock_init(&hd->cport_id_map_lock);
245
246         return hd;
247 }
248 EXPORT_SYMBOL_GPL(greybus_create_hd);
249
250 void greybus_remove_hd(struct greybus_host_device *hd)
251 {
252         kref_put_mutex(&hd->kref, free_hd, &hd_mutex);
253 }
254 EXPORT_SYMBOL_GPL(greybus_remove_hd);
255
256
257 static int __init gb_init(void)
258 {
259         int retval;
260
261         BUILD_BUG_ON(HOST_DEV_CPORT_ID_MAX >= (long)CPORT_ID_BAD);
262
263         retval = gb_debugfs_init();
264         if (retval) {
265                 pr_err("debugfs failed\n");
266                 return retval;
267         }
268
269         retval = bus_register(&greybus_bus_type);
270         if (retval) {
271                 pr_err("bus_register failed\n");
272                 goto error_bus;
273         }
274
275         retval = gb_ap_init();
276         if (retval) {
277                 pr_err("gb_ap_init failed\n");
278                 goto error_ap;
279         }
280
281         retval = gb_gbuf_init();
282         if (retval) {
283                 pr_err("gb_gbuf_init failed\n");
284                 goto error_gbuf;
285         }
286
287         retval = gb_operation_init();
288         if (retval) {
289                 pr_err("gb_operation_init failed\n");
290                 goto error_operation;
291         }
292
293         retval = gb_tty_init();
294         if (retval) {
295                 pr_err("gb_tty_init failed\n");
296                 goto error_tty;
297         }
298
299         return 0;
300
301 error_tty:
302         gb_operation_exit();
303
304 error_operation:
305         gb_gbuf_exit();
306
307 error_gbuf:
308         gb_ap_exit();
309
310 error_ap:
311         bus_unregister(&greybus_bus_type);
312
313 error_bus:
314         gb_debugfs_cleanup();
315
316         return retval;
317 }
318
319 static void __exit gb_exit(void)
320 {
321         gb_tty_exit();
322         gb_operation_exit();
323         gb_gbuf_exit();
324         gb_ap_exit();
325         bus_unregister(&greybus_bus_type);
326         gb_debugfs_cleanup();
327 }
328
329 module_init(gb_init);
330 module_exit(gb_exit);
331
332 MODULE_LICENSE("GPL");
333 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");