1 /*
2  * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above
10  *       copyright notice, this list of conditions and the following
11  *       disclaimer in the documentation and/or other materials provided
12  *       with the distribution.
13  *     * Neither the name of The Linux Foundation nor the names of its
14  *       contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "qd_utils.h"
31 
32 namespace qdutils {
33 
parseLine(char * input,char * tokens[],const uint32_t maxToken,uint32_t * count)34 int parseLine(char *input, char *tokens[], const uint32_t maxToken, uint32_t *count) {
35     char *tmpToken = NULL;
36     char *tmpPtr;
37     uint32_t index = 0;
38     const char *delim = ", =\n";
39     if (!input) {
40       return -1;
41     }
42     tmpToken = strtok_r(input, delim, &tmpPtr);
43     while (tmpToken && index < maxToken) {
44       tokens[index++] = tmpToken;
45       tmpToken = strtok_r(NULL, delim, &tmpPtr);
46     }
47     *count = index;
48 
49     return 0;
50 }
51 
querySDEInfo(HWQueryType type,int * value)52 int querySDEInfo(HWQueryType type, int *value) {
53     FILE *fileptr = NULL;
54     const char *featureName;
55     char stringBuffer[MAX_STRING_LENGTH];
56     uint32_t tokenCount = 0;
57     const uint32_t maxCount = 10;
58     char *tokens[maxCount] = { NULL };
59 
60     switch(type) {
61     case HAS_MACRO_TILE:
62         featureName = "tile_format";
63         break;
64 
65     default:
66         ALOGE("Invalid query type %d", type);
67         return -EINVAL;
68     }
69 
70     fileptr = fopen("/sys/devices/virtual/graphics/fb0/mdp/caps", "rb");
71     if (!fileptr) {
72         ALOGE("File '%s' not found", stringBuffer);
73         return -EINVAL;
74     }
75 
76     size_t len = MAX_STRING_LENGTH;
77     ssize_t read;
78     char *line = stringBuffer;
79     while ((read = getline(&line, &len, fileptr)) != -1) {
80         // parse the line and update information accordingly
81         if (parseLine(line, tokens, maxCount, &tokenCount)) {
82             continue;
83         }
84 
85         if (strncmp(tokens[0], "features", strlen("features"))) {
86             continue;
87         }
88 
89         for (uint32_t i = 0; i < tokenCount; i++) {
90             if (!strncmp(tokens[i], featureName, strlen(featureName))) {
91               *value = 1;
92             }
93         }
94     }
95     fclose(fileptr);
96 
97     return 0;
98 }
99 
getHDMINode(void)100 int getHDMINode(void)
101 {
102     FILE *displayDeviceFP = NULL;
103     char fbType[MAX_FRAME_BUFFER_NAME_SIZE];
104     char msmFbTypePath[MAX_FRAME_BUFFER_NAME_SIZE];
105     int j = 0;
106 
107     for(j = 0; j < HWC_NUM_DISPLAY_TYPES; j++) {
108         snprintf (msmFbTypePath, sizeof(msmFbTypePath),
109                   "/sys/class/graphics/fb%d/msm_fb_type", j);
110         displayDeviceFP = fopen(msmFbTypePath, "r");
111         if(displayDeviceFP) {
112             fread(fbType, sizeof(char), MAX_FRAME_BUFFER_NAME_SIZE,
113                     displayDeviceFP);
114             if(strncmp(fbType, "dtv panel", strlen("dtv panel")) == 0) {
115                 ALOGD("%s: HDMI is at fb%d", __func__, j);
116                 fclose(displayDeviceFP);
117                 break;
118             }
119             fclose(displayDeviceFP);
120         } else {
121             ALOGE("%s: Failed to open fb node %d", __func__, j);
122         }
123     }
124 
125     if (j < HWC_NUM_DISPLAY_TYPES)
126         return j;
127     else
128         ALOGE("%s: Failed to find HDMI node", __func__);
129 
130     return -1;
131 }
132 
getEdidRawData(char * buffer)133 int getEdidRawData(char *buffer)
134 {
135     int size;
136     int edidFile;
137     char msmFbTypePath[MAX_FRAME_BUFFER_NAME_SIZE];
138     int node_id = getHDMINode();
139 
140     if (node_id < 0) {
141         ALOGE("%s no HDMI node found", __func__);
142         return 0;
143     }
144 
145     snprintf(msmFbTypePath, sizeof(msmFbTypePath),
146                  "/sys/class/graphics/fb%d/edid_raw_data", node_id);
147 
148     edidFile = open(msmFbTypePath, O_RDONLY, 0);
149 
150     if (edidFile < 0) {
151         ALOGE("%s no edid raw data found", __func__);
152         return 0;
153     }
154 
155     size = (int)read(edidFile, (char*)buffer, EDID_RAW_DATA_SIZE);
156     close(edidFile);
157     return size;
158 }
159 
160 }; //namespace qdutils
161