908bbe6feb615998ae881073092e8ac129c975ab
[linux-2.6-microblaze.git] / drivers / gpu / drm / drm_displayid.c
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5
6 #include <drm/drm_displayid.h>
7 #include <drm/drm_edid.h>
8 #include <drm/drm_print.h>
9
10 static int validate_displayid(const u8 *displayid, int length, int idx)
11 {
12         int i, dispid_length;
13         u8 csum = 0;
14         const struct displayid_hdr *base;
15
16         base = (const struct displayid_hdr *)&displayid[idx];
17
18         DRM_DEBUG_KMS("base revision 0x%x, length %d, %d %d\n",
19                       base->rev, base->bytes, base->prod_id, base->ext_count);
20
21         /* +1 for DispID checksum */
22         dispid_length = sizeof(*base) + base->bytes + 1;
23         if (dispid_length > length - idx)
24                 return -EINVAL;
25
26         for (i = 0; i < dispid_length; i++)
27                 csum += displayid[idx + i];
28         if (csum) {
29                 DRM_NOTE("DisplayID checksum invalid, remainder is %d\n", csum);
30                 return -EINVAL;
31         }
32
33         return 0;
34 }
35
36 const u8 *drm_find_displayid_extension(const struct edid *edid,
37                                        int *length, int *idx,
38                                        int *ext_index)
39 {
40         const u8 *displayid = drm_find_edid_extension(edid, DISPLAYID_EXT, ext_index);
41         const struct displayid_hdr *base;
42         int ret;
43
44         if (!displayid)
45                 return NULL;
46
47         /* EDID extensions block checksum isn't for us */
48         *length = EDID_LENGTH - 1;
49         *idx = 1;
50
51         ret = validate_displayid(displayid, *length, *idx);
52         if (ret)
53                 return NULL;
54
55         base = (const struct displayid_hdr *)&displayid[*idx];
56         *length = *idx + sizeof(*base) + base->bytes;
57
58         return displayid;
59 }