4 * Copyright 2014 Google Inc.
6 * Released under the GPLv2 only.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
20 /* Allow greybus to be disabled at boot if needed */
21 static bool nogreybus
;
23 module_param(nogreybus
, bool, 0444);
25 core_param(nogreybus
, bool, 0444);
27 int greybus_disabled(void)
31 EXPORT_SYMBOL_GPL(greybus_disabled
);
33 static spinlock_t cport_id_map_lock
;
35 static int greybus_module_match(struct device
*dev
, struct device_driver
*drv
)
37 struct greybus_driver
*driver
= to_greybus_driver(dev
->driver
);
38 struct gb_module
*gmod
= to_gb_module(dev
);
39 const struct greybus_module_id
*id
;
41 id
= gb_module_match_id(gmod
, driver
->id_table
);
44 /* FIXME - Dyanmic ids? */
48 static int greybus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
50 /* struct gb_module *gmod = to_gb_module(dev); */
52 /* FIXME - add some uevents here... */
56 static struct bus_type greybus_bus_type
= {
58 .match
= greybus_module_match
,
59 .uevent
= greybus_uevent
,
62 static int greybus_probe(struct device
*dev
)
64 struct greybus_driver
*driver
= to_greybus_driver(dev
->driver
);
65 struct gb_module
*gmod
= to_gb_module(dev
);
66 const struct greybus_module_id
*id
;
70 id
= gb_module_match_id(gmod
, driver
->id_table
);
74 retval
= driver
->probe(gmod
, id
);
81 static int greybus_remove(struct device
*dev
)
83 struct greybus_driver
*driver
= to_greybus_driver(dev
->driver
);
84 struct gb_module
*gmod
= to_gb_module(dev
);
86 driver
->disconnect(gmod
);
90 int greybus_register_driver(struct greybus_driver
*driver
, struct module
*owner
,
95 if (greybus_disabled())
98 driver
->driver
.name
= driver
->name
;
99 driver
->driver
.probe
= greybus_probe
;
100 driver
->driver
.remove
= greybus_remove
;
101 driver
->driver
.owner
= owner
;
102 driver
->driver
.mod_name
= mod_name
;
104 retval
= driver_register(&driver
->driver
);
108 pr_info("registered new driver %s\n", driver
->name
);
111 EXPORT_SYMBOL_GPL(greybus_register_driver
);
113 void greybus_deregister(struct greybus_driver
*driver
)
115 driver_unregister(&driver
->driver
);
117 EXPORT_SYMBOL_GPL(greybus_deregister
);
120 static void greybus_module_release(struct device
*dev
)
122 struct gb_module
*gmod
= to_gb_module(dev
);
125 for (i
= 0; i
< gmod
->num_strings
; ++i
)
126 kfree(gmod
->string
[i
]);
131 static struct device_type greybus_module_type
= {
132 .name
= "greybus_module",
133 .release
= greybus_module_release
,
137 * This needs to be driven by the list of functions that the
138 * manifest says are present.
140 static int gb_init_subdevs(struct gb_module
*gmod
,
141 const struct greybus_module_id
*id
)
145 /* Allocate all of the different "sub device types" for this device */
148 * Decide what exactly we should get supplied for the i2c
149 * probe, and then work that back to what should be present
152 retval
= gb_i2c_probe(gmod
, id
);
156 retval
= gb_gpio_probe(gmod
, id
);
160 retval
= gb_sdio_probe(gmod
, id
);
164 retval
= gb_tty_probe(gmod
, id
);
168 retval
= gb_battery_probe(gmod
, id
);
174 gb_tty_disconnect(gmod
);
177 gb_sdio_disconnect(gmod
);
180 gb_gpio_disconnect(gmod
);
183 gb_i2c_disconnect(gmod
);
189 static const struct greybus_module_id fake_greybus_module_id
= {
190 GREYBUS_DEVICE(0x42, 0x42)
197 * Pass in a buffer that _should_ contain a Greybus module manifest
198 * and register a greybus device structure with the kernel core.
200 void gb_add_module(struct greybus_host_device
*hd
, u8 module_id
,
203 struct gb_module
*gmod
;
207 * Parse the manifest and build up our data structures
208 * representing what's in it.
210 gmod
= gb_manifest_parse(data
, size
);
212 dev_err(hd
->parent
, "manifest error\n");
218 * We've successfully parsed the manifest. Now we need to
219 * allocate CPort Id's for connecting to the CPorts found on
220 * other modules. For each of these, establish a connection
221 * between the local and remote CPorts (including
222 * configuring the switch to allow them to communicate).
225 gmod
->dev
.parent
= hd
->parent
;
226 gmod
->dev
.driver
= NULL
;
227 gmod
->dev
.bus
= &greybus_bus_type
;
228 gmod
->dev
.type
= &greybus_module_type
;
229 gmod
->dev
.groups
= greybus_module_groups
;
230 gmod
->dev
.dma_mask
= hd
->parent
->dma_mask
;
231 device_initialize(&gmod
->dev
);
232 dev_set_name(&gmod
->dev
, "%d", module_id
);
234 retval
= device_add(&gmod
->dev
);
238 retval
= gb_init_subdevs(gmod
, &fake_greybus_module_id
);
246 device_del(&gmod
->dev
);
249 put_device(&gmod
->dev
);
250 greybus_module_release(&gmod
->dev
);
253 void gb_remove_module(struct greybus_host_device
*hd
, u8 module_id
)
255 struct gb_module
*gmod
;
258 list_for_each_entry(gmod
, &hd
->modules
, links
)
259 if (gmod
->module_id
== module_id
) {
265 greybus_remove_device(gmod
);
267 dev_err(hd
->parent
, "module id %d remove error\n", module_id
);
270 void greybus_remove_device(struct gb_module
*gmod
)
272 /* tear down all of the "sub device types" for this device */
273 gb_i2c_disconnect(gmod
);
274 gb_gpio_disconnect(gmod
);
275 gb_sdio_disconnect(gmod
);
276 gb_tty_disconnect(gmod
);
277 gb_battery_disconnect(gmod
);
279 device_del(&gmod
->dev
);
280 put_device(&gmod
->dev
);
283 static DEFINE_MUTEX(hd_mutex
);
286 * Allocate an available CPort Id for use on the given host device.
287 * Returns the CPort Id, or CPORT_ID_BAD of none remain.
289 * The lowest-available id is returned, so the first call is
290 * guaranteed to allocate CPort Id 0.
292 u16
greybus_hd_cport_id_alloc(struct greybus_host_device
*hd
)
294 unsigned long cport_id
;
296 /* If none left, return BAD */
297 if (hd
->cport_id_count
== HOST_DEV_CPORT_ID_MAX
)
300 spin_lock_irq(&cport_id_map_lock
);
301 cport_id
= find_next_zero_bit(hd
->cport_id_map
, hd
->cport_id_count
,
302 hd
->cport_id_next_free
);
303 if (cport_id
< hd
->cport_id_count
) {
304 hd
->cport_id_next_free
= cport_id
+ 1; /* Success */
305 hd
->cport_id_count
++;
307 /* Lost a race for the last one */
308 if (hd
->cport_id_count
!= HOST_DEV_CPORT_ID_MAX
) {
309 pr_err("bad cport_id_count in alloc");
310 hd
->cport_id_count
= HOST_DEV_CPORT_ID_MAX
;
312 cport_id
= CPORT_ID_BAD
;
314 spin_unlock_irq(&cport_id_map_lock
);
320 * Free a previously-allocated CPort Id on the given host device.
322 void greybus_hd_cport_id_free(struct greybus_host_device
*hd
, u16 cport_id
)
324 if (cport_id
>= HOST_DEV_CPORT_ID_MAX
) {
325 pr_err("bad cport_id %hu\n", cport_id
);
328 if (!hd
->cport_id_count
) {
329 pr_err("too many cport_id frees\n");
333 spin_lock_irq(&cport_id_map_lock
);
334 if (test_and_clear_bit(cport_id
, hd
->cport_id_map
)) {
335 if (hd
->cport_id_count
) {
336 hd
->cport_id_count
--;
337 if (cport_id
< hd
->cport_id_next_free
)
338 hd
->cport_id_next_free
= cport_id
;
340 pr_err("bad cport_id_count in free");
343 pr_err("duplicate cport_id %hu free\n", cport_id
);
345 spin_unlock_irq(&cport_id_map_lock
);
348 static void free_hd(struct kref
*kref
)
350 struct greybus_host_device
*hd
;
352 hd
= container_of(kref
, struct greybus_host_device
, kref
);
357 struct greybus_host_device
*greybus_create_hd(struct greybus_host_driver
*driver
,
358 struct device
*parent
)
360 struct greybus_host_device
*hd
;
362 hd
= kzalloc(sizeof(*hd
) + driver
->hd_priv_size
, GFP_KERNEL
);
366 kref_init(&hd
->kref
);
369 INIT_LIST_HEAD(&hd
->modules
);
371 /* Pre-allocate CPort 0 for control stuff. XXX */
372 if (greybus_hd_cport_id_alloc(hd
) != 0) {
373 pr_err("couldn't allocate cport 0\n");
380 EXPORT_SYMBOL_GPL(greybus_create_hd
);
382 void greybus_remove_hd(struct greybus_host_device
*hd
)
384 kref_put_mutex(&hd
->kref
, free_hd
, &hd_mutex
);
386 EXPORT_SYMBOL_GPL(greybus_remove_hd
);
389 static int __init
gb_init(void)
393 BUILD_BUG_ON(HOST_DEV_CPORT_ID_MAX
>= (long)CPORT_ID_BAD
);
394 spin_lock_init(&cport_id_map_lock
);
396 retval
= gb_debugfs_init();
398 pr_err("debugfs failed\n");
402 retval
= bus_register(&greybus_bus_type
);
404 pr_err("bus_register failed\n");
408 retval
= gb_ap_init();
410 pr_err("gb_ap_init failed\n");
414 retval
= gb_gbuf_init();
416 pr_err("gb_gbuf_init failed\n");
420 retval
= gb_tty_init();
422 pr_err("gb_tty_init failed\n");
435 bus_unregister(&greybus_bus_type
);
438 gb_debugfs_cleanup();
443 static void __exit
gb_exit(void)
448 bus_unregister(&greybus_bus_type
);
449 gb_debugfs_cleanup();
452 module_init(gb_init
);
453 module_exit(gb_exit
);
455 MODULE_LICENSE("GPL");
456 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");