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     case HAS_UBWC:
66         featureName = "ubwc";
67         break;
68 
69     default:
70         ALOGE("Invalid query type %d", type);
71         return -EINVAL;
72     }
73 
74     fileptr = fopen("/sys/devices/virtual/graphics/fb0/mdp/caps", "rb");
75     if (!fileptr) {
76         ALOGE("File '%s' not found", stringBuffer);
77         return -EINVAL;
78     }
79 
80     size_t len = MAX_STRING_LENGTH;
81     ssize_t read;
82     char *line = stringBuffer;
83     while ((read = getline(&line, &len, fileptr)) != -1) {
84         // parse the line and update information accordingly
85         if (parseLine(line, tokens, maxCount, &tokenCount)) {
86             continue;
87         }
88 
89         if (strncmp(tokens[0], "features", strlen("features"))) {
90             continue;
91         }
92 
93         for (uint32_t i = 0; i < tokenCount; i++) {
94             if (!strncmp(tokens[i], featureName, strlen(featureName))) {
95               *value = 1;
96             }
97         }
98     }
99     fclose(fileptr);
100 
101     return 0;
102 }
103 
getHDMINode(void)104 int getHDMINode(void)
105 {
106     FILE *displayDeviceFP = NULL;
107     char fbType[MAX_FRAME_BUFFER_NAME_SIZE];
108     char msmFbTypePath[MAX_FRAME_BUFFER_NAME_SIZE];
109     int j = 0;
110 
111     for(j = 0; j < HWC_NUM_DISPLAY_TYPES; j++) {
112         snprintf (msmFbTypePath, sizeof(msmFbTypePath),
113                   "/sys/class/graphics/fb%d/msm_fb_type", j);
114         displayDeviceFP = fopen(msmFbTypePath, "r");
115         if(displayDeviceFP) {
116             fread(fbType, sizeof(char), MAX_FRAME_BUFFER_NAME_SIZE,
117                     displayDeviceFP);
118             if(strncmp(fbType, "dtv panel", strlen("dtv panel")) == 0) {
119                 ALOGD("%s: HDMI is at fb%d", __func__, j);
120                 fclose(displayDeviceFP);
121                 break;
122             }
123             fclose(displayDeviceFP);
124         } else {
125             ALOGE("%s: Failed to open fb node %d", __func__, j);
126         }
127     }
128 
129     if (j < HWC_NUM_DISPLAY_TYPES)
130         return j;
131     else
132         ALOGE("%s: Failed to find HDMI node", __func__);
133 
134     return -1;
135 }
136 
getEdidRawData(char * buffer)137 int getEdidRawData(char *buffer)
138 {
139     int size;
140     int edidFile;
141     char msmFbTypePath[MAX_FRAME_BUFFER_NAME_SIZE];
142     int node_id = getHDMINode();
143 
144     if (node_id < 0) {
145         ALOGE("%s no HDMI node found", __func__);
146         return 0;
147     }
148 
149     snprintf(msmFbTypePath, sizeof(msmFbTypePath),
150                  "/sys/class/graphics/fb%d/edid_raw_data", node_id);
151 
152     edidFile = open(msmFbTypePath, O_RDONLY, 0);
153 
154     if (edidFile < 0) {
155         ALOGE("%s no edid raw data found", __func__);
156         return 0;
157     }
158 
159     size = (int)read(edidFile, (char*)buffer, EDID_RAW_DATA_SIZE);
160     close(edidFile);
161     return size;
162 }
163 
164 }; //namespace qdutils
165