ftsTool.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * FTS Capacitive touch screen controller (FingerTipS)
  4. *
  5. * Copyright (C) 2016-2018, STMicroelectronics Limited.
  6. * Authors: AMG(Analog Mems Group) <[email protected]>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. *
  23. **************************************************************************
  24. ** STMicroelectronics **
  25. **************************************************************************
  26. ** [email protected] **
  27. **************************************************************************
  28. * *
  29. * FTS Utility Functions *
  30. * *
  31. **************************************************************************
  32. **************************************************************************
  33. *
  34. */
  35. #include <linux/init.h>
  36. #include <linux/errno.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/kernel.h>
  39. #include <linux/module.h>
  40. #include <linux/slab.h>
  41. #include <linux/string.h>
  42. #include <stdarg.h>
  43. #include <linux/input.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/serio.h>
  46. #include <linux/init.h>
  47. #include <linux/pm.h>
  48. #include <linux/delay.h>
  49. #include <linux/ctype.h>
  50. #include <linux/fs.h>
  51. #include <linux/uaccess.h>
  52. #include <linux/power_supply.h>
  53. #include <linux/firmware.h>
  54. #include <linux/gpio.h>
  55. #include "ftsCompensation.h"
  56. #include "ftsCrossCompile.h"
  57. #include "ftsError.h"
  58. #include "ftsHardware.h"
  59. #include "ftsIO.h"
  60. #include "ftsSoftware.h"
  61. #include "ftsTime.h"
  62. #include "ftsFlash.h"
  63. #include "ftsTool.h"
  64. #include "../fts.h"
  65. static char tag[8] = "[ FTS ]\0";
  66. static int reset_gpio = GPIO_NOT_DEFINED;
  67. static int system_resetted_up;
  68. static int system_resetted_down;
  69. int readB2(u16 address, u8 *outBuf, int len)
  70. {
  71. int remaining = len;
  72. int toRead = 0;
  73. int retry = 0;
  74. int ret;
  75. int event_to_search[3];
  76. char *temp = NULL;
  77. u8 *init_outBuf = outBuf;
  78. u16 init_addr = address;
  79. u8 readEvent[FIFO_EVENT_SIZE] = {0};
  80. u8 cmd[4] = { FTS_CMD_REQU_FW_CONF, 0x00, 0x00, (u8)len };
  81. if (readEvent == NULL) {
  82. logError(1, "%s %s:ERROR %02X\n", tag, __func__, ERROR_ALLOC);
  83. return ERROR_ALLOC;
  84. }
  85. u16ToU8_be(address, &cmd[1]);
  86. temp = printHex("Command B2 = ", cmd, 4);
  87. if (temp != NULL)
  88. logError(0, "%s %s", tag, temp);
  89. kfree(temp);
  90. do {
  91. remaining = len;
  92. ret = fts_writeFwCmd(cmd, 4);
  93. if (ret < 0) {
  94. logError(1, "%s %s: ERROR %02X\n",
  95. tag, __func__, ERROR_I2C_W);
  96. return ret;
  97. }
  98. //ask to the FW the data
  99. logError(0, "%s Command to FW sent!\n", tag);
  100. event_to_search[0] = (int)EVENTID_FW_CONFIGURATION;
  101. while (remaining > OK) {
  102. event_to_search[1] = (int)((address & 0xFF00) >> 8);
  103. event_to_search[2] = (int)(address & 0x00FF);
  104. if (remaining > B2_DATA_BYTES) {
  105. toRead = B2_DATA_BYTES;
  106. remaining -= B2_DATA_BYTES;
  107. } else {
  108. toRead = remaining;
  109. remaining = 0;
  110. }
  111. ret = pollForEvent(event_to_search, 3,
  112. readEvent, GENERAL_TIMEOUT);
  113. if (ret >= OK) {
  114. //start the polling for reading the reply
  115. memcpy(outBuf, &readEvent[3], toRead);
  116. retry = 0;
  117. outBuf += toRead;
  118. } else {
  119. retry += 1;
  120. break;
  121. }
  122. address += B2_DATA_BYTES;
  123. }
  124. logError(0, "%s %s:B2 failed...attempt = %d\n",
  125. tag, __func__, retry);
  126. outBuf = init_outBuf;
  127. address = init_addr;
  128. } while (retry < B2_RETRY && retry != 0);
  129. if (retry == B2_RETRY) {
  130. logError(1, "%s %s:ERROR %02X\n", tag, __func__, ERROR_TIMEOUT);
  131. return ERROR_TIMEOUT;
  132. }
  133. logError(0, "%s B2 read %d bytes\n", tag, len);
  134. return OK;
  135. }
  136. int readB2U16(u16 address, u8 *outBuf, int byteToRead)
  137. {
  138. int remaining = byteToRead;
  139. int toRead = 0;
  140. int ret;
  141. u8 *buff = (u8 *)kmalloc_array((B2_CHUNK + 1), sizeof(u8), GFP_KERNEL);
  142. if (buff == NULL) {
  143. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ERROR_ALLOC);
  144. return ERROR_ALLOC;
  145. }
  146. while (remaining > 0) {
  147. if (remaining >= B2_CHUNK) {
  148. toRead = B2_CHUNK;
  149. remaining -= B2_CHUNK;
  150. } else {
  151. toRead = remaining;
  152. remaining = 0;
  153. }
  154. ret = readB2(address, buff, toRead);
  155. if (ret < 0) {
  156. kfree(buff);
  157. return ret;
  158. }
  159. memcpy(outBuf, buff, toRead);
  160. address += toRead;
  161. outBuf += toRead;
  162. }
  163. kfree(buff);
  164. return OK;
  165. }
  166. int releaseInformation(void)
  167. {
  168. int ret;
  169. u8 cmd[1] = { FTS_CMD_RELEASE_INFO };
  170. int event_to_search[1];
  171. u8 readEvent[FIFO_EVENT_SIZE];
  172. event_to_search[0] = (int)EVENTID_RELEASE_INFO;
  173. logError(0, "%s %s: started... Chip INFO:\n", tag, __func__);
  174. ret = fts_writeFwCmd(cmd, 1);
  175. if (ret < OK) {
  176. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ret);
  177. return ret;
  178. }
  179. ret = pollForEvent(event_to_search, 1, &readEvent[0],
  180. RELEASE_INFO_TIMEOUT);
  181. //start the polling for reading the reply
  182. if (ret < OK) {
  183. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ret);
  184. return ret;
  185. }
  186. logError(0, "%s %s: Finished! %d\n", tag, __func__, ret);
  187. return OK;
  188. }
  189. int lockDownInfo(u8 *data, int len)
  190. {
  191. int ret;
  192. int i = 0, num_event;
  193. u8 cmd[1] = { FTS_CMD_LOCKDOWN_CMD };
  194. int event_to_search[3] = {EVENTID_LOCKDOWN_INFO,
  195. EVENT_TYPE_LOCKDOWN, 0x00};
  196. u8 readEvent[FIFO_EVENT_SIZE];
  197. logError(0, "%s %s:started...\n", tag, __func__);
  198. if (len <= 0)
  199. return ERROR_OP_NOT_ALLOW;
  200. ret = fts_writeFwCmd(cmd, 1);
  201. if (ret < OK) {
  202. logError(1, "%s %s:ERROR %02X\n", tag, __func__, ret);
  203. return ret;
  204. }
  205. num_event = (len + 3) / 4;
  206. logError(0, "%s %s:num_event = %d\n", tag, __func__, num_event);
  207. for (i = 0; i < num_event; i++) {
  208. ret = pollForEvent(event_to_search, 3,
  209. &readEvent[0], GENERAL_TIMEOUT);
  210. //start the polling for reading the reply
  211. if (ret < OK) {
  212. logError(1, "%s %s:ERROR %02X\n", tag, __func__, ret);
  213. return ret;
  214. }
  215. data[i * 4] = readEvent[3];
  216. data[i * 4 + 1] = readEvent[4];
  217. data[i * 4 + 2] = readEvent[5];
  218. data[i * 4 + 3] = readEvent[6];
  219. event_to_search[2] += 4;
  220. //logError(0, "%02X %02X %02X %02X ", readEvent[3],
  221. //readEvent[4], readEvent[5], readEvent[6]);
  222. }
  223. logError(0, "%s %s:Finished! %d\n", tag, __func__, ret);
  224. return OK;
  225. }
  226. int calculateCRC8(u8 *u8_srcBuff, int size, u8 *crc)
  227. {
  228. u8 u8_remainder;
  229. u8 bit;
  230. int i = 0;
  231. u8_remainder = 0x00;
  232. logError(0, "%s %s: Start CRC computing...\n", tag, __func__);
  233. if (size == 0 || u8_srcBuff == NULL) {
  234. logError(1, "Arguments passed not valid!");
  235. logError(1, "%s %s:Data pointer = NULL ", tag, __func__);
  236. logError(1, "or size = 0 (%d) ERROR %08X\n",
  237. size, ERROR_OP_NOT_ALLOW);
  238. return ERROR_OP_NOT_ALLOW;
  239. }
  240. // Perform modulo-2 division, a byte at a time.
  241. //Bring the next byte into the remainder.
  242. for (i = 0; i < size; i++) {
  243. //Perform modulo-2 division, a bit at a time.
  244. u8_remainder ^= u8_srcBuff[i];
  245. //Try to divide the current data bit.
  246. for (bit = 8; bit > 0; --bit) {
  247. if (u8_remainder & (0x1 << 7))
  248. u8_remainder = (u8_remainder << 1) ^ 0x9B;
  249. else
  250. u8_remainder = (u8_remainder << 1);
  251. }
  252. } //The final remainder is the CRC result.
  253. *crc = u8_remainder;
  254. logError(0, "%s %s: CRC value = %02X\n", tag, __func__, *crc);
  255. return OK;
  256. }
  257. int writeLockDownInfo(u8 *data, int size)
  258. {
  259. int ret, i, toWrite, retry = 0, offset = size;
  260. u8 cmd[2 + LOCKDOWN_CODE_WRITE_CHUNK] = {FTS_CMD_LOCKDOWN_FILL, 0x00};
  261. u8 crc = 0;
  262. int event_to_search[2] = {EVENTID_STATUS_UPDATE,
  263. EVENT_TYPE_LOCKDOWN_WRITE};
  264. u8 readEvent[FIFO_EVENT_SIZE];
  265. char *temp = NULL;
  266. logError(0, "%s %s: Writing Lockdown code into the IC...\n",
  267. tag, __func__);
  268. ret = fts_disableInterrupt();
  269. if (ret < OK) {
  270. logError(1, "%s %s: ERROR %08X\n", tag, __func__, ret);
  271. ret = (ret | ERROR_LOCKDOWN_CODE);
  272. goto ERROR;
  273. }
  274. if (size > LOCKDOWN_CODE_MAX_SIZE) {
  275. logError(1, "%s %s: Lockdown data to write too big! ",
  276. tag, __func__);
  277. logError(1, "%d>%d ERROR %08X\n",
  278. size, LOCKDOWN_CODE_MAX_SIZE, ret);
  279. ret = (ERROR_OP_NOT_ALLOW | ERROR_LOCKDOWN_CODE);
  280. goto ERROR;
  281. }
  282. temp = printHex("Lockdown Code = ", data, size);
  283. if (temp != NULL) {
  284. logError(0, "%s %s: %s", tag, __func__, temp);
  285. kfree(temp);
  286. }
  287. for (retry = 0; retry < LOCKDOWN_CODE_RETRY; retry++) {
  288. logError(0, "%s %s: Filling FW buffer...\n", tag, __func__);
  289. i = 0;
  290. offset = size;
  291. cmd[0] = FTS_CMD_LOCKDOWN_FILL;
  292. while (offset > 0) {
  293. if (offset > LOCKDOWN_CODE_WRITE_CHUNK)
  294. toWrite = LOCKDOWN_CODE_WRITE_CHUNK;
  295. else
  296. toWrite = offset;
  297. memcpy(&cmd[2], &data[i], toWrite);
  298. cmd[1] = i;
  299. temp = printHex("Commmand = ", cmd, 2 + toWrite);
  300. if (temp != NULL) {
  301. logError(0, "%s %s: %s", tag, __func__, temp);
  302. kfree(temp);
  303. }
  304. ret = fts_writeFwCmd(cmd, 2 + toWrite);
  305. if (ret < OK) {
  306. logError(1, "Unable to write Lockdown data ");
  307. logError(1, "%s %s:Lockdown data at %d ",
  308. tag, __func__, i);
  309. logError(1, "iteration.%08X\n", ret);
  310. ret = (ret | ERROR_LOCKDOWN_CODE);
  311. continue;
  312. }
  313. i += toWrite;//update the offset
  314. offset -= toWrite;
  315. }
  316. logError(0, "%s %s: Compute 8bit CRC...\n", tag, __func__);
  317. ret = calculateCRC8(data, size, &crc);
  318. if (ret < OK) {
  319. logError(1, "%s %s:Unable to compute CRC..ERROR %08X\n",
  320. tag, __func__, ret);
  321. ret = (ret | ERROR_LOCKDOWN_CODE);
  322. continue;
  323. }
  324. cmd[0] = FTS_CMD_LOCKDOWN_WRITE;
  325. cmd[1] = 0x00;
  326. cmd[2] = (u8)size;
  327. cmd[3] = crc;
  328. logError(0, "%s %s: Write Lockdown data...\n",
  329. tag, __func__);
  330. temp = printHex("Commmand = ", cmd, 4);
  331. if (temp != NULL) {
  332. logError(0, "%s %s: %s", tag, __func__, temp);
  333. kfree(temp);
  334. }
  335. ret = fts_writeFwCmd(cmd, 4);
  336. if (ret < OK) {
  337. logError(1, "%s%s:Unable to send Lockdown data ",
  338. tag, __func__);
  339. logError(1, "write command%08X\n", ret);
  340. ret = (ret | ERROR_LOCKDOWN_CODE);
  341. continue;
  342. }
  343. ret = pollForEvent(event_to_search,
  344. 2,
  345. &readEvent[0],
  346. GENERAL_TIMEOUT);
  347. //start the polling for reading the reply
  348. if (ret < OK) {
  349. logError(1, "%s%s:Cann't find lockdown code ",
  350. tag, __func__);
  351. logError(1, "%write reply %08X\n", ret);
  352. continue;
  353. }
  354. if (readEvent[2] != 0x00) {
  355. logError(1, "%s %s:Event check FAIL!%02X != 0x00 ",
  356. tag, __func__, readEvent[2]);
  357. logError(1, "%ERR%08X\n", ERROR_LOCKDOWN_CODE);
  358. ret = ERROR_LOCKDOWN_CODE;
  359. continue;
  360. } else {
  361. logError(0, "%s %s:Lockdown Code write DONE!\n",
  362. tag, __func__);
  363. ret = OK;
  364. break;
  365. }
  366. }
  367. ERROR:
  368. //ret = fts_enableInterrupt();
  369. //ensure that the interrupt are always renabled when exit from funct
  370. if (fts_enableInterrupt() < OK) {
  371. logError(1, "%s %s: Error while re-enabling the interrupt!\n",
  372. tag, __func__);
  373. }
  374. return ret;
  375. }
  376. int rewriteLockDownInfo(u8 *data, int size)
  377. {
  378. int ret, i, toWrite, retry = 0, offset = size;
  379. u8 cmd[2 + LOCKDOWN_CODE_WRITE_CHUNK] = {FTS_CMD_LOCKDOWN_FILL, 0x00};
  380. u8 crc = 0;
  381. int event_to_search[2] = {EVENTID_STATUS_UPDATE,
  382. EVENT_TYPE_LOCKDOWN_WRITE};
  383. u8 readEvent[FIFO_EVENT_SIZE];
  384. char *temp = NULL;
  385. logError(0, "%s %s: ReWriting Lockdown code into the IC start ...\n",
  386. tag, __func__);
  387. ret = fts_disableInterrupt();
  388. if (ret < OK) {
  389. logError(1, "%s %s: ERROR %08X\n", tag, __func__, ret);
  390. ret = (ret | ERROR_LOCKDOWN_CODE);
  391. goto ERROR;
  392. }
  393. if (size > LOCKDOWN_CODE_MAX_SIZE) {
  394. logError(1, "%s %s: Lockdown data to write too big! ",
  395. tag, __func__);
  396. logError(1, "%d>%d ERROR %08X\n",
  397. size, LOCKDOWN_CODE_MAX_SIZE, ret);
  398. ret = (ERROR_OP_NOT_ALLOW | ERROR_LOCKDOWN_CODE);
  399. goto ERROR;
  400. }
  401. temp = printHex("Lockdown Code = ", data, size);
  402. if (temp != NULL) {
  403. logError(0, "%s %s: %s", tag, __func__, temp);
  404. kfree(temp);
  405. }
  406. for (retry = 0; retry < LOCKDOWN_CODE_RETRY; retry++) {
  407. logError(0, "%s %s: Filling FW buffer ...\n", tag, __func__);
  408. i = 0;
  409. offset = size;
  410. cmd[0] = FTS_CMD_LOCKDOWN_FILL;
  411. while (offset > 0) {
  412. if (offset > LOCKDOWN_CODE_WRITE_CHUNK)
  413. toWrite = LOCKDOWN_CODE_WRITE_CHUNK;
  414. else
  415. toWrite = offset;
  416. memcpy(&cmd[2], &data[i], toWrite);
  417. cmd[1] = i;
  418. temp = printHex("Commmand = ", cmd, 2 + toWrite);
  419. if (temp != NULL) {
  420. logError(0, "%s %s: %s", tag, __func__, temp);
  421. kfree(temp);
  422. }
  423. ret = fts_writeFwCmd(cmd, 2 + toWrite);
  424. if (ret < OK) {
  425. logError(1, "Unable to rewrite Lockdown data");
  426. logError(1, "%s %s: at %d iteration ",
  427. tag, __func__, i);
  428. logError(1, "ERROR %08X\n", ret);
  429. ret = (ret | ERROR_LOCKDOWN_CODE);
  430. continue;
  431. }
  432. i += toWrite;//update the offset
  433. offset -= toWrite;
  434. }
  435. logError(0, "%s %s: Compute 8bit CRC...\n", tag, __func__);
  436. ret = calculateCRC8(data, size, &crc);
  437. if (ret < OK) {
  438. logError(1, "%s %s:Unable to compute CRC.. ",
  439. tag, __func__);
  440. logError(1, "ERROR %08X\n", ret);
  441. ret = (ret | ERROR_LOCKDOWN_CODE);
  442. continue;
  443. }
  444. cmd[0] = FTS_CMD_LOCKDOWN_WRITE;
  445. cmd[1] = 0x01;
  446. cmd[2] = (u8)size;
  447. cmd[3] = crc;
  448. temp = printHex("Commmand = ", cmd, 4);
  449. if (temp != NULL) {
  450. logError(0, "%s %s: %s", tag, __func__, temp);
  451. kfree(temp);
  452. }
  453. logError(0, "%s %s: ReWrite Lockdown data...\n", tag, __func__);
  454. ret = fts_writeFwCmd(cmd, 4);
  455. if (ret < OK) {
  456. logError(1, "Unable to send Lockdown data");
  457. logError(1, "%s %s:rewrite command... ERROR %08X\n",
  458. tag, __func__, ret);
  459. ret = (ret | ERROR_LOCKDOWN_CODE);
  460. continue;
  461. }
  462. //start the polling for reading the reply
  463. ret = pollForEvent(event_to_search, 2,
  464. &readEvent[0], GENERAL_TIMEOUT);
  465. if (ret >= OK) {
  466. if (readEvent[2] < 0x00) {
  467. logError(1, "%s %s:Event check FAIL! ",
  468. tag, __func__);
  469. logError(1, "%02X != 0x00 %08X\n",
  470. readEvent[2], ERROR_LOCKDOWN_CODE);
  471. ret = ERROR_LOCKDOWN_CODE;
  472. continue;
  473. } else {
  474. logError(0, "%s %s: Lockdown Code ",
  475. tag, __func__);
  476. logError(0, "rewrite DONE!\n");
  477. ret = OK;
  478. break;
  479. }
  480. } else {
  481. logError(1, "Can not find lockdown code write ");
  482. logError(1, "reply event!%s %s: ERROR %08X\n",
  483. tag, __func__, ret);
  484. }
  485. }
  486. ERROR:
  487. //ret = fts_enableInterrupt();
  488. //ensure that the interrupt are always renabled when exit from funct
  489. if (fts_enableInterrupt() < OK) {
  490. logError(1, "%s %s: Error while re-enabling the interrupt!\n",
  491. tag, __func__);
  492. }
  493. return ret;
  494. }
  495. int readLockDownInfo(u8 *lockData, int *size)
  496. {
  497. int ret, retry = 0, toRead = 0, byteToRead;
  498. u8 cmd = FTS_CMD_LOCKDOWN_READ;
  499. int event_to_search[3] = {EVENTID_LOCKDOWN_INFO_READ, -1, 0x00};
  500. u8 readEvent[FIFO_EVENT_SIZE];
  501. char *temp = NULL;
  502. lockData = NULL;
  503. logError(0, "%s %s: Reading Lockdown code from the IC...\n",
  504. tag, __func__);
  505. ret = fts_disableInterrupt();
  506. if (ret < OK) {
  507. logError(1, "%s %s: ERROR %08X\n", tag, __func__, ret);
  508. ret = (ret | ERROR_LOCKDOWN_CODE);
  509. goto ERROR;
  510. }
  511. for (retry = 0; retry < LOCKDOWN_CODE_RETRY; retry++) {
  512. event_to_search[2] = 0x00;
  513. logError(0, "%s %s: Read Lockdown data.(%d attempt)\n",
  514. tag, __func__, retry + 1);
  515. ret = fts_writeFwCmd(&cmd, 1);
  516. if (ret < OK) {
  517. logError(1, "%s%s:Unable to send Lockdown data ",
  518. tag, __func__);
  519. logError(1, "write CMD %08X\n", ret);
  520. ret = (ret | ERROR_LOCKDOWN_CODE);
  521. continue;
  522. }
  523. //start the polling for reading the reply
  524. ret = pollForEvent(event_to_search, 3,
  525. &readEvent[0], GENERAL_TIMEOUT);
  526. if (ret < OK) {
  527. logError(1, "Cann't find first lockdown code read");
  528. logError(1, "%s %s:reply event! ERROR %08X\n",
  529. tag, __func__, ret);
  530. continue;
  531. }
  532. byteToRead = readEvent[1];
  533. *size = byteToRead;
  534. logError(0, "%s %s:Lockdown Code size = %d\n",
  535. tag, __func__, *size);
  536. lockData = (u8 *)kmalloc_array((byteToRead),
  537. sizeof(u8), GFP_KERNEL);
  538. if (lockData == NULL) {
  539. logError(1, "%s %s:Unable to allocate lockData %08X\n",
  540. tag, __func__, ERROR_ALLOC);
  541. ret = (ERROR_ALLOC | ERROR_LOCKDOWN_CODE);
  542. continue;
  543. }
  544. while (byteToRead > 0) {
  545. if ((readEvent[1] - readEvent[2])
  546. > LOCKDOWN_CODE_READ_CHUNK) {
  547. toRead = LOCKDOWN_CODE_READ_CHUNK;
  548. } else {
  549. toRead = readEvent[1] - readEvent[2];
  550. }
  551. byteToRead -= toRead;
  552. memcpy(&lockData[readEvent[2]],
  553. &readEvent[3], toRead);
  554. event_to_search[2] += toRead;
  555. if (byteToRead <= 0)
  556. continue;
  557. ret = pollForEvent(event_to_search,
  558. 3,
  559. &readEvent[0],
  560. GENERAL_TIMEOUT);
  561. //start polling for reading reply
  562. if (ret < OK) {
  563. logError(1, "Can not find lockdow");
  564. logError(1, "code read reply event ");
  565. logError(1, "%s%s:offset%02X%08X\n",
  566. tag, __func__, event_to_search[2], ret);
  567. ret = (ERROR_ALLOC | ERROR_LOCKDOWN_CODE);
  568. break;
  569. }
  570. }
  571. if (byteToRead != 0) {
  572. logError(1, "%s %s:Read Lockdown code FAIL! ",
  573. tag, __func__);
  574. logError(1, "ERROR %08X\n", ret);
  575. continue;
  576. } else {
  577. logError(0, "%s %s: Lockdown Code read DONE!\n",
  578. tag, __func__);
  579. ret = OK;
  580. temp = printHex("Lockdown Code = ", lockData, *size);
  581. if (temp != NULL) {
  582. logError(0, "%s %s: %s", tag, __func__, temp);
  583. kfree(temp);
  584. }
  585. break;
  586. }
  587. }
  588. ERROR:
  589. //ret = fts_enableInterrupt();
  590. //ensure that the interrupt are always
  591. //renabled when exit from funct
  592. if (fts_enableInterrupt() < OK) {
  593. logError(1, "%s %s:Error while re-enabling the interrupt!\n",
  594. tag, __func__);
  595. }
  596. return ret;
  597. }
  598. char *printHex(char *label, u8 *buff, int count)
  599. {
  600. int i, offset;
  601. char *result = NULL;
  602. size_t len = 0;
  603. offset = strlen(label);
  604. len = (offset + 3 * count) + 2;
  605. result = (char *)kmalloc_array(len, sizeof(char), GFP_KERNEL);
  606. if (result != NULL) {
  607. strlcpy(result, label, len);
  608. for (i = 0; i < count; i++)
  609. snprintf(&result[offset + i * 3], 4, "%02X ", buff[i]);
  610. strlcat(result, "\n", len);
  611. }
  612. return result;
  613. }
  614. int pollForEvent(int *event_to_search, int event_bytes,
  615. u8 *readData, int time_to_wait)
  616. {
  617. int i, find, retry, count_err;
  618. int time_to_count;
  619. int err_handling = OK;
  620. struct StopWatch clock;
  621. u8 cmd[1] = { FIFO_CMD_READONE };
  622. char *temp = NULL;
  623. find = 0;
  624. retry = 0;
  625. count_err = 0;
  626. time_to_count = time_to_wait / TIMEOUT_RESOLUTION;
  627. startStopWatch(&clock);
  628. while (find != 1 && retry < time_to_count
  629. && fts_readCmd(cmd, 1, readData, FIFO_EVENT_SIZE) >= 0) {
  630. if (readData[0] == EVENTID_ERROR_EVENT) {
  631. temp = printHex("ERROR EVENT = ",
  632. readData, FIFO_EVENT_SIZE);
  633. if (temp != NULL)
  634. logError(0, "%s %s", tag, temp);
  635. kfree(temp);
  636. count_err++;
  637. err_handling = errorHandler(readData, FIFO_EVENT_SIZE);
  638. if ((err_handling & 0xF0FF0000)
  639. == ERROR_HANDLER_STOP_PROC) {
  640. logError(1, "%s %s: forced to be stopped! ",
  641. tag, __func__);
  642. logError(1, "ERROR %08X\n", err_handling);
  643. return err_handling;
  644. }
  645. } else {
  646. if (readData[0] != EVENTID_NO_EVENT) {
  647. temp = printHex("READ EVENT = ",
  648. readData, FIFO_EVENT_SIZE);
  649. if (temp != NULL)
  650. logError(0, "%s %s", tag, temp);
  651. kfree(temp);
  652. }
  653. if (readData[0] == EVENTID_CONTROL_READY &&
  654. event_to_search[0] != EVENTID_CONTROL_READY) {
  655. logError(0, "Unmanned Controller Ready Event!");
  656. logError(0, "%s %s:Setting reset flags...\n",
  657. tag, __func__);
  658. setSystemResettedUp(1);
  659. setSystemResettedDown(1);
  660. }
  661. }
  662. find = 1;
  663. for (i = 0; i < event_bytes; i++) {
  664. if (event_to_search[i] != -1
  665. && (int)readData[i] != event_to_search[i]) {
  666. find = 0;
  667. break;
  668. }
  669. }
  670. retry++;
  671. msleep(TIMEOUT_RESOLUTION);
  672. }
  673. stopStopWatch(&clock);
  674. if ((retry >= time_to_count) && find != 1) {
  675. logError(0, "%s %s: ERROR %02X\n",
  676. tag, __func__, ERROR_TIMEOUT);
  677. return ERROR_TIMEOUT;
  678. }
  679. if (find == 1) {
  680. temp = printHex("FOUND EVENT = ", readData, FIFO_EVENT_SIZE);
  681. if (temp != NULL)
  682. logError(0, "%s %s", tag, temp);
  683. kfree(temp);
  684. logError(0, "%s Event found in %d ms (%d iterations)!\n",
  685. tag, elapsedMillisecond(&clock), retry);
  686. logError(0, "Number of errors found = %d\n", count_err);
  687. return count_err;
  688. }
  689. logError(0, "%s %s: ERROR %02X\n", tag, __func__, ERROR_I2C_R);
  690. return ERROR_I2C_R;
  691. }
  692. int flushFIFO(void)
  693. {
  694. u8 cmd = FIFO_CMD_FLUSH;
  695. if (fts_writeCmd(&cmd, 1) < 0) {
  696. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ERROR_I2C_W);
  697. return ERROR_I2C_W;
  698. }
  699. logError(0, "%s FIFO flushed!\n", tag);
  700. return OK;
  701. }
  702. int fts_disableInterrupt(void)
  703. {
  704. //disable interrupt
  705. u8 cmd[4] = { FTS_CMD_HW_REG_W, 0x00, 0x00, IER_DISABLE };
  706. u16ToU8_be(IER_ADDR, &cmd[1]);
  707. if (fts_writeCmd(cmd, 4) < OK) {
  708. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ERROR_I2C_W);
  709. return ERROR_I2C_W;
  710. }
  711. logError(0, "%s Interrupt Disabled!\n", tag);
  712. return OK;
  713. }
  714. int fts_enableInterrupt(void)
  715. {
  716. u8 cmd[4] = { FTS_CMD_HW_REG_W, 0x00, 0x00, IER_ENABLE };
  717. u16ToU8_be(IER_ADDR, &cmd[1]);
  718. if (fts_writeCmd(cmd, 4) < 0) {
  719. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ERROR_I2C_W);
  720. return ERROR_I2C_W;
  721. }
  722. logError(0, "%s Interrupt Enabled!\n", tag);
  723. return OK;
  724. }
  725. int u8ToU16n(u8 *src, int src_length, u16 *dst)
  726. {
  727. int i, j;
  728. u16 *buf;
  729. if (src_length % 2 != 0)
  730. return -EINVAL;
  731. j = 0;
  732. buf = (u16 *)kmalloc_array((src_length / 2), sizeof(u16), GFP_KERNEL);
  733. if (!buf) {
  734. dst = NULL;
  735. return -EINVAL;
  736. }
  737. dst = buf;
  738. for (i = 0; i < src_length; i += 2) {
  739. dst[j] = ((src[i+1] & 0x00FF) << 8) + (src[i] & 0x00FF);
  740. j++;
  741. }
  742. return (src_length / 2);
  743. }
  744. int u8ToU16(u8 *src, u16 *dst)
  745. {
  746. *dst = (u16)(((src[1] & 0x00FF) << 8) + (src[0] & 0x00FF));
  747. return 0;
  748. }
  749. int u8ToU16_le(u8 *src, u16 *dst)
  750. {
  751. *dst = (u16)(((src[0] & 0x00FF) << 8) + (src[1] & 0x00FF));
  752. return 0;
  753. }
  754. int u16ToU8n(u16 *src, int src_length, u8 *dst)
  755. {
  756. int i, j;
  757. u8 *buf = (u8 *)kmalloc_array(2 * src_length, sizeof(u8), GFP_KERNEL);
  758. if (!buf) {
  759. dst = NULL;
  760. return -EINVAL;
  761. }
  762. dst = buf;
  763. j = 0;
  764. for (i = 0; i < src_length; i++) {
  765. dst[j] = (u8) (src[i] & 0xFF00) >> 8;
  766. dst[j+1] = (u8) (src[i] & 0x00FF);
  767. j += 2;
  768. }
  769. return src_length * 2;
  770. }
  771. int u16ToU8(u16 src, u8 *dst)
  772. {
  773. dst[0] = (u8)((src & 0xFF00) >> 8);
  774. dst[1] = (u8)(src & 0x00FF);
  775. return 0;
  776. }
  777. int u16ToU8_be(u16 src, u8 *dst)
  778. {
  779. dst[0] = (u8)((src & 0xFF00) >> 8);
  780. dst[1] = (u8)(src & 0x00FF);
  781. return 0;
  782. }
  783. int u16ToU8_le(u16 src, u8 *dst)
  784. {
  785. dst[1] = (u8)((src & 0xFF00) >> 8);
  786. dst[0] = (u8)(src & 0x00FF);
  787. return 0;
  788. }
  789. int u8ToU32(u8 *src, u32 *dst)
  790. {
  791. *dst = (u32)(((src[3] & 0xFF) << 24) + ((src[2] & 0xFF) << 16)
  792. + ((src[1] & 0xFF) << 8) + (src[0] & 0xFF));
  793. return 0;
  794. }
  795. int u32ToU8(u32 src, u8 *dst)
  796. {
  797. dst[3] = (u8)((src & 0xFF000000) >> 24);
  798. dst[2] = (u8)((src & 0x00FF0000) >> 16);
  799. dst[1] = (u8)((src & 0x0000FF00) >> 8);
  800. dst[0] = (u8)(src & 0x000000FF);
  801. return 0;
  802. }
  803. int attempt_function(int(*code)(void), unsigned long wait_before_retry,
  804. int retry_count)
  805. {
  806. int result;
  807. int count = 0;
  808. do {
  809. result = code();
  810. count++;
  811. msleep(wait_before_retry);
  812. } while (count < retry_count && result < 0);
  813. if (count == retry_count)
  814. result |= ERROR_TIMEOUT;
  815. return result;
  816. }
  817. void setResetGpio(int gpio)
  818. {
  819. reset_gpio = gpio;
  820. logError(0, "%s %s: reset_gpio = %d\n", tag, __func__, reset_gpio);
  821. }
  822. int fts_system_reset(void)
  823. {
  824. u8 readData[FIFO_EVENT_SIZE];
  825. int event_to_search;
  826. int res = -1;
  827. int i;
  828. u8 cmd[4] = { FTS_CMD_HW_REG_W, 0x00, 0x00, SYSTEM_RESET_VALUE };
  829. event_to_search = (int)EVENTID_CONTROL_READY;
  830. u16ToU8_be(SYSTEM_RESET_ADDRESS, &cmd[1]);
  831. logError(0, "%s System resetting...\n", tag);
  832. for (i = 0; i < SYSTEM_RESET_RETRY && res < 0; i++) {
  833. if (reset_gpio == GPIO_NOT_DEFINED) {
  834. #ifndef FTM3_CHIP
  835. res |= fts_warm_boot();
  836. #endif
  837. res = fts_writeCmd(cmd, 4);
  838. } else {
  839. gpio_set_value(reset_gpio, 0);
  840. msleep(20);
  841. gpio_set_value(reset_gpio, 1);
  842. res = OK;
  843. }
  844. if (res < OK) {
  845. logError(1, "%s %s:ERROR %02X\n",
  846. tag, __func__, ERROR_I2C_W);
  847. } else {
  848. res = pollForEvent(&event_to_search, 1,
  849. readData, GENERAL_TIMEOUT);
  850. if (res < OK) {
  851. logError(0, "%s %s: ERROR %02X\n",
  852. tag, __func__, res);
  853. }
  854. }
  855. }
  856. if (res < OK) {
  857. logError(1, "%s %s:failed after 3 attempts: ERROR %02X\n",
  858. tag, __func__, (res | ERROR_SYSTEM_RESET_FAIL));
  859. res = (res | ERROR_SYSTEM_RESET_FAIL);
  860. } else {
  861. logError(0, "%s System reset DONE!\n", tag);
  862. system_resetted_down = 1;
  863. system_resetted_up = 1;
  864. res = OK;
  865. }
  866. return res;
  867. }
  868. int isSystemResettedDown(void)
  869. {
  870. return system_resetted_down;
  871. }
  872. int isSystemResettedUp(void)
  873. {
  874. return system_resetted_up;
  875. }
  876. void setSystemResettedDown(int val)
  877. {
  878. system_resetted_down = val;
  879. }
  880. void setSystemResettedUp(int val)
  881. {
  882. system_resetted_up = val;
  883. }
  884. int senseOn(void)
  885. {
  886. int ret;
  887. u8 cmd[1] = { FTS_CMD_MS_MT_SENSE_ON };
  888. ret = fts_writeFwCmd(cmd, 1);
  889. if (ret < OK) {
  890. logError(1, "%s %s:ERROR %02X\n",
  891. tag, __func__, ERROR_SENSE_ON_FAIL);
  892. return (ret|ERROR_SENSE_ON_FAIL);
  893. }
  894. logError(0, "%s %s: SENSE ON\n", tag, __func__);
  895. return OK;
  896. }
  897. int senseOff(void)
  898. {
  899. int ret;
  900. u8 cmd[1] = { FTS_CMD_MS_MT_SENSE_OFF };
  901. ret = fts_writeFwCmd(cmd, 1);
  902. if (ret < OK) {
  903. logError(1, "%s %s:ERROR %02X\n",
  904. tag, __func__, ERROR_SENSE_OFF_FAIL);
  905. return (ret | ERROR_SENSE_OFF_FAIL);
  906. }
  907. logError(0, "%s %s: SENSE OFF\n", tag, __func__);
  908. return OK;
  909. }
  910. int keyOn(void)
  911. {
  912. int ret;
  913. u8 cmd[1] = { FTS_CMD_MS_KEY_ON };
  914. ret = fts_writeFwCmd(cmd, 1);
  915. if (ret < OK) {
  916. logError(1, "%s %s:ERROR %02X\n",
  917. tag, __func__, ERROR_SENSE_ON_FAIL);
  918. return (ret | ERROR_SENSE_ON_FAIL);
  919. }
  920. logError(0, "%s %s: KEY ON\n", tag, __func__);
  921. return OK;
  922. }
  923. int keyOff(void)
  924. {
  925. int ret;
  926. u8 cmd[1] = { FTS_CMD_MS_KEY_OFF };
  927. ret = fts_writeFwCmd(cmd, 1);
  928. if (ret < OK) {
  929. logError(1, "%s %s:ERROR %02X\n",
  930. tag, __func__, ERROR_SENSE_OFF_FAIL);
  931. return (ret | ERROR_SENSE_OFF_FAIL);
  932. }
  933. logError(0, "%s %s: KEY OFF\n", tag, __func__);
  934. return OK;
  935. }
  936. int cleanUp(int enableTouch)
  937. {
  938. int res;
  939. logError(0, "%s %s: system reset...\n", tag, __func__);
  940. res = fts_system_reset();
  941. if (res < OK)
  942. return res;
  943. if (enableTouch) {
  944. logError(0, "%s %s:enabling touches...\n", tag, __func__);
  945. res = senseOn();
  946. if (res < OK)
  947. return res;
  948. #ifdef PHONE_KEY
  949. res = keyOn();
  950. if (res < OK)
  951. return res;
  952. #endif
  953. logError(0, "%s %s:enabling interrupts...\n", tag, __func__);
  954. res = fts_enableInterrupt();
  955. if (res < OK)
  956. return res;
  957. }
  958. return OK;
  959. }
  960. int checkEcho(u8 *cmd, int size)
  961. {
  962. int ret, i;
  963. int event_to_search[FIFO_EVENT_SIZE + 1];
  964. u8 readData[FIFO_EVENT_SIZE];
  965. if ((ftsInfo.u32_echoEn & 0x00000001) != ECHO_ENABLED) {
  966. logError(0, "%s ECHO Not Enabled!\n", tag);
  967. return OK;
  968. }
  969. if (size < 1) {
  970. logError(1, "%s:Error Size = %d not valid!", tag, size);
  971. logError(1, " or ECHO not Enabled!%08X\n", ERROR_OP_NOT_ALLOW);
  972. return ERROR_OP_NOT_ALLOW;
  973. }
  974. if ((size + 2) > FIFO_EVENT_SIZE)
  975. size = FIFO_EVENT_SIZE - 2;
  976. //Echo event EC xx xx xx xx xx xx
  977. //fifo_status therefore for command
  978. //with more than 6 bytes will echo only the first 6
  979. event_to_search[0] = EVENTID_ECHO;
  980. for (i = 1; i <= size; i++)
  981. event_to_search[i] = cmd[i - 1];
  982. ret = pollForEvent(event_to_search, size + 1,
  983. readData, GENERAL_TIMEOUT);
  984. if (ret < OK) {
  985. logError(1, "%s %s:Echo Event not found! ERROR %02X\n",
  986. tag, __func__, ret);
  987. return (ret | ERROR_CHECK_ECHO_FAIL);
  988. }
  989. logError(0, "%s ECHO OK!\n", tag);
  990. ret = OK;
  991. return ret;
  992. }
  993. int featureEnableDisable(int on_off, u32 feature)
  994. {
  995. int ret;
  996. u8 cmd[5];
  997. if (on_off == FEAT_ENABLE) {
  998. cmd[0] = FTS_CMD_FEATURE_ENABLE;
  999. logError(0, "%s %s: Enabling feature %08X ...\n",
  1000. tag, __func__, feature);
  1001. } else {
  1002. cmd[0] = FTS_CMD_FEATURE_DISABLE;
  1003. logError(0, "%s %s: Disabling feature %08X ...\n",
  1004. tag, __func__, feature);
  1005. }
  1006. u32ToU8(feature, &cmd[1]);
  1007. //not use writeFwCmd because this function can be
  1008. //called also during interrupt enable and should be fast
  1009. ret = fts_writeCmd(cmd, 5);
  1010. if (ret < OK) {
  1011. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ret);
  1012. return (ret | ERROR_FEATURE_ENABLE_DISABLE);
  1013. }
  1014. logError(0, "%s %s: DONE!\n", tag, __func__);
  1015. return OK;
  1016. }
  1017. int writeNoiseParameters(u8 *noise)
  1018. {
  1019. int ret, i;
  1020. u8 cmd[2+NOISE_PARAMETERS_SIZE];
  1021. u8 readData[FIFO_EVENT_SIZE];
  1022. int event_to_search[2] = {EVENTID_NOISE_WRITE, NOISE_PARAMETERS};
  1023. logError(0, "%s %s: Writing noise parameters to the IC ...\n",
  1024. tag, __func__);
  1025. ret = fts_disableInterrupt();
  1026. if (ret < OK) {
  1027. logError(1, "%s %s: ERROR %08X\n", tag, __func__, ret);
  1028. ret = (ret | ERROR_NOISE_PARAMETERS);
  1029. goto ERROR;
  1030. }
  1031. cmd[0] = FTS_CMD_NOISE_WRITE;
  1032. cmd[1] = NOISE_PARAMETERS;
  1033. logError(0, "%s %s: Noise parameters = ", tag, __func__);
  1034. for (i = 0; i < NOISE_PARAMETERS_SIZE; i++) {
  1035. cmd[2 + i] = noise[i];
  1036. logError(0, "%02X", cmd[2 + i]);
  1037. }
  1038. logError(0, "\n");
  1039. ret = fts_writeCmd(cmd, NOISE_PARAMETERS_SIZE + 2);
  1040. //not use writeFwCmd because this function should be fast
  1041. if (ret < OK) {
  1042. logError(0, "%s %s:impossible write command... ERROR %02X\n",
  1043. tag, __func__, ret);
  1044. ret = (ret | ERROR_NOISE_PARAMETERS);
  1045. goto ERROR;
  1046. }
  1047. ret = pollForEvent(event_to_search, 2, readData, GENERAL_TIMEOUT);
  1048. if (ret < OK) {
  1049. logError(0, "%s %s: polling FIFO ERROR %02X\n",
  1050. tag, __func__, ret);
  1051. ret = (ret | ERROR_NOISE_PARAMETERS);
  1052. goto ERROR;
  1053. }
  1054. if (readData[2] != 0x00) {
  1055. logError(1, "%s %s:Event check FAIL! %02X != 0x00 ERROR%02X\n",
  1056. tag, __func__, readData[2], ERROR_NOISE_PARAMETERS);
  1057. ret = ERROR_NOISE_PARAMETERS;
  1058. goto ERROR;
  1059. }
  1060. logError(0, "%s %s:DONE!\n", tag, __func__);
  1061. ret = OK;
  1062. ERROR:
  1063. ret = fts_enableInterrupt();
  1064. //ensure that the interrupt are always renabled when exit from funct
  1065. if (ret < OK) {
  1066. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ret);
  1067. return (ret | ERROR_NOISE_PARAMETERS);
  1068. }
  1069. return ret;
  1070. }
  1071. int readNoiseParameters(u8 *noise)
  1072. {
  1073. int ret, i;
  1074. u8 cmd[2];
  1075. u8 readData[FIFO_EVENT_SIZE];
  1076. int event_to_search[2] = {EVENTID_NOISE_READ, NOISE_PARAMETERS};
  1077. logError(0, "%s %s:Reading noise parameters from the IC ...\n",
  1078. tag, __func__);
  1079. ret = fts_disableInterrupt();
  1080. if (ret < OK) {
  1081. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ret);
  1082. ret = (ret | ERROR_NOISE_PARAMETERS);
  1083. goto ERROR;
  1084. }
  1085. cmd[0] = FTS_CMD_NOISE_READ;
  1086. cmd[1] = NOISE_PARAMETERS;
  1087. ret = fts_writeCmd(cmd, 2);//not use writeFwCmd should be fast
  1088. if (ret < OK) {
  1089. logError(0, "%s %s:impossible write command... ERROR %02X\n",
  1090. tag, __func__, ret);
  1091. ret = (ret | ERROR_NOISE_PARAMETERS);
  1092. goto ERROR;
  1093. }
  1094. ret = pollForEvent(event_to_search, 2, readData, GENERAL_TIMEOUT);
  1095. if (ret < OK) {
  1096. logError(0, "%s %s: polling FIFO ERROR %02X\n",
  1097. tag, __func__, ret);
  1098. ret = (ret | ERROR_NOISE_PARAMETERS);
  1099. goto ERROR;
  1100. }
  1101. logError(0, "%s %s: Noise parameters = ", tag, __func__);
  1102. for (i = 0; i < NOISE_PARAMETERS_SIZE; i++) {
  1103. noise[i] = readData[2 + i];
  1104. logError(0, "%02X ", noise[i]);
  1105. }
  1106. logError(0, "\n");
  1107. logError(0, "%s %s: DONE!\n", tag, __func__);
  1108. ret = OK;
  1109. ERROR:
  1110. ret = fts_enableInterrupt();
  1111. //ensure that the interrupt are always renabled when exit from funct
  1112. if (ret < OK) {
  1113. logError(1, "%s %s: ERROR %02X\n", tag, __func__, ret);
  1114. return (ret | ERROR_NOISE_PARAMETERS);
  1115. }
  1116. return ret;
  1117. }
  1118. short **array1dTo2d_short(short *data, int size, int columns)
  1119. {
  1120. int i;
  1121. int count = size / columns;
  1122. short **matrix = (short **)kmalloc_array(count,
  1123. sizeof(short *), GFP_KERNEL);
  1124. if (matrix != NULL) {
  1125. for (i = 0; i < count; i++) {
  1126. matrix[i] = (short *)kmalloc_array(columns,
  1127. sizeof(short), GFP_KERNEL);
  1128. }
  1129. for (i = 0; i < size; i++)
  1130. matrix[i / columns][i % columns] = data[i];
  1131. }
  1132. return matrix;
  1133. }
  1134. u8 **array1dTo2d_u8(u8 *data, int size, int columns)
  1135. {
  1136. int i;
  1137. int count = size / columns;
  1138. u8 **matrix = (u8 **)kmalloc_array(count,
  1139. sizeof(u8 *), GFP_KERNEL);
  1140. if (matrix != NULL) {
  1141. for (i = 0; i < count; i++) {
  1142. matrix[i] = (u8 *)kmalloc_array(columns,
  1143. sizeof(u8), GFP_KERNEL);
  1144. }
  1145. for (i = 0; i < size; i++)
  1146. matrix[i / columns][i % columns] = data[i];
  1147. }
  1148. return matrix;
  1149. }
  1150. void print_frame_short(char *label, short **matrix, int row, int column)
  1151. {
  1152. int i, j;
  1153. logError(0, "%s %s\n", tag, label);
  1154. for (i = 0; i < row; i++) {
  1155. logError(0, "%s ", tag);
  1156. for (j = 0; j < column; j++)
  1157. logError(0, "%d", matrix[i][j]);
  1158. logError(0, "\n");
  1159. kfree(matrix[i]);
  1160. }
  1161. kfree(matrix);
  1162. }
  1163. void print_frame_u8(char *label, u8 **matrix, int row, int column)
  1164. {
  1165. int i, j;
  1166. logError(0, "%s %s\n", tag, label);
  1167. for (i = 0; i < row; i++) {
  1168. logError(0, "%s ", tag);
  1169. for (j = 0; j < column; j++)
  1170. logError(0, "%d ", matrix[i][j]);
  1171. logError(0, "\n");
  1172. kfree(matrix[i]);
  1173. }
  1174. kfree(matrix);
  1175. }
  1176. void print_frame_u32(char *label, u32 **matrix, int row, int column)
  1177. {
  1178. int i, j;
  1179. logError(0, "%s %s\n", tag, label);
  1180. for (i = 0; i < row; i++) {
  1181. logError(0, "%s ", tag);
  1182. for (j = 0; j < column; j++)
  1183. logError(0, "%d ", matrix[i][j]);
  1184. logError(0, "\n");
  1185. kfree(matrix[i]);
  1186. }
  1187. kfree(matrix);
  1188. }
  1189. void print_frame_int(char *label, int **matrix, int row, int column)
  1190. {
  1191. int i, j;
  1192. logError(0, "%s %s\n", tag, label);
  1193. for (i = 0; i < row; i++) {
  1194. logError(0, "%s ", tag);
  1195. for (j = 0; j < column; j++)
  1196. logError(0, "%d ", matrix[i][j]);
  1197. logError(0, "\n");
  1198. kfree(matrix[i]);
  1199. }
  1200. kfree(matrix);
  1201. }