wacom_i2c_flash.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. /*
  2. * wacom_i2c_flash.c - Wacom G5 Digitizer Controller (I2C bus)
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/wacom_i2c.h>
  20. #include "wacom_i2c_flash.h"
  21. #include <linux/uaccess.h>
  22. #define ERR_HEX 0x056
  23. #define RETRY_TRANSFER 5
  24. #define ERR_RET 1
  25. unsigned char checksum_data[4];
  26. int wacom_i2c_select_flash_code(struct wacom_i2c *wac_i2c)
  27. {
  28. int ret;
  29. dev_info(&wac_i2c->client->dev, "%s\n", __func__);
  30. if(wac_i2c->ic_mpu_ver < MPU_W9007)
  31. ret = wacom_i2c_flash(wac_i2c);
  32. else
  33. ret = wacom_i2c_flash_9007(wac_i2c);
  34. return ret;
  35. }
  36. int wacom_check_mpu_version(struct wacom_i2c *wac_i2c)
  37. {
  38. u8 buf[2] = {0xE0, 0};
  39. int ret;
  40. ret = wacom_i2c_send(wac_i2c, &buf[0], 1, WACOM_I2C_MODE_BOOT);
  41. if (ret == -ETIMEDOUT)
  42. goto err_i2c_timeout;
  43. usleep(1*1000);
  44. ret = wacom_i2c_recv(wac_i2c,
  45. &buf[1], 1, WACOM_I2C_MODE_BOOT);
  46. if (ret > 0)
  47. wac_i2c->ic_mpu_ver = MPU_W9007;
  48. else
  49. wac_i2c->ic_mpu_ver = MPU_W9001;
  50. dev_info(&wac_i2c->client->dev,
  51. "%s: boot mode : %x\n", __func__, buf[1]);
  52. return buf[1];
  53. err_i2c_timeout:
  54. return ret;
  55. }
  56. int calc_checksum(unsigned char *flash_data)
  57. {
  58. unsigned long i;
  59. if (ums_binary)
  60. return 0;
  61. for (i = 0; i < 4; i++)
  62. checksum_data[i] = 0;
  63. for (i = 0x0000; i < 0xC000; i += 4) {
  64. checksum_data[0] ^= flash_data[i];
  65. checksum_data[1] ^= flash_data[i+1];
  66. checksum_data[2] ^= flash_data[i+2];
  67. checksum_data[3] ^= flash_data[i+3];
  68. }
  69. printk(KERN_DEBUG "wacom_%s: %02x, %02x, %02x, %02x\n",
  70. __func__, checksum_data[0], checksum_data[1],
  71. checksum_data[2], checksum_data[3]);
  72. for (i = 0; i < 4; i++) {
  73. if (checksum_data[i] != Firmware_checksum[i+1])
  74. return -ERR_HEX;
  75. }
  76. return 0;
  77. }
  78. int wacom_i2c_flash_chksum(struct wacom_i2c *wac_i2c, unsigned char *flash_data,
  79. unsigned long *max_address)
  80. {
  81. unsigned long i;
  82. unsigned long chksum = 0;
  83. for (i = 0x0000; i <= *max_address; i++)
  84. chksum += flash_data[i];
  85. chksum &= 0xFFFF;
  86. return (int)chksum;
  87. }
  88. int wacom_i2c_flash_cmd(struct wacom_i2c *wac_i2c)
  89. {
  90. int ret, len, i;
  91. u8 buf[10], flashq;
  92. buf[0] = 0x0d;
  93. buf[1] = FLASH_START0;
  94. buf[2] = FLASH_START1;
  95. buf[3] = FLASH_START2;
  96. buf[4] = FLASH_START3;
  97. buf[5] = FLASH_START4;
  98. buf[6] = FLASH_START5;
  99. buf[7] = 0x0d;
  100. flashq = 0xE0;
  101. len = 1;
  102. ret = wacom_i2c_send(wac_i2c, &flashq, len, true);
  103. if (ret >= 0) {
  104. i = 0;
  105. do {
  106. usleep(1*1000);
  107. ret = wacom_i2c_recv(wac_i2c,
  108. &flashq, len, true);
  109. dev_err(&wac_i2c->client->dev,
  110. "%s: recv boot mode : %x\n", __func__, flashq);
  111. i++;
  112. if (i > RETRY)
  113. return -ERR_RET;
  114. } while (flashq == 0xff);
  115. } else {
  116. dev_err(&wac_i2c->client->dev, "%s: enter boot mode failed[%d], 1\n", __func__, ret);
  117. ret = wacom_i2c_send(wac_i2c, &flashq, len, true);
  118. if (ret < 0)
  119. dev_err(&wac_i2c->client->dev, "%s: enter boot mode failed[%d], 2\n", __func__, ret);
  120. usleep(1*1000);
  121. len = 8;
  122. ret = wacom_i2c_send(wac_i2c, buf, len, false);
  123. if (ret < 0) {
  124. dev_err(&wac_i2c->client->dev,
  125. "%s: Sending flash command failed\n",
  126. __func__);
  127. return -ERR_RET;
  128. }
  129. dev_info(&wac_i2c->client->dev,
  130. "%s: flash send?:%d\n", __func__, ret);
  131. msleep(270);
  132. }
  133. wac_i2c->boot_mode = true;
  134. return 0;
  135. }
  136. int wacom_i2c_flash_query(struct wacom_i2c *wac_i2c, u8 query, u8 recvdQuery)
  137. {
  138. int ret, len, i;
  139. u8 flashq;
  140. flashq = query;
  141. len = 1;
  142. ret = wacom_i2c_send(wac_i2c, &flashq, len, true);
  143. if (ret < 0) {
  144. dev_err(&wac_i2c->client->dev,
  145. "%s: query unsent:%d\n", __func__, ret);
  146. return -ERR_RET;
  147. }
  148. /*sleep*/
  149. usleep(10*1000);
  150. i = 0;
  151. do {
  152. usleep(1*1000);
  153. flashq = query;
  154. ret = wacom_i2c_recv(wac_i2c,
  155. &flashq, len, true);
  156. i++;
  157. if (i > RETRY)
  158. return -ERR_RET;
  159. dev_info(&wac_i2c->client->dev,
  160. "%s: ret:%d flashq:%x\n", __func__, ret, flashq);
  161. } while (recvdQuery == 0xff && flashq != recvdQuery);
  162. dev_info(&wac_i2c->client->dev,
  163. "%s: query:%x\n", __func__, flashq);
  164. return flashq;
  165. }
  166. int wacom_i2c_flash_end(struct wacom_i2c *wac_i2c)
  167. {
  168. int ret;
  169. /* 2012/07/04 Evaluation for 0x80 and 0xA0 added by Wacom*/
  170. do {
  171. ret = wacom_i2c_flash_query(wac_i2c, FLASH_END, FLASH_END);
  172. if (ret == -1)
  173. return ERR_FAILED_EXIT;
  174. } while (ret != 0x80); /*P130312-6278 (ret == 0xA0 || ret != 0x80);*/
  175. /* 2012/07/04 Evaluation for 0x80 and 0xA0 added by Wacom*/
  176. /*2012/07/05
  177. below added for giving firmware enough time to change to user mode*/
  178. msleep(1000);
  179. dev_info(&wac_i2c->client->dev,
  180. "%s: Digitizer activated\n", __func__);
  181. wac_i2c->boot_mode = false;
  182. return 0;
  183. }
  184. int wacom_i2c_flash_enter(struct wacom_i2c *wac_i2c)
  185. {
  186. if (wacom_i2c_flash_query(wac_i2c, FLASH_QUERY, FLASH_ACK) == -1)
  187. return ERR_NOT_FLASH;
  188. return 0;
  189. }
  190. int wacom_i2c_flash_BLVer(struct wacom_i2c *wac_i2c)
  191. {
  192. int ret = 0;
  193. ret = wacom_i2c_flash_query(wac_i2c, FLASH_BLVER, 0x40);
  194. if (ret == -1)
  195. return ERR_UNSENT;
  196. return ret;
  197. }
  198. int wacom_i2c_flash_mcuId(struct wacom_i2c *wac_i2c)
  199. {
  200. int ret = 0;
  201. ret = wacom_i2c_flash_query(wac_i2c, FLASH_MPU, 0x26);
  202. if (ret == -1)
  203. return ERR_UNSENT;
  204. return ret;
  205. }
  206. int wacom_i2c_flash_erase(struct wacom_i2c *wac_i2c, u8 cmd_erase,
  207. u8 cmd_block, u8 endBlock)
  208. {
  209. int len, ret, i, j;
  210. u8 buf[3], sum, block, flashq;
  211. unsigned long swtich_slot_time;
  212. ret = 0;
  213. for (i = cmd_block; i >= endBlock; i--) {
  214. block = i;
  215. block |= 0x80;
  216. sum = cmd_erase;
  217. sum += block;
  218. sum = ~sum + 1;
  219. buf[0] = cmd_erase;
  220. buf[1] = block;
  221. buf[2] = sum;
  222. len = 3;
  223. ret = wacom_i2c_send(wac_i2c, buf, len, true);
  224. if (ret < 0) {
  225. dev_err(&wac_i2c->client->dev,
  226. "%s: Erase failed\n", __func__);
  227. return -ERR_RET;
  228. }
  229. len = 1;
  230. flashq = 0;
  231. j = 0;
  232. do {
  233. /*sleep */
  234. msleep(100);
  235. ret = wacom_i2c_recv(wac_i2c,
  236. &flashq, len, true);
  237. j++;
  238. if (j > RETRY || flashq == 0x84 || flashq == 0x88
  239. || flashq == 0x8A || flashq == 0x90) {
  240. /*
  241. 0xff:No data
  242. 0x84:Erase failure
  243. 0x88:Erase time parameter error
  244. 0x8A:Write time parameter error
  245. 0x90:Checksum error
  246. */
  247. dev_err(&wac_i2c->client->dev,
  248. "%s: Error:%x\n", __func__, flashq);
  249. return -ERR_RET;
  250. }
  251. /* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
  252. } while (flashq != 0x06);
  253. /*P130312-6278 (flashq == 0xff || flashq != 0x06);*/
  254. if (printk_timed_ratelimit(&swtich_slot_time, 5000))
  255. dev_info(&wac_i2c->client->dev,
  256. "%s: Erasing at %d, ", __func__, i);
  257. /*sleep */
  258. usleep(1*1000);
  259. }
  260. dev_info(&wac_i2c->client->dev,
  261. "%s: Erasing done\n", __func__);
  262. return ret;
  263. }
  264. int wacom_i2c_flash_write(struct wacom_i2c *wac_i2c, unsigned long startAddr,
  265. u8 size, unsigned long maxAddr)
  266. {
  267. unsigned long ulAddr;
  268. int ret, len, i, j, k;
  269. char sum;
  270. u8 buf[WRITE_BUFF], bank;
  271. unsigned long swtich_slot_time;
  272. ret = len = i = 0;
  273. bank = BANK;
  274. for (ulAddr = startAddr; ulAddr <= maxAddr; ulAddr += BLOCK_SIZE_W) {
  275. sum = 0;
  276. buf[0] = FLASH_WRITE;
  277. buf[1] = (u8) (ulAddr & 0xff);
  278. buf[2] = (u8) ((ulAddr & 0xff00) >> 8);
  279. buf[3] = size;
  280. buf[4] = bank;
  281. /*Pass Garbage*/
  282. for (i = 0; i < BLOCK_SIZE_W; i++) {
  283. if (Binary[ulAddr+i] != 0xff)
  284. break;
  285. }
  286. if (i == BLOCK_SIZE_W) {
  287. dev_err(&wac_i2c->client->dev,
  288. "%s: Pass ulAddr %u\n",
  289. __func__, (unsigned int)ulAddr);
  290. continue;
  291. }
  292. for (i = 0; i < 5; i++)
  293. sum += buf[i];
  294. sum = ~sum + 1;
  295. buf[5] = sum;
  296. len = 6;
  297. /* 2012/07/18
  298. * if the returned data is not equal to the length of the bytes
  299. * that is supposed to send/receive, return it as fail
  300. */
  301. for (k = 0; k < RETRY_TRANSFER; k++) {
  302. ret = wacom_i2c_send(wac_i2c, buf, len, true);
  303. if (ret == len)
  304. break;
  305. if (ret < 0 || k == (RETRY_TRANSFER - 1)) {
  306. dev_err(&wac_i2c->client->dev,
  307. "%s: Write process aborted\n",
  308. __func__);
  309. return ERR_FAILED_ENTER;
  310. }
  311. }
  312. /*2012/07/18*/
  313. usleep(10*1000);
  314. len = 1;
  315. j = 0;
  316. do {
  317. usleep(5*1000);
  318. ret = wacom_i2c_recv(wac_i2c,
  319. buf, len, true);
  320. j++;
  321. if (j > RETRY || buf[0] == 0x90) {
  322. /*0xff:No data 0x90:Checksum error */
  323. dev_err(&wac_i2c->client->dev,
  324. "%s: Error: %x , 0x%lx(%d)\n",
  325. __func__, buf[0], ulAddr, __LINE__);
  326. return -ERR_RET;
  327. }
  328. /* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
  329. } while (buf[0] == 0xff || buf[0] != 0x06);
  330. /* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
  331. usleep(1*1000);
  332. sum = 0;
  333. for (i = 0; i < BLOCK_SIZE_W; i++) {
  334. buf[i] = Binary[ulAddr + i];
  335. sum += Binary[ulAddr + i];
  336. }
  337. sum = ~sum + 1;
  338. buf[BLOCK_SIZE_W] = sum;
  339. len = BLOCK_SIZE_W + 1;
  340. /* 2012/07/18
  341. * if the returned data is not equal to the length of the bytes
  342. * that is supposed to send/receive, return it as fail
  343. */
  344. for (k = 0; k < RETRY_TRANSFER; k++) {
  345. ret = wacom_i2c_send(wac_i2c, buf, len, true);
  346. if (ret == len)
  347. break;
  348. if (ret < 0 || k == (RETRY_TRANSFER - 1)) {
  349. dev_err(&wac_i2c->client->dev,
  350. "%s: Write process aborted\n",
  351. __func__);
  352. return ERR_FAILED_ENTER;
  353. }
  354. }
  355. /*2012/07/18*/
  356. msleep(50);
  357. len = 1;
  358. j = 0;
  359. do {
  360. usleep(10*1000);
  361. ret = wacom_i2c_recv(wac_i2c,
  362. buf, len, true);
  363. j++;
  364. if (j > RETRY || buf[0] == 0x82 || buf[0] == 0x90) {
  365. /*
  366. 0xff:No data
  367. 0x82:Write error
  368. 0x90:Checksum error
  369. */
  370. dev_err(&wac_i2c->client->dev,
  371. "%s: Error: %x , 0x%lx(%d)\n",
  372. __func__, buf[0], ulAddr, __LINE__);
  373. return -ERR_RET;
  374. }
  375. /* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
  376. } while (buf[0] == 0xff || buf[0] != 0x06);
  377. /* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
  378. if (printk_timed_ratelimit(&swtich_slot_time, 5000))
  379. dev_info(&wac_i2c->client->dev,
  380. "%s: Written on:0x%lx",
  381. __func__, ulAddr);
  382. usleep(1*1000);
  383. }
  384. dev_info(&wac_i2c->client->dev,
  385. "%s: Writing done\n", __func__);
  386. return 0;
  387. }
  388. int wacom_i2c_flash_verify(struct wacom_i2c *wac_i2c, unsigned long startAddr,
  389. u8 size, unsigned long maxAddr)
  390. {
  391. unsigned long ulAddr;
  392. int ret, len, i, j, k;
  393. char sum;
  394. u8 buf[WRITE_BUFF], bank;
  395. unsigned long swtich_slot_time;
  396. ret = len = i = 0;
  397. bank = BANK;
  398. for (ulAddr = startAddr; ulAddr <= maxAddr; ulAddr += BLOCK_SIZE_W) {
  399. sum = 0;
  400. buf[0] = FLASH_VERIFY;
  401. buf[1] = (u8) (ulAddr & 0xff);
  402. buf[2] = (u8) ((ulAddr & 0xff00) >> 8);
  403. buf[3] = size;
  404. buf[4] = bank;
  405. for (i = 0; i < 5; i++)
  406. sum += buf[i];
  407. sum = ~sum + 1;
  408. buf[5] = sum;
  409. len = 6;
  410. j = 0;
  411. /*sleep */
  412. /* 2012/07/18
  413. * if the returned data is not equal to the length of the bytes
  414. * that is supposed to send/receive, return it as fail
  415. */
  416. for (k = 0; k < RETRY_TRANSFER; k++) {
  417. ret = wacom_i2c_send(wac_i2c, buf, len, true);
  418. if (ret == len)
  419. break;
  420. if (ret < 0 || k == (RETRY_TRANSFER - 1)) {
  421. dev_err(&wac_i2c->client->dev,
  422. "%s: Write process aborted\n",
  423. __func__);
  424. return ERR_FAILED_ENTER;
  425. }
  426. }
  427. /*2012/07/18*/
  428. len = 1;
  429. do {
  430. usleep(1*1000);
  431. ret = wacom_i2c_recv(wac_i2c,
  432. buf, len, true);
  433. j++;
  434. if (j > RETRY || buf[0] == 0x90) {
  435. /*0xff:No data 0x90:Checksum error */
  436. dev_err(&wac_i2c->client->dev,
  437. "%s: Error: %x , 0x%lx(%d)\n",
  438. __func__, buf[0], ulAddr, __LINE__);
  439. return -ERR_RET;
  440. }
  441. /* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
  442. } while (buf[0] == 0xff || buf[0] != 0x06);
  443. /* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
  444. usleep(1*1000);
  445. sum = 0;
  446. for (i = 0; i < BLOCK_SIZE_W; i++) {
  447. buf[i] = Binary[ulAddr + i];
  448. sum += Binary[ulAddr + i];
  449. }
  450. sum = ~sum + 1;
  451. buf[BLOCK_SIZE_W] = sum;
  452. len = BLOCK_SIZE_W + 1;
  453. /* 2012/07/18
  454. * if the returned data is not equal to the length of the bytes
  455. * that is supposed to send/receive, return it as fail
  456. */
  457. for (k = 0; k < RETRY_TRANSFER; k++) {
  458. ret = wacom_i2c_send(wac_i2c, buf, len, true);
  459. if (ret == len)
  460. break;
  461. if (ret < 0 || k == (RETRY_TRANSFER - 1)) {
  462. dev_err(&wac_i2c->client->dev,
  463. "%s: Write process aborted\n",
  464. __func__);
  465. return ERR_FAILED_ENTER;
  466. }
  467. }
  468. /*2012/07/18*/
  469. usleep(3*1000);
  470. /* 2013/04/30 margin for HLTE firmware verification */
  471. len = 1;
  472. j = 0;
  473. do {
  474. usleep(2*1000);
  475. ret = wacom_i2c_recv(wac_i2c,
  476. buf, len, true);
  477. j++;
  478. if (j > RETRY || buf[0] == 0x81 || buf[0] == 0x90) {
  479. /*
  480. 0xff:No data
  481. 0x82:Write error
  482. 0x90:Checksum error
  483. */
  484. dev_err(&wac_i2c->client->dev,
  485. "%s: Error: %x , 0x%lx(%d)\n",
  486. __func__, buf[0], ulAddr, __LINE__);
  487. return -ERR_RET;
  488. }
  489. /* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
  490. } while (buf[0] == 0xff || buf[0] != 0x06);
  491. /* 2012/07/04 Evaluation if 0x06 or not added by Wacom*/
  492. if (printk_timed_ratelimit(&swtich_slot_time, 5000))
  493. dev_info(&wac_i2c->client->dev,
  494. "%s: Verified:0x%lx", __func__, ulAddr);
  495. usleep(1*1000);
  496. }
  497. dev_info(&wac_i2c->client->dev,
  498. "%s: Verifying done\n", __func__);
  499. return 0;
  500. }
  501. int wacom_i2c_flash(struct wacom_i2c *wac_i2c)
  502. {
  503. bool fw_update_enable = false;
  504. int ret = 0, blver = 0, mcu = 0;
  505. u32 max_addr = 0, cmd_addr = 0;
  506. if (system_rev >= WACOM_FW_UPDATE_REVISION) {
  507. dev_info(&wac_i2c->client->dev,
  508. "%s: run FW update..\n",
  509. __func__);
  510. } else {
  511. dev_info(&wac_i2c->client->dev,
  512. "%s: do not update..\n",
  513. __func__);
  514. return -ERR_RET;
  515. }
  516. #ifdef WACOM_HAVE_FWE_PIN
  517. wac_i2c->wac_pdata->compulsory_flash_mode(wac_i2c, true);
  518. /*Reset*/
  519. wac_i2c->wac_pdata->reset_platform_hw(wac_i2c);
  520. msleep(200);
  521. dev_err(&wac_i2c->client->dev, "%s: Set FWE\n", __func__);
  522. #endif
  523. ret = wacom_i2c_flash_cmd(wac_i2c);
  524. if (ret < 0)
  525. goto fw_update_error;
  526. usleep(10*1000);
  527. ret = wacom_i2c_flash_enter(wac_i2c);
  528. dev_info(&wac_i2c->client->dev,
  529. "%s: flashEnter:%d\n", __func__, ret);
  530. usleep(10*1000);
  531. blver = wacom_i2c_flash_BLVer(wac_i2c);
  532. dev_info(&wac_i2c->client->dev,
  533. "%s: blver:%d\n", __func__, blver);
  534. usleep(10*1000);
  535. mcu = wacom_i2c_flash_mcuId(wac_i2c);
  536. dev_info(&wac_i2c->client->dev,
  537. "%s: mcu:%x\n", __func__, mcu);
  538. if (MPU_W9001 != mcu) {
  539. dev_info(&wac_i2c->client->dev,
  540. "%s: mcu:%x\n", __func__, mcu);
  541. ret = -ENXIO;
  542. goto mcu_type_error;
  543. }
  544. usleep(1*1000);
  545. switch (mcu) {
  546. case MPUVER_W8501:
  547. dev_info(&wac_i2c->client->dev,
  548. "%s: flashing for w8501 started\n",
  549. __func__);
  550. max_addr = MAX_ADDR_W8501;
  551. cmd_addr = MAX_BLOCK_W8501;
  552. fw_update_enable = true;
  553. break;
  554. case MPUVER_514:
  555. dev_info(&wac_i2c->client->dev,
  556. "%s: Flashing for 514 started\n",
  557. __func__);
  558. max_addr = MAX_ADDR_514;
  559. cmd_addr = MAX_BLOCK_514;
  560. fw_update_enable = true;
  561. break;
  562. case MPUVER_505:
  563. dev_info(&wac_i2c->client->dev,
  564. "%s: Flashing for 505 started\n",
  565. __func__);
  566. max_addr = MAX_ADDR_505;
  567. cmd_addr = MAX_BLOCK_505;
  568. fw_update_enable = true;
  569. break;
  570. default:
  571. dev_info(&wac_i2c->client->dev,
  572. "%s: default called\n",
  573. __func__);
  574. break;
  575. }
  576. if (fw_update_enable) {
  577. bool valid_hex = false;
  578. int cnt = 0;
  579. /*2012/07/04: below modified by Wacom*/
  580. /*If wacom_i2c_flash_verify returns -ERR_HEX, */
  581. /*please redo whole process of flashing from */
  582. /*wacom_i2c_flash_erase */
  583. do {
  584. ret = wacom_i2c_flash_erase(wac_i2c, FLASH_ERASE,
  585. cmd_addr, END_BLOCK);
  586. if (ret < 0) {
  587. printk(KERN_ERR "[E-PEN] error - erase\n");
  588. continue;
  589. }
  590. msleep(20);
  591. ret = wacom_i2c_flash_write(wac_i2c, START_ADDR,
  592. NUM_BLOCK_2WRITE, max_addr);
  593. if (ret < 0) {
  594. dev_err(&wac_i2c->client->dev,
  595. "%s: error - writing\n",
  596. __func__);
  597. continue;
  598. }
  599. msleep(20);
  600. ret = wacom_i2c_flash_verify(wac_i2c, START_ADDR,
  601. NUM_BLOCK_2WRITE, max_addr);
  602. if (ret == -ERR_HEX)
  603. dev_err(&wac_i2c->client->dev,
  604. "%s: firmware is not valied\n",
  605. __func__);
  606. else if (ret < 0) {
  607. dev_info(&wac_i2c->client->dev,
  608. "%s: error - verifying\n",
  609. __func__);
  610. continue;
  611. } else
  612. valid_hex = true;
  613. } while ((!valid_hex) && (cnt < 10));
  614. /*2012/07/04: Wacom*/
  615. dev_info(&wac_i2c->client->dev,
  616. "%s: Firmware successfully updated\n",
  617. __func__);
  618. }
  619. usleep(1*1000);
  620. mcu_type_error:
  621. wacom_i2c_flash_end(wac_i2c);
  622. if (ret < 0)
  623. dev_err(&wac_i2c->client->dev,
  624. "%s: error - wacom_i2c_flash_end\n",
  625. __func__);
  626. fw_update_error:
  627. #ifdef WACOM_HAVE_FWE_PIN
  628. wac_i2c->wac_pdata->compulsory_flash_mode(wac_i2c, false);
  629. /*Reset*/
  630. wac_i2c->wac_pdata->reset_platform_hw(wac_i2c);
  631. msleep(200);
  632. dev_info(&wac_i2c->client->dev,
  633. "%s: Release FWE\n",
  634. __func__);
  635. #endif
  636. return ret;
  637. }
  638. static int wacom_enter_flash_mode(struct wacom_i2c *wac_i2c)
  639. {
  640. int ret, len = 0;
  641. char buf[8];
  642. dev_info(&wac_i2c->client->dev, "%s\n", __func__);
  643. memset(buf, 0, 8);
  644. strncpy(buf, "\rflash\r", 7);
  645. len = sizeof(buf) / sizeof(buf[0]);
  646. ret = wacom_i2c_send(wac_i2c, buf, len, WACOM_I2C_MODE_BOOT);
  647. if (ret < 0) {
  648. dev_err(&wac_i2c->client->dev,
  649. "%s: Sending flash command failed, %d\n",
  650. __func__, ret);
  651. return ret;
  652. }
  653. msleep(500);
  654. return 0;
  655. }
  656. static int wacom_set_cmd_feature(struct wacom_i2c *wac_i2c)
  657. {
  658. int ret, len = 0;
  659. u8 buf[4];
  660. buf[len++] = 4;
  661. buf[len++] = 0;
  662. buf[len++] = 0x37;
  663. buf[len++] = CMD_SET_FEATURE;
  664. ret = wacom_i2c_send(wac_i2c, buf, len, WACOM_I2C_MODE_BOOT);
  665. if (ret < 0)
  666. dev_err(&wac_i2c->client->dev,
  667. "%s: failed send CMD_SET_FEATURE, %d\n",
  668. __func__, ret);
  669. return ret;
  670. }
  671. static int wacom_get_cmd_feature(struct wacom_i2c *wac_i2c)
  672. {
  673. int ret, len = 0;
  674. u8 buf[4];
  675. buf[len++] = 4;
  676. buf[len++] = 0;
  677. buf[len++] = 0x38;
  678. buf[len++] = CMD_GET_FEATURE;
  679. ret = wacom_i2c_send(wac_i2c, buf, len, WACOM_I2C_MODE_BOOT);
  680. if (ret < 0)
  681. dev_err(&wac_i2c->client->dev,
  682. "%s: failed send CMD_GET_FEATURE, ret: %d\n",
  683. __func__, ret);
  684. len = 0;
  685. buf[len++] = 5;
  686. buf[len++] = 0;
  687. ret = wacom_i2c_send(wac_i2c, buf, len, WACOM_I2C_MODE_BOOT);
  688. if (ret < 0)
  689. dev_err(&wac_i2c->client->dev,
  690. "%s: failed send command, ret: %d\n",
  691. __func__, ret);
  692. return ret;
  693. }
  694. /*
  695. * mode1. BOOT_QUERY: check enter boot mode for flash.
  696. * mode2. BOOT_BLVER : check bootloader version.
  697. * mode3. BOOT_MPU : check MPU version
  698. */
  699. int wacom_check_flash_mode(struct wacom_i2c *wac_i2c, int mode)
  700. {
  701. int ret, ECH;
  702. unsigned char response_cmd = 0;
  703. unsigned char command[CMD_SIZE];
  704. unsigned char response[RSP_SIZE];
  705. switch (mode) {
  706. case BOOT_QUERY:
  707. response_cmd = QUERY_CMD;
  708. break;
  709. case BOOT_BLVER:
  710. response_cmd = BOOT_CMD;
  711. break;
  712. case BOOT_MPU:
  713. response_cmd = MPU_CMD;
  714. break;
  715. default :
  716. break;
  717. }
  718. dev_info(&wac_i2c->client->dev, "%s, mode = %s\n", __func__,
  719. (mode == BOOT_QUERY) ? "BOOT_QUERY" :
  720. (mode == BOOT_BLVER) ? "BOOT_BLVER" :
  721. (mode == BOOT_MPU) ? "BOOT_MPU" : "Not Support");
  722. ret = wacom_set_cmd_feature(wac_i2c);
  723. if (ret < 0)
  724. return ret;
  725. command[0] = 5;
  726. command[1] = 0;
  727. command[2] = 5;
  728. command[3] = 0;
  729. command[4] = BOOT_CMD_REPORT_ID;
  730. command[5] = mode;
  731. command[6] = ECH = 7;
  732. ret = wacom_i2c_send(wac_i2c, command, 7, WACOM_I2C_MODE_BOOT);
  733. if (ret < 0) {
  734. dev_err(&wac_i2c->client->dev,
  735. "%s: failed send REPORT_ID, %d\n",
  736. __func__, ret);
  737. return ret;
  738. }
  739. ret = wacom_get_cmd_feature(wac_i2c);
  740. if (ret < 0)
  741. return ret;
  742. usleep_range(10000, 10000);
  743. ret = wacom_i2c_recv(wac_i2c, response, BOOT_RSP_SIZE,
  744. WACOM_I2C_MODE_BOOT);
  745. if (ret < 0) {
  746. dev_err(&wac_i2c->client->dev,
  747. "%s: failed receive response, %d\n",
  748. __func__, ret);
  749. return ret;
  750. }
  751. if ((response[3] != response_cmd) || (response[4] != ECH)) {
  752. dev_err(&wac_i2c->client->dev,
  753. "%s: failed matching response3[%x], 4[%x]\n",
  754. __func__, response[3], response[4]);
  755. return -1;
  756. }
  757. return response[5];
  758. }
  759. int wacom_enter_bootloader(struct wacom_i2c *wac_i2c)
  760. {
  761. int ret;
  762. int retry = 0;
  763. dev_info(&wac_i2c->client->dev, "%s\n", __func__);
  764. ret = wacom_enter_flash_mode(wac_i2c);
  765. if (ret < 0)
  766. msleep(500);
  767. do {
  768. msleep(100);
  769. ret = wacom_check_flash_mode(wac_i2c, BOOT_QUERY);
  770. retry++;
  771. } while (ret < 0 && retry < 10);
  772. if (ret < 0)
  773. return EXIT_FAIL_GET_BOOT_LOADER_VERSION;
  774. ret = wacom_check_flash_mode(wac_i2c, BOOT_BLVER);
  775. wac_i2c->boot_ver = ret;
  776. if (ret < 0)
  777. return EXIT_FAIL_GET_BOOT_LOADER_VERSION;
  778. return ret;
  779. }
  780. static int wacom_flash_end(struct wacom_i2c *wac_i2c)
  781. {
  782. int ret, ECH;
  783. unsigned char command[CMD_SIZE];
  784. dev_info(&wac_i2c->client->dev, "%s\n", __func__);
  785. command[0] = 4;
  786. command[1] = 0;
  787. command[2] = 0x37;
  788. command[3] = CMD_SET_FEATURE;
  789. command[4] = 5;
  790. command[5] = 0;
  791. command[6] = 5;
  792. command[7] = 0;
  793. command[8] = BOOT_CMD_REPORT_ID;
  794. command[9] = BOOT_EXIT;
  795. command[10] = ECH = 7;
  796. ret = wacom_i2c_send(wac_i2c, command, 11, WACOM_I2C_MODE_BOOT);
  797. if (ret < 0)
  798. dev_err(&wac_i2c->client->dev,
  799. "%s failed, %d\n",
  800. __func__, ret);
  801. return ret;
  802. }
  803. static int wacom_flash_erase(struct wacom_i2c *wac_i2c,
  804. int *eraseBlock, int num)
  805. {
  806. int ret = 0, ECH;
  807. unsigned char sum;
  808. unsigned char cmd_chksum;
  809. int i, j;
  810. unsigned char command[CMD_SIZE];
  811. unsigned char response[RSP_SIZE];
  812. dev_info(&wac_i2c->client->dev, "%s\n", __func__);
  813. for (i = 0; i < num; i++) {
  814. /*msleep(500);*/
  815. retry:
  816. ret = wacom_set_cmd_feature(wac_i2c);
  817. if (ret < 0) {
  818. dev_info(&wac_i2c->client->dev,
  819. "%s: set command failed, retry= %d\n",
  820. __func__, i);
  821. return ret;
  822. }
  823. command[0] = 5;
  824. command[1] = 0;
  825. command[2] = 7;
  826. command[3] = 0;
  827. command[4] = BOOT_CMD_REPORT_ID;
  828. command[5] = BOOT_ERASE_FLASH;
  829. command[6] = ECH = i;
  830. command[7] = *eraseBlock;
  831. eraseBlock++;
  832. sum = 0;
  833. for (j = 0; j < 8; j++)
  834. sum += command[j];
  835. cmd_chksum = ~sum + 1;
  836. command[8] = cmd_chksum;
  837. ret = wacom_i2c_send(wac_i2c, command, 9, WACOM_I2C_MODE_BOOT);
  838. if (ret < 0) {
  839. dev_err(&wac_i2c->client->dev,
  840. "%s: failed send erase command, %d\n",
  841. __func__, ret);
  842. return ret;
  843. }
  844. msleep(300);
  845. ret = wacom_get_cmd_feature(wac_i2c);
  846. if (ret < 0) {
  847. dev_info(&wac_i2c->client->dev,
  848. "%s: get command failed, retry= %d\n",
  849. __func__, i);
  850. return ret;
  851. }
  852. ret = wacom_i2c_recv(wac_i2c, response, BOOT_RSP_SIZE,
  853. WACOM_I2C_MODE_BOOT);
  854. if (ret < 0) {
  855. dev_err(&wac_i2c->client->dev,
  856. "%s: failed receive response, %d\n",
  857. __func__, ret);
  858. return ret;
  859. }
  860. if ((response[3] != ERS_CMD) || (response[4] != ECH)) {
  861. dev_err(&wac_i2c->client->dev,
  862. "%s: failed matching response3[%x], 4[%x]\n",
  863. __func__, response[3], response[4]);
  864. return -1;
  865. }
  866. if (response[5] == 0x80) {
  867. dev_err(&wac_i2c->client->dev,
  868. "%s: erase retry\n",
  869. __func__);
  870. goto retry;
  871. }
  872. if (response[5] != ACK) {
  873. dev_err(&wac_i2c->client->dev,
  874. "%s: failed erase. response[%x], retry[%d]\n",
  875. __func__, response[5], i);
  876. return -1;
  877. }
  878. }
  879. return ret;
  880. }
  881. static int wacom_flash_write_block(struct wacom_i2c *wac_i2c, const unsigned char *flash_data,
  882. unsigned long ulAddress, u8 *pcommand_id)
  883. {
  884. const int MAX_COM_SIZE = (12 + FLASH_BLOCK_SIZE + 2);
  885. int ECH;
  886. int ret;
  887. unsigned char sum;
  888. unsigned char command[MAX_COM_SIZE];
  889. unsigned char response[RSP_SIZE];
  890. unsigned int i;
  891. ret = wacom_set_cmd_feature(wac_i2c);
  892. if (ret < 0)
  893. return ret;
  894. command[0] = 5;
  895. command[1] = 0;
  896. command[2] = 76;
  897. command[3] = 0;
  898. command[4] = BOOT_CMD_REPORT_ID;
  899. command[5] = BOOT_WRITE_FLASH;
  900. command[6] = ECH = ++(*pcommand_id);
  901. command[7] = ulAddress & 0x000000ff;
  902. command[8] = (ulAddress & 0x0000ff00) >> 8;
  903. command[9] = (ulAddress & 0x00ff0000) >> 16;
  904. command[10] = (ulAddress & 0xff000000) >> 24;
  905. command[11] = 8;
  906. sum = 0;
  907. for (i = 0; i < 12; i++)
  908. sum += command[i];
  909. command[MAX_COM_SIZE - 2] = ~sum + 1;
  910. sum = 0;
  911. for (i = 12; i < (FLASH_BLOCK_SIZE + 12); i++) {
  912. command[i] = flash_data[ulAddress + (i - 12)];
  913. sum += flash_data[ulAddress + (i - 12)];
  914. }
  915. command[MAX_COM_SIZE - 1] = ~sum + 1;
  916. ret = wacom_i2c_send(wac_i2c, command, BOOT_CMD_SIZE,
  917. WACOM_I2C_MODE_BOOT);
  918. if (ret < 0) {
  919. printk(KERN_DEBUG "epen:1 rv:%d\n", ret);
  920. return ret;
  921. }
  922. usleep_range(10000, 10000);
  923. ret = wacom_get_cmd_feature(wac_i2c);
  924. if (ret < 0)
  925. return ret;
  926. ret = wacom_i2c_recv(wac_i2c, response, BOOT_RSP_SIZE,
  927. WACOM_I2C_MODE_BOOT);
  928. if (ret < 0) {
  929. dev_err(&wac_i2c->client->dev,
  930. "%s: failed receive response, %d\n",
  931. __func__, ret);
  932. return ret;
  933. }
  934. if ((response[3] != WRITE_CMD) ||
  935. (response[4] != ECH) || response[5] != ACK)
  936. return -1;
  937. return 0;
  938. }
  939. static int wacom_flash_write(struct wacom_i2c *wac_i2c,
  940. const unsigned char *flash_data, size_t data_size,
  941. unsigned long start_address, unsigned long *max_address)
  942. {
  943. unsigned long ulAddress;
  944. bool ret;
  945. int i;
  946. unsigned long pageNo = 0;
  947. u8 command_id = 0;
  948. dev_info(&wac_i2c->client->dev, "%s\n", __func__);
  949. for (ulAddress = start_address; ulAddress < *max_address;
  950. ulAddress += FLASH_BLOCK_SIZE) {
  951. unsigned int j;
  952. bool bWrite = false;
  953. /* Wacom 2012/10/04: skip if all each data locating on
  954. from ulAddr to ulAddr+Block_SIZE_W are 0xff */
  955. for (i = 0; i < FLASH_BLOCK_SIZE; i++) {
  956. if (flash_data[ulAddress + i] != 0xFF)
  957. break;
  958. }
  959. if (i == (FLASH_BLOCK_SIZE)) {
  960. /*printk(KERN_DEBUG"epen:BLOCK PASSED\n"); */
  961. continue;
  962. }
  963. /* Wacom 2012/10/04 */
  964. for (j = 0; j < FLASH_BLOCK_SIZE; j++) {
  965. if (flash_data[ulAddress + j] == 0xFF)
  966. continue;
  967. else {
  968. bWrite = true;
  969. break;
  970. }
  971. }
  972. if (!bWrite) {
  973. pageNo++;
  974. continue;
  975. }
  976. ret = wacom_flash_write_block(wac_i2c, flash_data, ulAddress,
  977. &command_id);
  978. if (ret < 0)
  979. return ret;
  980. pageNo++;
  981. }
  982. return 0;
  983. }
  984. int wacom_i2c_flash_9007(struct wacom_i2c *wac_i2c)
  985. {
  986. unsigned long max_address = 0;
  987. unsigned long start_address = START_ADDR;
  988. int i, ret = 0;
  989. int eraseBlock[100], eraseBlockNum;
  990. unsigned long ulMaxRange;
  991. if (wac_i2c->ic_mpu_ver != MPU_W9007 && wac_i2c->ic_mpu_ver != MPU_W9010)
  992. return EXIT_FAIL_GET_MPU_TYPE;
  993. wac_i2c->wac_pdata->compulsory_flash_mode(wac_i2c, true);
  994. /*Reset*/
  995. wac_i2c->wac_pdata->reset_platform_hw(wac_i2c);
  996. msleep(200);
  997. dev_err(&wac_i2c->client->dev, "%s: Set FWE\n", __func__);
  998. /*Set start and end address and block numbers*/
  999. eraseBlockNum = 0;
  1000. start_address = START_ADDR_W9007;
  1001. max_address = MAX_ADDR_W9007;
  1002. for (i = BLOCK_NUM_W9007; i >= 8; i--) {
  1003. eraseBlock[eraseBlockNum] = i;
  1004. eraseBlockNum++;
  1005. }
  1006. /*Set adress range */
  1007. ulMaxRange = max_address;
  1008. ulMaxRange -= start_address;
  1009. ulMaxRange >>= 6;
  1010. if (max_address > (ulMaxRange << 6))
  1011. ulMaxRange++;
  1012. /*Erase the old program */
  1013. ret = wacom_flash_erase(wac_i2c, eraseBlock, eraseBlockNum);
  1014. if (ret < 0) {
  1015. dev_err(&wac_i2c->client->dev,
  1016. "%s: failed erase old firmware, %d\n",
  1017. __func__, ret);
  1018. goto flashing_fw_err;
  1019. }
  1020. /*Write the new program */
  1021. ret =
  1022. wacom_flash_write(wac_i2c, Binary, DATA_SIZE, start_address, &max_address);
  1023. if (ret < 0) {
  1024. dev_err(&wac_i2c->client->dev,
  1025. "%s: failed writing new firmware, %d\n",
  1026. __func__, ret);
  1027. goto flashing_fw_err;
  1028. }
  1029. /*Enable */
  1030. ret = wacom_flash_end(wac_i2c);
  1031. if (ret < 0) {
  1032. dev_err(&wac_i2c->client->dev,
  1033. "%s: failed flash mode close, %d\n",
  1034. __func__, ret);
  1035. goto flashing_fw_err;
  1036. }
  1037. dev_err(&wac_i2c->client->dev,
  1038. "%s: Successed new Firmware writing\n",
  1039. __func__);
  1040. flashing_fw_err:
  1041. wac_i2c->wac_pdata->compulsory_flash_mode(wac_i2c, false);
  1042. /*Reset */
  1043. wac_i2c->wac_pdata->reset_platform_hw(wac_i2c);
  1044. msleep(200);
  1045. return ret;
  1046. }
  1047. int wacom_fw_load_from_UMS(struct wacom_i2c *wac_i2c)
  1048. {
  1049. struct file *fp;
  1050. mm_segment_t old_fs;
  1051. long fsize, nread;
  1052. int ret = 0;
  1053. const struct firmware *firm_data = NULL;
  1054. dev_info(&wac_i2c->client->dev,
  1055. "%s: Start firmware flashing (UMS).\n", __func__);
  1056. old_fs = get_fs();
  1057. set_fs(KERNEL_DS);
  1058. fp = filp_open(WACOM_FW_PATH, O_RDONLY, S_IRUSR);
  1059. if (IS_ERR(fp)) {
  1060. dev_err(&wac_i2c->client->dev,
  1061. "%s: failed to open %s.\n",
  1062. __func__, WACOM_FW_PATH);
  1063. ret = -ENOENT;
  1064. goto open_err;
  1065. }
  1066. fsize= fp->f_path.dentry->d_inode->i_size;
  1067. dev_info(&wac_i2c->client->dev,
  1068. "%s: start, file path %s, size %ld Bytes\n",
  1069. __func__, WACOM_FW_PATH, fsize);
  1070. firm_data = kzalloc(fsize, GFP_KERNEL);
  1071. if (IS_ERR(firm_data)) {
  1072. dev_err(&wac_i2c->client->dev,
  1073. "%s, kmalloc failed\n", __func__);
  1074. ret = -EFAULT;
  1075. goto malloc_error;
  1076. }
  1077. nread = vfs_read(fp, (char __user *)firm_data,
  1078. fsize, &fp->f_pos);
  1079. dev_info(&wac_i2c->client->dev,
  1080. "%s: nread %ld Bytes\n", __func__, nread);
  1081. if (nread != fsize) {
  1082. dev_err(&wac_i2c->client->dev,
  1083. "%s: failed to read firmware file, nread %ld Bytes\n",
  1084. __func__, nread);
  1085. ret = -EIO;
  1086. goto read_err;
  1087. }
  1088. filp_close(fp, current->files);
  1089. set_fs(old_fs);
  1090. /*start firm update*/
  1091. wacom_i2c_set_firm_data((unsigned char *)firm_data);
  1092. return 0;
  1093. read_err:
  1094. kfree(firm_data);
  1095. malloc_error:
  1096. filp_close(fp, current->files);
  1097. open_err:
  1098. set_fs(old_fs);
  1099. return ret;
  1100. }
  1101. int wacom_load_fw_from_req_fw(struct wacom_i2c *wac_i2c)
  1102. {
  1103. int ret = 0;
  1104. const struct firmware *firm_data = NULL;
  1105. char fw_path[WACOM_MAX_FW_PATH];
  1106. dev_info(&wac_i2c->client->dev, "%s\n", __func__);
  1107. /*Obtain MPU type: this can be manually done in user space */
  1108. dev_info(&wac_i2c->client->dev,
  1109. "%s: MPU type: %x , BOOT ver: %x\n",
  1110. __func__, wac_i2c->ic_mpu_ver, wac_i2c->boot_ver);
  1111. memset(&fw_path, 0, WACOM_MAX_FW_PATH);
  1112. if (wac_i2c->ic_mpu_ver == MPU_W9001) {
  1113. snprintf(fw_path, WACOM_MAX_FW_PATH,
  1114. "%s", WACOM_FW_NAME_W9001);
  1115. } else if (wac_i2c->ic_mpu_ver == MPU_W9007) {
  1116. if (wac_i2c->boot_ver == 0x91)
  1117. snprintf(fw_path, WACOM_MAX_FW_PATH,
  1118. "%s", WACOM_FW_NAME_W9007_BL91);
  1119. else if (wac_i2c->boot_ver == 0x92)
  1120. snprintf(fw_path, WACOM_MAX_FW_PATH,
  1121. "%s", WACOM_FW_NAME_W9007_BL92);
  1122. } else if (wac_i2c->ic_mpu_ver == MPU_W9010) {
  1123. if (system_rev >= WACOM_FW_UPDATE_REVISION) {
  1124. snprintf(fw_path, WACOM_MAX_FW_PATH,
  1125. "%s", WACOM_FW_NAME_W9010);
  1126. } else {
  1127. dev_info(&wac_i2c->client->dev,
  1128. "%s: B934 PANEL, firmware name is NULL. return -1\n",
  1129. __func__);
  1130. ret = -1;
  1131. goto firm_name_null_err;
  1132. }
  1133. } else {
  1134. dev_info(&wac_i2c->client->dev,
  1135. "%s: Not apply WACOM IC, firmware name is NULL. return -1\n",
  1136. __func__);
  1137. ret = -1;
  1138. goto firm_name_null_err;
  1139. }
  1140. ret = request_firmware(&firm_data, fw_path, &wac_i2c->client->dev);
  1141. if (ret < 0) {
  1142. dev_err(&wac_i2c->client->dev,
  1143. "%s: Unable to open firmware. ret %d\n",
  1144. __func__, ret);
  1145. goto request_firm_err;
  1146. }
  1147. dev_info(&wac_i2c->client->dev, "%s: firmware name: %s, size: %d\n",
  1148. __func__, fw_path, firm_data->size);
  1149. /* firmware version check */
  1150. if (wac_i2c->ic_mpu_ver == MPU_W9010 || wac_i2c->ic_mpu_ver == MPU_W9007)
  1151. wac_i2c->wac_feature->fw_version =
  1152. firm_data->data[FIRM_VER_LB_ADDR_W9007] |(firm_data->data[FIRM_VER_UB_ADDR_W9007] << 8);
  1153. dev_info(&wac_i2c->client->dev, "%s: firmware version = %x\n",
  1154. __func__, wac_i2c->wac_feature->fw_version);
  1155. wacom_i2c_set_firm_data((unsigned char *)firm_data->data);
  1156. release_firmware(firm_data);
  1157. firm_name_null_err:
  1158. request_firm_err:
  1159. return ret;
  1160. }