rmi_fw_update.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380
  1. /*
  2. * Synaptics DSX touchscreen driver
  3. *
  4. * Copyright (C) 2012 Synaptics Incorporated
  5. *
  6. * Copyright (C) 2012 Alexandra Chin <alexandra.chin@tw.synaptics.com>
  7. * Copyright (C) 2012 Scott Lin <scott.lin@tw.synaptics.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <asm/unaligned.h>
  22. #include <linux/slab.h>
  23. #include <linux/i2c.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <linux/input.h>
  27. #include <linux/firmware.h>
  28. #include "synaptics_i2c_rmi.h"
  29. #define DO_STARTUP_FW_UPDATE
  30. #define STARTUP_FW_UPDATE_DELAY_MS 1000 /* ms */
  31. #define FORCE_UPDATE false
  32. #define DO_LOCKDOWN false
  33. #define MAX_IMAGE_NAME_LEN 256
  34. #define MAX_FIRMWARE_ID_LEN 10
  35. #define F01_DEVICE_STATUS 0X0004
  36. #define BOOTLOADER_ID_OFFSET 0
  37. #define BLOCK_NUMBER_OFFSET 0
  38. #define V5_PROPERTIES_OFFSET 2
  39. #define V5_BLOCK_SIZE_OFFSET 3
  40. #define V5_BLOCK_COUNT_OFFSET 5
  41. #define V5_BLOCK_DATA_OFFSET 2
  42. #define V6_PROPERTIES_OFFSET 1
  43. #define V6_BLOCK_SIZE_OFFSET 2
  44. #define V6_BLOCK_COUNT_OFFSET 3
  45. #define V6_BLOCK_DATA_OFFSET 1
  46. #define V6_FLASH_COMMAND_OFFSET 2
  47. #define V6_FLASH_STATUS_OFFSET 3
  48. #define IMG_VERSION_OFFSET 0x07
  49. #define IMG_X10_TOP_CONTAINER_OFFSET 0x0C
  50. #define IMG_X0_X6_FW_OFFSET 0x100
  51. #define UI_CONFIG_AREA 0x00
  52. #define PERM_CONFIG_AREA 0x01
  53. #define BL_CONFIG_AREA 0x02
  54. #define DISP_CONFIG_AREA 0x03
  55. #define SLEEP_MODE_NORMAL (0x00)
  56. #define SLEEP_MODE_SENSOR_SLEEP (0x01)
  57. #define SLEEP_MODE_RESERVED0 (0x02)
  58. #define SLEEP_MODE_RESERVED1 (0x03)
  59. #define ENABLE_WAIT_MS (1 * 1000)
  60. #define WRITE_WAIT_MS (3 * 1000)
  61. #define ERASE_WAIT_MS (5 * 1000)
  62. #define MIN_SLEEP_TIME_US 50
  63. #define MAX_SLEEP_TIME_US 100
  64. #define STATUS_POLLING_PERIOD_US 3000
  65. #define POLLING_MODE_DEFAULT 0
  66. static ssize_t fwu_sysfs_show_image(struct file *data_file,
  67. struct kobject *kobj, struct bin_attribute *attributes,
  68. char *buf, loff_t pos, size_t count);
  69. static ssize_t fwu_sysfs_store_image(struct file *data_file,
  70. struct kobject *kobj, struct bin_attribute *attributes,
  71. char *buf, loff_t pos, size_t count);
  72. static ssize_t fwu_sysfs_do_reflash_store(struct device *dev,
  73. struct device_attribute *attr, const char *buf, size_t count);
  74. static ssize_t fwu_sysfs_write_config_store(struct device *dev,
  75. struct device_attribute *attr, const char *buf, size_t count);
  76. static ssize_t fwu_sysfs_read_config_store(struct device *dev,
  77. struct device_attribute *attr, const char *buf, size_t count);
  78. static ssize_t fwu_sysfs_config_area_store(struct device *dev,
  79. struct device_attribute *attr, const char *buf, size_t count);
  80. /*
  81. static ssize_t fwu_sysfs_image_name_store(struct device *dev,
  82. struct device_attribute *attr, const char *buf, size_t count);
  83. */
  84. static ssize_t fwu_sysfs_image_size_store(struct device *dev,
  85. struct device_attribute *attr, const char *buf, size_t count);
  86. static ssize_t fwu_sysfs_block_size_show(struct device *dev,
  87. struct device_attribute *attr, char *buf);
  88. static ssize_t fwu_sysfs_firmware_block_count_show(struct device *dev,
  89. struct device_attribute *attr, char *buf);
  90. static ssize_t fwu_sysfs_configuration_block_count_show(struct device *dev,
  91. struct device_attribute *attr, char *buf);
  92. static ssize_t fwu_sysfs_perm_config_block_count_show(struct device *dev,
  93. struct device_attribute *attr, char *buf);
  94. static ssize_t fwu_sysfs_bl_config_block_count_show(struct device *dev,
  95. struct device_attribute *attr, char *buf);
  96. static ssize_t fwu_sysfs_disp_config_block_count_show(struct device *dev,
  97. struct device_attribute *attr, char *buf);
  98. static ssize_t fwu_sysfs_guest_code_block_count_show(struct device *dev,
  99. struct device_attribute *attr, char *buf);
  100. static ssize_t fwu_sysfs_write_guest_code_store(struct device *dev,
  101. struct device_attribute *attr, const char *buf, size_t count);
  102. void fwu_img_parse_format(void);
  103. static int fwu_do_write_guest_code(void);
  104. enum bl_version {
  105. V5 = 5,
  106. V6 = 6,
  107. };
  108. enum flash_area {
  109. NONE,
  110. UI_FIRMWARE,
  111. CONFIG_AREA,
  112. };
  113. enum update_mode {
  114. NORMAL = 1,
  115. FORCE = 2,
  116. LOCKDOWN = 8,
  117. };
  118. enum flash_command {
  119. CMD_IDLE = 0x0,
  120. CMD_WRITE_FW_BLOCK = 0x2,
  121. CMD_ERASE_ALL = 0x3,
  122. CMD_WRITE_LOCKDOWN_BLOCK = 0x4,
  123. CMD_READ_CONFIG_BLOCK = 0x5,
  124. CMD_WRITE_CONFIG_BLOCK = 0x6,
  125. CMD_ERASE_CONFIG = 0x7,
  126. CMD_READ_SENSOR_ID = 0x8,
  127. CMD_ERASE_BL_CONFIG = 0x9,
  128. CMD_ERASE_DISP_CONFIG = 0xA,
  129. CMD_ERASE_GUEST_CODE = 0xB,
  130. CMD_WRITE_GUEST_CODE = 0xC,
  131. CMD_ENABLE_FLASH_PROG = 0xF
  132. };
  133. struct img_x0x6_header {
  134. /* 0x00 - 0x0f */
  135. unsigned char checksum[4];
  136. unsigned char reserved_04;
  137. unsigned char reserved_05;
  138. unsigned char options_firmware_id:1;
  139. unsigned char options_contain_bootloader:1;
  140. unsigned char options_reserved:6;
  141. unsigned char bootloader_version;
  142. unsigned char firmware_size[4];
  143. unsigned char config_size[4];
  144. /* 0x10 - 0x1f */
  145. unsigned char product_id[SYNAPTICS_RMI4_PRODUCT_ID_SIZE];
  146. unsigned char package_id[2];
  147. unsigned char package_id_revision[2];
  148. unsigned char product_info[SYNAPTICS_RMI4_PRODUCT_INFO_SIZE];
  149. /* 0x20 - 0x2f */
  150. unsigned char reserved_20_2f[16];
  151. /* 0x30 - 0x3f */
  152. unsigned char ds_firmware_info[16];
  153. /* 0x40 - 0x4f */
  154. unsigned char ds_info[10];
  155. unsigned char reserved_4a_4f[6];
  156. /* 0x50 - 0x53 */
  157. unsigned char firmware_id[4];
  158. };
  159. enum img_x10_container_id
  160. {
  161. ID_TOP_LEVEL_CONTAINER = 0,
  162. ID_UI_CONTAINER,
  163. ID_UI_CONFIGURATION,
  164. ID_BOOTLOADER_CONTAINER,
  165. ID_BOOTLOADER_IMAGE_CONTAINER,
  166. ID_BOOTLOADER_CONFIGURATION_CONTAINER,
  167. ID_BOOTLOADER_LOCKDOWN_INFORMATION_CONTAINER,
  168. ID_PERMANENT_CONFIGURATION_CONTAINER,
  169. ID_GUEST_CODE_CONTAINER,
  170. ID_BOOTLOADER_PROTOCOL_DESCRIPTOR_CONTAINER,
  171. ID_UI_PROTOCOL_DESCRIPTOR_CONTAINER,
  172. ID_RMI_SELF_DISCOVERY_CONTAINER,
  173. ID_RMI_PAGE_CONTENT_CONTAINER,
  174. ID_GENERAL_INFORMATION_CONTAINER,
  175. RESERVERD
  176. };
  177. struct block_data
  178. {
  179. unsigned char * data;
  180. int size;
  181. };
  182. struct img_file_content
  183. {
  184. unsigned char *fw_image;
  185. unsigned int image_size;
  186. unsigned char *image_name;
  187. unsigned char imageFileVersion;
  188. struct block_data uiFirmware;
  189. struct block_data uiConfig;
  190. struct block_data guestCode;
  191. struct block_data lockdown;
  192. struct block_data permanent;
  193. struct block_data bootloaderInfo;
  194. unsigned char blMajorVersion;
  195. unsigned char blMinorVersion;
  196. unsigned char *configId; //len 0x4
  197. unsigned char *firmwareId; //len 0x4
  198. unsigned char *packageId; //len 0x4
  199. unsigned char *dsFirmwareInfo; //len 0x10
  200. };
  201. struct img_x10_descriptor
  202. {
  203. unsigned char contentChecksum[4];
  204. unsigned char containerID[2];
  205. unsigned char minorVersion;
  206. unsigned char majorVersion;
  207. unsigned char reserverd[4];
  208. unsigned char containerOptionFlags[4];
  209. unsigned char contentOptionLength[4];
  210. unsigned char contentOptionAddress[4];
  211. unsigned char contentLength[4];
  212. unsigned char contentAddress[4];
  213. };
  214. struct img_x10_bl_container
  215. {
  216. unsigned char majorVersion;
  217. unsigned char minorVersion;
  218. unsigned char reserved[2];
  219. unsigned char * subContainer;
  220. };
  221. struct pdt_properties {
  222. union {
  223. struct {
  224. unsigned char reserved_1:6;
  225. unsigned char has_bsr:1;
  226. unsigned char reserved_2:1;
  227. } __packed;
  228. unsigned char data[1];
  229. };
  230. };
  231. struct f01_device_status {
  232. union {
  233. struct {
  234. unsigned char status_code:4;
  235. unsigned char reserved:2;
  236. unsigned char flash_prog:1;
  237. unsigned char unconfigured:1;
  238. } __packed;
  239. unsigned char data[1];
  240. };
  241. };
  242. struct f01_device_control {
  243. union {
  244. struct {
  245. unsigned char sleep_mode:2;
  246. unsigned char nosleep:1;
  247. unsigned char reserved:2;
  248. unsigned char charger_connected:1;
  249. unsigned char report_rate:1;
  250. unsigned char configured:1;
  251. } __packed;
  252. unsigned char data[1];
  253. };
  254. };
  255. struct f34_properties_query {
  256. union {
  257. struct {
  258. unsigned char reg_map:1;
  259. unsigned char unlocked:1;
  260. unsigned char has_config_id:1;
  261. unsigned char has_perm_config:1;
  262. unsigned char has_bl_config:1;
  263. unsigned char has_disp_config:1;
  264. unsigned char has_ctrl1:1;
  265. unsigned char has_flash_query4:1;
  266. } __packed;
  267. unsigned char data[1];
  268. };
  269. };
  270. struct f34_query_04 {
  271. union {
  272. struct {
  273. unsigned char has_guest_code:1;
  274. unsigned char reserved:1;
  275. } __packed;
  276. unsigned char data[1];
  277. };
  278. };
  279. struct synaptics_rmi4_fwu_handle {
  280. enum bl_version bl_version;
  281. bool initialized;
  282. bool program_enabled;
  283. bool has_perm_config;
  284. bool has_bl_config;
  285. bool has_disp_config;
  286. bool has_guest_code;
  287. bool force_update;
  288. bool in_flash_prog_mode;
  289. bool do_lockdown;
  290. bool can_guest_bootloader;
  291. unsigned int data_pos;
  292. unsigned char *ext_data_source;
  293. unsigned char *read_config_buf;
  294. unsigned char intr_mask;
  295. unsigned char command;
  296. unsigned char bootloader_id[4];
  297. unsigned char flash_status;
  298. unsigned char productinfo1;
  299. unsigned char productinfo2;
  300. unsigned char properties_off;
  301. unsigned char blk_size_off;
  302. unsigned char blk_count_off;
  303. unsigned char blk_data_off;
  304. unsigned char flash_cmd_off;
  305. unsigned char flash_status_off;
  306. unsigned short block_size;
  307. unsigned short fw_block_count;
  308. unsigned short config_block_count;
  309. unsigned short perm_config_block_count;
  310. unsigned short bl_config_block_count;
  311. unsigned short disp_config_block_count;
  312. unsigned short guest_code_block_count;
  313. unsigned short config_size;
  314. unsigned short config_area;
  315. char product_id[SYNAPTICS_RMI4_PRODUCT_ID_SIZE + 1];
  316. struct f34_properties_query flash_properties;
  317. struct workqueue_struct *fwu_workqueue;
  318. struct delayed_work fwu_work;
  319. struct synaptics_rmi4_fn_desc f01_fd;
  320. struct synaptics_rmi4_fn_desc f34_fd;
  321. struct synaptics_rmi4_exp_fn_ptr *fn_ptr;
  322. struct synaptics_rmi4_data *rmi4_data;
  323. struct img_file_content img;
  324. bool polling_mode;
  325. };
  326. static struct bin_attribute dev_attr_data = {
  327. .attr = {
  328. .name = "data",
  329. .mode = (S_IRUGO | S_IWUSR | S_IWGRP),
  330. },
  331. .size = 0,
  332. .read = fwu_sysfs_show_image,
  333. .write = fwu_sysfs_store_image,
  334. };
  335. static struct device_attribute attrs[] = {
  336. __ATTR(doreflash, S_IWUSR | S_IWGRP,
  337. synaptics_rmi4_show_error,
  338. fwu_sysfs_do_reflash_store),
  339. __ATTR(writeconfig, S_IWUSR | S_IWGRP,
  340. synaptics_rmi4_show_error,
  341. fwu_sysfs_write_config_store),
  342. __ATTR(readconfig, S_IWUSR | S_IWGRP,
  343. synaptics_rmi4_show_error,
  344. fwu_sysfs_read_config_store),
  345. __ATTR(configarea, S_IWUSR | S_IWGRP,
  346. synaptics_rmi4_show_error,
  347. fwu_sysfs_config_area_store),
  348. __ATTR(imagesize, S_IWUSR | S_IWGRP,
  349. synaptics_rmi4_show_error,
  350. fwu_sysfs_image_size_store),
  351. __ATTR(blocksize, S_IRUGO,
  352. fwu_sysfs_block_size_show,
  353. synaptics_rmi4_store_error),
  354. __ATTR(fwblockcount, S_IRUGO,
  355. fwu_sysfs_firmware_block_count_show,
  356. synaptics_rmi4_store_error),
  357. __ATTR(configblockcount, S_IRUGO,
  358. fwu_sysfs_configuration_block_count_show,
  359. synaptics_rmi4_store_error),
  360. __ATTR(permconfigblockcount, S_IRUGO,
  361. fwu_sysfs_perm_config_block_count_show,
  362. synaptics_rmi4_store_error),
  363. __ATTR(blconfigblockcount, S_IRUGO,
  364. fwu_sysfs_bl_config_block_count_show,
  365. synaptics_rmi4_store_error),
  366. __ATTR(dispconfigblockcount, S_IRUGO,
  367. fwu_sysfs_disp_config_block_count_show,
  368. synaptics_rmi4_store_error),
  369. __ATTR(guestcodeblockcount, S_IRUGO,
  370. fwu_sysfs_guest_code_block_count_show,
  371. synaptics_rmi4_store_error),
  372. __ATTR(writeguestcode, S_IWUSR | S_IWGRP,
  373. synaptics_rmi4_show_error,
  374. fwu_sysfs_write_guest_code_store),
  375. };
  376. static struct synaptics_rmi4_fwu_handle *fwu;
  377. static unsigned int extract_uint_le(const unsigned char *ptr)
  378. {
  379. return (unsigned int)ptr[0] +
  380. (unsigned int)ptr[1] * 0x100 +
  381. (unsigned int)ptr[2] * 0x10000 +
  382. (unsigned int)ptr[3] * 0x1000000;
  383. }
  384. #ifdef FW_UPDATE_GO_NOGO
  385. static unsigned int extract_uint_be(const unsigned char *ptr)
  386. {
  387. return (unsigned int)ptr[3] +
  388. (unsigned int)ptr[2] * 0x100 +
  389. (unsigned int)ptr[1] * 0x10000 +
  390. (unsigned int)ptr[0] * 0x1000000;
  391. }
  392. #endif
  393. static unsigned short extract_ushort_le(const unsigned char *ptr)
  394. {
  395. return (unsigned int)ptr[0] + (unsigned int)ptr[1] * 0x100;
  396. }
  397. static int fwu_read_f01_device_status(struct f01_device_status *status)
  398. {
  399. int retval;
  400. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  401. retval = fwu->fn_ptr->read(rmi4_data,
  402. fwu->f01_fd.data_base_addr,
  403. status->data,
  404. sizeof(status->data));
  405. if (retval < 0) {
  406. dev_err(&rmi4_data->i2c_client->dev,
  407. "%s: Failed to read F01 device status\n",
  408. __func__);
  409. return retval;
  410. }
  411. return 0;
  412. }
  413. static int fwu_read_f34_queries(void)
  414. {
  415. int retval;
  416. unsigned char count;
  417. unsigned char buf[10];
  418. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  419. retval = fwu->fn_ptr->read(rmi4_data,
  420. fwu->f34_fd.query_base_addr + BOOTLOADER_ID_OFFSET,
  421. fwu->bootloader_id,
  422. sizeof(fwu->bootloader_id));
  423. if (retval < 0) {
  424. dev_err(&rmi4_data->i2c_client->dev,
  425. "%s: Failed to read bootloader ID\n",
  426. __func__);
  427. return retval;
  428. }
  429. if (fwu->bootloader_id[1] == '5') {
  430. fwu->bl_version = V5;
  431. } else if (fwu->bootloader_id[1] == '6') {
  432. fwu->bl_version = V6;
  433. } else {
  434. dev_err(&rmi4_data->i2c_client->dev,
  435. "%s: Unrecognized bootloader version\n",
  436. __func__);
  437. return -EINVAL;
  438. }
  439. dev_info(&fwu->rmi4_data->i2c_client->dev,
  440. "%s: F34 Query : ID: %s\n",
  441. __func__, fwu->bootloader_id);
  442. if (fwu->bl_version == V5) {
  443. fwu->properties_off = V5_PROPERTIES_OFFSET;
  444. fwu->blk_size_off = V5_BLOCK_SIZE_OFFSET;
  445. fwu->blk_count_off = V5_BLOCK_COUNT_OFFSET;
  446. fwu->blk_data_off = V5_BLOCK_DATA_OFFSET;
  447. } else if (fwu->bl_version == V6) {
  448. fwu->properties_off = V6_PROPERTIES_OFFSET;
  449. fwu->blk_size_off = V6_BLOCK_SIZE_OFFSET;
  450. fwu->blk_count_off = V6_BLOCK_COUNT_OFFSET;
  451. fwu->blk_data_off = V6_BLOCK_DATA_OFFSET;
  452. }
  453. retval = fwu->fn_ptr->read(rmi4_data,
  454. fwu->f34_fd.query_base_addr + fwu->properties_off,
  455. fwu->flash_properties.data,
  456. sizeof(fwu->flash_properties.data));
  457. if (retval < 0) {
  458. dev_err(&rmi4_data->i2c_client->dev,
  459. "%s: Failed to read flash properties\n",
  460. __func__);
  461. return retval;
  462. }
  463. count = 4;
  464. if (fwu->flash_properties.has_perm_config) {
  465. fwu->has_perm_config = 1;
  466. count += 2;
  467. }
  468. if (fwu->flash_properties.has_bl_config) {
  469. fwu->has_bl_config = 1;
  470. count += 2;
  471. }
  472. if (fwu->flash_properties.has_disp_config) {
  473. fwu->has_disp_config = 1;
  474. count += 2;
  475. }
  476. if (fwu->flash_properties.has_flash_query4) {
  477. struct f34_query_04 query4;
  478. retval = fwu->fn_ptr->read(rmi4_data,
  479. fwu->f34_fd.query_base_addr + fwu->blk_size_off + 2,
  480. query4.data,
  481. sizeof(query4.data));
  482. if (retval < 0) {
  483. dev_err(&rmi4_data->i2c_client->dev,
  484. "%s: Failed to read block size info\n",
  485. __func__);
  486. return retval;
  487. }
  488. if (query4.has_guest_code) {
  489. retval = fwu->fn_ptr->read(rmi4_data,
  490. fwu->f34_fd.query_base_addr + fwu->blk_size_off + 3,
  491. buf,
  492. 2);
  493. if (retval < 0) {
  494. dev_err(&rmi4_data->i2c_client->dev,
  495. "%s: Failed to read block size info\n",
  496. __func__);
  497. return retval;
  498. }
  499. batohs(&fwu->guest_code_block_count, buf);
  500. fwu->has_guest_code = 1;
  501. } else {
  502. dev_info(&rmi4_data->i2c_client->dev,
  503. "%s: query data do not supply guest image.\n",
  504. __func__);
  505. }
  506. }
  507. retval = fwu->fn_ptr->read(rmi4_data,
  508. fwu->f34_fd.query_base_addr + fwu->blk_size_off,
  509. buf,
  510. 2);
  511. if (retval < 0) {
  512. dev_err(&rmi4_data->i2c_client->dev,
  513. "%s: Failed to read block size info\n",
  514. __func__);
  515. return retval;
  516. }
  517. batohs(&fwu->block_size, &(buf[0]));
  518. if (fwu->bl_version == V5) {
  519. fwu->flash_cmd_off = fwu->blk_data_off + fwu->block_size;
  520. fwu->flash_status_off = fwu->flash_cmd_off;
  521. } else if (fwu->bl_version == V6) {
  522. fwu->flash_cmd_off = V6_FLASH_COMMAND_OFFSET;
  523. fwu->flash_status_off = V6_FLASH_STATUS_OFFSET;
  524. }
  525. retval = fwu->fn_ptr->read(rmi4_data,
  526. fwu->f34_fd.query_base_addr + fwu->blk_count_off,
  527. buf,
  528. count);
  529. if (retval < 0) {
  530. dev_err(&rmi4_data->i2c_client->dev,
  531. "%s: Failed to read block count info\n",
  532. __func__);
  533. return retval;
  534. }
  535. batohs(&fwu->fw_block_count, &(buf[0]));
  536. batohs(&fwu->config_block_count, &(buf[2]));
  537. count = 4;
  538. if (fwu->has_perm_config) {
  539. batohs(&fwu->perm_config_block_count, &(buf[count]));
  540. count += 2;
  541. }
  542. if (fwu->has_bl_config) {
  543. batohs(&fwu->bl_config_block_count, &(buf[count]));
  544. count += 2;
  545. }
  546. if (fwu->has_disp_config)
  547. batohs(&fwu->disp_config_block_count, &(buf[count]));
  548. return 0;
  549. }
  550. static int fwu_read_f34_flash_status(void)
  551. {
  552. int retval;
  553. unsigned char status;
  554. unsigned char command;
  555. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  556. retval = fwu->fn_ptr->read(rmi4_data,
  557. fwu->f34_fd.data_base_addr + fwu->flash_status_off,
  558. &status,
  559. sizeof(status));
  560. if (retval < 0) {
  561. dev_err(&rmi4_data->i2c_client->dev,
  562. "%s: Failed to read flash status\n",
  563. __func__);
  564. return retval;
  565. }
  566. fwu->program_enabled = status >> 7;
  567. if (fwu->bl_version == V5)
  568. fwu->flash_status = (status >> 4) & MASK_3BIT;
  569. else if (fwu->bl_version == V6)
  570. fwu->flash_status = status & MASK_3BIT;
  571. retval = fwu->fn_ptr->read(rmi4_data,
  572. fwu->f34_fd.data_base_addr + fwu->flash_cmd_off,
  573. &command,
  574. sizeof(command));
  575. if (retval < 0) {
  576. dev_err(&rmi4_data->i2c_client->dev,
  577. "%s: Failed to read flash command\n",
  578. __func__);
  579. return retval;
  580. }
  581. fwu->command = command & MASK_4BIT;
  582. return 0;
  583. }
  584. static int fwu_write_f34_command(unsigned char cmd)
  585. {
  586. int retval;
  587. unsigned char command = cmd & MASK_4BIT;
  588. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  589. fwu->command = cmd;
  590. retval = fwu->fn_ptr->write(rmi4_data,
  591. fwu->f34_fd.data_base_addr + fwu->flash_cmd_off,
  592. &command,
  593. sizeof(command));
  594. if (retval < 0) {
  595. dev_err(&rmi4_data->i2c_client->dev,
  596. "%s: Failed to write command 0x%02x\n",
  597. __func__, command);
  598. return retval;
  599. }
  600. return 0;
  601. }
  602. static int fwu_wait_for_idle(int timeout_ms)
  603. {
  604. int count = 0;
  605. int timeout_count = ((timeout_ms * 1000) / MAX_SLEEP_TIME_US) + 1;
  606. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  607. do {
  608. if (fwu->polling_mode || count == timeout_count)
  609. fwu_read_f34_flash_status();
  610. if ((fwu->command == 0x00) && (fwu->flash_status == 0x00))
  611. {
  612. if (count == timeout_count)
  613. fwu->polling_mode = true;
  614. return 0;
  615. }
  616. usleep_range(MIN_SLEEP_TIME_US, MAX_SLEEP_TIME_US);
  617. count++;
  618. } while (count <= timeout_count);
  619. dev_err(&rmi4_data->i2c_client->dev,
  620. "%s: Timed out waiting for idle status\n",
  621. __func__);
  622. return -ETIMEDOUT;
  623. }
  624. #ifdef FW_UPDATE_GO_NOGO
  625. static enum flash_area fwu_go_nogo(void)
  626. {
  627. int retval;
  628. enum flash_area flash_area = NONE;
  629. unsigned char index = 0;
  630. unsigned char config_id[4];
  631. unsigned int device_config_id;
  632. unsigned int image_config_id;
  633. unsigned int device_fw_id;
  634. unsigned long image_fw_id;
  635. char *strptr;
  636. char *firmware_id;
  637. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  638. if (fwu->force_update) {
  639. flash_area = UI_FIRMWARE;
  640. goto exit;
  641. }
  642. /* Update both UI and config if device is in bootloader mode */
  643. if (fwu->in_flash_prog_mode) {
  644. flash_area = UI_FIRMWARE;
  645. goto exit;
  646. }
  647. /* Get device firmware ID */
  648. device_fw_id = rmi4_data->rmi4_mod_info.build_id[0] +
  649. rmi4_data->rmi4_mod_info.build_id[1] * 0x100 +
  650. rmi4_data->rmi4_mod_info.build_id[2] * 0x10000;
  651. dev_info(&rmi4_data->i2c_client->dev,
  652. "%s: Device firmware ID = %d\n",
  653. __func__, device_fw_id);
  654. /* Get image firmware ID */
  655. if (fwu->img.firmwareId != NULL) {
  656. image_fw_id = extract_uint_le(fwu->img.firmwareId);
  657. } else {
  658. strptr = strstr(fwu->img.image_name, "PR");
  659. if (!strptr) {
  660. dev_err(&rmi4_data->i2c_client->dev,
  661. "%s: No valid PR number (PRxxxxxxx) "
  662. "found in image file name (%s)\n",
  663. __func__, fwu->img.image_name);
  664. flash_area = NONE;
  665. goto exit;
  666. }
  667. strptr += 2;
  668. firmware_id = kzalloc(MAX_FIRMWARE_ID_LEN, GFP_KERNEL);
  669. while (strptr[index] >= '0' && strptr[index] <= '9') {
  670. firmware_id[index] = strptr[index];
  671. index++;
  672. }
  673. retval = kstrtoul(firmware_id, 10, &image_fw_id);
  674. kfree(firmware_id);
  675. if (retval) {
  676. dev_err(&rmi4_data->i2c_client->dev,
  677. "%s: Failed to obtain image firmware ID\n",
  678. __func__);
  679. flash_area = NONE;
  680. goto exit;
  681. }
  682. }
  683. dev_info(&rmi4_data->i2c_client->dev,
  684. "%s: Image firmware ID = %d\n",
  685. __func__, (unsigned int)image_fw_id);
  686. if (image_fw_id > device_fw_id) {
  687. flash_area = UI_FIRMWARE;
  688. goto exit;
  689. } else if (image_fw_id < device_fw_id) {
  690. dev_info(&rmi4_data->i2c_client->dev,
  691. "%s: Image firmware ID older than device firmware ID\n",
  692. __func__);
  693. flash_area = NONE;
  694. goto exit;
  695. }
  696. /* Get device config ID */
  697. retval = fwu->fn_ptr->read(rmi4_data,
  698. fwu->f34_fd.ctrl_base_addr,
  699. config_id,
  700. sizeof(config_id));
  701. if (retval < 0) {
  702. dev_err(&rmi4_data->i2c_client->dev,
  703. "%s: Failed to read device config ID\n",
  704. __func__);
  705. flash_area = NONE;
  706. goto exit;
  707. }
  708. device_config_id = extract_uint_be(config_id);
  709. dev_info(&rmi4_data->i2c_client->dev,
  710. "%s: Device config ID = 0x%02x 0x%02x 0x%02x 0x%02x\n",
  711. __func__,
  712. config_id[0],
  713. config_id[1],
  714. config_id[2],
  715. config_id[3]);
  716. /* Get image config ID */
  717. image_config_id = extract_uint_be(fwu->img.configId);
  718. dev_info(&rmi4_data->i2c_client->dev,
  719. "%s: Image config ID = 0x%02x 0x%02x 0x%02x 0x%02x\n",
  720. __func__,
  721. fwu->img.configId[0],
  722. fwu->img.configId[1],
  723. fwu->img.configId[2],
  724. fwu->img.configId[3]);
  725. if (image_config_id > device_config_id) {
  726. flash_area = CONFIG_AREA;
  727. goto exit;
  728. }
  729. exit:
  730. if (flash_area == NONE) {
  731. dev_info(&rmi4_data->i2c_client->dev,
  732. "%s: No need to do reflash\n",
  733. __func__);
  734. } else {
  735. dev_info(&rmi4_data->i2c_client->dev,
  736. "%s: Updating %s\n",
  737. __func__,
  738. flash_area == UI_FIRMWARE ?
  739. "UI firmware" :
  740. "config only");
  741. }
  742. return flash_area;
  743. }
  744. #endif
  745. static int fwu_scan_pdt(void)
  746. {
  747. int retval;
  748. unsigned char ii;
  749. unsigned char intr_count = 0;
  750. unsigned char intr_off;
  751. unsigned char intr_src;
  752. unsigned short addr;
  753. bool f01found = false;
  754. bool f34found = false;
  755. struct synaptics_rmi4_fn_desc rmi_fd;
  756. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  757. for (addr = PDT_START; addr > PDT_END; addr -= PDT_ENTRY_SIZE) {
  758. retval = fwu->fn_ptr->read(rmi4_data,
  759. addr,
  760. (unsigned char *)&rmi_fd,
  761. sizeof(rmi_fd));
  762. if (retval < 0)
  763. return retval;
  764. if (rmi_fd.fn_number) {
  765. dev_dbg(&rmi4_data->i2c_client->dev,
  766. "%s: Found F%02x\n",
  767. __func__, rmi_fd.fn_number);
  768. switch (rmi_fd.fn_number) {
  769. case SYNAPTICS_RMI4_F01:
  770. f01found = true;
  771. fwu->f01_fd.query_base_addr =
  772. rmi_fd.query_base_addr;
  773. fwu->f01_fd.ctrl_base_addr =
  774. rmi_fd.ctrl_base_addr;
  775. fwu->f01_fd.data_base_addr =
  776. rmi_fd.data_base_addr;
  777. fwu->f01_fd.cmd_base_addr =
  778. rmi_fd.cmd_base_addr;
  779. break;
  780. case SYNAPTICS_RMI4_F34:
  781. f34found = true;
  782. fwu->f34_fd.query_base_addr =
  783. rmi_fd.query_base_addr;
  784. fwu->f34_fd.ctrl_base_addr =
  785. rmi_fd.ctrl_base_addr;
  786. fwu->f34_fd.data_base_addr =
  787. rmi_fd.data_base_addr;
  788. fwu->intr_mask = 0;
  789. intr_src = rmi_fd.intr_src_count;
  790. intr_off = intr_count % 8;
  791. for (ii = intr_off;
  792. ii < ((intr_src & MASK_3BIT) +
  793. intr_off);
  794. ii++) {
  795. fwu->intr_mask |= 1 << ii;
  796. }
  797. break;
  798. }
  799. } else {
  800. break;
  801. }
  802. intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
  803. }
  804. if (!f01found || !f34found) {
  805. dev_err(&rmi4_data->i2c_client->dev,
  806. "%s: Failed to find both F01 and F34\n",
  807. __func__);
  808. return -EINVAL;
  809. }
  810. fwu->polling_mode = POLLING_MODE_DEFAULT;
  811. return 0;
  812. }
  813. static int fwu_write_blocks(unsigned char *block_ptr, unsigned int block_size,
  814. enum flash_command command)
  815. {
  816. int retval;
  817. unsigned char block_offset[] = {0, 0};
  818. unsigned short block_num;
  819. unsigned short block_cnt = block_size / fwu->block_size;
  820. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  821. if (block_ptr == NULL) {
  822. dev_err(&rmi4_data->i2c_client->dev,
  823. "%s: Cannot find block data (%x)\n",
  824. __func__, command);
  825. return -EINVAL;
  826. }
  827. if (command == CMD_WRITE_CONFIG_BLOCK)
  828. block_offset[1] |= (fwu->config_area << 5);
  829. retval = fwu->fn_ptr->write(rmi4_data,
  830. fwu->f34_fd.data_base_addr + BLOCK_NUMBER_OFFSET,
  831. block_offset,
  832. sizeof(block_offset));
  833. if (retval < 0) {
  834. dev_err(&rmi4_data->i2c_client->dev,
  835. "%s: Failed to write to block number registers\n",
  836. __func__);
  837. return retval;
  838. }
  839. for (block_num = 0; block_num < block_cnt; block_num++) {
  840. retval = fwu->fn_ptr->write(rmi4_data,
  841. fwu->f34_fd.data_base_addr + fwu->blk_data_off,
  842. block_ptr,
  843. fwu->block_size);
  844. if (retval < 0) {
  845. dev_err(&rmi4_data->i2c_client->dev,
  846. "%s: Failed to write block data (block %d)\n",
  847. __func__, block_num);
  848. return retval;
  849. }
  850. retval = fwu_write_f34_command(command);
  851. if (retval < 0) {
  852. dev_err(&rmi4_data->i2c_client->dev,
  853. "%s: Failed to write command for block %d\n",
  854. __func__, block_num);
  855. return retval;
  856. }
  857. retval = fwu_wait_for_idle(WRITE_WAIT_MS);
  858. if (retval < 0) {
  859. dev_err(&rmi4_data->i2c_client->dev,
  860. "%s: Failed to wait for idle status (block %d)\n",
  861. __func__, block_num);
  862. return retval;
  863. }
  864. block_ptr += fwu->block_size;
  865. }
  866. return 0;
  867. }
  868. static int fwu_write_firmware_block(void)
  869. {
  870. return fwu_write_blocks(fwu->img.uiFirmware.data,
  871. fwu->img.uiFirmware.size, CMD_WRITE_FW_BLOCK);
  872. }
  873. static int fwu_write_config_block(void)
  874. {
  875. printk(KERN_INFO "[synaptics] %s: size : %d, cmd : %d\n", __func__, fwu->img.uiConfig.size, CMD_WRITE_CONFIG_BLOCK);
  876. return fwu_write_blocks(fwu->img.uiConfig.data,
  877. fwu->img.uiConfig.size, CMD_WRITE_CONFIG_BLOCK);
  878. }
  879. /*
  880. static int fwu_write_lockdown_block(void)
  881. {
  882. return fwu_write_blocks(fwu->img.lockdown.data,
  883. fwu->img.lockdown.size, CMD_WRITE_LOCKDOWN_BLOCK);
  884. }
  885. */
  886. static int fwu_write_guest_code_block(void)
  887. {
  888. return fwu_write_blocks(fwu->img.guestCode.data,
  889. fwu->img.guestCode.size, CMD_WRITE_GUEST_CODE);
  890. }
  891. static int fwu_write_bootloader_id(void)
  892. {
  893. int retval;
  894. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  895. retval = fwu->fn_ptr->write(rmi4_data,
  896. fwu->f34_fd.data_base_addr + fwu->blk_data_off,
  897. fwu->bootloader_id,
  898. sizeof(fwu->bootloader_id));
  899. if (retval < 0) {
  900. dev_err(&rmi4_data->i2c_client->dev,
  901. "%s: Failed to write bootloader ID\n",
  902. __func__);
  903. return retval;
  904. }
  905. return 0;
  906. }
  907. static int fwu_enter_flash_prog(void)
  908. {
  909. int retval;
  910. struct f01_device_status f01_device_status;
  911. struct f01_device_control f01_device_control;
  912. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  913. unsigned char int_enable = 0x00;
  914. retval = fwu_read_f34_flash_status();
  915. if (retval < 0)
  916. return retval;
  917. if (fwu->program_enabled)
  918. return 0;
  919. /* To block interrupt from finger or hovering during enter flash program mode
  920. * 1. F01_RMI_CTRL1(Interrupt Enable 0) register is cleared on this position.
  921. * 2. After enter flash program mode, interrupt occured only by Flash bit(0x01)
  922. * 3. After finish all flashing and reset, Interrupt enable register will be set as default value
  923. */
  924. retval = fwu->fn_ptr->write(rmi4_data,
  925. fwu->f01_fd.ctrl_base_addr + 1,
  926. &int_enable, sizeof(int_enable));
  927. if (retval < 0) {
  928. dev_err(&rmi4_data->i2c_client->dev,
  929. "%s: Failed to write interrupt enable register\n", __func__);
  930. return retval;
  931. }
  932. msleep(20);
  933. retval = fwu_write_bootloader_id();
  934. if (retval < 0)
  935. return retval;
  936. retval = fwu_write_f34_command(CMD_ENABLE_FLASH_PROG);
  937. if (retval < 0)
  938. return retval;
  939. retval = fwu_wait_for_idle(ENABLE_WAIT_MS);
  940. if (retval < 0)
  941. return retval;
  942. if (!fwu->program_enabled) {
  943. dev_err(&rmi4_data->i2c_client->dev,
  944. "%s: Program enabled bit not set\n",
  945. __func__);
  946. return -EINVAL;
  947. }
  948. retval = fwu_scan_pdt();
  949. if (retval < 0)
  950. return retval;
  951. retval = fwu_read_f01_device_status(&f01_device_status);
  952. if (retval < 0)
  953. return retval;
  954. if (!f01_device_status.flash_prog) {
  955. dev_err(&rmi4_data->i2c_client->dev,
  956. "%s: Not in flash prog mode\n",
  957. __func__);
  958. return -EINVAL;
  959. }
  960. retval = fwu_read_f34_queries();
  961. if (retval < 0)
  962. return retval;
  963. retval = fwu->fn_ptr->read(rmi4_data,
  964. fwu->f01_fd.ctrl_base_addr,
  965. f01_device_control.data,
  966. sizeof(f01_device_control.data));
  967. if (retval < 0) {
  968. dev_err(&rmi4_data->i2c_client->dev,
  969. "%s: Failed to read F01 device control\n",
  970. __func__);
  971. return retval;
  972. }
  973. f01_device_control.nosleep = true;
  974. f01_device_control.sleep_mode = SLEEP_MODE_NORMAL;
  975. retval = fwu->fn_ptr->write(rmi4_data,
  976. fwu->f01_fd.ctrl_base_addr,
  977. f01_device_control.data,
  978. sizeof(f01_device_control.data));
  979. if (retval < 0) {
  980. dev_err(&rmi4_data->i2c_client->dev,
  981. "%s: Failed to write F01 device control\n",
  982. __func__);
  983. return retval;
  984. }
  985. return retval;
  986. }
  987. static int fwu_do_reflash(void)
  988. {
  989. int retval;
  990. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  991. retval = fwu_enter_flash_prog();
  992. if (retval < 0)
  993. return retval;
  994. dev_info(&rmi4_data->i2c_client->dev,
  995. "%s: Entered flash prog mode\n",
  996. __func__);
  997. retval = fwu_write_bootloader_id();
  998. if (retval < 0)
  999. return retval;
  1000. dev_info(&rmi4_data->i2c_client->dev,
  1001. "%s: Bootloader ID written\n",
  1002. __func__);
  1003. retval = fwu_write_f34_command(CMD_ERASE_ALL);
  1004. if (retval < 0)
  1005. return retval;
  1006. dev_info(&rmi4_data->i2c_client->dev,
  1007. "%s: Erase all command written\n",
  1008. __func__);
  1009. usleep_range(MAX_SLEEP_TIME_US, MAX_SLEEP_TIME_US + MIN_SLEEP_TIME_US);
  1010. retval = fwu_wait_for_idle(ERASE_WAIT_MS);
  1011. if (retval < 0)
  1012. return retval;
  1013. dev_info(&rmi4_data->i2c_client->dev,
  1014. "%s: Idle status detected\n",
  1015. __func__);
  1016. if (fwu->img.uiFirmware.data) {
  1017. retval = fwu_write_firmware_block();
  1018. if (retval < 0)
  1019. return retval;
  1020. dev_info(&rmi4_data->i2c_client->dev, "%s: Firmware programmed\n",
  1021. __func__);
  1022. }
  1023. if (fwu->img.uiConfig.data) {
  1024. retval = fwu_write_config_block();
  1025. if (retval < 0)
  1026. return retval;
  1027. dev_info(&rmi4_data->i2c_client->dev, "%s: Configuration programmed\n",
  1028. __func__);
  1029. }
  1030. if (fwu->img.guestCode.data) {
  1031. retval = fwu_do_write_guest_code();
  1032. if (retval < 0)
  1033. return retval;
  1034. dev_info(&rmi4_data->i2c_client->dev, "%s: guest-thread programmed\n",
  1035. __func__);
  1036. } else {
  1037. dev_info(&rmi4_data->i2c_client->dev, "%s: is not guestcode data in fw img\n",
  1038. __func__);
  1039. }
  1040. return retval;
  1041. }
  1042. static int fwu_do_write_config(void)
  1043. {
  1044. int retval;
  1045. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1046. retval = fwu_enter_flash_prog();
  1047. if (retval < 0)
  1048. return retval;
  1049. dev_dbg(&rmi4_data->i2c_client->dev,
  1050. "%s: Entered flash prog mode\n",
  1051. __func__);
  1052. if (fwu->config_area == PERM_CONFIG_AREA) {
  1053. fwu->config_block_count = fwu->perm_config_block_count;
  1054. goto write_config;
  1055. }
  1056. retval = fwu_write_bootloader_id();
  1057. if (retval < 0)
  1058. return retval;
  1059. dev_info(&rmi4_data->i2c_client->dev,
  1060. "%s: Bootloader ID written, AREA : %d\n",
  1061. __func__, fwu->config_area);
  1062. switch (fwu->config_area) {
  1063. case UI_CONFIG_AREA:
  1064. retval = fwu_write_f34_command(CMD_ERASE_CONFIG);
  1065. break;
  1066. case BL_CONFIG_AREA:
  1067. retval = fwu_write_f34_command(CMD_ERASE_BL_CONFIG);
  1068. fwu->config_block_count = fwu->bl_config_block_count;
  1069. break;
  1070. case DISP_CONFIG_AREA:
  1071. retval = fwu_write_f34_command(CMD_ERASE_DISP_CONFIG);
  1072. fwu->config_block_count = fwu->disp_config_block_count;
  1073. break;
  1074. }
  1075. if (retval < 0)
  1076. return retval;
  1077. dev_dbg(&rmi4_data->i2c_client->dev,
  1078. "%s: Erase command written\n",
  1079. __func__);
  1080. retval = fwu_wait_for_idle(ERASE_WAIT_MS);
  1081. if (retval < 0)
  1082. return retval;
  1083. dev_dbg(&rmi4_data->i2c_client->dev,
  1084. "%s: Idle status detected\n",
  1085. __func__);
  1086. write_config:
  1087. retval = fwu_write_config_block();
  1088. if (retval < 0)
  1089. return retval;
  1090. dev_info(&rmi4_data->i2c_client->dev, "%s: Config written\n",
  1091. __func__);
  1092. return retval;
  1093. }
  1094. static int fwu_start_reflash(void)
  1095. {
  1096. int retval = 0, retry = 3;
  1097. #ifdef FW_UPDATE_GO_NOGO
  1098. enum flash_area flash_area;
  1099. #endif
  1100. struct f01_device_status f01_device_status;
  1101. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1102. if (rmi4_data->sensor_sleep || rmi4_data->touch_stopped) {
  1103. dev_err(&rmi4_data->i2c_client->dev,
  1104. "%s: Sensor sleeping or stopped\n",
  1105. __func__);
  1106. return -ENODEV;
  1107. }
  1108. rmi4_data->stay_awake = true;
  1109. dev_info(&rmi4_data->i2c_client->dev,
  1110. "%s: start\n", __func__);
  1111. if (!fwu->ext_data_source) {
  1112. dev_err(&rmi4_data->i2c_client->dev,
  1113. "%s: Firmware data is NULL\n", __func__);
  1114. return -ENODEV;
  1115. }
  1116. fwu->img.fw_image = fwu->ext_data_source;
  1117. fwu_img_parse_format();
  1118. if (fwu->bl_version != fwu->img.blMajorVersion) {
  1119. dev_err(&rmi4_data->i2c_client->dev,
  1120. "%s: Bootloader version mismatch\n",
  1121. __func__);
  1122. retval = -EINVAL;
  1123. goto exit;
  1124. }
  1125. while (retry--) {
  1126. mutex_lock(&(rmi4_data->rmi4_reflash_mutex));
  1127. rmi4_data->doing_reflash = true;
  1128. #ifdef FW_UPDATE_GO_NOGO
  1129. flash_area = fwu_go_nogo();
  1130. switch (flash_area) {
  1131. case UI_FIRMWARE:
  1132. retval = fwu_do_reflash();
  1133. break;
  1134. case CONFIG_AREA:
  1135. retval = fwu_do_write_config();
  1136. break;
  1137. case NONE:
  1138. default:
  1139. dev_info(&rmi4_data->i2c_client->dev, "%s: case is NONE or default\n",
  1140. __func__);
  1141. goto exit;
  1142. }
  1143. #else
  1144. retval = fwu_do_reflash();
  1145. #endif
  1146. if (retval < 0) {
  1147. dev_err(&rmi4_data->i2c_client->dev,
  1148. "%s: Failed to do reflash\n",
  1149. __func__);
  1150. }
  1151. exit:
  1152. rmi4_data->reset_device(rmi4_data);
  1153. rmi4_data->doing_reflash = false;
  1154. mutex_unlock(&(rmi4_data->rmi4_reflash_mutex));
  1155. retval = fwu_read_f01_device_status(&f01_device_status);
  1156. if (retval < 0)
  1157. goto exit;
  1158. else
  1159. break;
  1160. }
  1161. dev_info(&rmi4_data->i2c_client->dev, "%s: End of reflash process\n",
  1162. __func__);
  1163. rmi4_data->stay_awake = false;
  1164. return retval;
  1165. }
  1166. static int fwu_do_write_guest_code(void)
  1167. {
  1168. int retval;
  1169. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1170. retval = fwu_enter_flash_prog();
  1171. if (retval < 0)
  1172. return retval;
  1173. dev_dbg(&rmi4_data->i2c_client->dev,
  1174. "%s: Entered flash prog mode\n",
  1175. __func__);
  1176. retval = fwu_write_bootloader_id();
  1177. if (retval < 0)
  1178. return retval;
  1179. dev_dbg(&rmi4_data->i2c_client->dev,
  1180. "%s: Bootloader ID written\n",
  1181. __func__);
  1182. retval = fwu_write_f34_command(CMD_ERASE_GUEST_CODE);
  1183. if (retval < 0)
  1184. return retval;
  1185. dev_dbg(&rmi4_data->i2c_client->dev,
  1186. "%s: Erase command written\n",
  1187. __func__);
  1188. retval = fwu_wait_for_idle(ERASE_WAIT_MS);
  1189. if (retval < 0)
  1190. return retval;
  1191. dev_dbg(&rmi4_data->i2c_client->dev,
  1192. "%s: Idle status detected\n",
  1193. __func__);
  1194. retval = fwu_write_guest_code_block();
  1195. if (retval < 0)
  1196. return retval;
  1197. dev_info(&fwu->rmi4_data->i2c_client->dev, "%s: guest code written\n",
  1198. __func__);
  1199. return retval;
  1200. }
  1201. static int fwu_start_write_config(void)
  1202. {
  1203. int retval;
  1204. unsigned short block_count;
  1205. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1206. switch (fwu->config_area) {
  1207. case UI_CONFIG_AREA:
  1208. block_count = fwu->config_block_count;
  1209. break;
  1210. case PERM_CONFIG_AREA:
  1211. if (!fwu->has_perm_config)
  1212. return -EINVAL;
  1213. block_count = fwu->perm_config_block_count;
  1214. break;
  1215. case BL_CONFIG_AREA:
  1216. if (!fwu->has_bl_config)
  1217. return -EINVAL;
  1218. block_count = fwu->bl_config_block_count;
  1219. break;
  1220. case DISP_CONFIG_AREA:
  1221. if (!fwu->has_disp_config)
  1222. return -EINVAL;
  1223. block_count = fwu->disp_config_block_count;
  1224. break;
  1225. default:
  1226. return -EINVAL;
  1227. }
  1228. if (fwu->ext_data_source)
  1229. fwu->img.uiConfig.data = fwu->ext_data_source;
  1230. else
  1231. return -EINVAL;
  1232. fwu->config_size = fwu->block_size * block_count;
  1233. /* Jump to the config area if given a packrat image */
  1234. if ((fwu->config_area == UI_CONFIG_AREA) &&
  1235. (fwu->config_size != fwu->img.image_size)) {
  1236. fwu_img_parse_format();
  1237. }
  1238. dev_info(&rmi4_data->i2c_client->dev, "%s: Start of write config process\n",
  1239. __func__);
  1240. rmi4_data->doing_reflash = true;
  1241. retval = fwu_do_write_config();
  1242. if (retval < 0) {
  1243. dev_err(&rmi4_data->i2c_client->dev,
  1244. "%s: Failed to write config\n",
  1245. __func__);
  1246. }
  1247. rmi4_data->reset_device(rmi4_data);
  1248. rmi4_data->doing_reflash = false;
  1249. dev_info(&rmi4_data->i2c_client->dev, "%s: End of write config process\n",
  1250. __func__);
  1251. return retval;
  1252. }
  1253. #define CHECKSUM_SIZE 4
  1254. static void synaptics_rmi_calculate_checksum(unsigned short *data,
  1255. unsigned short len, unsigned long *result)
  1256. {
  1257. unsigned long temp;
  1258. unsigned long sum1 = 0xffff;
  1259. unsigned long sum2 = 0xffff;
  1260. *result = 0xffffffff;
  1261. while (len--) {
  1262. temp = *data;
  1263. sum1 += temp;
  1264. sum2 += sum1;
  1265. sum1 = (sum1 & 0xffff) + (sum1 >> 16);
  1266. sum2 = (sum2 & 0xffff) + (sum2 >> 16);
  1267. data++;
  1268. }
  1269. *result = sum2 << 16 | sum1;
  1270. return;
  1271. }
  1272. static void synaptics_rmi_rewrite_checksum(unsigned char *dest,
  1273. unsigned long src)
  1274. {
  1275. dest[0] = (unsigned char)(src & 0xff);
  1276. dest[1] = (unsigned char)((src >> 8) & 0xff);
  1277. dest[2] = (unsigned char)((src >> 16) & 0xff);
  1278. dest[3] = (unsigned char)((src >> 24) & 0xff);
  1279. return;
  1280. }
  1281. int synaptics_rmi4_set_tsp_test_result_in_config(int pass_fail)
  1282. {
  1283. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1284. int retval;
  1285. unsigned char buf[10] = {0, };
  1286. unsigned long checksum;
  1287. dev_info(&rmi4_data->i2c_client->dev, "%s: test %s\n",
  1288. __func__, pass_fail == SYNAPTICS_FACTORY_TEST_PASS ? "PASS" :
  1289. pass_fail == SYNAPTICS_FACTORY_TEST_FAIL ? "FAIL" : "NONE");
  1290. /* read config from IC */
  1291. memset(buf, 0, sizeof(buf));
  1292. snprintf(buf, 2, "%u\n", 1);
  1293. fwu_sysfs_read_config_store(&rmi4_data->i2c_client->dev, NULL, buf, 1);
  1294. /* set test result value */
  1295. if (pass_fail == SYNAPTICS_FACTORY_TEST_PASS) {
  1296. fwu->read_config_buf[0] |= 0x20;
  1297. fwu->read_config_buf[0] &= ~0x10;
  1298. } else if (pass_fail == SYNAPTICS_FACTORY_TEST_FAIL) {
  1299. fwu->read_config_buf[0] |= 0x10;
  1300. fwu->read_config_buf[0] &= ~0x20;
  1301. } else if (pass_fail == SYNAPTICS_FACTORY_TEST_NONE) {
  1302. fwu->read_config_buf[0] &= ~0x10;
  1303. fwu->read_config_buf[0] &= ~0x20;
  1304. }
  1305. /* check CRC checksum value and re-write checksum in config */
  1306. synaptics_rmi_calculate_checksum((unsigned short *)fwu->read_config_buf,
  1307. (fwu->config_size - CHECKSUM_SIZE) / 2, &checksum);
  1308. synaptics_rmi_rewrite_checksum(&fwu->read_config_buf[fwu->config_size - CHECKSUM_SIZE],
  1309. checksum);
  1310. retval = fwu_enter_flash_prog();
  1311. if (retval < 0)
  1312. goto err_config_write;
  1313. retval = fwu_write_bootloader_id();
  1314. if (retval < 0)
  1315. goto err_config_write;
  1316. dev_info(&fwu->rmi4_data->i2c_client->dev,
  1317. "%s: Bootloader ID written\n",
  1318. __func__);
  1319. retval = fwu_write_f34_command(CMD_ERASE_CONFIG);
  1320. if (retval < 0)
  1321. goto err_config_write;
  1322. dev_info(&fwu->rmi4_data->i2c_client->dev,
  1323. "%s: Erase command written\n",
  1324. __func__);
  1325. retval = fwu_wait_for_idle(ERASE_WAIT_MS);
  1326. if (retval < 0)
  1327. goto err_config_write;
  1328. dev_info(&fwu->rmi4_data->i2c_client->dev,
  1329. "%s: Idle status detected\n",
  1330. __func__);
  1331. fwu_write_blocks(fwu->read_config_buf,
  1332. fwu->config_size, CMD_WRITE_CONFIG_BLOCK);
  1333. dev_info(&fwu->rmi4_data->i2c_client->dev, "%s: Config written\n",
  1334. __func__);
  1335. err_config_write:
  1336. fwu->rmi4_data->reset_device(fwu->rmi4_data);
  1337. fwu->rmi4_data->doing_reflash = false;
  1338. return retval;
  1339. }
  1340. static int fwu_start_write_guest_code(void)
  1341. {
  1342. int retval;
  1343. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1344. if (!fwu->ext_data_source)
  1345. return -EINVAL;
  1346. fwu_img_parse_format();
  1347. dev_info(&rmi4_data->i2c_client->dev,
  1348. "%s: Start of update guest code process\n", __func__);
  1349. retval = fwu_do_write_guest_code();
  1350. if (retval < 0) {
  1351. dev_err(&rmi4_data->i2c_client->dev,
  1352. "%s: Failed to write config\n",
  1353. __func__);
  1354. }
  1355. rmi4_data->reset_device(rmi4_data);
  1356. dev_info(&rmi4_data->i2c_client->dev,
  1357. "%s: End of write guest code process\n", __func__);
  1358. return retval;
  1359. }
  1360. static int fwu_do_read_config(void)
  1361. {
  1362. int retval;
  1363. unsigned char block_offset[] = {0, 0};
  1364. unsigned short block_num;
  1365. unsigned short block_count;
  1366. unsigned short index = 0;
  1367. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1368. retval = fwu_enter_flash_prog();
  1369. if (retval < 0)
  1370. goto exit;
  1371. dev_dbg(&rmi4_data->i2c_client->dev,
  1372. "%s: Entered flash prog mode\n",
  1373. __func__);
  1374. switch (fwu->config_area) {
  1375. case UI_CONFIG_AREA:
  1376. block_count = fwu->config_block_count;
  1377. break;
  1378. case PERM_CONFIG_AREA:
  1379. if (!fwu->has_perm_config) {
  1380. retval = -EINVAL;
  1381. goto exit;
  1382. }
  1383. block_count = fwu->perm_config_block_count;
  1384. break;
  1385. case BL_CONFIG_AREA:
  1386. if (!fwu->has_bl_config) {
  1387. retval = -EINVAL;
  1388. goto exit;
  1389. }
  1390. block_count = fwu->bl_config_block_count;
  1391. break;
  1392. case DISP_CONFIG_AREA:
  1393. if (!fwu->has_disp_config) {
  1394. retval = -EINVAL;
  1395. goto exit;
  1396. }
  1397. block_count = fwu->disp_config_block_count;
  1398. break;
  1399. default:
  1400. retval = -EINVAL;
  1401. goto exit;
  1402. }
  1403. fwu->config_size = fwu->block_size * block_count;
  1404. kfree(fwu->read_config_buf);
  1405. fwu->read_config_buf = kzalloc(fwu->config_size, GFP_KERNEL);
  1406. if (!fwu->read_config_buf) {
  1407. dev_err(&fwu->rmi4_data->i2c_client->dev,
  1408. "%s: Failed to alloc mem for config size data\n",
  1409. __func__);
  1410. return -ENOMEM;
  1411. }
  1412. block_offset[1] |= (fwu->config_area << 5);
  1413. retval = fwu->fn_ptr->write(rmi4_data,
  1414. fwu->f34_fd.data_base_addr + BLOCK_NUMBER_OFFSET,
  1415. block_offset,
  1416. sizeof(block_offset));
  1417. if (retval < 0) {
  1418. dev_err(&rmi4_data->i2c_client->dev,
  1419. "%s: Failed to write to block number registers\n",
  1420. __func__);
  1421. goto exit;
  1422. }
  1423. for (block_num = 0; block_num < block_count; block_num++) {
  1424. retval = fwu_write_f34_command(CMD_READ_CONFIG_BLOCK);
  1425. if (retval < 0) {
  1426. dev_err(&rmi4_data->i2c_client->dev,
  1427. "%s: Failed to write read config command\n",
  1428. __func__);
  1429. goto exit;
  1430. }
  1431. retval = fwu_wait_for_idle(WRITE_WAIT_MS);
  1432. if (retval < 0) {
  1433. dev_err(&rmi4_data->i2c_client->dev,
  1434. "%s: Failed to wait for idle status\n",
  1435. __func__);
  1436. goto exit;
  1437. }
  1438. retval = fwu->fn_ptr->read(rmi4_data,
  1439. fwu->f34_fd.data_base_addr + fwu->blk_data_off,
  1440. &fwu->read_config_buf[index],
  1441. fwu->block_size);
  1442. if (retval < 0) {
  1443. dev_err(&rmi4_data->i2c_client->dev,
  1444. "%s: Failed to read block data (block %d)\n",
  1445. __func__, block_num);
  1446. goto exit;
  1447. }
  1448. index += fwu->block_size;
  1449. }
  1450. exit:
  1451. fwu->rmi4_data->reset_device(rmi4_data);
  1452. return retval;
  1453. }
  1454. /*
  1455. static int fwu_do_lockdown(void)
  1456. {
  1457. int retval;
  1458. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1459. retval = fwu_enter_flash_prog();
  1460. if (retval < 0)
  1461. return retval;
  1462. retval = fwu->fn_ptr->read(rmi4_data,
  1463. fwu->f34_fd.query_base_addr + fwu->properties_off,
  1464. fwu->flash_properties.data,
  1465. sizeof(fwu->flash_properties.data));
  1466. if (retval < 0) {
  1467. dev_err(&rmi4_data->i2c_client->dev,
  1468. "%s: Failed to read flash properties\n",
  1469. __func__);
  1470. return retval;
  1471. }
  1472. if (fwu->flash_properties.unlocked == 0) {
  1473. dev_info(&rmi4_data->i2c_client->dev,
  1474. "%s: Device already locked down\n",
  1475. __func__);
  1476. return retval;
  1477. }
  1478. retval = fwu_write_lockdown_block();
  1479. if (retval < 0)
  1480. return retval;
  1481. pr_notice("%s: Lockdown programmed\n", __func__);
  1482. return retval;
  1483. }
  1484. */
  1485. int synaptics_fw_updater(unsigned char *fw_data)
  1486. {
  1487. struct synaptics_rmi4_data *rmi4_data;
  1488. int retval;
  1489. if (!fwu)
  1490. return -ENODEV;
  1491. if (!fwu->initialized)
  1492. return -ENODEV;
  1493. if (!fw_data) {
  1494. dev_err(&fwu->rmi4_data->i2c_client->dev,
  1495. "%s: Firmware data is NULL\n", __func__);
  1496. return -ENODEV;
  1497. }
  1498. rmi4_data = fwu->rmi4_data;
  1499. #if defined(CONFIG_SEC_F_PROJECT)
  1500. rmi4_data->firmware_cracked = false;
  1501. #endif
  1502. fwu->ext_data_source = fw_data;
  1503. fwu->config_area = UI_CONFIG_AREA;
  1504. retval = fwu_start_reflash();
  1505. return retval;
  1506. }
  1507. EXPORT_SYMBOL(synaptics_fw_updater);
  1508. #if 0
  1509. static void fwu_startup_fw_update_work(struct work_struct *work)
  1510. {
  1511. synaptics_fw_updater(NULL);
  1512. return;
  1513. }
  1514. #endif
  1515. static ssize_t fwu_sysfs_show_image(struct file *data_file,
  1516. struct kobject *kobj, struct bin_attribute *attributes,
  1517. char *buf, loff_t pos, size_t count)
  1518. {
  1519. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1520. if (count < fwu->config_size) {
  1521. dev_err(&rmi4_data->i2c_client->dev,
  1522. "%s: Not enough space (%d bytes) in buffer\n",
  1523. __func__, count);
  1524. return -EINVAL;
  1525. }
  1526. memcpy(buf, fwu->read_config_buf, fwu->config_size);
  1527. return fwu->config_size;
  1528. }
  1529. static ssize_t fwu_sysfs_store_image(struct file *data_file,
  1530. struct kobject *kobj, struct bin_attribute *attributes,
  1531. char *buf, loff_t pos, size_t count)
  1532. {
  1533. memcpy((void *)(&fwu->ext_data_source[fwu->data_pos]),
  1534. (const void *)buf,
  1535. count);
  1536. fwu->data_pos += count;
  1537. fwu->img.fw_image = fwu->ext_data_source;
  1538. return count;
  1539. }
  1540. static ssize_t fwu_sysfs_do_reflash_store(struct device *dev,
  1541. struct device_attribute *attr, const char *buf, size_t count)
  1542. {
  1543. int retval;
  1544. unsigned int input;
  1545. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1546. if (sscanf(buf, "%u", &input) != 1) {
  1547. retval = -EINVAL;
  1548. goto exit;
  1549. }
  1550. if (input & LOCKDOWN) {
  1551. fwu->do_lockdown = true;
  1552. input &= ~LOCKDOWN;
  1553. }
  1554. if ((input != NORMAL) && (input != FORCE)) {
  1555. retval = -EINVAL;
  1556. goto exit;
  1557. }
  1558. if (input == FORCE)
  1559. fwu->force_update = true;
  1560. retval = synaptics_fw_updater(fwu->ext_data_source);
  1561. if (retval < 0) {
  1562. dev_err(&rmi4_data->i2c_client->dev,
  1563. "%s: Failed to do reflash\n",
  1564. __func__);
  1565. goto exit;
  1566. }
  1567. retval = count;
  1568. exit:
  1569. kfree(fwu->ext_data_source);
  1570. fwu->ext_data_source = NULL;
  1571. fwu->force_update = FORCE_UPDATE;
  1572. fwu->do_lockdown = DO_LOCKDOWN;
  1573. return retval;
  1574. }
  1575. static ssize_t fwu_sysfs_write_config_store(struct device *dev,
  1576. struct device_attribute *attr, const char *buf, size_t count)
  1577. {
  1578. int retval;
  1579. unsigned int input;
  1580. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1581. if (sscanf(buf, "%u", &input) != 1) {
  1582. retval = -EINVAL;
  1583. goto exit;
  1584. }
  1585. if (input != 1) {
  1586. retval = -EINVAL;
  1587. goto exit;
  1588. }
  1589. retval = fwu_start_write_config();
  1590. if (retval < 0) {
  1591. dev_err(&rmi4_data->i2c_client->dev,
  1592. "%s: Failed to write config\n",
  1593. __func__);
  1594. goto exit;
  1595. }
  1596. retval = count;
  1597. exit:
  1598. kfree(fwu->ext_data_source);
  1599. fwu->ext_data_source = NULL;
  1600. return retval;
  1601. }
  1602. static ssize_t fwu_sysfs_read_config_store(struct device *dev,
  1603. struct device_attribute *attr, const char *buf, size_t count)
  1604. {
  1605. int retval;
  1606. unsigned int input;
  1607. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1608. if (sscanf(buf, "%u", &input) != 1)
  1609. return -EINVAL;
  1610. if (input != 1)
  1611. return -EINVAL;
  1612. retval = fwu_do_read_config();
  1613. if (retval < 0) {
  1614. dev_err(&rmi4_data->i2c_client->dev,
  1615. "%s: Failed to read config\n",
  1616. __func__);
  1617. return retval;
  1618. }
  1619. return count;
  1620. }
  1621. static ssize_t fwu_sysfs_config_area_store(struct device *dev,
  1622. struct device_attribute *attr, const char *buf, size_t count)
  1623. {
  1624. int retval;
  1625. unsigned long config_area;
  1626. retval = kstrtoul(buf, 10, &config_area);
  1627. if (retval)
  1628. return retval;
  1629. fwu->config_area = config_area;
  1630. return count;
  1631. }
  1632. /*
  1633. static ssize_t fwu_sysfs_image_name_store(struct device *dev,
  1634. struct device_attribute *attr, const char *buf, size_t count)
  1635. {
  1636. memcpy(fwu->img.image_name, buf, count);
  1637. return count;
  1638. }
  1639. */
  1640. static ssize_t fwu_sysfs_image_size_store(struct device *dev,
  1641. struct device_attribute *attr, const char *buf, size_t count)
  1642. {
  1643. int retval;
  1644. unsigned long size;
  1645. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1646. retval = kstrtoul(buf, 10, &size);
  1647. if (retval)
  1648. return retval;
  1649. fwu->img.image_size = size;
  1650. fwu->data_pos = 0;
  1651. kfree(fwu->ext_data_source);
  1652. fwu->ext_data_source = kzalloc(fwu->img.image_size, GFP_KERNEL);
  1653. if (!fwu->ext_data_source) {
  1654. dev_err(&rmi4_data->i2c_client->dev,
  1655. "%s: Failed to alloc mem for image data\n",
  1656. __func__);
  1657. return -ENOMEM;
  1658. }
  1659. return count;
  1660. }
  1661. static ssize_t fwu_sysfs_block_size_show(struct device *dev,
  1662. struct device_attribute *attr, char *buf)
  1663. {
  1664. return snprintf(buf, PAGE_SIZE, "%u\n", fwu->block_size);
  1665. }
  1666. static ssize_t fwu_sysfs_firmware_block_count_show(struct device *dev,
  1667. struct device_attribute *attr, char *buf)
  1668. {
  1669. return snprintf(buf, PAGE_SIZE, "%u\n", fwu->fw_block_count);
  1670. }
  1671. static ssize_t fwu_sysfs_configuration_block_count_show(struct device *dev,
  1672. struct device_attribute *attr, char *buf)
  1673. {
  1674. return snprintf(buf, PAGE_SIZE, "%u\n", fwu->config_block_count);
  1675. }
  1676. static ssize_t fwu_sysfs_perm_config_block_count_show(struct device *dev,
  1677. struct device_attribute *attr, char *buf)
  1678. {
  1679. return snprintf(buf, PAGE_SIZE, "%u\n", fwu->perm_config_block_count);
  1680. }
  1681. static ssize_t fwu_sysfs_bl_config_block_count_show(struct device *dev,
  1682. struct device_attribute *attr, char *buf)
  1683. {
  1684. return snprintf(buf, PAGE_SIZE, "%u\n", fwu->bl_config_block_count);
  1685. }
  1686. static ssize_t fwu_sysfs_disp_config_block_count_show(struct device *dev,
  1687. struct device_attribute *attr, char *buf)
  1688. {
  1689. return snprintf(buf, PAGE_SIZE, "%u\n", fwu->disp_config_block_count);
  1690. }
  1691. static ssize_t fwu_sysfs_guest_code_block_count_show(struct device *dev,
  1692. struct device_attribute *attr, char *buf)
  1693. {
  1694. return snprintf(buf, PAGE_SIZE, "%u\n", fwu->guest_code_block_count);
  1695. }
  1696. static ssize_t fwu_sysfs_write_guest_code_store(struct device *dev,
  1697. struct device_attribute *attr, const char *buf, size_t count)
  1698. {
  1699. int retval;
  1700. unsigned int input;
  1701. struct synaptics_rmi4_data *rmi4_data = fwu->rmi4_data;
  1702. if (sscanf(buf, "%u", &input) != 1) {
  1703. retval = -EINVAL;
  1704. goto exit;
  1705. }
  1706. if (input != 1) {
  1707. retval = -EINVAL;
  1708. goto exit;
  1709. }
  1710. retval = fwu_start_write_guest_code();
  1711. if (retval < 0) {
  1712. dev_err(&rmi4_data->i2c_client->dev,
  1713. "%s: Failed to write guest code\n",
  1714. __func__);
  1715. goto exit;
  1716. }
  1717. retval = count;
  1718. exit:
  1719. kfree(fwu->ext_data_source);
  1720. fwu->ext_data_source = NULL;
  1721. return retval;
  1722. }
  1723. static void synaptics_rmi4_fwu_attn(struct synaptics_rmi4_data *rmi4_data,
  1724. unsigned char intr_mask)
  1725. {
  1726. if (!fwu)
  1727. return;
  1728. if (fwu->intr_mask & intr_mask)
  1729. fwu_read_f34_flash_status();
  1730. return;
  1731. }
  1732. void fwu_img_scan_x10_container(unsigned int listLength, unsigned char *startAddr)
  1733. {
  1734. unsigned int i;
  1735. unsigned int length;
  1736. unsigned int contentAddr, addr;
  1737. unsigned short containerId;
  1738. struct block_data block;
  1739. struct img_x10_descriptor * containerDescriptor;
  1740. struct img_x10_bl_container * blContainer;
  1741. for (i = 0; i < listLength; i += 4) {
  1742. contentAddr = extract_uint_le(startAddr + i);
  1743. containerDescriptor =
  1744. (struct img_x10_descriptor *)(fwu->img.fw_image + contentAddr);
  1745. containerId = extract_ushort_le(containerDescriptor->containerID);
  1746. addr = extract_uint_le(containerDescriptor->contentAddress);
  1747. length = extract_uint_le(containerDescriptor->contentLength);
  1748. block.data = fwu->img.fw_image + addr;
  1749. block.size = length;
  1750. switch (containerId) {
  1751. case ID_UI_CONTAINER:
  1752. fwu->img.uiFirmware = block;
  1753. break;
  1754. case ID_UI_CONFIGURATION:
  1755. fwu->img.uiConfig = block;
  1756. fwu->img.configId = fwu->img.uiConfig.data;
  1757. break;
  1758. case ID_BOOTLOADER_LOCKDOWN_INFORMATION_CONTAINER:
  1759. fwu->img.lockdown = block;
  1760. break;
  1761. case ID_GUEST_CODE_CONTAINER:
  1762. fwu->img.guestCode = block;
  1763. break;
  1764. case ID_BOOTLOADER_CONTAINER:
  1765. blContainer =
  1766. (struct img_x10_bl_container *)(fwu->img.fw_image + addr);
  1767. fwu->img.blMajorVersion = blContainer->majorVersion;
  1768. fwu->img.blMinorVersion = blContainer->minorVersion;
  1769. fwu->img.bootloaderInfo = block;
  1770. break;
  1771. case ID_PERMANENT_CONFIGURATION_CONTAINER:
  1772. fwu->img.permanent = block;
  1773. break;
  1774. case ID_GENERAL_INFORMATION_CONTAINER:
  1775. fwu->img.packageId = fwu->img.fw_image + addr + 0;
  1776. fwu->img.firmwareId = fwu->img.fw_image + addr + 4;
  1777. fwu->img.dsFirmwareInfo = fwu->img.fw_image + addr + 8;
  1778. break;
  1779. default:
  1780. break;
  1781. }
  1782. }
  1783. }
  1784. void fwu_img_parse_x10_topcontainer(void)
  1785. {
  1786. struct img_x10_descriptor * descriptor;
  1787. unsigned int topAddr;
  1788. unsigned int listLength, blLength;
  1789. unsigned char *startAddr;
  1790. topAddr = extract_uint_le(fwu->img.fw_image +
  1791. IMG_X10_TOP_CONTAINER_OFFSET);
  1792. descriptor = (struct img_x10_descriptor *)
  1793. (fwu->img.fw_image + topAddr);
  1794. listLength = extract_uint_le(descriptor->contentLength);
  1795. startAddr = fwu->img.fw_image +
  1796. extract_uint_le(descriptor->contentAddress);
  1797. fwu_img_scan_x10_container(listLength, startAddr);
  1798. /* scan sub bootloader container (lockdown container) */
  1799. if (fwu->img.bootloaderInfo.data != NULL) {
  1800. blLength = fwu->img.bootloaderInfo.size - 4;
  1801. if (blLength)
  1802. fwu_img_scan_x10_container(blLength,
  1803. fwu->img.bootloaderInfo.data);
  1804. }
  1805. }
  1806. void fwu_img_parse_x10(void)
  1807. {
  1808. fwu_img_parse_x10_topcontainer();
  1809. }
  1810. void fwu_img_parse_x0_x6(void)
  1811. {
  1812. struct img_x0x6_header * header = (struct img_x0x6_header *)fwu->img.fw_image;
  1813. if (header->bootloader_version > 6)
  1814. return;
  1815. fwu->img.blMajorVersion = header->bootloader_version;
  1816. fwu->img.uiFirmware.size = extract_uint_le(header->firmware_size);
  1817. fwu->img.uiFirmware.data = fwu->img.fw_image + IMG_X0_X6_FW_OFFSET;
  1818. fwu->img.uiConfig.size = extract_uint_le(header->config_size);
  1819. fwu->img.uiConfig.data = fwu->img.uiFirmware.data + fwu->img.uiFirmware.size;
  1820. fwu->img.configId = fwu->img.uiConfig.data;
  1821. switch (fwu->img.imageFileVersion)
  1822. {
  1823. case 0x2:
  1824. fwu->img.lockdown.size = 0x30;
  1825. break;
  1826. case 0x3:
  1827. case 0x4:
  1828. fwu->img.lockdown.size = 0x40;
  1829. break;
  1830. case 0x5:
  1831. case 0x6:
  1832. fwu->img.lockdown.size = 0x50;
  1833. if (header->options_firmware_id)
  1834. {
  1835. fwu->img.firmwareId = header->firmware_id;
  1836. fwu->img.packageId = header->package_id;
  1837. fwu->img.dsFirmwareInfo = header->ds_firmware_info;
  1838. }
  1839. break;
  1840. default:
  1841. break;
  1842. }
  1843. fwu->img.lockdown.data = fwu->img.fw_image +
  1844. IMG_X0_X6_FW_OFFSET - fwu->img.lockdown.size;
  1845. }
  1846. void fwu_img_parse_format(void)
  1847. {
  1848. fwu->polling_mode = POLLING_MODE_DEFAULT;
  1849. fwu->img.firmwareId = NULL;
  1850. fwu->img.packageId = NULL;
  1851. fwu->img.dsFirmwareInfo = NULL;
  1852. fwu->img.uiFirmware.data = NULL;
  1853. fwu->img.uiConfig.data = NULL;
  1854. fwu->img.lockdown.data = NULL;
  1855. fwu->img.guestCode.data = NULL;
  1856. fwu->img.uiConfig.size = 0;
  1857. fwu->img.uiFirmware.size = 0;
  1858. fwu->img.lockdown.size = 0;
  1859. fwu->img.guestCode.size = 0;
  1860. fwu->img.imageFileVersion = fwu->img.fw_image[IMG_VERSION_OFFSET];
  1861. switch (fwu->img.imageFileVersion) {
  1862. case 0x10:
  1863. fwu_img_parse_x10();
  1864. break;
  1865. case 0x5:
  1866. case 0x6:
  1867. fwu_img_parse_x0_x6();
  1868. break;
  1869. default:
  1870. dev_err(&fwu->rmi4_data->i2c_client->dev,
  1871. "%s: Unsupported image file format $%X\n",
  1872. __func__, fwu->img.imageFileVersion);
  1873. break;
  1874. }
  1875. }
  1876. static int synaptics_rmi4_fwu_init(struct synaptics_rmi4_data *rmi4_data)
  1877. {
  1878. int retval;
  1879. unsigned char attr_count;
  1880. int attr_count_num;
  1881. struct pdt_properties pdt_props;
  1882. fwu = kzalloc(sizeof(*fwu), GFP_KERNEL);
  1883. if (!fwu) {
  1884. dev_err(&rmi4_data->i2c_client->dev,
  1885. "%s: Failed to alloc mem for fwu\n",
  1886. __func__);
  1887. retval = -ENOMEM;
  1888. goto exit;
  1889. }
  1890. fwu->fn_ptr = kzalloc(sizeof(*(fwu->fn_ptr)), GFP_KERNEL);
  1891. if (!fwu->fn_ptr) {
  1892. dev_err(&rmi4_data->i2c_client->dev,
  1893. "%s: Failed to alloc mem for fn_ptr\n",
  1894. __func__);
  1895. retval = -ENOMEM;
  1896. goto exit_free_fwu;
  1897. }
  1898. memset(&fwu->img, 0, sizeof(fwu->img));
  1899. fwu->img.image_name = kzalloc(MAX_IMAGE_NAME_LEN, GFP_KERNEL);
  1900. if (!fwu->img.image_name) {
  1901. dev_err(&rmi4_data->i2c_client->dev,
  1902. "%s: Failed to alloc mem for image name\n",
  1903. __func__);
  1904. retval = -ENOMEM;
  1905. goto exit_free_fn_ptr;
  1906. }
  1907. fwu->rmi4_data = rmi4_data;
  1908. fwu->fn_ptr->read = rmi4_data->i2c_read;
  1909. fwu->fn_ptr->write = rmi4_data->i2c_write;
  1910. fwu->fn_ptr->enable = rmi4_data->irq_enable;
  1911. retval = fwu->fn_ptr->read(rmi4_data,
  1912. PDT_PROPS,
  1913. pdt_props.data,
  1914. sizeof(pdt_props.data));
  1915. if (retval < 0) {
  1916. dev_info(&rmi4_data->i2c_client->dev,
  1917. "%s: Failed to read PDT properties, assuming 0x00\n",
  1918. __func__);
  1919. } else if (pdt_props.has_bsr) {
  1920. dev_err(&rmi4_data->i2c_client->dev,
  1921. "%s: Reflash for LTS not currently supported\n",
  1922. __func__);
  1923. retval = -ENODEV;
  1924. goto exit_free_mem;
  1925. }
  1926. retval = fwu_scan_pdt();
  1927. if (retval < 0)
  1928. goto exit_free_mem;
  1929. fwu->productinfo1 = rmi4_data->rmi4_mod_info.product_info[0];
  1930. fwu->productinfo2 = rmi4_data->rmi4_mod_info.product_info[1];
  1931. memcpy(fwu->product_id, rmi4_data->rmi4_mod_info.product_id_string,
  1932. SYNAPTICS_RMI4_PRODUCT_ID_SIZE);
  1933. fwu->product_id[SYNAPTICS_RMI4_PRODUCT_ID_SIZE] = 0;
  1934. /* Check the IC revision from product ID value
  1935. * we can check the ic revision with f34_ctrl_3 but to read production
  1936. * ID is more safity. because it is non-user writerble area.
  1937. */
  1938. dev_info(&rmi4_data->i2c_client->dev,
  1939. "%s: [IC] [F01 product info, ID] [0x%04X 0x%04X, %s]\n",
  1940. __func__, fwu->productinfo1,
  1941. fwu->productinfo2, fwu->product_id);
  1942. retval = fwu_read_f34_queries();
  1943. if (retval < 0)
  1944. goto exit_free_mem;
  1945. fwu->force_update = FORCE_UPDATE;
  1946. fwu->do_lockdown = DO_LOCKDOWN;
  1947. fwu->initialized = true;
  1948. retval = sysfs_create_bin_file(&rmi4_data->input_dev->dev.kobj,
  1949. &dev_attr_data);
  1950. if (retval < 0) {
  1951. dev_err(&rmi4_data->i2c_client->dev,
  1952. "%s: Failed to create sysfs bin file\n",
  1953. __func__);
  1954. goto exit_free_mem;
  1955. }
  1956. for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
  1957. retval = sysfs_create_file(&rmi4_data->input_dev->dev.kobj,
  1958. &attrs[attr_count].attr);
  1959. if (retval < 0) {
  1960. dev_err(&rmi4_data->i2c_client->dev,
  1961. "%s: Failed to create sysfs attributes\n",
  1962. __func__);
  1963. retval = -ENODEV;
  1964. goto exit_remove_attrs;
  1965. }
  1966. }
  1967. return 0;
  1968. exit_remove_attrs:
  1969. attr_count_num = (int)attr_count;
  1970. for (attr_count_num--; attr_count_num >= 0; attr_count_num--) {
  1971. sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
  1972. &attrs[attr_count_num].attr);
  1973. }
  1974. sysfs_remove_bin_file(&rmi4_data->input_dev->dev.kobj, &dev_attr_data);
  1975. exit_free_mem:
  1976. kfree(fwu->img.image_name);
  1977. exit_free_fn_ptr:
  1978. kfree(fwu->fn_ptr);
  1979. exit_free_fwu:
  1980. kfree(fwu);
  1981. fwu = NULL;
  1982. exit:
  1983. return retval;
  1984. }
  1985. static void synaptics_rmi4_fwu_remove(struct synaptics_rmi4_data *rmi4_data)
  1986. {
  1987. unsigned char attr_count;
  1988. if (!fwu)
  1989. goto exit;
  1990. for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
  1991. sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
  1992. &attrs[attr_count].attr);
  1993. }
  1994. sysfs_remove_bin_file(&rmi4_data->input_dev->dev.kobj, &dev_attr_data);
  1995. kfree(fwu->read_config_buf);
  1996. kfree(fwu->img.image_name);
  1997. kfree(fwu);
  1998. fwu = NULL;
  1999. exit:
  2000. return;
  2001. }
  2002. int rmi4_fw_update_module_register(void)
  2003. {
  2004. int retval;
  2005. retval = synaptics_rmi4_new_function(RMI_FW_UPDATER,
  2006. synaptics_rmi4_fwu_init,
  2007. synaptics_rmi4_fwu_remove,
  2008. synaptics_rmi4_fwu_attn);
  2009. return retval;
  2010. }