Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Greybus manifest parsing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2014-2015 Google Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright 2014-2015 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/greybus.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) static const char *get_descriptor_type_string(u8 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	case GREYBUS_TYPE_INVALID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 		return "invalid";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	case GREYBUS_TYPE_STRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 		return "string";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	case GREYBUS_TYPE_INTERFACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 		return "interface";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	case GREYBUS_TYPE_CPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 		return "cport";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	case GREYBUS_TYPE_BUNDLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		return "bundle";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		return "unknown";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * We scan the manifest once to identify where all the descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * are.  The result is a list of these manifest_desc structures.  We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * then pick through them for what we're looking for (starting with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * the interface descriptor).  As each is processed we remove it from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * the list.  When we're done the list should (probably) be empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) struct manifest_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct list_head		links;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	size_t				size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	void				*data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	enum greybus_descriptor_type	type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static void release_manifest_descriptor(struct manifest_desc *descriptor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	list_del(&descriptor->links);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	kfree(descriptor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static void release_manifest_descriptors(struct gb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct manifest_desc *descriptor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct manifest_desc *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		release_manifest_descriptor(descriptor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static void release_cport_descriptors(struct list_head *head, u8 bundle_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct manifest_desc *desc, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct greybus_descriptor_cport *desc_cport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	list_for_each_entry_safe(desc, tmp, head, links) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		desc_cport = desc->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		if (desc->type != GREYBUS_TYPE_CPORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		if (desc_cport->bundle == bundle_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			release_manifest_descriptor(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static struct manifest_desc *get_next_bundle_desc(struct gb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	struct manifest_desc *descriptor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct manifest_desc *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	list_for_each_entry_safe(descriptor, next, &intf->manifest_descs, links)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		if (descriptor->type == GREYBUS_TYPE_BUNDLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			return descriptor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * Validate the given descriptor.  Its reported size must fit within
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * the number of bytes remaining, and it must have a recognized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * type.  Check that the reported size is at least as big as what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * we expect to see.  (It could be bigger, perhaps for a new version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * of the format.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * Returns the (non-zero) number of bytes consumed by the descriptor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  * or a negative errno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) static int identify_descriptor(struct gb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			       struct greybus_descriptor *desc, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct greybus_descriptor_header *desc_header = &desc->header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	struct manifest_desc *descriptor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	size_t desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	size_t expected_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (size < sizeof(*desc_header)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		dev_err(&intf->dev, "manifest too small (%zu < %zu)\n", size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			sizeof(*desc_header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		return -EINVAL;		/* Must at least have header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	desc_size = le16_to_cpu(desc_header->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (desc_size > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		dev_err(&intf->dev, "descriptor too big (%zu > %zu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			desc_size, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	/* Descriptor needs to at least have a header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	expected_size = sizeof(*desc_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	switch (desc_header->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	case GREYBUS_TYPE_STRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		expected_size += sizeof(struct greybus_descriptor_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		expected_size += desc->string.length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		/* String descriptors are padded to 4 byte boundaries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		expected_size = ALIGN(expected_size, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	case GREYBUS_TYPE_INTERFACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		expected_size += sizeof(struct greybus_descriptor_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	case GREYBUS_TYPE_BUNDLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		expected_size += sizeof(struct greybus_descriptor_bundle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	case GREYBUS_TYPE_CPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		expected_size += sizeof(struct greybus_descriptor_cport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	case GREYBUS_TYPE_INVALID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		dev_err(&intf->dev, "invalid descriptor type (%u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			desc_header->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (desc_size < expected_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		dev_err(&intf->dev, "%s descriptor too small (%zu < %zu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			get_descriptor_type_string(desc_header->type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			desc_size, expected_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	/* Descriptor bigger than what we expect */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (desc_size > expected_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		dev_warn(&intf->dev, "%s descriptor size mismatch (want %zu got %zu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			 get_descriptor_type_string(desc_header->type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			 expected_size, desc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	descriptor = kzalloc(sizeof(*descriptor), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (!descriptor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	descriptor->size = desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	descriptor->data = (char *)desc + sizeof(*desc_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	descriptor->type = desc_header->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	list_add_tail(&descriptor->links, &intf->manifest_descs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	/* desc_size is positive and is known to fit in a signed int */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	return desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  * Find the string descriptor having the given id, validate it, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * allocate a duplicate copy of it.  The duplicate has an extra byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  * which guarantees the returned string is NUL-terminated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  * String index 0 is valid (it represents "no string"), and for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  * that a null pointer is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  * Otherwise returns a pointer to a newly-allocated copy of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * descriptor string, or an error-coded pointer on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static char *gb_string_get(struct gb_interface *intf, u8 string_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct greybus_descriptor_string *desc_string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct manifest_desc *descriptor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	char *string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	/* A zero string id means no string (but no error) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (!string_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	list_for_each_entry(descriptor, &intf->manifest_descs, links) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		if (descriptor->type != GREYBUS_TYPE_STRING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		desc_string = descriptor->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		if (desc_string->id == string_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (!found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	/* Allocate an extra byte so we can guarantee it's NUL-terminated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	string = kmemdup(&desc_string->string, desc_string->length + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (!string)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	string[desc_string->length] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	/* Ok we've used this string, so we're done with it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	release_manifest_descriptor(descriptor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	return string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * Find cport descriptors in the manifest associated with the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  * bundle, and set up data structures for the functions that use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  * them.  Returns the number of cports set up for the bundle, or 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  * if there is an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static u32 gb_manifest_parse_cports(struct gb_bundle *bundle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	struct gb_interface *intf = bundle->intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	struct greybus_descriptor_cport *desc_cport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	struct manifest_desc *desc, *next, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	u8 bundle_id = bundle->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	u16 cport_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	u32 count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	/* Set up all cport descriptors associated with this bundle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	list_for_each_entry_safe(desc, next, &intf->manifest_descs, links) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		if (desc->type != GREYBUS_TYPE_CPORT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		desc_cport = desc->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		if (desc_cport->bundle != bundle_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		cport_id = le16_to_cpu(desc_cport->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		if (cport_id > CPORT_ID_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		/* Nothing else should have its cport_id as control cport id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		if (cport_id == GB_CONTROL_CPORT_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			dev_err(&bundle->dev, "invalid cport id found (%02u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 				cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		 * Found one, move it to our temporary list after checking for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		 * duplicates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		list_for_each_entry(tmp, &list, links) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			desc_cport = tmp->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			if (cport_id == le16_to_cpu(desc_cport->id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 				dev_err(&bundle->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 					"duplicate CPort %u found\n", cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 				goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		list_move_tail(&desc->links, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	bundle->cport_desc = kcalloc(count, sizeof(*bundle->cport_desc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 				     GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (!bundle->cport_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	bundle->num_cports = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	list_for_each_entry_safe(desc, next, &list, links) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		desc_cport = desc->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		memcpy(&bundle->cport_desc[i++], desc_cport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		       sizeof(*desc_cport));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		/* Release the cport descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		release_manifest_descriptor(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	release_cport_descriptors(&list, bundle_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	 * Free all cports for this bundle to avoid 'excess descriptors'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	 * warnings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	release_cport_descriptors(&intf->manifest_descs, bundle_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	return 0;	/* Error; count should also be 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  * Find bundle descriptors in the manifest and set up their data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  * structures.  Returns the number of bundles set up for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  * given interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static u32 gb_manifest_parse_bundles(struct gb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	struct manifest_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	struct gb_bundle *bundle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	struct gb_bundle *bundle_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	u32 count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	u8 bundle_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	u8 class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	while ((desc = get_next_bundle_desc(intf))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		struct greybus_descriptor_bundle *desc_bundle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		/* Found one.  Set up its bundle structure*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		desc_bundle = desc->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		bundle_id = desc_bundle->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		class = desc_bundle->class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		/* Done with this bundle descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		release_manifest_descriptor(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		/* Ignore any legacy control bundles */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		if (bundle_id == GB_CONTROL_BUNDLE_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			dev_dbg(&intf->dev, "%s - ignoring control bundle\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 				__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			release_cport_descriptors(&intf->manifest_descs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 						  bundle_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		/* Nothing else should have its class set to control class */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		if (class == GREYBUS_CLASS_CONTROL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			dev_err(&intf->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 				"bundle %u cannot use control class\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 				bundle_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		bundle = gb_bundle_create(intf, bundle_id, class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		if (!bundle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		 * Now go set up this bundle's functions and cports.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		 * A 'bundle' represents a device in greybus. It may require
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		 * multiple cports for its functioning. If we fail to setup any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		 * cport of a bundle, we better reject the complete bundle as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		 * the device may not be able to function properly then.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		 * But, failing to setup a cport of bundle X doesn't mean that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		 * the device corresponding to bundle Y will not work properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		 * Bundles should be treated as separate independent devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		 * While parsing manifest for an interface, treat bundles as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		 * separate entities and don't reject entire interface and its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		 * bundles on failing to initialize a cport. But make sure the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		 * bundle which needs the cport, gets destroyed properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		if (!gb_manifest_parse_cports(bundle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			gb_bundle_destroy(bundle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	/* An error occurred; undo any changes we've made */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	list_for_each_entry_safe(bundle, bundle_next, &intf->bundles, links) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		gb_bundle_destroy(bundle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	return 0;	/* Error; count should also be 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static bool gb_manifest_parse_interface(struct gb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 					struct manifest_desc *interface_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	struct greybus_descriptor_interface *desc_intf = interface_desc->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	struct gb_control *control = intf->control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	/* Handle the strings first--they can fail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	str = gb_string_get(intf, desc_intf->vendor_stringid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	if (IS_ERR(str))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	control->vendor_string = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	str = gb_string_get(intf, desc_intf->product_stringid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (IS_ERR(str))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		goto out_free_vendor_string;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	control->product_string = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	/* Assign feature flags communicated via manifest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	intf->features = desc_intf->features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	/* Release the interface descriptor, now that we're done with it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	release_manifest_descriptor(interface_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	/* An interface must have at least one bundle descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	if (!gb_manifest_parse_bundles(intf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		dev_err(&intf->dev, "manifest bundle descriptors not valid\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	kfree(control->product_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	control->product_string = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) out_free_vendor_string:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	kfree(control->vendor_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	control->vendor_string = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)  * Parse a buffer containing an interface manifest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)  * If we find anything wrong with the content/format of the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)  * we reject it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)  * The first requirement is that the manifest's version is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)  * one we can parse.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)  * We make an initial pass through the buffer and identify all of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)  * the descriptors it contains, keeping track for each its type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)  * and the location size of its data in the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)  * Next we scan the descriptors, looking for an interface descriptor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)  * there must be exactly one of those.  When found, we record the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)  * information it contains, and then remove that descriptor (and any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)  * string descriptors it refers to) from further consideration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)  * After that we look for the interface's bundles--there must be at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)  * least one of those.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)  * Returns true if parsing was successful, false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) bool gb_manifest_parse(struct gb_interface *intf, void *data, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	struct greybus_manifest *manifest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	struct greybus_manifest_header *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	struct greybus_descriptor *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	struct manifest_desc *descriptor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	struct manifest_desc *interface_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	u16 manifest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	u32 found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	bool result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	/* Manifest descriptor list should be empty here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (WARN_ON(!list_empty(&intf->manifest_descs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	/* we have to have at _least_ the manifest header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	if (size < sizeof(*header)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		dev_err(&intf->dev, "short manifest (%zu < %zu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			size, sizeof(*header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	/* Make sure the size is right */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	manifest = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	header = &manifest->header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	manifest_size = le16_to_cpu(header->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if (manifest_size != size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		dev_err(&intf->dev, "manifest size mismatch (%zu != %u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 			size, manifest_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	/* Validate major/minor number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	if (header->version_major > GREYBUS_VERSION_MAJOR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		dev_err(&intf->dev, "manifest version too new (%u.%u > %u.%u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			header->version_major, header->version_minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	/* OK, find all the descriptors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	desc = manifest->descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	size -= sizeof(*header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	while (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		int desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		desc_size = identify_descriptor(intf, desc, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		if (desc_size < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			result = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		desc = (struct greybus_descriptor *)((char *)desc + desc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		size -= desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	/* There must be a single interface descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	list_for_each_entry(descriptor, &intf->manifest_descs, links) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		if (descriptor->type == GREYBUS_TYPE_INTERFACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			if (!found++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 				interface_desc = descriptor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	if (found != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		dev_err(&intf->dev, "manifest must have 1 interface descriptor (%u found)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		result = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	/* Parse the manifest, starting with the interface descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	result = gb_manifest_parse_interface(intf, interface_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	 * We really should have no remaining descriptors, but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	 * don't know what newer format manifests might leave.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	if (result && !list_empty(&intf->manifest_descs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		dev_info(&intf->dev, "excess descriptors in interface manifest\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	release_manifest_descriptors(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }