#include <linux/greybus.h>
static const char *get_descriptor_type_string(u8 type)
{
switch (type) {
case GREYBUS_TYPE_INVALID:
return "invalid";
case GREYBUS_TYPE_STRING:
return "string";
case GREYBUS_TYPE_INTERFACE:
return "interface";
case GREYBUS_TYPE_CPORT:
return "cport";
case GREYBUS_TYPE_BUNDLE:
return "bundle";
default:
WARN_ON(1);
return "unknown";
}
}
struct manifest_desc {
struct list_head links;
size_t size;
void *data;
enum greybus_descriptor_type type;
};
static void release_manifest_descriptor(struct manifest_desc *descriptor)
{
list_del(&descriptor->links);
kfree(descriptor);
}
static void release_manifest_descriptors(struct gb_interface *intf)
{
struct manifest_desc *descriptor;
struct manifest_desc *next;
list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
release_manifest_descriptor(descriptor);
}
static void release_cport_descriptors(struct list_head *head, u8 bundle_id)
{
struct manifest_desc *desc, *tmp;
struct greybus_descriptor_cport *desc_cport;
list_for_each_entry_safe(desc, tmp, head, links) {
desc_cport = desc->data;
if (desc->type != GREYBUS_TYPE_CPORT)
continue;
if (desc_cport->bundle == bundle_id)
release_manifest_descriptor(desc);
}
}
static struct manifest_desc *get_next_bundle_desc(struct gb_interface *intf)
{
struct manifest_desc *descriptor;
struct manifest_desc *next;
list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
if (descriptor->type == GREYBUS_TYPE_BUNDLE)
return descriptor;
return NULL;
}
static int identify_descriptor(struct gb_interface *intf,
struct greybus_descriptor *desc, size_t size)
{
struct greybus_descriptor_header *desc_header = &desc->header;
struct manifest_desc *descriptor;
size_t desc_size;
size_t expected_size;
if (size < sizeof(*desc_header)) {
dev_err(&intf->dev, "manifest too small (%zu < %zu)\n", size,
sizeof(*desc_header));
return -EINVAL;
}
desc_size = le16_to_cpu(desc_header->size);
if (desc_size > size) {
dev_err(&intf->dev, "descriptor too big (%zu > %zu)\n",
desc_size, size);
return -EINVAL;
}
expected_size = sizeof(*desc_header);
switch (desc_header->type) {
case GREYBUS_TYPE_STRING:
expected_size += sizeof(struct greybus_descriptor_string);
expected_size += desc->string.length;
expected_size = ALIGN(expected_size, 4);
break;
case GREYBUS_TYPE_INTERFACE:
expected_size += sizeof(struct greybus_descriptor_interface);
break;
case GREYBUS_TYPE_BUNDLE:
expected_size += sizeof(struct greybus_descriptor_bundle);
break;
case GREYBUS_TYPE_CPORT:
expected_size += sizeof(struct greybus_descriptor_cport);
break;
case GREYBUS_TYPE_INVALID:
default:
dev_err(&intf->dev, "invalid descriptor type (%u)\n",
desc_header->type);
return -EINVAL;
}
if (desc_size < expected_size) {
dev_err(&intf->dev, "%s descriptor too small (%zu < %zu)\n",
get_descriptor_type_string(desc_header->type),
desc_size, expected_size);
return -EINVAL;
}
if (desc_size > expected_size) {
dev_warn(&intf->dev, "%s descriptor size mismatch (want %zu got %zu)\n",
get_descriptor_type_string(desc_header->type),
expected_size, desc_size);
}
descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
if (!descriptor)
return -ENOMEM;
descriptor->size = desc_size;
descriptor->data = (char *)desc + sizeof(*desc_header);
descriptor->type = desc_header->type;
list_add_tail(&descriptor->links, &intf->manifest_descs);
return desc_size;
}
static char *gb_string_get(struct gb_interface *intf, u8 string_id)
{
struct greybus_descriptor_string *desc_string;
struct manifest_desc *descriptor;
bool found = false;
char *string;
if (!string_id)
return NULL;
list_for_each_entry(descriptor, &intf->manifest_descs, links) {
if (descriptor->type != GREYBUS_TYPE_STRING)
continue;
desc_string = descriptor->data;
if (desc_string->id == string_id) {
found = true;
break;
}
}
if (!found)
return ERR_PTR(-ENOENT);
string = kmemdup(&desc_string->string, desc_string->length + 1,
GFP_KERNEL);
if (!string)
return ERR_PTR(-ENOMEM);
string[desc_string->length] = '\0';
release_manifest_descriptor(descriptor);
return string;
}
static u32 gb_manifest_parse_cports(struct gb_bundle *bundle)
{
struct gb_interface *intf = bundle->intf;
struct greybus_descriptor_cport *desc_cport;
struct manifest_desc *desc, *next, *tmp;
LIST_HEAD(list);
u8 bundle_id = bundle->id;
u16 cport_id;
u32 count = 0;
int i;
list_for_each_entry_safe(desc, next, &intf->manifest_descs, links) {
if (desc->type != GREYBUS_TYPE_CPORT)
continue;
desc_cport = desc->data;
if (desc_cport->bundle != bundle_id)
continue;
cport_id = le16_to_cpu(desc_cport->id);
if (cport_id > CPORT_ID_MAX)
goto exit;
if (cport_id == GB_CONTROL_CPORT_ID) {
dev_err(&bundle->dev, "invalid cport id found (%02u)\n",
cport_id);
goto exit;
}
list_for_each_entry(tmp, &list, links) {
desc_cport = tmp->data;
if (cport_id == le16_to_cpu(desc_cport->id)) {
dev_err(&bundle->dev,
"duplicate CPort %u found\n", cport_id);
goto exit;
}
}
list_move_tail(&desc->links, &list);
count++;
}
if (!count)
return 0;
bundle->cport_desc = kcalloc(count, sizeof(*bundle->cport_desc),
GFP_KERNEL);
if (!bundle->cport_desc)
goto exit;
bundle->num_cports = count;
i = 0;
list_for_each_entry_safe(desc, next, &list, links) {
desc_cport = desc->data;
memcpy(&bundle->cport_desc[i++], desc_cport,
sizeof(*desc_cport));
release_manifest_descriptor(desc);
}
return count;
exit:
release_cport_descriptors(&list, bundle_id);
release_cport_descriptors(&intf->manifest_descs, bundle_id);
return 0;
}
static u32 gb_manifest_parse_bundles(struct gb_interface *intf)
{
struct manifest_desc *desc;
struct gb_bundle *bundle;
struct gb_bundle *bundle_next;
u32 count = 0;
u8 bundle_id;
u8 class;
while ((desc = get_next_bundle_desc(intf))) {
struct greybus_descriptor_bundle *desc_bundle;
desc_bundle = desc->data;
bundle_id = desc_bundle->id;
class = desc_bundle->class;
release_manifest_descriptor(desc);
if (bundle_id == GB_CONTROL_BUNDLE_ID) {
dev_dbg(&intf->dev, "%s - ignoring control bundle\n",
__func__);
release_cport_descriptors(&intf->manifest_descs,
bundle_id);
continue;
}
if (class == GREYBUS_CLASS_CONTROL) {
dev_err(&intf->dev,
"bundle %u cannot use control class\n",
bundle_id);
goto cleanup;
}
bundle = gb_bundle_create(intf, bundle_id, class);
if (!bundle)
goto cleanup;
if (!gb_manifest_parse_cports(bundle)) {
gb_bundle_destroy(bundle);
continue;
}
count++;
}
return count;
cleanup:
list_for_each_entry_safe(bundle, bundle_next, &intf->bundles, links) {
gb_bundle_destroy(bundle);
count--;
}
return 0;
}
static bool gb_manifest_parse_interface(struct gb_interface *intf,
struct manifest_desc *interface_desc)
{
struct greybus_descriptor_interface *desc_intf = interface_desc->data;
struct gb_control *control = intf->control;
char *str;
str = gb_string_get(intf, desc_intf->vendor_stringid);
if (IS_ERR(str))
return false;
control->vendor_string = str;
str = gb_string_get(intf, desc_intf->product_stringid);
if (IS_ERR(str))
goto out_free_vendor_string;
control->product_string = str;
intf->features = desc_intf->features;
release_manifest_descriptor(interface_desc);
if (!gb_manifest_parse_bundles(intf)) {
dev_err(&intf->dev, "manifest bundle descriptors not valid\n");
goto out_err;
}
return true;
out_err:
kfree(control->product_string);
control->product_string = NULL;
out_free_vendor_string:
kfree(control->vendor_string);
control->vendor_string = NULL;
return false;
}
bool gb_manifest_parse(struct gb_interface *intf, void *data, size_t size)
{
struct greybus_manifest *manifest;
struct greybus_manifest_header *header;
struct greybus_descriptor *desc;
struct manifest_desc *descriptor;
struct manifest_desc *interface_desc = NULL;
u16 manifest_size;
u32 found = 0;
bool result;
if (WARN_ON(!list_empty(&intf->manifest_descs)))
return false;
if (size < sizeof(*header)) {
dev_err(&intf->dev, "short manifest (%zu < %zu)\n",
size, sizeof(*header));
return false;
}
manifest = data;
header = &manifest->header;
manifest_size = le16_to_cpu(header->size);
if (manifest_size != size) {
dev_err(&intf->dev, "manifest size mismatch (%zu != %u)\n",
size, manifest_size);
return false;
}
if (header->version_major > GREYBUS_VERSION_MAJOR) {
dev_err(&intf->dev, "manifest version too new (%u.%u > %u.%u)\n",
header->version_major, header->version_minor,
GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
return false;
}
desc = manifest->descriptors;
size -= sizeof(*header);
while (size) {
int desc_size;
desc_size = identify_descriptor(intf, desc, size);
if (desc_size < 0) {
result = false;
goto out;
}
desc = (struct greybus_descriptor *)((char *)desc + desc_size);
size -= desc_size;
}
list_for_each_entry(descriptor, &intf->manifest_descs, links) {
if (descriptor->type == GREYBUS_TYPE_INTERFACE)
if (!found++)
interface_desc = descriptor;
}
if (found != 1) {
dev_err(&intf->dev, "manifest must have 1 interface descriptor (%u found)\n",
found);
result = false;
goto out;
}
result = gb_manifest_parse_interface(intf, interface_desc);
if (result && !list_empty(&intf->manifest_descs))
dev_info(&intf->dev, "excess descriptors in interface manifest\n");
out:
release_manifest_descriptors(intf);
return result;
}