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