xref: /openbmc/linux/drivers/greybus/manifest.c (revision 8465def4)
18465def4SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
28465def4SGreg Kroah-Hartman /*
38465def4SGreg Kroah-Hartman  * Greybus manifest parsing
48465def4SGreg Kroah-Hartman  *
58465def4SGreg Kroah-Hartman  * Copyright 2014-2015 Google Inc.
68465def4SGreg Kroah-Hartman  * Copyright 2014-2015 Linaro Ltd.
78465def4SGreg Kroah-Hartman  */
88465def4SGreg Kroah-Hartman 
98465def4SGreg Kroah-Hartman #include <linux/greybus.h>
108465def4SGreg Kroah-Hartman 
get_descriptor_type_string(u8 type)118465def4SGreg Kroah-Hartman static const char *get_descriptor_type_string(u8 type)
128465def4SGreg Kroah-Hartman {
138465def4SGreg Kroah-Hartman 	switch (type) {
148465def4SGreg Kroah-Hartman 	case GREYBUS_TYPE_INVALID:
158465def4SGreg Kroah-Hartman 		return "invalid";
168465def4SGreg Kroah-Hartman 	case GREYBUS_TYPE_STRING:
178465def4SGreg Kroah-Hartman 		return "string";
188465def4SGreg Kroah-Hartman 	case GREYBUS_TYPE_INTERFACE:
198465def4SGreg Kroah-Hartman 		return "interface";
208465def4SGreg Kroah-Hartman 	case GREYBUS_TYPE_CPORT:
218465def4SGreg Kroah-Hartman 		return "cport";
228465def4SGreg Kroah-Hartman 	case GREYBUS_TYPE_BUNDLE:
238465def4SGreg Kroah-Hartman 		return "bundle";
248465def4SGreg Kroah-Hartman 	default:
258465def4SGreg Kroah-Hartman 		WARN_ON(1);
268465def4SGreg Kroah-Hartman 		return "unknown";
278465def4SGreg Kroah-Hartman 	}
288465def4SGreg Kroah-Hartman }
298465def4SGreg Kroah-Hartman 
308465def4SGreg Kroah-Hartman /*
318465def4SGreg Kroah-Hartman  * We scan the manifest once to identify where all the descriptors
328465def4SGreg Kroah-Hartman  * are.  The result is a list of these manifest_desc structures.  We
338465def4SGreg Kroah-Hartman  * then pick through them for what we're looking for (starting with
348465def4SGreg Kroah-Hartman  * the interface descriptor).  As each is processed we remove it from
358465def4SGreg Kroah-Hartman  * the list.  When we're done the list should (probably) be empty.
368465def4SGreg Kroah-Hartman  */
378465def4SGreg Kroah-Hartman struct manifest_desc {
388465def4SGreg Kroah-Hartman 	struct list_head		links;
398465def4SGreg Kroah-Hartman 
408465def4SGreg Kroah-Hartman 	size_t				size;
418465def4SGreg Kroah-Hartman 	void				*data;
428465def4SGreg Kroah-Hartman 	enum greybus_descriptor_type	type;
438465def4SGreg Kroah-Hartman };
448465def4SGreg Kroah-Hartman 
release_manifest_descriptor(struct manifest_desc * descriptor)458465def4SGreg Kroah-Hartman static void release_manifest_descriptor(struct manifest_desc *descriptor)
468465def4SGreg Kroah-Hartman {
478465def4SGreg Kroah-Hartman 	list_del(&descriptor->links);
488465def4SGreg Kroah-Hartman 	kfree(descriptor);
498465def4SGreg Kroah-Hartman }
508465def4SGreg Kroah-Hartman 
release_manifest_descriptors(struct gb_interface * intf)518465def4SGreg Kroah-Hartman static void release_manifest_descriptors(struct gb_interface *intf)
528465def4SGreg Kroah-Hartman {
538465def4SGreg Kroah-Hartman 	struct manifest_desc *descriptor;
548465def4SGreg Kroah-Hartman 	struct manifest_desc *next;
558465def4SGreg Kroah-Hartman 
568465def4SGreg Kroah-Hartman 	list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
578465def4SGreg Kroah-Hartman 		release_manifest_descriptor(descriptor);
588465def4SGreg Kroah-Hartman }
598465def4SGreg Kroah-Hartman 
release_cport_descriptors(struct list_head * head,u8 bundle_id)608465def4SGreg Kroah-Hartman static void release_cport_descriptors(struct list_head *head, u8 bundle_id)
618465def4SGreg Kroah-Hartman {
628465def4SGreg Kroah-Hartman 	struct manifest_desc *desc, *tmp;
638465def4SGreg Kroah-Hartman 	struct greybus_descriptor_cport *desc_cport;
648465def4SGreg Kroah-Hartman 
658465def4SGreg Kroah-Hartman 	list_for_each_entry_safe(desc, tmp, head, links) {
668465def4SGreg Kroah-Hartman 		desc_cport = desc->data;
678465def4SGreg Kroah-Hartman 
688465def4SGreg Kroah-Hartman 		if (desc->type != GREYBUS_TYPE_CPORT)
698465def4SGreg Kroah-Hartman 			continue;
708465def4SGreg Kroah-Hartman 
718465def4SGreg Kroah-Hartman 		if (desc_cport->bundle == bundle_id)
728465def4SGreg Kroah-Hartman 			release_manifest_descriptor(desc);
738465def4SGreg Kroah-Hartman 	}
748465def4SGreg Kroah-Hartman }
758465def4SGreg Kroah-Hartman 
get_next_bundle_desc(struct gb_interface * intf)768465def4SGreg Kroah-Hartman static struct manifest_desc *get_next_bundle_desc(struct gb_interface *intf)
778465def4SGreg Kroah-Hartman {
788465def4SGreg Kroah-Hartman 	struct manifest_desc *descriptor;
798465def4SGreg Kroah-Hartman 	struct manifest_desc *next;
808465def4SGreg Kroah-Hartman 
818465def4SGreg Kroah-Hartman 	list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
828465def4SGreg Kroah-Hartman 		if (descriptor->type == GREYBUS_TYPE_BUNDLE)
838465def4SGreg Kroah-Hartman 			return descriptor;
848465def4SGreg Kroah-Hartman 
858465def4SGreg Kroah-Hartman 	return NULL;
868465def4SGreg Kroah-Hartman }
878465def4SGreg Kroah-Hartman 
888465def4SGreg Kroah-Hartman /*
898465def4SGreg Kroah-Hartman  * Validate the given descriptor.  Its reported size must fit within
908465def4SGreg Kroah-Hartman  * the number of bytes remaining, and it must have a recognized
918465def4SGreg Kroah-Hartman  * type.  Check that the reported size is at least as big as what
928465def4SGreg Kroah-Hartman  * we expect to see.  (It could be bigger, perhaps for a new version
938465def4SGreg Kroah-Hartman  * of the format.)
948465def4SGreg Kroah-Hartman  *
958465def4SGreg Kroah-Hartman  * Returns the (non-zero) number of bytes consumed by the descriptor,
968465def4SGreg Kroah-Hartman  * or a negative errno.
978465def4SGreg Kroah-Hartman  */
identify_descriptor(struct gb_interface * intf,struct greybus_descriptor * desc,size_t size)988465def4SGreg Kroah-Hartman static int identify_descriptor(struct gb_interface *intf,
998465def4SGreg Kroah-Hartman 			       struct greybus_descriptor *desc, size_t size)
1008465def4SGreg Kroah-Hartman {
1018465def4SGreg Kroah-Hartman 	struct greybus_descriptor_header *desc_header = &desc->header;
1028465def4SGreg Kroah-Hartman 	struct manifest_desc *descriptor;
1038465def4SGreg Kroah-Hartman 	size_t desc_size;
1048465def4SGreg Kroah-Hartman 	size_t expected_size;
1058465def4SGreg Kroah-Hartman 
1068465def4SGreg Kroah-Hartman 	if (size < sizeof(*desc_header)) {
1078465def4SGreg Kroah-Hartman 		dev_err(&intf->dev, "manifest too small (%zu < %zu)\n", size,
1088465def4SGreg Kroah-Hartman 			sizeof(*desc_header));
1098465def4SGreg Kroah-Hartman 		return -EINVAL;		/* Must at least have header */
1108465def4SGreg Kroah-Hartman 	}
1118465def4SGreg Kroah-Hartman 
1128465def4SGreg Kroah-Hartman 	desc_size = le16_to_cpu(desc_header->size);
1138465def4SGreg Kroah-Hartman 	if (desc_size > size) {
1148465def4SGreg Kroah-Hartman 		dev_err(&intf->dev, "descriptor too big (%zu > %zu)\n",
1158465def4SGreg Kroah-Hartman 			desc_size, size);
1168465def4SGreg Kroah-Hartman 		return -EINVAL;
1178465def4SGreg Kroah-Hartman 	}
1188465def4SGreg Kroah-Hartman 
1198465def4SGreg Kroah-Hartman 	/* Descriptor needs to at least have a header */
1208465def4SGreg Kroah-Hartman 	expected_size = sizeof(*desc_header);
1218465def4SGreg Kroah-Hartman 
1228465def4SGreg Kroah-Hartman 	switch (desc_header->type) {
1238465def4SGreg Kroah-Hartman 	case GREYBUS_TYPE_STRING:
1248465def4SGreg Kroah-Hartman 		expected_size += sizeof(struct greybus_descriptor_string);
1258465def4SGreg Kroah-Hartman 		expected_size += desc->string.length;
1268465def4SGreg Kroah-Hartman 
1278465def4SGreg Kroah-Hartman 		/* String descriptors are padded to 4 byte boundaries */
1288465def4SGreg Kroah-Hartman 		expected_size = ALIGN(expected_size, 4);
1298465def4SGreg Kroah-Hartman 		break;
1308465def4SGreg Kroah-Hartman 	case GREYBUS_TYPE_INTERFACE:
1318465def4SGreg Kroah-Hartman 		expected_size += sizeof(struct greybus_descriptor_interface);
1328465def4SGreg Kroah-Hartman 		break;
1338465def4SGreg Kroah-Hartman 	case GREYBUS_TYPE_BUNDLE:
1348465def4SGreg Kroah-Hartman 		expected_size += sizeof(struct greybus_descriptor_bundle);
1358465def4SGreg Kroah-Hartman 		break;
1368465def4SGreg Kroah-Hartman 	case GREYBUS_TYPE_CPORT:
1378465def4SGreg Kroah-Hartman 		expected_size += sizeof(struct greybus_descriptor_cport);
1388465def4SGreg Kroah-Hartman 		break;
1398465def4SGreg Kroah-Hartman 	case GREYBUS_TYPE_INVALID:
1408465def4SGreg Kroah-Hartman 	default:
1418465def4SGreg Kroah-Hartman 		dev_err(&intf->dev, "invalid descriptor type (%u)\n",
1428465def4SGreg Kroah-Hartman 			desc_header->type);
1438465def4SGreg Kroah-Hartman 		return -EINVAL;
1448465def4SGreg Kroah-Hartman 	}
1458465def4SGreg Kroah-Hartman 
1468465def4SGreg Kroah-Hartman 	if (desc_size < expected_size) {
1478465def4SGreg Kroah-Hartman 		dev_err(&intf->dev, "%s descriptor too small (%zu < %zu)\n",
1488465def4SGreg Kroah-Hartman 			get_descriptor_type_string(desc_header->type),
1498465def4SGreg Kroah-Hartman 			desc_size, expected_size);
1508465def4SGreg Kroah-Hartman 		return -EINVAL;
1518465def4SGreg Kroah-Hartman 	}
1528465def4SGreg Kroah-Hartman 
1538465def4SGreg Kroah-Hartman 	/* Descriptor bigger than what we expect */
1548465def4SGreg Kroah-Hartman 	if (desc_size > expected_size) {
1558465def4SGreg Kroah-Hartman 		dev_warn(&intf->dev, "%s descriptor size mismatch (want %zu got %zu)\n",
1568465def4SGreg Kroah-Hartman 			 get_descriptor_type_string(desc_header->type),
1578465def4SGreg Kroah-Hartman 			 expected_size, desc_size);
1588465def4SGreg Kroah-Hartman 	}
1598465def4SGreg Kroah-Hartman 
1608465def4SGreg Kroah-Hartman 	descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
1618465def4SGreg Kroah-Hartman 	if (!descriptor)
1628465def4SGreg Kroah-Hartman 		return -ENOMEM;
1638465def4SGreg Kroah-Hartman 
1648465def4SGreg Kroah-Hartman 	descriptor->size = desc_size;
1658465def4SGreg Kroah-Hartman 	descriptor->data = (char *)desc + sizeof(*desc_header);
1668465def4SGreg Kroah-Hartman 	descriptor->type = desc_header->type;
1678465def4SGreg Kroah-Hartman 	list_add_tail(&descriptor->links, &intf->manifest_descs);
1688465def4SGreg Kroah-Hartman 
1698465def4SGreg Kroah-Hartman 	/* desc_size is positive and is known to fit in a signed int */
1708465def4SGreg Kroah-Hartman 
1718465def4SGreg Kroah-Hartman 	return desc_size;
1728465def4SGreg Kroah-Hartman }
1738465def4SGreg Kroah-Hartman 
1748465def4SGreg Kroah-Hartman /*
1758465def4SGreg Kroah-Hartman  * Find the string descriptor having the given id, validate it, and
1768465def4SGreg Kroah-Hartman  * allocate a duplicate copy of it.  The duplicate has an extra byte
1778465def4SGreg Kroah-Hartman  * which guarantees the returned string is NUL-terminated.
1788465def4SGreg Kroah-Hartman  *
1798465def4SGreg Kroah-Hartman  * String index 0 is valid (it represents "no string"), and for
1808465def4SGreg Kroah-Hartman  * that a null pointer is returned.
1818465def4SGreg Kroah-Hartman  *
1828465def4SGreg Kroah-Hartman  * Otherwise returns a pointer to a newly-allocated copy of the
1838465def4SGreg Kroah-Hartman  * descriptor string, or an error-coded pointer on failure.
1848465def4SGreg Kroah-Hartman  */
gb_string_get(struct gb_interface * intf,u8 string_id)1858465def4SGreg Kroah-Hartman static char *gb_string_get(struct gb_interface *intf, u8 string_id)
1868465def4SGreg Kroah-Hartman {
1878465def4SGreg Kroah-Hartman 	struct greybus_descriptor_string *desc_string;
1888465def4SGreg Kroah-Hartman 	struct manifest_desc *descriptor;
1898465def4SGreg Kroah-Hartman 	bool found = false;
1908465def4SGreg Kroah-Hartman 	char *string;
1918465def4SGreg Kroah-Hartman 
1928465def4SGreg Kroah-Hartman 	/* A zero string id means no string (but no error) */
1938465def4SGreg Kroah-Hartman 	if (!string_id)
1948465def4SGreg Kroah-Hartman 		return NULL;
1958465def4SGreg Kroah-Hartman 
1968465def4SGreg Kroah-Hartman 	list_for_each_entry(descriptor, &intf->manifest_descs, links) {
1978465def4SGreg Kroah-Hartman 		if (descriptor->type != GREYBUS_TYPE_STRING)
1988465def4SGreg Kroah-Hartman 			continue;
1998465def4SGreg Kroah-Hartman 
2008465def4SGreg Kroah-Hartman 		desc_string = descriptor->data;
2018465def4SGreg Kroah-Hartman 		if (desc_string->id == string_id) {
2028465def4SGreg Kroah-Hartman 			found = true;
2038465def4SGreg Kroah-Hartman 			break;
2048465def4SGreg Kroah-Hartman 		}
2058465def4SGreg Kroah-Hartman 	}
2068465def4SGreg Kroah-Hartman 	if (!found)
2078465def4SGreg Kroah-Hartman 		return ERR_PTR(-ENOENT);
2088465def4SGreg Kroah-Hartman 
2098465def4SGreg Kroah-Hartman 	/* Allocate an extra byte so we can guarantee it's NUL-terminated */
2108465def4SGreg Kroah-Hartman 	string = kmemdup(&desc_string->string, desc_string->length + 1,
2118465def4SGreg Kroah-Hartman 			 GFP_KERNEL);
2128465def4SGreg Kroah-Hartman 	if (!string)
2138465def4SGreg Kroah-Hartman 		return ERR_PTR(-ENOMEM);
2148465def4SGreg Kroah-Hartman 	string[desc_string->length] = '\0';
2158465def4SGreg Kroah-Hartman 
2168465def4SGreg Kroah-Hartman 	/* Ok we've used this string, so we're done with it */
2178465def4SGreg Kroah-Hartman 	release_manifest_descriptor(descriptor);
2188465def4SGreg Kroah-Hartman 
2198465def4SGreg Kroah-Hartman 	return string;
2208465def4SGreg Kroah-Hartman }
2218465def4SGreg Kroah-Hartman 
2228465def4SGreg Kroah-Hartman /*
2238465def4SGreg Kroah-Hartman  * Find cport descriptors in the manifest associated with the given
2248465def4SGreg Kroah-Hartman  * bundle, and set up data structures for the functions that use
2258465def4SGreg Kroah-Hartman  * them.  Returns the number of cports set up for the bundle, or 0
2268465def4SGreg Kroah-Hartman  * if there is an error.
2278465def4SGreg Kroah-Hartman  */
gb_manifest_parse_cports(struct gb_bundle * bundle)2288465def4SGreg Kroah-Hartman static u32 gb_manifest_parse_cports(struct gb_bundle *bundle)
2298465def4SGreg Kroah-Hartman {
2308465def4SGreg Kroah-Hartman 	struct gb_interface *intf = bundle->intf;
2318465def4SGreg Kroah-Hartman 	struct greybus_descriptor_cport *desc_cport;
2328465def4SGreg Kroah-Hartman 	struct manifest_desc *desc, *next, *tmp;
2338465def4SGreg Kroah-Hartman 	LIST_HEAD(list);
2348465def4SGreg Kroah-Hartman 	u8 bundle_id = bundle->id;
2358465def4SGreg Kroah-Hartman 	u16 cport_id;
2368465def4SGreg Kroah-Hartman 	u32 count = 0;
2378465def4SGreg Kroah-Hartman 	int i;
2388465def4SGreg Kroah-Hartman 
2398465def4SGreg Kroah-Hartman 	/* Set up all cport descriptors associated with this bundle */
2408465def4SGreg Kroah-Hartman 	list_for_each_entry_safe(desc, next, &intf->manifest_descs, links) {
2418465def4SGreg Kroah-Hartman 		if (desc->type != GREYBUS_TYPE_CPORT)
2428465def4SGreg Kroah-Hartman 			continue;
2438465def4SGreg Kroah-Hartman 
2448465def4SGreg Kroah-Hartman 		desc_cport = desc->data;
2458465def4SGreg Kroah-Hartman 		if (desc_cport->bundle != bundle_id)
2468465def4SGreg Kroah-Hartman 			continue;
2478465def4SGreg Kroah-Hartman 
2488465def4SGreg Kroah-Hartman 		cport_id = le16_to_cpu(desc_cport->id);
2498465def4SGreg Kroah-Hartman 		if (cport_id > CPORT_ID_MAX)
2508465def4SGreg Kroah-Hartman 			goto exit;
2518465def4SGreg Kroah-Hartman 
2528465def4SGreg Kroah-Hartman 		/* Nothing else should have its cport_id as control cport id */
2538465def4SGreg Kroah-Hartman 		if (cport_id == GB_CONTROL_CPORT_ID) {
2548465def4SGreg Kroah-Hartman 			dev_err(&bundle->dev, "invalid cport id found (%02u)\n",
2558465def4SGreg Kroah-Hartman 				cport_id);
2568465def4SGreg Kroah-Hartman 			goto exit;
2578465def4SGreg Kroah-Hartman 		}
2588465def4SGreg Kroah-Hartman 
2598465def4SGreg Kroah-Hartman 		/*
2608465def4SGreg Kroah-Hartman 		 * Found one, move it to our temporary list after checking for
2618465def4SGreg Kroah-Hartman 		 * duplicates.
2628465def4SGreg Kroah-Hartman 		 */
2638465def4SGreg Kroah-Hartman 		list_for_each_entry(tmp, &list, links) {
2648465def4SGreg Kroah-Hartman 			desc_cport = tmp->data;
2658465def4SGreg Kroah-Hartman 			if (cport_id == le16_to_cpu(desc_cport->id)) {
2668465def4SGreg Kroah-Hartman 				dev_err(&bundle->dev,
2678465def4SGreg Kroah-Hartman 					"duplicate CPort %u found\n", cport_id);
2688465def4SGreg Kroah-Hartman 				goto exit;
2698465def4SGreg Kroah-Hartman 			}
2708465def4SGreg Kroah-Hartman 		}
2718465def4SGreg Kroah-Hartman 		list_move_tail(&desc->links, &list);
2728465def4SGreg Kroah-Hartman 		count++;
2738465def4SGreg Kroah-Hartman 	}
2748465def4SGreg Kroah-Hartman 
2758465def4SGreg Kroah-Hartman 	if (!count)
2768465def4SGreg Kroah-Hartman 		return 0;
2778465def4SGreg Kroah-Hartman 
2788465def4SGreg Kroah-Hartman 	bundle->cport_desc = kcalloc(count, sizeof(*bundle->cport_desc),
2798465def4SGreg Kroah-Hartman 				     GFP_KERNEL);
2808465def4SGreg Kroah-Hartman 	if (!bundle->cport_desc)
2818465def4SGreg Kroah-Hartman 		goto exit;
2828465def4SGreg Kroah-Hartman 
2838465def4SGreg Kroah-Hartman 	bundle->num_cports = count;
2848465def4SGreg Kroah-Hartman 
2858465def4SGreg Kroah-Hartman 	i = 0;
2868465def4SGreg Kroah-Hartman 	list_for_each_entry_safe(desc, next, &list, links) {
2878465def4SGreg Kroah-Hartman 		desc_cport = desc->data;
2888465def4SGreg Kroah-Hartman 		memcpy(&bundle->cport_desc[i++], desc_cport,
2898465def4SGreg Kroah-Hartman 		       sizeof(*desc_cport));
2908465def4SGreg Kroah-Hartman 
2918465def4SGreg Kroah-Hartman 		/* Release the cport descriptor */
2928465def4SGreg Kroah-Hartman 		release_manifest_descriptor(desc);
2938465def4SGreg Kroah-Hartman 	}
2948465def4SGreg Kroah-Hartman 
2958465def4SGreg Kroah-Hartman 	return count;
2968465def4SGreg Kroah-Hartman exit:
2978465def4SGreg Kroah-Hartman 	release_cport_descriptors(&list, bundle_id);
2988465def4SGreg Kroah-Hartman 	/*
2998465def4SGreg Kroah-Hartman 	 * Free all cports for this bundle to avoid 'excess descriptors'
3008465def4SGreg Kroah-Hartman 	 * warnings.
3018465def4SGreg Kroah-Hartman 	 */
3028465def4SGreg Kroah-Hartman 	release_cport_descriptors(&intf->manifest_descs, bundle_id);
3038465def4SGreg Kroah-Hartman 
3048465def4SGreg Kroah-Hartman 	return 0;	/* Error; count should also be 0 */
3058465def4SGreg Kroah-Hartman }
3068465def4SGreg Kroah-Hartman 
3078465def4SGreg Kroah-Hartman /*
3088465def4SGreg Kroah-Hartman  * Find bundle descriptors in the manifest and set up their data
3098465def4SGreg Kroah-Hartman  * structures.  Returns the number of bundles set up for the
3108465def4SGreg Kroah-Hartman  * given interface.
3118465def4SGreg Kroah-Hartman  */
gb_manifest_parse_bundles(struct gb_interface * intf)3128465def4SGreg Kroah-Hartman static u32 gb_manifest_parse_bundles(struct gb_interface *intf)
3138465def4SGreg Kroah-Hartman {
3148465def4SGreg Kroah-Hartman 	struct manifest_desc *desc;
3158465def4SGreg Kroah-Hartman 	struct gb_bundle *bundle;
3168465def4SGreg Kroah-Hartman 	struct gb_bundle *bundle_next;
3178465def4SGreg Kroah-Hartman 	u32 count = 0;
3188465def4SGreg Kroah-Hartman 	u8 bundle_id;
3198465def4SGreg Kroah-Hartman 	u8 class;
3208465def4SGreg Kroah-Hartman 
3218465def4SGreg Kroah-Hartman 	while ((desc = get_next_bundle_desc(intf))) {
3228465def4SGreg Kroah-Hartman 		struct greybus_descriptor_bundle *desc_bundle;
3238465def4SGreg Kroah-Hartman 
3248465def4SGreg Kroah-Hartman 		/* Found one.  Set up its bundle structure*/
3258465def4SGreg Kroah-Hartman 		desc_bundle = desc->data;
3268465def4SGreg Kroah-Hartman 		bundle_id = desc_bundle->id;
3278465def4SGreg Kroah-Hartman 		class = desc_bundle->class;
3288465def4SGreg Kroah-Hartman 
3298465def4SGreg Kroah-Hartman 		/* Done with this bundle descriptor */
3308465def4SGreg Kroah-Hartman 		release_manifest_descriptor(desc);
3318465def4SGreg Kroah-Hartman 
3328465def4SGreg Kroah-Hartman 		/* Ignore any legacy control bundles */
3338465def4SGreg Kroah-Hartman 		if (bundle_id == GB_CONTROL_BUNDLE_ID) {
3348465def4SGreg Kroah-Hartman 			dev_dbg(&intf->dev, "%s - ignoring control bundle\n",
3358465def4SGreg Kroah-Hartman 				__func__);
3368465def4SGreg Kroah-Hartman 			release_cport_descriptors(&intf->manifest_descs,
3378465def4SGreg Kroah-Hartman 						  bundle_id);
3388465def4SGreg Kroah-Hartman 			continue;
3398465def4SGreg Kroah-Hartman 		}
3408465def4SGreg Kroah-Hartman 
3418465def4SGreg Kroah-Hartman 		/* Nothing else should have its class set to control class */
3428465def4SGreg Kroah-Hartman 		if (class == GREYBUS_CLASS_CONTROL) {
3438465def4SGreg Kroah-Hartman 			dev_err(&intf->dev,
3448465def4SGreg Kroah-Hartman 				"bundle %u cannot use control class\n",
3458465def4SGreg Kroah-Hartman 				bundle_id);
3468465def4SGreg Kroah-Hartman 			goto cleanup;
3478465def4SGreg Kroah-Hartman 		}
3488465def4SGreg Kroah-Hartman 
3498465def4SGreg Kroah-Hartman 		bundle = gb_bundle_create(intf, bundle_id, class);
3508465def4SGreg Kroah-Hartman 		if (!bundle)
3518465def4SGreg Kroah-Hartman 			goto cleanup;
3528465def4SGreg Kroah-Hartman 
3538465def4SGreg Kroah-Hartman 		/*
3548465def4SGreg Kroah-Hartman 		 * Now go set up this bundle's functions and cports.
3558465def4SGreg Kroah-Hartman 		 *
3568465def4SGreg Kroah-Hartman 		 * A 'bundle' represents a device in greybus. It may require
3578465def4SGreg Kroah-Hartman 		 * multiple cports for its functioning. If we fail to setup any
3588465def4SGreg Kroah-Hartman 		 * cport of a bundle, we better reject the complete bundle as
3598465def4SGreg Kroah-Hartman 		 * the device may not be able to function properly then.
3608465def4SGreg Kroah-Hartman 		 *
3618465def4SGreg Kroah-Hartman 		 * But, failing to setup a cport of bundle X doesn't mean that
3628465def4SGreg Kroah-Hartman 		 * the device corresponding to bundle Y will not work properly.
3638465def4SGreg Kroah-Hartman 		 * Bundles should be treated as separate independent devices.
3648465def4SGreg Kroah-Hartman 		 *
3658465def4SGreg Kroah-Hartman 		 * While parsing manifest for an interface, treat bundles as
3668465def4SGreg Kroah-Hartman 		 * separate entities and don't reject entire interface and its
3678465def4SGreg Kroah-Hartman 		 * bundles on failing to initialize a cport. But make sure the
3688465def4SGreg Kroah-Hartman 		 * bundle which needs the cport, gets destroyed properly.
3698465def4SGreg Kroah-Hartman 		 */
3708465def4SGreg Kroah-Hartman 		if (!gb_manifest_parse_cports(bundle)) {
3718465def4SGreg Kroah-Hartman 			gb_bundle_destroy(bundle);
3728465def4SGreg Kroah-Hartman 			continue;
3738465def4SGreg Kroah-Hartman 		}
3748465def4SGreg Kroah-Hartman 
3758465def4SGreg Kroah-Hartman 		count++;
3768465def4SGreg Kroah-Hartman 	}
3778465def4SGreg Kroah-Hartman 
3788465def4SGreg Kroah-Hartman 	return count;
3798465def4SGreg Kroah-Hartman cleanup:
3808465def4SGreg Kroah-Hartman 	/* An error occurred; undo any changes we've made */
3818465def4SGreg Kroah-Hartman 	list_for_each_entry_safe(bundle, bundle_next, &intf->bundles, links) {
3828465def4SGreg Kroah-Hartman 		gb_bundle_destroy(bundle);
3838465def4SGreg Kroah-Hartman 		count--;
3848465def4SGreg Kroah-Hartman 	}
3858465def4SGreg Kroah-Hartman 	return 0;	/* Error; count should also be 0 */
3868465def4SGreg Kroah-Hartman }
3878465def4SGreg Kroah-Hartman 
gb_manifest_parse_interface(struct gb_interface * intf,struct manifest_desc * interface_desc)3888465def4SGreg Kroah-Hartman static bool gb_manifest_parse_interface(struct gb_interface *intf,
3898465def4SGreg Kroah-Hartman 					struct manifest_desc *interface_desc)
3908465def4SGreg Kroah-Hartman {
3918465def4SGreg Kroah-Hartman 	struct greybus_descriptor_interface *desc_intf = interface_desc->data;
3928465def4SGreg Kroah-Hartman 	struct gb_control *control = intf->control;
3938465def4SGreg Kroah-Hartman 	char *str;
3948465def4SGreg Kroah-Hartman 
3958465def4SGreg Kroah-Hartman 	/* Handle the strings first--they can fail */
3968465def4SGreg Kroah-Hartman 	str = gb_string_get(intf, desc_intf->vendor_stringid);
3978465def4SGreg Kroah-Hartman 	if (IS_ERR(str))
3988465def4SGreg Kroah-Hartman 		return false;
3998465def4SGreg Kroah-Hartman 	control->vendor_string = str;
4008465def4SGreg Kroah-Hartman 
4018465def4SGreg Kroah-Hartman 	str = gb_string_get(intf, desc_intf->product_stringid);
4028465def4SGreg Kroah-Hartman 	if (IS_ERR(str))
4038465def4SGreg Kroah-Hartman 		goto out_free_vendor_string;
4048465def4SGreg Kroah-Hartman 	control->product_string = str;
4058465def4SGreg Kroah-Hartman 
4068465def4SGreg Kroah-Hartman 	/* Assign feature flags communicated via manifest */
4078465def4SGreg Kroah-Hartman 	intf->features = desc_intf->features;
4088465def4SGreg Kroah-Hartman 
4098465def4SGreg Kroah-Hartman 	/* Release the interface descriptor, now that we're done with it */
4108465def4SGreg Kroah-Hartman 	release_manifest_descriptor(interface_desc);
4118465def4SGreg Kroah-Hartman 
4128465def4SGreg Kroah-Hartman 	/* An interface must have at least one bundle descriptor */
4138465def4SGreg Kroah-Hartman 	if (!gb_manifest_parse_bundles(intf)) {
4148465def4SGreg Kroah-Hartman 		dev_err(&intf->dev, "manifest bundle descriptors not valid\n");
4158465def4SGreg Kroah-Hartman 		goto out_err;
4168465def4SGreg Kroah-Hartman 	}
4178465def4SGreg Kroah-Hartman 
4188465def4SGreg Kroah-Hartman 	return true;
4198465def4SGreg Kroah-Hartman out_err:
4208465def4SGreg Kroah-Hartman 	kfree(control->product_string);
4218465def4SGreg Kroah-Hartman 	control->product_string = NULL;
4228465def4SGreg Kroah-Hartman out_free_vendor_string:
4238465def4SGreg Kroah-Hartman 	kfree(control->vendor_string);
4248465def4SGreg Kroah-Hartman 	control->vendor_string = NULL;
4258465def4SGreg Kroah-Hartman 
4268465def4SGreg Kroah-Hartman 	return false;
4278465def4SGreg Kroah-Hartman }
4288465def4SGreg Kroah-Hartman 
4298465def4SGreg Kroah-Hartman /*
4308465def4SGreg Kroah-Hartman  * Parse a buffer containing an interface manifest.
4318465def4SGreg Kroah-Hartman  *
4328465def4SGreg Kroah-Hartman  * If we find anything wrong with the content/format of the buffer
4338465def4SGreg Kroah-Hartman  * we reject it.
4348465def4SGreg Kroah-Hartman  *
4358465def4SGreg Kroah-Hartman  * The first requirement is that the manifest's version is
4368465def4SGreg Kroah-Hartman  * one we can parse.
4378465def4SGreg Kroah-Hartman  *
4388465def4SGreg Kroah-Hartman  * We make an initial pass through the buffer and identify all of
4398465def4SGreg Kroah-Hartman  * the descriptors it contains, keeping track for each its type
4408465def4SGreg Kroah-Hartman  * and the location size of its data in the buffer.
4418465def4SGreg Kroah-Hartman  *
4428465def4SGreg Kroah-Hartman  * Next we scan the descriptors, looking for an interface descriptor;
4438465def4SGreg Kroah-Hartman  * there must be exactly one of those.  When found, we record the
4448465def4SGreg Kroah-Hartman  * information it contains, and then remove that descriptor (and any
4458465def4SGreg Kroah-Hartman  * string descriptors it refers to) from further consideration.
4468465def4SGreg Kroah-Hartman  *
4478465def4SGreg Kroah-Hartman  * After that we look for the interface's bundles--there must be at
4488465def4SGreg Kroah-Hartman  * least one of those.
4498465def4SGreg Kroah-Hartman  *
4508465def4SGreg Kroah-Hartman  * Returns true if parsing was successful, false otherwise.
4518465def4SGreg Kroah-Hartman  */
gb_manifest_parse(struct gb_interface * intf,void * data,size_t size)4528465def4SGreg Kroah-Hartman bool gb_manifest_parse(struct gb_interface *intf, void *data, size_t size)
4538465def4SGreg Kroah-Hartman {
4548465def4SGreg Kroah-Hartman 	struct greybus_manifest *manifest;
4558465def4SGreg Kroah-Hartman 	struct greybus_manifest_header *header;
4568465def4SGreg Kroah-Hartman 	struct greybus_descriptor *desc;
4578465def4SGreg Kroah-Hartman 	struct manifest_desc *descriptor;
4588465def4SGreg Kroah-Hartman 	struct manifest_desc *interface_desc = NULL;
4598465def4SGreg Kroah-Hartman 	u16 manifest_size;
4608465def4SGreg Kroah-Hartman 	u32 found = 0;
4618465def4SGreg Kroah-Hartman 	bool result;
4628465def4SGreg Kroah-Hartman 
4638465def4SGreg Kroah-Hartman 	/* Manifest descriptor list should be empty here */
4648465def4SGreg Kroah-Hartman 	if (WARN_ON(!list_empty(&intf->manifest_descs)))
4658465def4SGreg Kroah-Hartman 		return false;
4668465def4SGreg Kroah-Hartman 
4678465def4SGreg Kroah-Hartman 	/* we have to have at _least_ the manifest header */
4688465def4SGreg Kroah-Hartman 	if (size < sizeof(*header)) {
4698465def4SGreg Kroah-Hartman 		dev_err(&intf->dev, "short manifest (%zu < %zu)\n",
4708465def4SGreg Kroah-Hartman 			size, sizeof(*header));
4718465def4SGreg Kroah-Hartman 		return false;
4728465def4SGreg Kroah-Hartman 	}
4738465def4SGreg Kroah-Hartman 
4748465def4SGreg Kroah-Hartman 	/* Make sure the size is right */
4758465def4SGreg Kroah-Hartman 	manifest = data;
4768465def4SGreg Kroah-Hartman 	header = &manifest->header;
4778465def4SGreg Kroah-Hartman 	manifest_size = le16_to_cpu(header->size);
4788465def4SGreg Kroah-Hartman 	if (manifest_size != size) {
4798465def4SGreg Kroah-Hartman 		dev_err(&intf->dev, "manifest size mismatch (%zu != %u)\n",
4808465def4SGreg Kroah-Hartman 			size, manifest_size);
4818465def4SGreg Kroah-Hartman 		return false;
4828465def4SGreg Kroah-Hartman 	}
4838465def4SGreg Kroah-Hartman 
4848465def4SGreg Kroah-Hartman 	/* Validate major/minor number */
4858465def4SGreg Kroah-Hartman 	if (header->version_major > GREYBUS_VERSION_MAJOR) {
4868465def4SGreg Kroah-Hartman 		dev_err(&intf->dev, "manifest version too new (%u.%u > %u.%u)\n",
4878465def4SGreg Kroah-Hartman 			header->version_major, header->version_minor,
4888465def4SGreg Kroah-Hartman 			GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
4898465def4SGreg Kroah-Hartman 		return false;
4908465def4SGreg Kroah-Hartman 	}
4918465def4SGreg Kroah-Hartman 
4928465def4SGreg Kroah-Hartman 	/* OK, find all the descriptors */
4938465def4SGreg Kroah-Hartman 	desc = manifest->descriptors;
4948465def4SGreg Kroah-Hartman 	size -= sizeof(*header);
4958465def4SGreg Kroah-Hartman 	while (size) {
4968465def4SGreg Kroah-Hartman 		int desc_size;
4978465def4SGreg Kroah-Hartman 
4988465def4SGreg Kroah-Hartman 		desc_size = identify_descriptor(intf, desc, size);
4998465def4SGreg Kroah-Hartman 		if (desc_size < 0) {
5008465def4SGreg Kroah-Hartman 			result = false;
5018465def4SGreg Kroah-Hartman 			goto out;
5028465def4SGreg Kroah-Hartman 		}
5038465def4SGreg Kroah-Hartman 		desc = (struct greybus_descriptor *)((char *)desc + desc_size);
5048465def4SGreg Kroah-Hartman 		size -= desc_size;
5058465def4SGreg Kroah-Hartman 	}
5068465def4SGreg Kroah-Hartman 
5078465def4SGreg Kroah-Hartman 	/* There must be a single interface descriptor */
5088465def4SGreg Kroah-Hartman 	list_for_each_entry(descriptor, &intf->manifest_descs, links) {
5098465def4SGreg Kroah-Hartman 		if (descriptor->type == GREYBUS_TYPE_INTERFACE)
5108465def4SGreg Kroah-Hartman 			if (!found++)
5118465def4SGreg Kroah-Hartman 				interface_desc = descriptor;
5128465def4SGreg Kroah-Hartman 	}
5138465def4SGreg Kroah-Hartman 	if (found != 1) {
5148465def4SGreg Kroah-Hartman 		dev_err(&intf->dev, "manifest must have 1 interface descriptor (%u found)\n",
5158465def4SGreg Kroah-Hartman 			found);
5168465def4SGreg Kroah-Hartman 		result = false;
5178465def4SGreg Kroah-Hartman 		goto out;
5188465def4SGreg Kroah-Hartman 	}
5198465def4SGreg Kroah-Hartman 
5208465def4SGreg Kroah-Hartman 	/* Parse the manifest, starting with the interface descriptor */
5218465def4SGreg Kroah-Hartman 	result = gb_manifest_parse_interface(intf, interface_desc);
5228465def4SGreg Kroah-Hartman 
5238465def4SGreg Kroah-Hartman 	/*
5248465def4SGreg Kroah-Hartman 	 * We really should have no remaining descriptors, but we
5258465def4SGreg Kroah-Hartman 	 * don't know what newer format manifests might leave.
5268465def4SGreg Kroah-Hartman 	 */
5278465def4SGreg Kroah-Hartman 	if (result && !list_empty(&intf->manifest_descs))
5288465def4SGreg Kroah-Hartman 		dev_info(&intf->dev, "excess descriptors in interface manifest\n");
5298465def4SGreg Kroah-Hartman out:
5308465def4SGreg Kroah-Hartman 	release_manifest_descriptors(intf);
5318465def4SGreg Kroah-Hartman 
5328465def4SGreg Kroah-Hartman 	return result;
5338465def4SGreg Kroah-Hartman }
534