2014-10-02 02:54:11 +00:00
|
|
|
/*
|
|
|
|
* Greybus modules
|
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Released under the GPLv2 only.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "greybus.h"
|
|
|
|
|
|
|
|
/* XXX This could be per-host device */
|
|
|
|
static DEFINE_SPINLOCK(gb_modules_lock);
|
|
|
|
|
|
|
|
static int gb_module_match_one_id(struct gb_module *gmod,
|
|
|
|
const struct greybus_module_id *id)
|
|
|
|
{
|
|
|
|
if ((id->match_flags & GREYBUS_DEVICE_ID_MATCH_VENDOR) &&
|
2014-10-02 02:54:16 +00:00
|
|
|
(id->vendor != gmod->vendor))
|
2014-10-02 02:54:11 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((id->match_flags & GREYBUS_DEVICE_ID_MATCH_PRODUCT) &&
|
2014-10-02 02:54:16 +00:00
|
|
|
(id->product != gmod->product))
|
2014-10-02 02:54:11 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((id->match_flags & GREYBUS_DEVICE_ID_MATCH_SERIAL) &&
|
2014-10-02 17:30:02 +00:00
|
|
|
(id->unique_id != gmod->unique_id))
|
2014-10-02 02:54:11 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct greybus_module_id *gb_module_match_id(struct gb_module *gmod,
|
|
|
|
const struct greybus_module_id *id)
|
|
|
|
{
|
|
|
|
if (id == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2014-10-02 17:30:02 +00:00
|
|
|
for (; id->vendor || id->product || id->unique_id ||
|
2014-10-02 02:54:11 +00:00
|
|
|
id->driver_info; id++) {
|
|
|
|
if (gb_module_match_one_id(gmod, id))
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-24 09:34:46 +00:00
|
|
|
static void greybus_module_release(struct device *dev)
|
2014-10-21 04:01:04 +00:00
|
|
|
{
|
2014-10-24 09:34:46 +00:00
|
|
|
struct gb_module *gmod = to_gb_module(dev);
|
2014-10-21 04:01:04 +00:00
|
|
|
|
2014-10-24 09:34:46 +00:00
|
|
|
kfree(gmod);
|
2014-10-21 04:01:04 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 09:34:46 +00:00
|
|
|
static struct device_type greybus_module_type = {
|
|
|
|
.name = "greybus_module",
|
|
|
|
.release = greybus_module_release,
|
|
|
|
};
|
|
|
|
|
2014-10-02 02:54:11 +00:00
|
|
|
/*
|
|
|
|
* A Greybus module represents a user-replacable component on an Ara
|
|
|
|
* phone.
|
|
|
|
*
|
|
|
|
* Create a gb_module structure to represent a discovered module.
|
|
|
|
* The position within the Endo is encoded in the "module_id" argument.
|
|
|
|
* Returns a pointer to the new module or a null pointer if a
|
|
|
|
* failure occurs due to memory exhaustion.
|
|
|
|
*/
|
|
|
|
struct gb_module *gb_module_create(struct greybus_host_device *hd, u8 module_id)
|
|
|
|
{
|
2014-10-16 11:35:35 +00:00
|
|
|
struct gb_module *gmod;
|
2014-10-24 09:34:46 +00:00
|
|
|
int retval;
|
2014-10-02 02:54:11 +00:00
|
|
|
|
2014-10-16 11:35:35 +00:00
|
|
|
gmod = kzalloc(sizeof(*gmod), GFP_KERNEL);
|
|
|
|
if (!gmod)
|
2014-10-02 02:54:11 +00:00
|
|
|
return NULL;
|
|
|
|
|
2014-10-16 11:35:35 +00:00
|
|
|
gmod->hd = hd; /* XXX refcount? */
|
2014-10-21 04:01:04 +00:00
|
|
|
gmod->module_id = module_id; /* XXX check for dups */
|
2014-10-16 11:35:35 +00:00
|
|
|
INIT_LIST_HEAD(&gmod->interfaces);
|
2014-10-02 02:54:11 +00:00
|
|
|
|
|
|
|
spin_lock_irq(&gb_modules_lock);
|
2014-10-16 11:35:35 +00:00
|
|
|
list_add_tail(&gmod->links, &hd->modules);
|
2014-10-02 02:54:11 +00:00
|
|
|
spin_unlock_irq(&gb_modules_lock);
|
|
|
|
|
2014-10-24 09:34:46 +00:00
|
|
|
gmod->dev.parent = hd->parent;
|
|
|
|
gmod->dev.driver = NULL;
|
|
|
|
gmod->dev.bus = &greybus_bus_type;
|
|
|
|
gmod->dev.type = &greybus_module_type;
|
|
|
|
gmod->dev.groups = greybus_module_groups;
|
|
|
|
gmod->dev.dma_mask = hd->parent->dma_mask;
|
|
|
|
device_initialize(&gmod->dev);
|
|
|
|
dev_set_name(&gmod->dev, "%d", module_id);
|
|
|
|
|
|
|
|
retval = device_add(&gmod->dev);
|
|
|
|
if (retval) {
|
|
|
|
put_device(&gmod->dev);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-16 11:35:35 +00:00
|
|
|
return gmod;
|
2014-10-02 02:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tear down a previously set up module.
|
|
|
|
*/
|
2014-10-16 11:35:35 +00:00
|
|
|
void gb_module_destroy(struct gb_module *gmod)
|
2014-10-02 02:54:11 +00:00
|
|
|
{
|
2014-10-16 11:35:35 +00:00
|
|
|
if (WARN_ON(!gmod))
|
2014-10-02 02:54:11 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
spin_lock_irq(&gb_modules_lock);
|
2014-10-16 11:35:35 +00:00
|
|
|
list_del(&gmod->links);
|
2014-10-02 02:54:11 +00:00
|
|
|
spin_unlock_irq(&gb_modules_lock);
|
|
|
|
|
2014-10-21 04:01:04 +00:00
|
|
|
/* XXX Do something with gmod->gb_tty */
|
|
|
|
|
2014-10-24 09:34:46 +00:00
|
|
|
gb_interface_destroy(gmod);
|
2014-10-21 04:01:04 +00:00
|
|
|
|
|
|
|
kfree(gmod->product_string);
|
|
|
|
kfree(gmod->vendor_string);
|
2014-10-02 02:54:11 +00:00
|
|
|
/* kref_put(module->hd); */
|
|
|
|
|
2014-10-24 09:34:46 +00:00
|
|
|
device_del(&gmod->dev);
|
2014-10-16 11:35:35 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 02:43:29 +00:00
|
|
|
struct gb_module *gb_module_find(struct greybus_host_device *hd, u8 module_id)
|
|
|
|
{
|
|
|
|
struct gb_module *module;
|
|
|
|
|
|
|
|
list_for_each_entry(module, &hd->modules, links)
|
|
|
|
if (module->module_id == module_id)
|
|
|
|
return module;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-22 07:04:32 +00:00
|
|
|
int
|
|
|
|
gb_module_interface_init(struct gb_module *gmod, u8 interface_id, u8 device_id)
|
2014-10-16 11:35:35 +00:00
|
|
|
{
|
|
|
|
struct gb_interface *interface;
|
2014-10-22 07:04:32 +00:00
|
|
|
int ret;
|
2014-10-16 11:35:35 +00:00
|
|
|
|
2014-10-22 07:04:32 +00:00
|
|
|
interface = gb_interface_find(gmod, interface_id);
|
|
|
|
if (!interface) {
|
|
|
|
dev_err(gmod->hd->parent, "module %hhu not found\n",
|
|
|
|
interface_id);
|
|
|
|
return -ENOENT;
|
2014-10-16 11:35:35 +00:00
|
|
|
}
|
2014-10-22 10:36:17 +00:00
|
|
|
|
|
|
|
ret = svc_set_route_send(interface, gmod->hd);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(gmod->hd->parent, "failed to set route (%d)\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-22 07:04:32 +00:00
|
|
|
ret = gb_interface_connections_init(interface);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(gmod->hd->parent, "module interface init error %d\n",
|
|
|
|
ret);
|
2014-10-22 10:36:17 +00:00
|
|
|
/* XXX clear route */
|
2014-10-22 07:04:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
interface->device_id = device_id;
|
|
|
|
|
2014-10-22 10:36:17 +00:00
|
|
|
return 0;
|
2014-10-02 02:54:11 +00:00
|
|
|
}
|