synaptics_tcm_recovery.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*
  2. * Synaptics TCM touchscreen driver
  3. *
  4. * Copyright (C) 2017-2019 Synaptics Incorporated. All rights reserved.
  5. *
  6. * Copyright (C) 2017-2019 Scott Lin <[email protected]>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * INFORMATION CONTAINED IN THIS DOCUMENT IS PROVIDED "AS-IS," AND SYNAPTICS
  19. * EXPRESSLY DISCLAIMS ALL EXPRESS AND IMPLIED WARRANTIES, INCLUDING ANY
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
  21. * AND ANY WARRANTIES OF NON-INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS.
  22. * IN NO EVENT SHALL SYNAPTICS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, PUNITIVE, OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR IN CONNECTION
  24. * WITH THE USE OF THE INFORMATION CONTAINED IN THIS DOCUMENT, HOWEVER CAUSED
  25. * AND BASED ON ANY THEORY OF LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  26. * NEGLIGENCE OR OTHER TORTIOUS ACTION, AND EVEN IF SYNAPTICS WAS ADVISED OF
  27. * THE POSSIBILITY OF SUCH DAMAGE. IF A TRIBUNAL OF COMPETENT JURISDICTION DOES
  28. * NOT PERMIT THE DISCLAIMER OF DIRECT DAMAGES OR ANY OTHER DAMAGES, SYNAPTICS'
  29. * TOTAL CUMULATIVE LIABILITY TO ANY PARTY SHALL NOT EXCEED ONE HUNDRED U.S.
  30. * DOLLARS.
  31. */
  32. #include "synaptics_tcm_core.h"
  33. #define SET_UP_RECOVERY_MODE true
  34. #define ENABLE_SYSFS_INTERFACE true
  35. #define SYSFS_DIR_NAME "recovery"
  36. #define IHEX_BUF_SIZE (2048 * 1024)
  37. #define DATA_BUF_SIZE (512 * 1024)
  38. #define IHEX_RECORD_SIZE 14
  39. #define PDT_START_ADDR 0x00e9
  40. #define UBL_FN_NUMBER 0x35
  41. #define F35_CHUNK_SIZE 16
  42. #define F35_CHUNK_SIZE_WORDS 8
  43. #define F35_ERASE_ALL_WAIT_MS 5000
  44. #define F35_ERASE_ALL_POLL_MS 100
  45. #define F35_DATA5_OFFSET 5
  46. #define F35_CTRL3_OFFSET 18
  47. #define F35_RESET_COMMAND 16
  48. #define F35_ERASE_ALL_COMMAND 3
  49. #define F35_WRITE_CHUNK_COMMAND 2
  50. #define F35_READ_FLASH_STATUS_COMMAND 1
  51. struct rmi_pdt_entry {
  52. unsigned char query_base_addr;
  53. unsigned char command_base_addr;
  54. unsigned char control_base_addr;
  55. unsigned char data_base_addr;
  56. unsigned char intr_src_count:3;
  57. unsigned char reserved_1:2;
  58. unsigned char fn_version:2;
  59. unsigned char reserved_2:1;
  60. unsigned char fn_number;
  61. } __packed;
  62. struct rmi_addr {
  63. unsigned short query_base;
  64. unsigned short command_base;
  65. unsigned short control_base;
  66. unsigned short data_base;
  67. };
  68. struct recovery_hcd {
  69. bool set_up_recovery_mode;
  70. unsigned char chunk_buf[F35_CHUNK_SIZE + 3];
  71. unsigned char out_buf[3];
  72. unsigned char *ihex_buf;
  73. unsigned char *data_buf;
  74. unsigned int ihex_size;
  75. unsigned int ihex_records;
  76. unsigned int data_entries;
  77. struct kobject *sysfs_dir;
  78. struct rmi_addr f35_addr;
  79. struct syna_tcm_hcd *tcm_hcd;
  80. };
  81. DECLARE_COMPLETION(recovery_remove_complete);
  82. static struct recovery_hcd *recovery_hcd;
  83. static int recovery_do_recovery(void);
  84. STORE_PROTOTYPE(recovery, recovery);
  85. static struct device_attribute *attrs[] = {
  86. ATTRIFY(recovery),
  87. };
  88. static ssize_t recovery_sysfs_ihex_store(struct file *data_file,
  89. struct kobject *kobj, struct bin_attribute *attributes,
  90. char *buf, loff_t pos, size_t count);
  91. static struct bin_attribute bin_attr = {
  92. .attr = {
  93. .name = "ihex",
  94. .mode = 0220,
  95. },
  96. .size = 0,
  97. .write = recovery_sysfs_ihex_store,
  98. };
  99. static ssize_t recovery_sysfs_recovery_store(struct device *dev,
  100. struct device_attribute *attr, const char *buf, size_t count)
  101. {
  102. int retval;
  103. unsigned int input;
  104. struct syna_tcm_hcd *tcm_hcd = recovery_hcd->tcm_hcd;
  105. if (kstrtouint(buf, 10, &input))
  106. return -EINVAL;
  107. if (input == 1)
  108. recovery_hcd->set_up_recovery_mode = true;
  109. else if (input == 2)
  110. recovery_hcd->set_up_recovery_mode = false;
  111. else
  112. return -EINVAL;
  113. mutex_lock(&tcm_hcd->extif_mutex);
  114. if (recovery_hcd->ihex_size == 0) {
  115. LOGE(tcm_hcd->pdev->dev.parent,
  116. "Failed to get ihex data\n");
  117. retval = -EINVAL;
  118. goto exit;
  119. }
  120. if (recovery_hcd->ihex_size % IHEX_RECORD_SIZE) {
  121. LOGE(tcm_hcd->pdev->dev.parent,
  122. "Invalid ihex data\n");
  123. retval = -EINVAL;
  124. goto exit;
  125. }
  126. recovery_hcd->ihex_records = recovery_hcd->ihex_size / IHEX_RECORD_SIZE;
  127. retval = recovery_do_recovery();
  128. if (retval < 0) {
  129. LOGE(tcm_hcd->pdev->dev.parent,
  130. "Failed to do recovery\n");
  131. goto exit;
  132. }
  133. retval = count;
  134. exit:
  135. recovery_hcd->set_up_recovery_mode = SET_UP_RECOVERY_MODE;
  136. mutex_unlock(&tcm_hcd->extif_mutex);
  137. return retval;
  138. }
  139. static ssize_t recovery_sysfs_ihex_store(struct file *data_file,
  140. struct kobject *kobj, struct bin_attribute *attributes,
  141. char *buf, loff_t pos, size_t count)
  142. {
  143. int retval;
  144. struct syna_tcm_hcd *tcm_hcd = recovery_hcd->tcm_hcd;
  145. mutex_lock(&tcm_hcd->extif_mutex);
  146. retval = secure_memcpy(&recovery_hcd->ihex_buf[pos],
  147. IHEX_BUF_SIZE - pos,
  148. buf,
  149. count,
  150. count);
  151. if (retval < 0) {
  152. LOGE(tcm_hcd->pdev->dev.parent,
  153. "Failed to copy ihex data\n");
  154. recovery_hcd->ihex_size = 0;
  155. goto exit;
  156. }
  157. recovery_hcd->ihex_size = pos + count;
  158. retval = count;
  159. exit:
  160. mutex_unlock(&tcm_hcd->extif_mutex);
  161. return retval;
  162. }
  163. static int recovery_device_reset(void)
  164. {
  165. int retval;
  166. unsigned char command;
  167. struct syna_tcm_hcd *tcm_hcd = recovery_hcd->tcm_hcd;
  168. const struct syna_tcm_board_data *bdata = tcm_hcd->hw_if->bdata;
  169. command = F35_RESET_COMMAND;
  170. retval = syna_tcm_rmi_write(tcm_hcd,
  171. recovery_hcd->f35_addr.control_base + F35_CTRL3_OFFSET,
  172. &command,
  173. sizeof(command));
  174. if (retval < 0) {
  175. LOGE(tcm_hcd->pdev->dev.parent,
  176. "Failed to write F$35 command\n");
  177. return retval;
  178. }
  179. msleep(bdata->reset_delay_ms);
  180. return 0;
  181. }
  182. static int recovery_add_data_entry(unsigned char data)
  183. {
  184. struct syna_tcm_hcd *tcm_hcd = recovery_hcd->tcm_hcd;
  185. if (recovery_hcd->data_entries >= DATA_BUF_SIZE) {
  186. LOGE(tcm_hcd->pdev->dev.parent,
  187. "Reached data buffer size limit\n");
  188. return -EINVAL;
  189. }
  190. recovery_hcd->data_buf[recovery_hcd->data_entries++] = data;
  191. return 0;
  192. }
  193. static int recovery_add_padding(unsigned int *words)
  194. {
  195. int retval;
  196. unsigned int padding;
  197. struct syna_tcm_hcd *tcm_hcd = recovery_hcd->tcm_hcd;
  198. padding = (F35_CHUNK_SIZE_WORDS - *words % F35_CHUNK_SIZE_WORDS);
  199. padding %= F35_CHUNK_SIZE_WORDS;
  200. while (padding) {
  201. retval = recovery_add_data_entry(0xff);
  202. if (retval < 0) {
  203. LOGE(tcm_hcd->pdev->dev.parent,
  204. "Failed to add data entry\n");
  205. return retval;
  206. }
  207. retval = recovery_add_data_entry(0xff);
  208. if (retval < 0) {
  209. LOGE(tcm_hcd->pdev->dev.parent,
  210. "Failed to add data entry\n");
  211. return retval;
  212. }
  213. (*words)++;
  214. padding--;
  215. }
  216. return 0;
  217. }
  218. static int recovery_parse_ihex(void)
  219. {
  220. int retval;
  221. unsigned char colon;
  222. unsigned char *buf;
  223. unsigned int addr;
  224. unsigned int type;
  225. unsigned int addrl;
  226. unsigned int addrh;
  227. unsigned int data0;
  228. unsigned int data1;
  229. unsigned int count;
  230. unsigned int words;
  231. unsigned int offset;
  232. unsigned int record;
  233. struct syna_tcm_hcd *tcm_hcd = recovery_hcd->tcm_hcd;
  234. words = 0;
  235. offset = 0;
  236. buf = recovery_hcd->ihex_buf;
  237. recovery_hcd->data_entries = 0;
  238. for (record = 0; record < recovery_hcd->ihex_records; record++) {
  239. buf[(record + 1) * IHEX_RECORD_SIZE - 1] = 0x00;
  240. retval = sscanf(&buf[record * IHEX_RECORD_SIZE],
  241. "%c%02x%02x%02x%02x%02x%02x",
  242. &colon,
  243. &count,
  244. &addrh,
  245. &addrl,
  246. &type,
  247. &data0,
  248. &data1);
  249. if (retval != 7) {
  250. LOGE(tcm_hcd->pdev->dev.parent,
  251. "Failed to read ihex record\n");
  252. return -EINVAL;
  253. }
  254. if (type == 0x00) {
  255. if ((words % F35_CHUNK_SIZE_WORDS) == 0) {
  256. addr = (addrh << 8) + addrl;
  257. addr += offset;
  258. addr >>= 4;
  259. retval = recovery_add_data_entry(addr);
  260. if (retval < 0) {
  261. LOGE(tcm_hcd->pdev->dev.parent,
  262. "Failed to add data entry\n");
  263. return retval;
  264. }
  265. retval = recovery_add_data_entry(addr >> 8);
  266. if (retval < 0) {
  267. LOGE(tcm_hcd->pdev->dev.parent,
  268. "Failed to add data entry\n");
  269. return retval;
  270. }
  271. }
  272. retval = recovery_add_data_entry(data0);
  273. if (retval < 0) {
  274. LOGE(tcm_hcd->pdev->dev.parent,
  275. "Failed to add data entry\n");
  276. return retval;
  277. }
  278. retval = recovery_add_data_entry(data1);
  279. if (retval < 0) {
  280. LOGE(tcm_hcd->pdev->dev.parent,
  281. "Failed to add data entry\n");
  282. return retval;
  283. }
  284. words++;
  285. } else if (type == 0x02) {
  286. retval = recovery_add_padding(&words);
  287. if (retval < 0) {
  288. LOGE(tcm_hcd->pdev->dev.parent,
  289. "Failed to add padding\n");
  290. return retval;
  291. }
  292. offset = (data0 << 8) + data1;
  293. offset <<= 4;
  294. }
  295. }
  296. retval = recovery_add_padding(&words);
  297. if (retval < 0) {
  298. LOGE(tcm_hcd->pdev->dev.parent,
  299. "Failed to add padding\n");
  300. return retval;
  301. }
  302. return 0;
  303. }
  304. static int recovery_check_status(void)
  305. {
  306. int retval;
  307. unsigned char status;
  308. struct syna_tcm_hcd *tcm_hcd = recovery_hcd->tcm_hcd;
  309. retval = syna_tcm_rmi_read(tcm_hcd,
  310. recovery_hcd->f35_addr.data_base,
  311. &status,
  312. sizeof(status));
  313. if (retval < 0) {
  314. LOGE(tcm_hcd->pdev->dev.parent,
  315. "Failed to read status\n");
  316. return retval;
  317. }
  318. status = status & 0x1f;
  319. if (status != 0x00) {
  320. LOGE(tcm_hcd->pdev->dev.parent,
  321. "Recovery mode status = 0x%02x\n",
  322. status);
  323. return -EINVAL;
  324. }
  325. return 0;
  326. }
  327. static int recovery_write_flash(void)
  328. {
  329. int retval;
  330. unsigned char *data_ptr;
  331. unsigned int chunk_buf_size;
  332. unsigned int chunk_data_size;
  333. unsigned int entries_written;
  334. unsigned int entries_to_write;
  335. struct syna_tcm_hcd *tcm_hcd = recovery_hcd->tcm_hcd;
  336. entries_written = 0;
  337. data_ptr = recovery_hcd->data_buf;
  338. chunk_buf_size = sizeof(recovery_hcd->chunk_buf);
  339. chunk_data_size = chunk_buf_size - 1;
  340. recovery_hcd->chunk_buf[chunk_buf_size - 1] = F35_WRITE_CHUNK_COMMAND;
  341. while (entries_written < recovery_hcd->data_entries) {
  342. entries_to_write = F35_CHUNK_SIZE + 2;
  343. retval = secure_memcpy(recovery_hcd->chunk_buf,
  344. chunk_buf_size - 1,
  345. data_ptr,
  346. recovery_hcd->data_entries - entries_written,
  347. entries_to_write);
  348. if (retval < 0) {
  349. LOGE(tcm_hcd->pdev->dev.parent,
  350. "Failed to copy chunk data\n");
  351. return retval;
  352. }
  353. retval = syna_tcm_rmi_write(tcm_hcd,
  354. recovery_hcd->f35_addr.control_base,
  355. recovery_hcd->chunk_buf,
  356. chunk_buf_size);
  357. if (retval < 0) {
  358. LOGE(tcm_hcd->pdev->dev.parent,
  359. "Failed to write chunk data\n");
  360. return retval;
  361. }
  362. data_ptr += entries_to_write;
  363. entries_written += entries_to_write;
  364. }
  365. retval = recovery_check_status();
  366. if (retval < 0) {
  367. LOGE(tcm_hcd->pdev->dev.parent,
  368. "Failed to get no error recovery mode status\n");
  369. return retval;
  370. }
  371. return 0;
  372. }
  373. static int recovery_poll_erase_completion(void)
  374. {
  375. int retval;
  376. unsigned char status;
  377. unsigned char command;
  378. unsigned char data_base;
  379. unsigned int timeout;
  380. struct syna_tcm_hcd *tcm_hcd = recovery_hcd->tcm_hcd;
  381. timeout = F35_ERASE_ALL_WAIT_MS;
  382. data_base = recovery_hcd->f35_addr.data_base;
  383. do {
  384. command = F35_READ_FLASH_STATUS_COMMAND;
  385. retval = syna_tcm_rmi_write(tcm_hcd,
  386. recovery_hcd->f35_addr.command_base,
  387. &command,
  388. sizeof(command));
  389. if (retval < 0) {
  390. LOGE(tcm_hcd->pdev->dev.parent,
  391. "Failed to write F$35 command\n");
  392. return retval;
  393. }
  394. do {
  395. retval = syna_tcm_rmi_read(tcm_hcd,
  396. recovery_hcd->f35_addr.command_base,
  397. &command,
  398. sizeof(command));
  399. if (retval < 0) {
  400. LOGE(tcm_hcd->pdev->dev.parent,
  401. "Failed to read command status\n");
  402. return retval;
  403. }
  404. if (command == 0x00)
  405. break;
  406. if (timeout == 0)
  407. break;
  408. msleep(F35_ERASE_ALL_POLL_MS);
  409. timeout -= F35_ERASE_ALL_POLL_MS;
  410. } while (true);
  411. if (command != 0 && timeout == 0) {
  412. retval = -EINVAL;
  413. goto exit;
  414. }
  415. retval = syna_tcm_rmi_read(tcm_hcd,
  416. data_base + F35_DATA5_OFFSET,
  417. &status,
  418. sizeof(status));
  419. if (retval < 0) {
  420. LOGE(tcm_hcd->pdev->dev.parent,
  421. "Failed to read flash status\n");
  422. return retval;
  423. }
  424. if ((status & 0x01) == 0x00)
  425. break;
  426. if (timeout == 0) {
  427. retval = -EINVAL;
  428. goto exit;
  429. }
  430. msleep(F35_ERASE_ALL_POLL_MS);
  431. timeout -= F35_ERASE_ALL_POLL_MS;
  432. } while (true);
  433. retval = 0;
  434. exit:
  435. if (retval < 0) {
  436. LOGE(tcm_hcd->pdev->dev.parent,
  437. "Failed to get erase completion\n");
  438. }
  439. return retval;
  440. }
  441. static int recovery_erase_flash(void)
  442. {
  443. int retval;
  444. unsigned char command;
  445. struct syna_tcm_hcd *tcm_hcd = recovery_hcd->tcm_hcd;
  446. command = F35_ERASE_ALL_COMMAND;
  447. retval = syna_tcm_rmi_write(tcm_hcd,
  448. recovery_hcd->f35_addr.control_base + F35_CTRL3_OFFSET,
  449. &command,
  450. sizeof(command));
  451. if (retval < 0) {
  452. LOGE(tcm_hcd->pdev->dev.parent,
  453. "Failed to write F$35 command\n");
  454. return retval;
  455. }
  456. if (recovery_hcd->f35_addr.command_base) {
  457. retval = recovery_poll_erase_completion();
  458. if (retval < 0) {
  459. LOGE(tcm_hcd->pdev->dev.parent,
  460. "Failed to wait for erase completion\n");
  461. return retval;
  462. }
  463. } else {
  464. msleep(F35_ERASE_ALL_WAIT_MS);
  465. }
  466. retval = recovery_check_status();
  467. if (retval < 0) {
  468. LOGE(tcm_hcd->pdev->dev.parent,
  469. "Failed to get no error recovery mode status\n");
  470. return retval;
  471. }
  472. return 0;
  473. }
  474. static int recovery_set_up_recovery_mode(void)
  475. {
  476. int retval;
  477. struct syna_tcm_hcd *tcm_hcd = recovery_hcd->tcm_hcd;
  478. const struct syna_tcm_board_data *bdata = tcm_hcd->hw_if->bdata;
  479. retval = tcm_hcd->identify(tcm_hcd, true);
  480. if (retval < 0) {
  481. LOGE(tcm_hcd->pdev->dev.parent,
  482. "Failed to do identification\n");
  483. return retval;
  484. }
  485. if (tcm_hcd->id_info.mode == MODE_APPLICATION) {
  486. retval = tcm_hcd->switch_mode(tcm_hcd, FW_MODE_BOOTLOADER);
  487. if (retval < 0) {
  488. LOGE(tcm_hcd->pdev->dev.parent,
  489. "Failed to enter bootloader mode\n");
  490. return retval;
  491. }
  492. }
  493. retval = tcm_hcd->write_message(tcm_hcd,
  494. recovery_hcd->out_buf[0],
  495. &recovery_hcd->out_buf[1],
  496. 2,
  497. NULL,
  498. NULL,
  499. NULL,
  500. NULL,
  501. 0);
  502. if (retval < 0) {
  503. LOGE(tcm_hcd->pdev->dev.parent,
  504. "Failed to write command %s\n",
  505. STR(CMD_REBOOT_TO_ROM_BOOTLOADER));
  506. return retval;
  507. }
  508. msleep(bdata->reset_delay_ms);
  509. return 0;
  510. }
  511. static int recovery_do_recovery(void)
  512. {
  513. int retval;
  514. struct rmi_pdt_entry p_entry;
  515. struct syna_tcm_hcd *tcm_hcd = recovery_hcd->tcm_hcd;
  516. retval = recovery_parse_ihex();
  517. if (retval < 0) {
  518. LOGE(tcm_hcd->pdev->dev.parent,
  519. "Failed to parse ihex data\n");
  520. return retval;
  521. }
  522. if (recovery_hcd->set_up_recovery_mode) {
  523. retval = recovery_set_up_recovery_mode();
  524. if (retval < 0) {
  525. LOGE(tcm_hcd->pdev->dev.parent,
  526. "Failed to set up recovery mode\n");
  527. return retval;
  528. }
  529. }
  530. tcm_hcd->update_watchdog(tcm_hcd, false);
  531. retval = syna_tcm_rmi_read(tcm_hcd,
  532. PDT_START_ADDR,
  533. (unsigned char *)&p_entry,
  534. sizeof(p_entry));
  535. if (retval < 0) {
  536. LOGE(tcm_hcd->pdev->dev.parent,
  537. "Failed to read PDT entry\n");
  538. return retval;
  539. }
  540. if (p_entry.fn_number != UBL_FN_NUMBER) {
  541. LOGE(tcm_hcd->pdev->dev.parent,
  542. "Failed to find F$35\n");
  543. return -ENODEV;
  544. }
  545. recovery_hcd->f35_addr.query_base = p_entry.query_base_addr;
  546. recovery_hcd->f35_addr.command_base = p_entry.command_base_addr;
  547. recovery_hcd->f35_addr.control_base = p_entry.control_base_addr;
  548. recovery_hcd->f35_addr.data_base = p_entry.data_base_addr;
  549. LOGN(tcm_hcd->pdev->dev.parent,
  550. "Start of recovery\n");
  551. retval = recovery_erase_flash();
  552. if (retval < 0) {
  553. LOGE(tcm_hcd->pdev->dev.parent,
  554. "Failed to erase flash\n");
  555. return retval;
  556. }
  557. LOGN(tcm_hcd->pdev->dev.parent,
  558. "Flash erased\n");
  559. retval = recovery_write_flash();
  560. if (retval < 0) {
  561. LOGE(tcm_hcd->pdev->dev.parent,
  562. "Failed to write to flash\n");
  563. return retval;
  564. }
  565. LOGN(tcm_hcd->pdev->dev.parent,
  566. "Flash written\n");
  567. retval = recovery_device_reset();
  568. if (retval < 0) {
  569. LOGE(tcm_hcd->pdev->dev.parent,
  570. "Failed to do reset\n");
  571. return retval;
  572. }
  573. LOGN(tcm_hcd->pdev->dev.parent,
  574. "End of recovery\n");
  575. if (recovery_hcd->set_up_recovery_mode)
  576. return 0;
  577. tcm_hcd->update_watchdog(tcm_hcd, true);
  578. retval = tcm_hcd->enable_irq(tcm_hcd, true, NULL);
  579. if (retval < 0) {
  580. LOGE(tcm_hcd->pdev->dev.parent,
  581. "Failed to enable interrupt\n");
  582. return retval;
  583. }
  584. retval = tcm_hcd->identify(tcm_hcd, true);
  585. if (retval < 0) {
  586. LOGE(tcm_hcd->pdev->dev.parent,
  587. "Failed to do identification\n");
  588. return retval;
  589. }
  590. if (tcm_hcd->id_info.mode != MODE_APPLICATION) {
  591. retval = tcm_hcd->switch_mode(tcm_hcd, FW_MODE_APPLICATION);
  592. if (retval < 0) {
  593. LOGE(tcm_hcd->pdev->dev.parent,
  594. "Failed to run application firmware\n");
  595. return retval;
  596. }
  597. }
  598. return 0;
  599. }
  600. static int recovery_init(struct syna_tcm_hcd *tcm_hcd)
  601. {
  602. int retval = 0;
  603. int idx;
  604. recovery_hcd = kzalloc(sizeof(*recovery_hcd), GFP_KERNEL);
  605. if (!recovery_hcd) {
  606. LOGE(tcm_hcd->pdev->dev.parent,
  607. "Failed to allocate memory for recovery_hcd\n");
  608. return -ENOMEM;
  609. }
  610. recovery_hcd->ihex_buf = kzalloc(IHEX_BUF_SIZE, GFP_KERNEL);
  611. if (!recovery_hcd->ihex_buf) {
  612. LOGE(tcm_hcd->pdev->dev.parent,
  613. "Failed to allocate memory for ihex_buf\n");
  614. goto err_allocate_ihex_buf;
  615. }
  616. recovery_hcd->data_buf = kzalloc(DATA_BUF_SIZE, GFP_KERNEL);
  617. if (!recovery_hcd->data_buf) {
  618. LOGE(tcm_hcd->pdev->dev.parent,
  619. "Failed to allocate memory for data_buf\n");
  620. goto err_allocate_data_buf;
  621. }
  622. recovery_hcd->tcm_hcd = tcm_hcd;
  623. recovery_hcd->set_up_recovery_mode = SET_UP_RECOVERY_MODE;
  624. recovery_hcd->out_buf[0] = CMD_REBOOT_TO_ROM_BOOTLOADER;
  625. recovery_hcd->out_buf[1] = 0;
  626. recovery_hcd->out_buf[2] = 0;
  627. if (!ENABLE_SYSFS_INTERFACE)
  628. return 0;
  629. recovery_hcd->sysfs_dir = kobject_create_and_add(SYSFS_DIR_NAME,
  630. tcm_hcd->sysfs_dir);
  631. if (!recovery_hcd->sysfs_dir) {
  632. LOGE(tcm_hcd->pdev->dev.parent,
  633. "Failed to create sysfs directory\n");
  634. retval = -EINVAL;
  635. goto err_sysfs_create_dir;
  636. }
  637. for (idx = 0; idx < ARRAY_SIZE(attrs); idx++) {
  638. retval = sysfs_create_file(recovery_hcd->sysfs_dir,
  639. &(*attrs[idx]).attr);
  640. if (retval < 0) {
  641. LOGE(tcm_hcd->pdev->dev.parent,
  642. "Failed to create sysfs file\n");
  643. goto err_sysfs_create_file;
  644. }
  645. }
  646. retval = sysfs_create_bin_file(recovery_hcd->sysfs_dir, &bin_attr);
  647. if (retval < 0) {
  648. LOGE(tcm_hcd->pdev->dev.parent,
  649. "Failed to create sysfs bin file\n");
  650. goto err_sysfs_create_bin_file;
  651. }
  652. return 0;
  653. err_sysfs_create_bin_file:
  654. err_sysfs_create_file:
  655. for (idx--; idx >= 0; idx--)
  656. sysfs_remove_file(recovery_hcd->sysfs_dir, &(*attrs[idx]).attr);
  657. kobject_put(recovery_hcd->sysfs_dir);
  658. err_sysfs_create_dir:
  659. kfree(recovery_hcd->data_buf);
  660. err_allocate_data_buf:
  661. kfree(recovery_hcd->ihex_buf);
  662. err_allocate_ihex_buf:
  663. kfree(recovery_hcd);
  664. recovery_hcd = NULL;
  665. return retval;
  666. }
  667. static int recovery_remove(struct syna_tcm_hcd *tcm_hcd)
  668. {
  669. int idx;
  670. if (!recovery_hcd)
  671. goto exit;
  672. if (ENABLE_SYSFS_INTERFACE) {
  673. sysfs_remove_bin_file(recovery_hcd->sysfs_dir, &bin_attr);
  674. for (idx = 0; idx < ARRAY_SIZE(attrs); idx++) {
  675. sysfs_remove_file(recovery_hcd->sysfs_dir,
  676. &(*attrs[idx]).attr);
  677. }
  678. kobject_put(recovery_hcd->sysfs_dir);
  679. }
  680. kfree(recovery_hcd->data_buf);
  681. kfree(recovery_hcd->ihex_buf);
  682. kfree(recovery_hcd);
  683. recovery_hcd = NULL;
  684. exit:
  685. complete(&recovery_remove_complete);
  686. return 0;
  687. }
  688. static int recovery_reset(struct syna_tcm_hcd *tcm_hcd)
  689. {
  690. int retval;
  691. if (!recovery_hcd) {
  692. retval = recovery_init(tcm_hcd);
  693. return retval;
  694. }
  695. return 0;
  696. }
  697. static struct syna_tcm_module_cb recovery_module = {
  698. .type = TCM_RECOVERY,
  699. .init = recovery_init,
  700. .remove = recovery_remove,
  701. .syncbox = NULL,
  702. .asyncbox = NULL,
  703. .reset = recovery_reset,
  704. .suspend = NULL,
  705. .resume = NULL,
  706. .early_suspend = NULL,
  707. };
  708. static int __init recovery_module_init(void)
  709. {
  710. return syna_tcm_add_module(&recovery_module, true);
  711. }
  712. static void __exit recovery_module_exit(void)
  713. {
  714. syna_tcm_add_module(&recovery_module, false);
  715. wait_for_completion(&recovery_remove_complete);
  716. }
  717. module_init(recovery_module_init);
  718. module_exit(recovery_module_exit);
  719. MODULE_AUTHOR("Synaptics, Inc.");
  720. MODULE_DESCRIPTION("Synaptics TCM Recovery Module");
  721. MODULE_LICENSE("GPL v2");