Ken Zhang | 61cc603 | 2013-06-02 10:54:16 -0400 | [diff] [blame] | 1 | /* |
Ramkumar Radhakrishnan | b3d15b6 | 2017-01-06 22:35:06 -0800 | [diff] [blame] | 2 | * Copyright (c) 2013, 2017 The Linux Foundation. All rights reserved. |
Ken Zhang | 61cc603 | 2013-06-02 10:54:16 -0400 | [diff] [blame] | 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" |
Ken Zhang | 61cc603 | 2013-06-02 10:54:16 -0400 | [diff] [blame] | 31 | |
Tatenda Chipeperekwa | ce8d5c4 | 2014-08-13 10:53:49 -0700 | [diff] [blame] | 32 | namespace qdutils { |
| 33 | |
Ramkumar Radhakrishnan | b3d15b6 | 2017-01-06 22:35:06 -0800 | [diff] [blame] | 34 | static int parseLine(char *input, char *tokens[], const uint32_t maxToken, uint32_t *count) { |
Ramkumar Radhakrishnan | 29a36a5 | 2015-06-16 20:22:42 -0700 | [diff] [blame] | 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 | |
Ramkumar Radhakrishnan | b3d15b6 | 2017-01-06 22:35:06 -0800 | [diff] [blame] | 52 | static int getExternalNode(const char *type) { |
| 53 | FILE *displayDeviceFP = NULL; |
| 54 | char fbType[MAX_FRAME_BUFFER_NAME_SIZE]; |
| 55 | char msmFbTypePath[MAX_FRAME_BUFFER_NAME_SIZE]; |
| 56 | int j = 0; |
| 57 | |
| 58 | for(j = 0; j < HWC_NUM_DISPLAY_TYPES; j++) { |
| 59 | snprintf (msmFbTypePath, sizeof(msmFbTypePath), |
| 60 | "/sys/class/graphics/fb%d/msm_fb_type", j); |
| 61 | displayDeviceFP = fopen(msmFbTypePath, "r"); |
| 62 | if(displayDeviceFP) { |
| 63 | fread(fbType, sizeof(char), MAX_FRAME_BUFFER_NAME_SIZE, |
| 64 | displayDeviceFP); |
| 65 | if(strncmp(fbType, type, strlen(type)) == 0) { |
| 66 | ALOGD("%s: %s is at fb%d", __func__, type, j); |
| 67 | fclose(displayDeviceFP); |
| 68 | break; |
| 69 | } |
| 70 | fclose(displayDeviceFP); |
| 71 | } else { |
| 72 | ALOGE("%s: Failed to open fb node %d", __func__, j); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (j < HWC_NUM_DISPLAY_TYPES) |
| 77 | return j; |
| 78 | else |
| 79 | ALOGE("%s: Failed to find %s node", __func__, type); |
| 80 | |
| 81 | return -1; |
| 82 | } |
| 83 | |
Ramkumar Radhakrishnan | 29a36a5 | 2015-06-16 20:22:42 -0700 | [diff] [blame] | 84 | int querySDEInfo(HWQueryType type, int *value) { |
| 85 | FILE *fileptr = NULL; |
| 86 | const char *featureName; |
| 87 | char stringBuffer[MAX_STRING_LENGTH]; |
| 88 | uint32_t tokenCount = 0; |
| 89 | const uint32_t maxCount = 10; |
| 90 | char *tokens[maxCount] = { NULL }; |
| 91 | |
| 92 | switch(type) { |
| 93 | case HAS_MACRO_TILE: |
| 94 | featureName = "tile_format"; |
| 95 | break; |
Sushil Chauhan | 0136141 | 2016-04-25 16:36:18 -0700 | [diff] [blame] | 96 | case HAS_UBWC: |
| 97 | featureName = "ubwc"; |
| 98 | break; |
Jeykumar Sankaran | 9bc1a78 | 2015-12-14 18:36:27 -0800 | [diff] [blame] | 99 | case HAS_WB_UBWC: |
| 100 | featureName = "wb_ubwc"; |
| 101 | break; |
Ramkumar Radhakrishnan | 29a36a5 | 2015-06-16 20:22:42 -0700 | [diff] [blame] | 102 | default: |
| 103 | ALOGE("Invalid query type %d", type); |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | |
| 107 | fileptr = fopen("/sys/devices/virtual/graphics/fb0/mdp/caps", "rb"); |
| 108 | if (!fileptr) { |
| 109 | ALOGE("File '%s' not found", stringBuffer); |
| 110 | return -EINVAL; |
| 111 | } |
| 112 | |
| 113 | size_t len = MAX_STRING_LENGTH; |
| 114 | ssize_t read; |
| 115 | char *line = stringBuffer; |
| 116 | while ((read = getline(&line, &len, fileptr)) != -1) { |
| 117 | // parse the line and update information accordingly |
| 118 | if (parseLine(line, tokens, maxCount, &tokenCount)) { |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | if (strncmp(tokens[0], "features", strlen("features"))) { |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | for (uint32_t i = 0; i < tokenCount; i++) { |
| 127 | if (!strncmp(tokens[i], featureName, strlen(featureName))) { |
| 128 | *value = 1; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | fclose(fileptr); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Ramkumar Radhakrishnan | b3d15b6 | 2017-01-06 22:35:06 -0800 | [diff] [blame] | 137 | int getHDMINode(void) { |
| 138 | return getExternalNode("dtv panel"); |
Ken Zhang | 61cc603 | 2013-06-02 10:54:16 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | int getEdidRawData(char *buffer) |
| 142 | { |
| 143 | int size; |
| 144 | int edidFile; |
| 145 | char msmFbTypePath[MAX_FRAME_BUFFER_NAME_SIZE]; |
| 146 | int node_id = getHDMINode(); |
| 147 | |
| 148 | if (node_id < 0) { |
| 149 | ALOGE("%s no HDMI node found", __func__); |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | snprintf(msmFbTypePath, sizeof(msmFbTypePath), |
| 154 | "/sys/class/graphics/fb%d/edid_raw_data", node_id); |
| 155 | |
Ramkumar Radhakrishnan | 29a36a5 | 2015-06-16 20:22:42 -0700 | [diff] [blame] | 156 | edidFile = open(msmFbTypePath, O_RDONLY, 0); |
Ken Zhang | 61cc603 | 2013-06-02 10:54:16 -0400 | [diff] [blame] | 157 | |
| 158 | if (edidFile < 0) { |
| 159 | ALOGE("%s no edid raw data found", __func__); |
| 160 | return 0; |
| 161 | } |
| 162 | |
Dileep Kumar Reddi | bf333c7 | 2014-02-25 14:32:51 +0530 | [diff] [blame] | 163 | size = (int)read(edidFile, (char*)buffer, EDID_RAW_DATA_SIZE); |
Ken Zhang | 61cc603 | 2013-06-02 10:54:16 -0400 | [diff] [blame] | 164 | close(edidFile); |
| 165 | return size; |
| 166 | } |
| 167 | |
Ramkumar Radhakrishnan | b3d15b6 | 2017-01-06 22:35:06 -0800 | [diff] [blame] | 168 | bool isDPConnected() { |
| 169 | char connectPath[MAX_FRAME_BUFFER_NAME_SIZE]; |
| 170 | FILE *connectFile = NULL; |
| 171 | size_t len = MAX_STRING_LENGTH; |
| 172 | char stringBuffer[MAX_STRING_LENGTH]; |
| 173 | char *line = stringBuffer; |
| 174 | |
| 175 | int nodeId = getExternalNode("dp panel"); |
| 176 | if (nodeId < 0) { |
| 177 | ALOGE("%s no DP node found", __func__); |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | snprintf(connectPath, sizeof(connectPath), |
| 182 | "/sys/class/graphics/fb%d/connected", nodeId); |
| 183 | |
| 184 | connectFile = fopen(connectPath, "rb"); |
| 185 | if (!connectFile) { |
| 186 | ALOGW("Failed to open connect node for device node %d", nodeId); |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | if (getline(&line, &len, connectFile) < 0) { |
| 191 | fclose(connectFile); |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | fclose(connectFile); |
| 196 | |
| 197 | return atoi(line); |
| 198 | } |
| 199 | |
| 200 | int getDPTestConfig(uint32_t *panelBpp, uint32_t *patternType) { |
| 201 | if (!panelBpp || !patternType) { |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | char configPath[MAX_FRAME_BUFFER_NAME_SIZE]; |
| 206 | FILE *configFile = NULL; |
| 207 | uint32_t tokenCount = 0; |
| 208 | const uint32_t maxCount = 10; |
| 209 | char *tokens[maxCount] = { NULL }; |
| 210 | size_t len = MAX_STRING_LENGTH; |
| 211 | char stringBuffer[MAX_STRING_LENGTH]; |
| 212 | char *line = stringBuffer; |
| 213 | |
| 214 | int nodeId = getExternalNode("dp panel"); |
| 215 | if (nodeId < 0) { |
| 216 | ALOGE("%s no DP node found", __func__); |
| 217 | return -EINVAL; |
| 218 | } |
| 219 | |
| 220 | snprintf(configPath, sizeof(configPath), |
| 221 | "/sys/class/graphics/fb%d/config", nodeId); |
| 222 | |
| 223 | configFile = fopen(configPath, "rb"); |
| 224 | if (!configFile) { |
| 225 | ALOGW("Failed to open config node for device node %d", nodeId); |
| 226 | return -EINVAL; |
| 227 | } |
| 228 | |
| 229 | while (getline(&line, &len, configFile) != -1) { |
| 230 | if (!parseLine(line, tokens, maxCount, &tokenCount)) { |
| 231 | if (!strncmp(tokens[0], "bpp", strlen("bpp"))) { |
| 232 | *panelBpp = static_cast<uint32_t>(atoi(tokens[1])); |
| 233 | } else if (!strncmp(tokens[0], "pattern", strlen("pattern"))) { |
| 234 | *patternType = static_cast<uint32_t>(atoi(tokens[1])); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | fclose(configFile); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Tatenda Chipeperekwa | ce8d5c4 | 2014-08-13 10:53:49 -0700 | [diff] [blame] | 244 | }; //namespace qdutils |