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