Andrew Duggan | 052556f | 2014-04-16 11:32:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Andrew Duggan |
| 3 | * Copyright (C) 2014 Synaptics Inc |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <time.h> |
| 20 | #include <string.h> |
| 21 | #include <errno.h> |
Andrew Duggan | 777d012 | 2014-08-05 13:59:55 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 23 | |
| 24 | #include "rmidevice.h" |
| 25 | |
| 26 | #define RMI_DEVICE_PDT_ENTRY_SIZE 6 |
| 27 | #define RMI_DEVICE_PAGE_SELECT_REGISTER 0xFF |
| 28 | #define RMI_DEVICE_MAX_PAGE 0xFF |
| 29 | #define RMI_DEVICE_PAGE_SIZE 0x100 |
| 30 | #define RMI_DEVICE_PAGE_SCAN_START 0x00e9 |
| 31 | #define RMI_DEVICE_PAGE_SCAN_END 0x0005 |
Andrew Duggan | fc17a65 | 2014-05-06 15:11:20 -0700 | [diff] [blame] | 32 | #define RMI_DEVICE_F01_BASIC_QUERY_LEN 11 |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 33 | #define RMI_DEVICE_F01_QRY5_YEAR_MASK 0x1f |
| 34 | #define RMI_DEVICE_F01_QRY6_MONTH_MASK 0x0f |
| 35 | #define RMI_DEVICE_F01_QRY7_DAY_MASK 0x1f |
| 36 | |
| 37 | #define RMI_DEVICE_F01_QRY1_HAS_LTS (1 << 2) |
| 38 | #define RMI_DEVICE_F01_QRY1_HAS_SENSOR_ID (1 << 3) |
| 39 | #define RMI_DEVICE_F01_QRY1_HAS_CHARGER_INP (1 << 4) |
| 40 | #define RMI_DEVICE_F01_QRY1_HAS_ADJ_DOZE (1 << 5) |
| 41 | #define RMI_DEVICE_F01_QRY1_HAS_ADJ_DOZE_HOFF (1 << 6) |
| 42 | #define RMI_DEVICE_F01_QRY1_HAS_PROPS_2 (1 << 7) |
| 43 | |
| 44 | #define RMI_DEVICE_F01_LTS_RESERVED_SIZE 19 |
| 45 | |
| 46 | #define RMI_DEVICE_F01_QRY42_DS4_QUERIES (1 << 0) |
| 47 | #define RMI_DEVICE_F01_QRY42_MULTI_PHYS (1 << 1) |
| 48 | |
| 49 | #define RMI_DEVICE_F01_QRY43_01_PACKAGE_ID (1 << 0) |
| 50 | #define RMI_DEVICE_F01_QRY43_01_BUILD_ID (1 << 1) |
| 51 | |
| 52 | #define PACKAGE_ID_BYTES 4 |
| 53 | #define BUILD_ID_BYTES 3 |
| 54 | |
| 55 | #define RMI_F01_CMD_DEVICE_RESET 1 |
| 56 | #define RMI_F01_DEFAULT_RESET_DELAY_MS 100 |
| 57 | |
| 58 | int RMIDevice::SetRMIPage(unsigned char page) |
| 59 | { |
Andrew Duggan | 5d47750 | 2014-04-07 10:44:40 -0700 | [diff] [blame] | 60 | int rc; |
| 61 | |
| 62 | if (m_page == page) |
| 63 | return 0; |
| 64 | |
| 65 | m_page = page; |
| 66 | rc = Write(RMI_DEVICE_PAGE_SELECT_REGISTER, &page, 1); |
| 67 | if (rc < 0) { |
| 68 | m_page = -1; |
| 69 | return rc; |
| 70 | } |
| 71 | return 0; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | int RMIDevice::QueryBasicProperties() |
| 75 | { |
| 76 | int rc; |
| 77 | unsigned char basicQuery[RMI_DEVICE_F01_BASIC_QUERY_LEN]; |
| 78 | unsigned short queryAddr; |
| 79 | unsigned char infoBuf[4]; |
| 80 | unsigned short prodInfoAddr; |
| 81 | RMIFunction f01; |
| 82 | |
| 83 | if (GetFunction(f01, 1)) { |
| 84 | queryAddr = f01.GetQueryBase(); |
| 85 | |
| 86 | rc = Read(queryAddr, basicQuery, RMI_DEVICE_F01_BASIC_QUERY_LEN); |
| 87 | if (rc < 0) { |
| 88 | fprintf(stderr, "Failed to read the basic query: %s\n", strerror(errno)); |
| 89 | return rc; |
| 90 | } |
| 91 | m_manufacturerID = basicQuery[0]; |
| 92 | m_hasLTS = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_LTS; |
| 93 | m_hasSensorID = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_SENSOR_ID; |
| 94 | m_hasAdjustableDoze = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_ADJ_DOZE; |
| 95 | m_hasAdjustableDozeHoldoff = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_ADJ_DOZE_HOFF; |
| 96 | m_hasQuery42 = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_PROPS_2; |
Andrew Duggan | 777d012 | 2014-08-05 13:59:55 -0700 | [diff] [blame] | 97 | m_firmwareVersionMajor = basicQuery[2]; |
| 98 | m_firmwareVersionMinor = basicQuery[3]; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 99 | |
| 100 | snprintf(m_dom, sizeof(m_dom), "20%02d/%02d/%02d", |
| 101 | basicQuery[5] & RMI_DEVICE_F01_QRY5_YEAR_MASK, |
| 102 | basicQuery[6] & RMI_DEVICE_F01_QRY6_MONTH_MASK, |
| 103 | basicQuery[7] & RMI_DEVICE_F01_QRY7_DAY_MASK); |
| 104 | |
Andrew Duggan | fc17a65 | 2014-05-06 15:11:20 -0700 | [diff] [blame] | 105 | queryAddr += 11; |
| 106 | rc = Read(queryAddr, m_productID, RMI_PRODUCT_ID_LENGTH); |
| 107 | if (rc < 0) { |
| 108 | fprintf(stderr, "Failed to read the product id: %s\n", strerror(errno)); |
| 109 | return rc; |
| 110 | } |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 111 | m_productID[RMI_PRODUCT_ID_LENGTH] = '\0'; |
| 112 | |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 113 | prodInfoAddr = queryAddr + 6; |
| 114 | queryAddr += 10; |
| 115 | |
| 116 | if (m_hasLTS) |
| 117 | ++queryAddr; |
| 118 | |
| 119 | if (m_hasSensorID) { |
| 120 | rc = Read(queryAddr++, &m_sensorID, 1); |
| 121 | if (rc < 0) { |
| 122 | fprintf(stderr, "Failed to read sensor id: %s\n", strerror(errno)); |
| 123 | return rc; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (m_hasLTS) |
| 128 | queryAddr += RMI_DEVICE_F01_LTS_RESERVED_SIZE; |
| 129 | |
| 130 | if (m_hasQuery42) { |
| 131 | rc = Read(queryAddr++, infoBuf, 1); |
| 132 | if (rc < 0) { |
| 133 | fprintf(stderr, "Failed to read query 42: %s\n", strerror(errno)); |
| 134 | return rc; |
| 135 | } |
| 136 | |
| 137 | m_hasDS4Queries = infoBuf[0] & RMI_DEVICE_F01_QRY42_DS4_QUERIES; |
| 138 | m_hasMultiPhysical = infoBuf[0] & RMI_DEVICE_F01_QRY42_MULTI_PHYS; |
| 139 | } |
| 140 | |
| 141 | if (m_hasDS4Queries) { |
| 142 | rc = Read(queryAddr++, &m_ds4QueryLength, 1); |
| 143 | if (rc < 0) { |
| 144 | fprintf(stderr, "Failed to read DS4 query length: %s\n", strerror(errno)); |
| 145 | return rc; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | for (int i = 1; i <= m_ds4QueryLength; ++i) { |
| 150 | unsigned char val; |
| 151 | rc = Read(queryAddr++, &val, 1); |
| 152 | if (rc < 0) { |
| 153 | fprintf(stderr, "Failed to read F01 Query43.%02d: %s\n", i, strerror(errno)); |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | switch(i) { |
| 158 | case 1: |
| 159 | m_hasPackageIDQuery = val & RMI_DEVICE_F01_QRY43_01_PACKAGE_ID; |
| 160 | m_hasBuildIDQuery = val & RMI_DEVICE_F01_QRY43_01_BUILD_ID; |
| 161 | break; |
| 162 | case 2: |
| 163 | case 3: |
| 164 | default: |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (m_hasPackageIDQuery) { |
| 170 | rc = Read(prodInfoAddr++, infoBuf, PACKAGE_ID_BYTES); |
| 171 | if (rc > 0) { |
| 172 | unsigned short *val = (unsigned short *)infoBuf; |
| 173 | m_packageID = *val; |
| 174 | val = (unsigned short *)(infoBuf + 2); |
| 175 | m_packageRev = *val; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (m_hasBuildIDQuery) { |
| 180 | rc = Read(prodInfoAddr, infoBuf, BUILD_ID_BYTES); |
| 181 | if (rc > 0) { |
| 182 | unsigned short *val = (unsigned short *)infoBuf; |
| 183 | m_buildID = *val; |
| 184 | m_buildID += infoBuf[2] * 65536; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | void RMIDevice::PrintProperties() |
| 192 | { |
| 193 | fprintf(stdout, "manufacturerID:\t\t%d\n", m_manufacturerID); |
| 194 | fprintf(stdout, "Has LTS?:\t\t%d\n", m_hasLTS); |
| 195 | fprintf(stdout, "Has Sensor ID?:\t\t%d\n", m_hasSensorID); |
| 196 | fprintf(stdout, "Has Adjustable Doze?:\t%d\n", m_hasAdjustableDoze); |
| 197 | fprintf(stdout, "Has Query 42?:\t\t%d\n", m_hasQuery42); |
| 198 | fprintf(stdout, "Date of Manufacturer:\t%s\n", m_dom); |
| 199 | fprintf(stdout, "Product ID:\t\t%s\n", m_productID); |
Andrew Duggan | 777d012 | 2014-08-05 13:59:55 -0700 | [diff] [blame] | 200 | fprintf(stdout, "Firmware Version:\t%d.%d\n", m_firmwareVersionMajor, m_firmwareVersionMinor); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 201 | fprintf(stdout, "Package ID:\t\t%d\n", m_packageID); |
| 202 | fprintf(stdout, "Package Rev:\t\t%d\n", m_packageRev); |
| 203 | fprintf(stdout, "Build ID:\t\t%ld\n", m_buildID); |
| 204 | fprintf(stdout, "Sensor ID:\t\t%d\n", m_sensorID); |
| 205 | fprintf(stdout, "Has DS4 Queries?:\t%d\n", m_hasDS4Queries); |
| 206 | fprintf(stdout, "Has Multi Phys?:\t%d\n", m_hasMultiPhysical); |
| 207 | fprintf(stdout, "\n"); |
| 208 | } |
| 209 | |
| 210 | int RMIDevice::Reset() |
| 211 | { |
| 212 | int rc; |
| 213 | RMIFunction f01; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 214 | const unsigned char deviceReset = RMI_F01_CMD_DEVICE_RESET; |
| 215 | |
| 216 | if (!GetFunction(f01, 1)) |
| 217 | return -1; |
| 218 | |
| 219 | fprintf(stdout, "Resetting...\n"); |
| 220 | rc = Write(f01.GetCommandBase(), &deviceReset, 1); |
| 221 | if (rc < 0) |
| 222 | return rc; |
| 223 | |
Andrew Duggan | 5d47750 | 2014-04-07 10:44:40 -0700 | [diff] [blame] | 224 | rc = Sleep(RMI_F01_DEFAULT_RESET_DELAY_MS); |
| 225 | if (rc < 0) |
| 226 | return -1; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 227 | fprintf(stdout, "Reset completed.\n"); |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | bool RMIDevice::GetFunction(RMIFunction &func, int functionNumber) |
| 232 | { |
| 233 | std::vector<RMIFunction>::iterator funcIter; |
| 234 | |
| 235 | for (funcIter = m_functionList.begin(); funcIter != m_functionList.end(); ++funcIter) { |
| 236 | if (funcIter->GetFunctionNumber() == functionNumber) { |
| 237 | func = *funcIter; |
| 238 | return true; |
| 239 | } |
| 240 | } |
| 241 | return false; |
| 242 | } |
| 243 | |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame] | 244 | void RMIDevice::PrintFunctions() |
| 245 | { |
| 246 | std::vector<RMIFunction>::iterator funcIter; |
| 247 | |
| 248 | for (funcIter = m_functionList.begin(); funcIter != m_functionList.end(); ++funcIter) |
| 249 | fprintf(stdout, "0x%02x (%d) (%d): 0x%02x 0x%02x 0x%02x 0x%02x\n", |
| 250 | funcIter->GetFunctionNumber(), funcIter->GetFunctionVersion(), |
| 251 | funcIter->GetInterruptSourceCount(), funcIter->GetDataBase(), |
| 252 | funcIter->GetControlBase(), funcIter->GetCommandBase(), |
| 253 | funcIter->GetQueryBase()); |
| 254 | } |
| 255 | |
Andrew Duggan | c14ec9c | 2014-08-13 14:42:37 -0700 | [diff] [blame^] | 256 | int RMIDevice::ScanPDT(int endFunc) |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 257 | { |
| 258 | int rc; |
| 259 | unsigned int page; |
| 260 | unsigned int addr; |
| 261 | unsigned char entry[RMI_DEVICE_PDT_ENTRY_SIZE]; |
| 262 | |
| 263 | m_functionList.clear(); |
| 264 | |
| 265 | for (page = 0; page < RMI_DEVICE_MAX_PAGE; ++page) { |
| 266 | unsigned int page_start = RMI_DEVICE_PAGE_SIZE * page; |
| 267 | unsigned int pdt_start = page_start + RMI_DEVICE_PAGE_SCAN_START; |
| 268 | unsigned int pdt_end = page_start + RMI_DEVICE_PAGE_SCAN_END; |
| 269 | bool found = false; |
| 270 | |
| 271 | SetRMIPage(page); |
| 272 | |
| 273 | for (addr = pdt_start; addr >= pdt_end; addr -= RMI_DEVICE_PDT_ENTRY_SIZE) { |
| 274 | rc = Read(addr, entry, RMI_DEVICE_PDT_ENTRY_SIZE); |
| 275 | if (rc < 0) { |
| 276 | fprintf(stderr, "Failed to read PDT entry at address (0x%04x)\n", addr); |
| 277 | return rc; |
| 278 | } |
| 279 | |
| 280 | RMIFunction func(entry); |
| 281 | if (func.GetFunctionNumber() == 0) |
| 282 | break; |
| 283 | |
| 284 | m_functionList.push_back(func); |
| 285 | found = true; |
Andrew Duggan | c14ec9c | 2014-08-13 14:42:37 -0700 | [diff] [blame^] | 286 | |
| 287 | if (func.GetFunctionNumber() == endFunc) |
| 288 | return 0; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | if (!found) |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | return 0; |
Andrew Duggan | 65e5553 | 2014-04-04 16:59:54 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Andrew Duggan | 2f5c0c0 | 2014-08-08 13:30:49 -0700 | [diff] [blame] | 298 | bool RMIDevice::InBootloader() |
| 299 | { |
| 300 | RMIFunction f01; |
| 301 | if (GetFunction(f01, 0x01)) { |
| 302 | int rc; |
| 303 | unsigned char status; |
| 304 | |
| 305 | rc = Read(f01.GetDataBase(), &status, 1); |
| 306 | if (rc < 0) |
| 307 | return true; |
| 308 | |
| 309 | return !!(status & 0x40); |
| 310 | } |
| 311 | return true; |
| 312 | } |
| 313 | |
Andrew Duggan | 65e5553 | 2014-04-04 16:59:54 -0700 | [diff] [blame] | 314 | long long diff_time(struct timespec *start, struct timespec *end) |
| 315 | { |
| 316 | long long diff; |
| 317 | diff = (end->tv_sec - start->tv_sec) * 1000 * 1000; |
| 318 | diff += (end->tv_nsec - start->tv_nsec) / 1000; |
| 319 | return diff; |
Andrew Duggan | 5d47750 | 2014-04-07 10:44:40 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | int Sleep(int ms) |
| 323 | { |
| 324 | struct timespec ts; |
| 325 | struct timespec rem; |
| 326 | |
| 327 | ts.tv_sec = ms / 1000; |
| 328 | ts.tv_nsec = (ms % 1000) * 1000 * 1000; |
| 329 | for (;;) { |
| 330 | if (nanosleep(&ts, &rem) == 0) { |
| 331 | break; |
| 332 | } else { |
| 333 | if (errno == EINTR) { |
| 334 | ts = rem; |
| 335 | continue; |
| 336 | } |
| 337 | return -1; |
| 338 | } |
| 339 | } |
| 340 | return 0; |
Andrew Duggan | e9a5cd0 | 2014-04-29 13:34:42 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void print_buffer(const unsigned char *buf, unsigned int len) |
| 344 | { |
| 345 | for (unsigned int i = 0; i < len; ++i) { |
| 346 | fprintf(stdout, "0x%02X ", buf[i]); |
| 347 | if (i % 8 == 7) |
| 348 | fprintf(stdout, "\n"); |
| 349 | } |
| 350 | fprintf(stdout, "\n"); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 351 | } |