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