2014-10-02 02:54:11 +00:00
|
|
|
/*
|
2014-12-19 22:56:31 +00:00
|
|
|
* Greybus interface code
|
2014-10-02 02:54:11 +00:00
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
2014-12-12 18:08:42 +00:00
|
|
|
* Copyright 2014 Linaro Ltd.
|
2014-10-02 02:54:11 +00:00
|
|
|
*
|
|
|
|
* Released under the GPLv2 only.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "greybus.h"
|
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
/* interface sysfs attributes */
|
|
|
|
#define gb_interface_attr(field, type) \
|
|
|
|
static ssize_t field##_show(struct device *dev, \
|
|
|
|
struct device_attribute *attr, \
|
|
|
|
char *buf) \
|
2014-12-11 22:10:59 +00:00
|
|
|
{ \
|
2014-12-19 22:56:31 +00:00
|
|
|
struct gb_interface *intf = to_gb_interface(dev); \
|
|
|
|
return sprintf(buf, "%"#type"\n", intf->field); \
|
2014-12-11 22:10:59 +00:00
|
|
|
} \
|
|
|
|
static DEVICE_ATTR_RO(field)
|
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
gb_interface_attr(vendor, x);
|
|
|
|
gb_interface_attr(product, x);
|
|
|
|
gb_interface_attr(unique_id, llX);
|
|
|
|
gb_interface_attr(vendor_string, s);
|
|
|
|
gb_interface_attr(product_string, s);
|
2014-12-11 22:10:59 +00:00
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
static struct attribute *interface_attrs[] = {
|
2014-12-11 22:10:59 +00:00
|
|
|
&dev_attr_vendor.attr,
|
|
|
|
&dev_attr_product.attr,
|
|
|
|
&dev_attr_unique_id.attr,
|
|
|
|
&dev_attr_vendor_string.attr,
|
|
|
|
&dev_attr_product_string.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
2014-12-19 22:56:31 +00:00
|
|
|
ATTRIBUTE_GROUPS(interface);
|
2014-12-11 22:10:59 +00:00
|
|
|
|
|
|
|
|
2014-10-02 02:54:11 +00:00
|
|
|
/* XXX This could be per-host device */
|
|
|
|
static DEFINE_SPINLOCK(gb_modules_lock);
|
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
static int gb_interface_match_one_id(struct gb_interface *intf,
|
|
|
|
const struct greybus_interface_block_id *id)
|
2014-10-02 02:54:11 +00:00
|
|
|
{
|
2014-12-11 22:11:02 +00:00
|
|
|
if ((id->match_flags & GREYBUS_ID_MATCH_VENDOR) &&
|
2014-12-19 22:56:31 +00:00
|
|
|
(id->vendor != intf->vendor))
|
2014-10-02 02:54:11 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-12-11 22:11:02 +00:00
|
|
|
if ((id->match_flags & GREYBUS_ID_MATCH_PRODUCT) &&
|
2014-12-19 22:56:31 +00:00
|
|
|
(id->product != intf->product))
|
2014-10-02 02:54:11 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-12-11 22:11:02 +00:00
|
|
|
if ((id->match_flags & GREYBUS_ID_MATCH_SERIAL) &&
|
2014-12-19 22:56:31 +00:00
|
|
|
(id->unique_id != intf->unique_id))
|
2014-10-02 02:54:11 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-12-11 22:11:02 +00:00
|
|
|
const struct greybus_interface_block_id *
|
2014-12-19 22:56:31 +00:00
|
|
|
gb_interface_match_id(struct gb_interface *intf,
|
|
|
|
const struct greybus_interface_block_id *id)
|
2014-10-02 02:54:11 +00:00
|
|
|
{
|
|
|
|
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++) {
|
2014-12-19 22:56:31 +00:00
|
|
|
if (gb_interface_match_one_id(intf, id))
|
2014-10-02 02:54:11 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
struct gb_interface *gb_interface_find(struct greybus_host_device *hd,
|
|
|
|
u8 module_id)
|
2014-11-14 11:55:06 +00:00
|
|
|
{
|
2014-12-19 22:56:31 +00:00
|
|
|
struct gb_interface *intf;
|
2014-11-14 11:55:06 +00:00
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
list_for_each_entry(intf, &hd->modules, links)
|
|
|
|
if (intf->module_id == module_id)
|
|
|
|
return intf;
|
2014-11-14 11:55:06 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
static void greybus_interface_release(struct device *dev)
|
2014-10-21 04:01:04 +00:00
|
|
|
{
|
2014-12-19 22:56:31 +00:00
|
|
|
struct gb_interface *intf = to_gb_interface(dev);
|
2014-10-21 04:01:04 +00:00
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
kfree(intf);
|
2014-10-21 04:01:04 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
struct device_type greybus_interface_type = {
|
|
|
|
.name = "greybus_interface",
|
|
|
|
.release = greybus_interface_release,
|
2014-10-24 09:34:46 +00:00
|
|
|
};
|
|
|
|
|
2014-10-02 02:54:11 +00:00
|
|
|
/*
|
2014-11-21 05:56:30 +00:00
|
|
|
* A Greybus module represents a user-replicable component on an Ara
|
2014-12-19 22:56:31 +00:00
|
|
|
* phone. An interface is the physical connection on that module. A
|
|
|
|
* module may have more than one interface.
|
2014-10-02 02:54:11 +00:00
|
|
|
*
|
2014-12-19 22:56:31 +00:00
|
|
|
* Create a gb_interface structure to represent a discovered module.
|
2014-10-02 02:54:11 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-12-19 22:56:31 +00:00
|
|
|
static struct gb_interface *gb_interface_create(struct greybus_host_device *hd,
|
|
|
|
u8 module_id)
|
2014-10-02 02:54:11 +00:00
|
|
|
{
|
2014-12-19 22:56:31 +00:00
|
|
|
struct gb_interface *intf;
|
2014-10-24 09:34:46 +00:00
|
|
|
int retval;
|
2014-10-02 02:54:11 +00:00
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
intf = gb_interface_find(hd, module_id);
|
|
|
|
if (intf) {
|
2014-10-24 10:46:15 +00:00
|
|
|
dev_err(hd->parent, "Duplicate module id %d will not be created\n",
|
|
|
|
module_id);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
intf = kzalloc(sizeof(*intf), GFP_KERNEL);
|
|
|
|
if (!intf)
|
2014-10-02 02:54:11 +00:00
|
|
|
return NULL;
|
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
intf->hd = hd; /* XXX refcount? */
|
|
|
|
intf->module_id = module_id;
|
|
|
|
INIT_LIST_HEAD(&intf->bundles);
|
2014-10-02 02:54:11 +00:00
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
intf->dev.parent = hd->parent;
|
|
|
|
intf->dev.bus = &greybus_bus_type;
|
|
|
|
intf->dev.type = &greybus_interface_type;
|
|
|
|
intf->dev.groups = interface_groups;
|
|
|
|
intf->dev.dma_mask = hd->parent->dma_mask;
|
|
|
|
device_initialize(&intf->dev);
|
|
|
|
dev_set_name(&intf->dev, "%d", module_id);
|
2014-10-24 09:34:46 +00:00
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
retval = device_add(&intf->dev);
|
2014-10-24 09:34:46 +00:00
|
|
|
if (retval) {
|
2014-11-05 22:03:12 +00:00
|
|
|
pr_err("failed to add module device for id 0x%02hhx\n",
|
|
|
|
module_id);
|
2014-12-19 22:56:31 +00:00
|
|
|
put_device(&intf->dev);
|
|
|
|
kfree(intf);
|
2014-10-24 09:34:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-13 12:44:37 +00:00
|
|
|
spin_lock_irq(&gb_modules_lock);
|
2014-12-19 22:56:31 +00:00
|
|
|
list_add_tail(&intf->links, &hd->modules);
|
2014-11-13 12:44:37 +00:00
|
|
|
spin_unlock_irq(&gb_modules_lock);
|
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
return intf;
|
2014-10-02 02:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tear down a previously set up module.
|
|
|
|
*/
|
2014-12-19 22:56:31 +00:00
|
|
|
static void gb_interface_destroy(struct gb_interface *intf)
|
2014-10-02 02:54:11 +00:00
|
|
|
{
|
2014-12-19 22:56:31 +00:00
|
|
|
if (WARN_ON(!intf))
|
2014-10-02 02:54:11 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
spin_lock_irq(&gb_modules_lock);
|
2014-12-19 22:56:31 +00:00
|
|
|
list_del(&intf->links);
|
2014-10-02 02:54:11 +00:00
|
|
|
spin_unlock_irq(&gb_modules_lock);
|
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
gb_bundle_destroy(intf);
|
2014-10-21 04:01:04 +00:00
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
kfree(intf->product_string);
|
|
|
|
kfree(intf->vendor_string);
|
2014-10-02 02:54:11 +00:00
|
|
|
/* kref_put(module->hd); */
|
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
device_del(&intf->dev);
|
2014-10-16 11:35:35 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:55:07 +00:00
|
|
|
/**
|
|
|
|
* gb_add_module
|
|
|
|
*
|
|
|
|
* Pass in a buffer that _should_ contain a Greybus module manifest
|
|
|
|
* and register a greybus device structure with the kernel core.
|
|
|
|
*/
|
|
|
|
void gb_add_module(struct greybus_host_device *hd, u8 module_id,
|
|
|
|
u8 *data, int size)
|
|
|
|
{
|
2014-12-19 22:56:31 +00:00
|
|
|
struct gb_interface *intf;
|
2014-11-14 11:55:07 +00:00
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
intf = gb_interface_create(hd, module_id);
|
|
|
|
if (!intf) {
|
|
|
|
dev_err(hd->parent, "failed to create interface\n");
|
2014-11-14 11:55:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the manifest and build up our data structures
|
|
|
|
* representing what's in it.
|
|
|
|
*/
|
2014-12-19 22:56:31 +00:00
|
|
|
if (!gb_manifest_parse(intf, data, size)) {
|
2014-11-14 11:55:07 +00:00
|
|
|
dev_err(hd->parent, "manifest error\n");
|
|
|
|
goto err_module;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* We've successfully parsed the manifest. Now we need to
|
|
|
|
* allocate CPort Id's for connecting to the CPorts found on
|
|
|
|
* other modules. For each of these, establish a connection
|
|
|
|
* between the local and remote CPorts (including
|
|
|
|
* configuring the switch to allow them to communicate).
|
|
|
|
*/
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
err_module:
|
2014-12-19 22:56:31 +00:00
|
|
|
gb_interface_destroy(intf);
|
2014-11-14 11:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gb_remove_module(struct greybus_host_device *hd, u8 module_id)
|
|
|
|
{
|
2014-12-19 22:56:31 +00:00
|
|
|
struct gb_interface *intf = gb_interface_find(hd, module_id);
|
2014-11-14 11:55:07 +00:00
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
if (intf)
|
|
|
|
gb_interface_destroy(intf);
|
2014-11-14 11:55:07 +00:00
|
|
|
else
|
2014-12-19 22:56:31 +00:00
|
|
|
dev_err(hd->parent, "interface id %d not found\n", module_id);
|
2014-11-14 11:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gb_remove_modules(struct greybus_host_device *hd)
|
|
|
|
{
|
2014-12-19 22:56:31 +00:00
|
|
|
struct gb_interface *intf, *temp;
|
2014-11-14 11:55:07 +00:00
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
list_for_each_entry_safe(intf, temp, &hd->modules, links)
|
|
|
|
gb_interface_destroy(intf);
|
2014-11-14 11:55:07 +00:00
|
|
|
}
|