synaptics_dsx_gesture.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291
  1. /*
  2. * Synaptics DSX touchscreen driver
  3. *
  4. * Copyright (C) 2012-2016 Synaptics Incorporated. All rights reserved.
  5. *
  6. * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
  7. * Copyright (C) 2012 Alexandra Chin <[email protected]>
  8. * Copyright (C) 2012 Scott Lin <[email protected]>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * INFORMATION CONTAINED IN THIS DOCUMENT IS PROVIDED "AS-IS," AND SYNAPTICS
  21. * EXPRESSLY DISCLAIMS ALL EXPRESS AND IMPLIED WARRANTIES, INCLUDING ANY
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
  23. * AND ANY WARRANTIES OF NON-INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS.
  24. * IN NO EVENT SHALL SYNAPTICS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, PUNITIVE, OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR IN CONNECTION
  26. * WITH THE USE OF THE INFORMATION CONTAINED IN THIS DOCUMENT, HOWEVER CAUSED
  27. * AND BASED ON ANY THEORY OF LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  28. * NEGLIGENCE OR OTHER TORTIOUS ACTION, AND EVEN IF SYNAPTICS WAS ADVISED OF
  29. * THE POSSIBILITY OF SUCH DAMAGE. IF A TRIBUNAL OF COMPETENT JURISDICTION DOES
  30. * NOT PERMIT THE DISCLAIMER OF DIRECT DAMAGES OR ANY OTHER DAMAGES, SYNAPTICS'
  31. * TOTAL CUMULATIVE LIABILITY TO ANY PARTY SHALL NOT EXCEED ONE HUNDRED U.S.
  32. * DOLLARS.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/slab.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/delay.h>
  39. #include <linux/input.h>
  40. #include <linux/platform_device.h>
  41. #include <linux/input/synaptics_dsx.h>
  42. #include "synaptics_dsx_core.h"
  43. #define GESTURE_PHYS_NAME "synaptics_dsx/gesture"
  44. #define TUNING_SYSFS_DIR_NAME "tuning"
  45. #define STORE_GESTURES
  46. #ifdef STORE_GESTURES
  47. #define GESTURES_TO_STORE 10
  48. #endif
  49. #define CTRL23_FINGER_REPORT_ENABLE_BIT 0
  50. #define CTRL27_UDG_ENABLE_BIT 4
  51. #define WAKEUP_GESTURE_MODE 0x02
  52. static ssize_t udg_sysfs_engine_enable_store(struct device *dev,
  53. struct device_attribute *attr, const char *buf, size_t count);
  54. static ssize_t udg_sysfs_detection_enable_store(struct device *dev,
  55. struct device_attribute *attr, const char *buf, size_t count);
  56. static ssize_t udg_sysfs_detection_score_show(struct device *dev,
  57. struct device_attribute *attr, char *buf);
  58. static ssize_t udg_sysfs_detection_index_show(struct device *dev,
  59. struct device_attribute *attr, char *buf);
  60. static ssize_t udg_sysfs_registration_enable_store(struct device *dev,
  61. struct device_attribute *attr, const char *buf, size_t count);
  62. static ssize_t udg_sysfs_registration_begin_store(struct device *dev,
  63. struct device_attribute *attr, const char *buf, size_t count);
  64. static ssize_t udg_sysfs_registration_status_show(struct device *dev,
  65. struct device_attribute *attr, char *buf);
  66. static ssize_t udg_sysfs_template_size_show(struct device *dev,
  67. struct device_attribute *attr, char *buf);
  68. static ssize_t udg_sysfs_template_max_index_show(struct device *dev,
  69. struct device_attribute *attr, char *buf);
  70. static ssize_t udg_sysfs_template_detection_show(struct device *dev,
  71. struct device_attribute *attr, char *buf);
  72. static ssize_t udg_sysfs_template_index_store(struct device *dev,
  73. struct device_attribute *attr, const char *buf, size_t count);
  74. static ssize_t udg_sysfs_template_valid_show(struct device *dev,
  75. struct device_attribute *attr, char *buf);
  76. static ssize_t udg_sysfs_template_valid_store(struct device *dev,
  77. struct device_attribute *attr, const char *buf, size_t count);
  78. static ssize_t udg_sysfs_template_clear_store(struct device *dev,
  79. struct device_attribute *attr, const char *buf, size_t count);
  80. static ssize_t udg_sysfs_trace_size_show(struct device *dev,
  81. struct device_attribute *attr, char *buf);
  82. static ssize_t udg_sysfs_template_data_show(struct file *data_file,
  83. struct kobject *kobj, struct bin_attribute *attributes,
  84. char *buf, loff_t pos, size_t count);
  85. static ssize_t udg_sysfs_template_data_store(struct file *data_file,
  86. struct kobject *kobj, struct bin_attribute *attributes,
  87. char *buf, loff_t pos, size_t count);
  88. static ssize_t udg_sysfs_trace_data_show(struct file *data_file,
  89. struct kobject *kobj, struct bin_attribute *attributes,
  90. char *buf, loff_t pos, size_t count);
  91. static ssize_t udg_sysfs_template_displacement_show(struct device *dev,
  92. struct device_attribute *attr, char *buf);
  93. static ssize_t udg_sysfs_template_displacement_store(struct device *dev,
  94. struct device_attribute *attr, const char *buf, size_t count);
  95. static ssize_t udg_sysfs_rotation_invariance_show(struct device *dev,
  96. struct device_attribute *attr, char *buf);
  97. static ssize_t udg_sysfs_rotation_invariance_store(struct device *dev,
  98. struct device_attribute *attr, const char *buf, size_t count);
  99. static ssize_t udg_sysfs_scale_invariance_show(struct device *dev,
  100. struct device_attribute *attr, char *buf);
  101. static ssize_t udg_sysfs_scale_invariance_store(struct device *dev,
  102. struct device_attribute *attr, const char *buf, size_t count);
  103. static ssize_t udg_sysfs_threshold_factor_show(struct device *dev,
  104. struct device_attribute *attr, char *buf);
  105. static ssize_t udg_sysfs_threshold_factor_store(struct device *dev,
  106. struct device_attribute *attr, const char *buf, size_t count);
  107. static ssize_t udg_sysfs_match_metric_threshold_show(struct device *dev,
  108. struct device_attribute *attr, char *buf);
  109. static ssize_t udg_sysfs_match_metric_threshold_store(struct device *dev,
  110. struct device_attribute *attr, const char *buf, size_t count);
  111. static ssize_t udg_sysfs_max_inter_stroke_time_show(struct device *dev,
  112. struct device_attribute *attr, char *buf);
  113. static ssize_t udg_sysfs_max_inter_stroke_time_store(struct device *dev,
  114. struct device_attribute *attr, const char *buf, size_t count);
  115. static int udg_read_tuning_params(void);
  116. static int udg_write_tuning_params(void);
  117. static int udg_detection_enable(bool enable);
  118. static int udg_engine_enable(bool enable);
  119. static int udg_set_index(unsigned char index);
  120. #ifdef STORE_GESTURES
  121. static int udg_read_valid_data(void);
  122. static int udg_write_valid_data(void);
  123. static int udg_read_template_data(unsigned char index);
  124. static int udg_write_template_data(void);
  125. #endif
  126. enum gesture_type {
  127. DETECTION = 0x0f,
  128. REGISTRATION = 0x10,
  129. };
  130. struct udg_tuning {
  131. union {
  132. struct {
  133. unsigned char maximum_number_of_templates;
  134. unsigned char template_size;
  135. unsigned char template_disp_lsb;
  136. unsigned char template_disp_msb;
  137. unsigned char rotation_inv_lsb;
  138. unsigned char rotation_inv_msb;
  139. unsigned char scale_inv_lsb;
  140. unsigned char scale_inv_msb;
  141. unsigned char thres_factor_lsb;
  142. unsigned char thres_factor_msb;
  143. unsigned char metric_thres_lsb;
  144. unsigned char metric_thres_msb;
  145. unsigned char inter_stroke_lsb;
  146. unsigned char inter_stroke_msb;
  147. } __packed;
  148. unsigned char data[14];
  149. };
  150. };
  151. struct udg_addr {
  152. unsigned short data_4;
  153. unsigned short ctrl_18;
  154. unsigned short ctrl_20;
  155. unsigned short ctrl_23;
  156. unsigned short ctrl_27;
  157. unsigned short ctrl_41;
  158. unsigned short trace_x;
  159. unsigned short trace_y;
  160. unsigned short trace_segment;
  161. unsigned short template_helper;
  162. unsigned short template_data;
  163. unsigned short template_flags;
  164. };
  165. struct synaptics_rmi4_f12_query_0 {
  166. union {
  167. struct {
  168. struct {
  169. unsigned char has_register_descriptors:1;
  170. unsigned char has_closed_cover:1;
  171. unsigned char has_fast_glove_detect:1;
  172. unsigned char has_dribble:1;
  173. unsigned char has_4p4_jitter_filter_strength:1;
  174. unsigned char f12_query0_s0_b5__7:3;
  175. } __packed;
  176. struct {
  177. unsigned char max_num_templates:4;
  178. unsigned char f12_query0_s1_b4__7:4;
  179. unsigned char template_size_lsb;
  180. unsigned char template_size_msb;
  181. } __packed;
  182. };
  183. unsigned char data[4];
  184. };
  185. };
  186. struct synaptics_rmi4_f12_query_5 {
  187. union {
  188. struct {
  189. unsigned char size_of_query6;
  190. struct {
  191. unsigned char ctrl0_is_present:1;
  192. unsigned char ctrl1_is_present:1;
  193. unsigned char ctrl2_is_present:1;
  194. unsigned char ctrl3_is_present:1;
  195. unsigned char ctrl4_is_present:1;
  196. unsigned char ctrl5_is_present:1;
  197. unsigned char ctrl6_is_present:1;
  198. unsigned char ctrl7_is_present:1;
  199. } __packed;
  200. struct {
  201. unsigned char ctrl8_is_present:1;
  202. unsigned char ctrl9_is_present:1;
  203. unsigned char ctrl10_is_present:1;
  204. unsigned char ctrl11_is_present:1;
  205. unsigned char ctrl12_is_present:1;
  206. unsigned char ctrl13_is_present:1;
  207. unsigned char ctrl14_is_present:1;
  208. unsigned char ctrl15_is_present:1;
  209. } __packed;
  210. struct {
  211. unsigned char ctrl16_is_present:1;
  212. unsigned char ctrl17_is_present:1;
  213. unsigned char ctrl18_is_present:1;
  214. unsigned char ctrl19_is_present:1;
  215. unsigned char ctrl20_is_present:1;
  216. unsigned char ctrl21_is_present:1;
  217. unsigned char ctrl22_is_present:1;
  218. unsigned char ctrl23_is_present:1;
  219. } __packed;
  220. struct {
  221. unsigned char ctrl24_is_present:1;
  222. unsigned char ctrl25_is_present:1;
  223. unsigned char ctrl26_is_present:1;
  224. unsigned char ctrl27_is_present:1;
  225. unsigned char ctrl28_is_present:1;
  226. unsigned char ctrl29_is_present:1;
  227. unsigned char ctrl30_is_present:1;
  228. unsigned char ctrl31_is_present:1;
  229. } __packed;
  230. struct {
  231. unsigned char ctrl32_is_present:1;
  232. unsigned char ctrl33_is_present:1;
  233. unsigned char ctrl34_is_present:1;
  234. unsigned char ctrl35_is_present:1;
  235. unsigned char ctrl36_is_present:1;
  236. unsigned char ctrl37_is_present:1;
  237. unsigned char ctrl38_is_present:1;
  238. unsigned char ctrl39_is_present:1;
  239. } __packed;
  240. struct {
  241. unsigned char ctrl40_is_present:1;
  242. unsigned char ctrl41_is_present:1;
  243. unsigned char ctrl42_is_present:1;
  244. unsigned char ctrl43_is_present:1;
  245. unsigned char ctrl44_is_present:1;
  246. unsigned char ctrl45_is_present:1;
  247. unsigned char ctrl46_is_present:1;
  248. unsigned char ctrl47_is_present:1;
  249. } __packed;
  250. };
  251. unsigned char data[7];
  252. };
  253. };
  254. struct synaptics_rmi4_f12_query_8 {
  255. union {
  256. struct {
  257. unsigned char size_of_query9;
  258. struct {
  259. unsigned char data0_is_present:1;
  260. unsigned char data1_is_present:1;
  261. unsigned char data2_is_present:1;
  262. unsigned char data3_is_present:1;
  263. unsigned char data4_is_present:1;
  264. unsigned char data5_is_present:1;
  265. unsigned char data6_is_present:1;
  266. unsigned char data7_is_present:1;
  267. } __packed;
  268. struct {
  269. unsigned char data8_is_present:1;
  270. unsigned char data9_is_present:1;
  271. unsigned char data10_is_present:1;
  272. unsigned char data11_is_present:1;
  273. unsigned char data12_is_present:1;
  274. unsigned char data13_is_present:1;
  275. unsigned char data14_is_present:1;
  276. unsigned char data15_is_present:1;
  277. } __packed;
  278. struct {
  279. unsigned char data16_is_present:1;
  280. unsigned char data17_is_present:1;
  281. unsigned char data18_is_present:1;
  282. unsigned char data19_is_present:1;
  283. unsigned char data20_is_present:1;
  284. unsigned char data21_is_present:1;
  285. unsigned char data22_is_present:1;
  286. unsigned char data23_is_present:1;
  287. } __packed;
  288. };
  289. unsigned char data[4];
  290. };
  291. };
  292. struct synaptics_rmi4_f12_control_41 {
  293. union {
  294. struct {
  295. unsigned char enable_registration:1;
  296. unsigned char template_index:4;
  297. unsigned char begin:1;
  298. unsigned char f12_ctrl41_b6__7:2;
  299. } __packed;
  300. unsigned char data[1];
  301. };
  302. };
  303. struct synaptics_rmi4_udg_handle {
  304. atomic_t attn_event;
  305. unsigned char intr_mask;
  306. unsigned char report_flags;
  307. unsigned char object_type_enable1;
  308. unsigned char object_type_enable2;
  309. unsigned char trace_size;
  310. unsigned char template_index;
  311. unsigned char max_num_templates;
  312. unsigned char detection_score;
  313. unsigned char detection_index;
  314. unsigned char detection_status;
  315. unsigned char registration_status;
  316. unsigned char *ctrl_buf;
  317. unsigned char *trace_data_buf;
  318. unsigned char *template_data_buf;
  319. #ifdef STORE_GESTURES
  320. unsigned char gestures_to_store;
  321. unsigned char *storage_buf;
  322. unsigned char valid_buf[2];
  323. #endif
  324. unsigned short trace_data_buf_size;
  325. unsigned short template_size;
  326. unsigned short template_data_size;
  327. unsigned short query_base_addr;
  328. unsigned short control_base_addr;
  329. unsigned short data_base_addr;
  330. unsigned short command_base_addr;
  331. unsigned short ctrl_18_sub10_off;
  332. unsigned short ctrl_20_sub1_off;
  333. unsigned short ctrl_23_sub3_off;
  334. unsigned short ctrl_27_sub5_off;
  335. struct input_dev *udg_dev;
  336. struct kobject *tuning_dir;
  337. struct udg_addr addr;
  338. struct udg_tuning tuning;
  339. struct synaptics_rmi4_data *rmi4_data;
  340. };
  341. static struct device_attribute attrs[] = {
  342. __ATTR(engine_enable, 0220,
  343. synaptics_rmi4_show_error,
  344. udg_sysfs_engine_enable_store),
  345. __ATTR(detection_enable, 0220,
  346. synaptics_rmi4_show_error,
  347. udg_sysfs_detection_enable_store),
  348. __ATTR(detection_score, 0444,
  349. udg_sysfs_detection_score_show,
  350. synaptics_rmi4_store_error),
  351. __ATTR(detection_index, 0444,
  352. udg_sysfs_detection_index_show,
  353. synaptics_rmi4_store_error),
  354. __ATTR(registration_enable, 0220,
  355. synaptics_rmi4_show_error,
  356. udg_sysfs_registration_enable_store),
  357. __ATTR(registration_begin, 0220,
  358. synaptics_rmi4_show_error,
  359. udg_sysfs_registration_begin_store),
  360. __ATTR(registration_status, 0444,
  361. udg_sysfs_registration_status_show,
  362. synaptics_rmi4_store_error),
  363. __ATTR(template_size, 0444,
  364. udg_sysfs_template_size_show,
  365. synaptics_rmi4_store_error),
  366. __ATTR(template_max_index, 0444,
  367. udg_sysfs_template_max_index_show,
  368. synaptics_rmi4_store_error),
  369. __ATTR(template_detection, 0444,
  370. udg_sysfs_template_detection_show,
  371. synaptics_rmi4_store_error),
  372. __ATTR(template_index, 0220,
  373. synaptics_rmi4_show_error,
  374. udg_sysfs_template_index_store),
  375. __ATTR(template_valid, 0664,
  376. udg_sysfs_template_valid_show,
  377. udg_sysfs_template_valid_store),
  378. __ATTR(template_clear, 0220,
  379. synaptics_rmi4_show_error,
  380. udg_sysfs_template_clear_store),
  381. __ATTR(trace_size, 0444,
  382. udg_sysfs_trace_size_show,
  383. synaptics_rmi4_store_error),
  384. };
  385. static struct bin_attribute template_data = {
  386. .attr = {
  387. .name = "template_data",
  388. .mode = 0664,
  389. },
  390. .size = 0,
  391. .read = udg_sysfs_template_data_show,
  392. .write = udg_sysfs_template_data_store,
  393. };
  394. static struct bin_attribute trace_data = {
  395. .attr = {
  396. .name = "trace_data",
  397. .mode = 0444,
  398. },
  399. .size = 0,
  400. .read = udg_sysfs_trace_data_show,
  401. .write = NULL,
  402. };
  403. static struct device_attribute params[] = {
  404. __ATTR(template_displacement, 0664,
  405. udg_sysfs_template_displacement_show,
  406. udg_sysfs_template_displacement_store),
  407. __ATTR(rotation_invariance, 0664,
  408. udg_sysfs_rotation_invariance_show,
  409. udg_sysfs_rotation_invariance_store),
  410. __ATTR(scale_invariance, 0664,
  411. udg_sysfs_scale_invariance_show,
  412. udg_sysfs_scale_invariance_store),
  413. __ATTR(threshold_factor, 0664,
  414. udg_sysfs_threshold_factor_show,
  415. udg_sysfs_threshold_factor_store),
  416. __ATTR(match_metric_threshold, 0664,
  417. udg_sysfs_match_metric_threshold_show,
  418. udg_sysfs_match_metric_threshold_store),
  419. __ATTR(max_inter_stroke_time, 0664,
  420. udg_sysfs_max_inter_stroke_time_show,
  421. udg_sysfs_max_inter_stroke_time_store),
  422. };
  423. static struct synaptics_rmi4_udg_handle *udg;
  424. static unsigned char ctrl_18_sub_size[] = {10, 10, 10, 2, 3, 4, 3, 3, 1, 1};
  425. static unsigned char ctrl_20_sub_size[] = {2};
  426. static unsigned char ctrl_23_sub_size[] = {1, 1, 1};
  427. static unsigned char ctrl_27_sub_size[] = {1, 5, 2, 1, 7};
  428. DECLARE_COMPLETION(udg_remove_complete);
  429. static ssize_t udg_sysfs_engine_enable_store(struct device *dev,
  430. struct device_attribute *attr, const char *buf, size_t count)
  431. {
  432. int retval;
  433. bool enable;
  434. unsigned int input;
  435. if (kstrtouint(buf, 10, &input) != 1)
  436. return -EINVAL;
  437. if (input == 1)
  438. enable = true;
  439. else if (input == 0)
  440. enable = false;
  441. else
  442. return -EINVAL;
  443. retval = udg_engine_enable(enable);
  444. if (retval < 0)
  445. return retval;
  446. return count;
  447. }
  448. static ssize_t udg_sysfs_detection_enable_store(struct device *dev,
  449. struct device_attribute *attr, const char *buf, size_t count)
  450. {
  451. int retval;
  452. bool enable;
  453. unsigned int input;
  454. if (kstrtouint(buf, 10, &input) != 1)
  455. return -EINVAL;
  456. if (input == 1)
  457. enable = true;
  458. else if (input == 0)
  459. enable = false;
  460. else
  461. return -EINVAL;
  462. udg->detection_status = 0;
  463. retval = udg_detection_enable(enable);
  464. if (retval < 0)
  465. return retval;
  466. return count;
  467. }
  468. static ssize_t udg_sysfs_detection_score_show(struct device *dev,
  469. struct device_attribute *attr, char *buf)
  470. {
  471. return snprintf(buf, PAGE_SIZE, "%u\n", udg->detection_score);
  472. }
  473. static ssize_t udg_sysfs_detection_index_show(struct device *dev,
  474. struct device_attribute *attr, char *buf)
  475. {
  476. return snprintf(buf, PAGE_SIZE, "%u\n", udg->detection_index);
  477. }
  478. static ssize_t udg_sysfs_registration_enable_store(struct device *dev,
  479. struct device_attribute *attr, const char *buf, size_t count)
  480. {
  481. int retval;
  482. bool enable;
  483. unsigned int input;
  484. struct synaptics_rmi4_f12_control_41 control_41;
  485. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  486. if (kstrtouint(buf, 10, &input) != 1)
  487. return -EINVAL;
  488. if (input == 1)
  489. enable = true;
  490. else if (input == 0)
  491. enable = false;
  492. else
  493. return -EINVAL;
  494. if (enable) {
  495. retval = synaptics_rmi4_reg_read(rmi4_data,
  496. udg->addr.ctrl_23,
  497. udg->ctrl_buf,
  498. udg->ctrl_23_sub3_off + 1);
  499. if (retval < 0)
  500. return retval;
  501. udg->ctrl_buf[0] = 0;
  502. udg->ctrl_buf[0] |= (1 << CTRL23_FINGER_REPORT_ENABLE_BIT);
  503. if (udg->ctrl_23_sub3_off)
  504. udg->ctrl_buf[udg->ctrl_23_sub3_off] = 0;
  505. retval = synaptics_rmi4_reg_write(rmi4_data,
  506. udg->addr.ctrl_23,
  507. udg->ctrl_buf,
  508. udg->ctrl_23_sub3_off + 1);
  509. if (retval < 0)
  510. return retval;
  511. } else {
  512. retval = synaptics_rmi4_reg_read(rmi4_data,
  513. udg->addr.ctrl_23,
  514. udg->ctrl_buf,
  515. udg->ctrl_23_sub3_off + 1);
  516. if (retval < 0)
  517. return retval;
  518. udg->ctrl_buf[0] = udg->object_type_enable1;
  519. if (udg->ctrl_23_sub3_off) {
  520. udg->ctrl_buf[udg->ctrl_23_sub3_off] =
  521. udg->object_type_enable2;
  522. }
  523. retval = synaptics_rmi4_reg_write(rmi4_data,
  524. udg->addr.ctrl_23,
  525. udg->ctrl_buf,
  526. udg->ctrl_23_sub3_off + 1);
  527. if (retval < 0)
  528. return retval;
  529. }
  530. retval = synaptics_rmi4_reg_read(rmi4_data,
  531. udg->addr.ctrl_41,
  532. control_41.data,
  533. sizeof(control_41.data));
  534. if (retval < 0)
  535. return retval;
  536. control_41.enable_registration = enable ? 1 : 0;
  537. retval = synaptics_rmi4_reg_write(rmi4_data,
  538. udg->addr.ctrl_41,
  539. control_41.data,
  540. sizeof(control_41.data));
  541. if (retval < 0)
  542. return retval;
  543. return count;
  544. }
  545. static ssize_t udg_sysfs_registration_begin_store(struct device *dev,
  546. struct device_attribute *attr, const char *buf, size_t count)
  547. {
  548. int retval;
  549. bool begin;
  550. unsigned int input;
  551. struct synaptics_rmi4_f12_control_41 control_41;
  552. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  553. if (kstrtouint(buf, 10, &input) != 1)
  554. return -EINVAL;
  555. if (input == 1)
  556. begin = true;
  557. else if (input == 0)
  558. begin = false;
  559. else
  560. return -EINVAL;
  561. retval = synaptics_rmi4_reg_read(rmi4_data,
  562. udg->addr.ctrl_41,
  563. control_41.data,
  564. sizeof(control_41.data));
  565. if (retval < 0)
  566. return retval;
  567. control_41.begin = begin ? 1 : 0;
  568. retval = synaptics_rmi4_reg_write(rmi4_data,
  569. udg->addr.ctrl_41,
  570. control_41.data,
  571. sizeof(control_41.data));
  572. if (retval < 0)
  573. return retval;
  574. return count;
  575. }
  576. static ssize_t udg_sysfs_registration_status_show(struct device *dev,
  577. struct device_attribute *attr, char *buf)
  578. {
  579. return snprintf(buf, PAGE_SIZE, "0x%02x\n", udg->registration_status);
  580. }
  581. static ssize_t udg_sysfs_template_size_show(struct device *dev,
  582. struct device_attribute *attr, char *buf)
  583. {
  584. return snprintf(buf, PAGE_SIZE, "%u\n", udg->template_size);
  585. }
  586. static ssize_t udg_sysfs_template_max_index_show(struct device *dev,
  587. struct device_attribute *attr, char *buf)
  588. {
  589. return snprintf(buf, PAGE_SIZE, "%u\n", udg->max_num_templates - 1);
  590. }
  591. static ssize_t udg_sysfs_template_detection_show(struct device *dev,
  592. struct device_attribute *attr, char *buf)
  593. {
  594. int retval;
  595. int attn_event;
  596. unsigned char detection_status;
  597. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  598. attn_event = atomic_read(&udg->attn_event);
  599. atomic_set(&udg->attn_event, 0);
  600. if (attn_event == 0)
  601. return snprintf(buf, PAGE_SIZE, "0\n");
  602. if (udg->detection_status == 0) {
  603. retval = synaptics_rmi4_reg_read(rmi4_data,
  604. udg->addr.data_4,
  605. rmi4_data->gesture_detection,
  606. sizeof(rmi4_data->gesture_detection));
  607. if (retval < 0)
  608. return retval;
  609. udg->detection_status = rmi4_data->gesture_detection[0];
  610. }
  611. detection_status = udg->detection_status;
  612. udg->detection_status = 0;
  613. switch (detection_status) {
  614. case DETECTION:
  615. udg->detection_score = rmi4_data->gesture_detection[1];
  616. udg->detection_index = rmi4_data->gesture_detection[4];
  617. udg->trace_size = rmi4_data->gesture_detection[3];
  618. break;
  619. case REGISTRATION:
  620. udg->registration_status = rmi4_data->gesture_detection[1];
  621. udg->trace_size = rmi4_data->gesture_detection[3];
  622. break;
  623. default:
  624. return snprintf(buf, PAGE_SIZE, "0\n");
  625. }
  626. return snprintf(buf, PAGE_SIZE, "0x%02x\n", detection_status);
  627. }
  628. static ssize_t udg_sysfs_template_index_store(struct device *dev,
  629. struct device_attribute *attr, const char *buf, size_t count)
  630. {
  631. int retval;
  632. unsigned long index;
  633. retval = sstrtoul(buf, 10, &index);
  634. if (retval)
  635. return retval;
  636. retval = udg_set_index((unsigned char)index);
  637. if (retval < 0)
  638. return retval;
  639. return count;
  640. }
  641. static ssize_t udg_sysfs_template_valid_show(struct device *dev,
  642. struct device_attribute *attr, char *buf)
  643. {
  644. int retval;
  645. unsigned char valid;
  646. unsigned char offset;
  647. unsigned char byte_num;
  648. unsigned char template_flags[2];
  649. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  650. byte_num = udg->template_index / 8;
  651. offset = udg->template_index % 8;
  652. retval = synaptics_rmi4_reg_read(rmi4_data,
  653. udg->addr.template_flags,
  654. template_flags,
  655. sizeof(template_flags));
  656. if (retval < 0)
  657. return retval;
  658. valid = (template_flags[byte_num] & (1 << offset)) >> offset;
  659. return snprintf(buf, PAGE_SIZE, "%u\n", valid);
  660. }
  661. static ssize_t udg_sysfs_template_valid_store(struct device *dev,
  662. struct device_attribute *attr, const char *buf, size_t count)
  663. {
  664. int retval;
  665. unsigned long valid;
  666. unsigned char offset;
  667. unsigned char byte_num;
  668. unsigned char template_flags[2];
  669. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  670. retval = sstrtoul(buf, 10, &valid);
  671. if (retval)
  672. return retval;
  673. if (valid > 0)
  674. valid = 1;
  675. byte_num = udg->template_index / 8;
  676. offset = udg->template_index % 8;
  677. retval = synaptics_rmi4_reg_read(rmi4_data,
  678. udg->addr.template_flags,
  679. template_flags,
  680. sizeof(template_flags));
  681. if (retval < 0)
  682. return retval;
  683. if (valid)
  684. template_flags[byte_num] |= (1 << offset);
  685. else
  686. template_flags[byte_num] &= ~(1 << offset);
  687. retval = synaptics_rmi4_reg_write(rmi4_data,
  688. udg->addr.template_flags,
  689. template_flags,
  690. sizeof(template_flags));
  691. if (retval < 0)
  692. return retval;
  693. #ifdef STORE_GESTURES
  694. udg_read_valid_data();
  695. #endif
  696. return count;
  697. }
  698. static ssize_t udg_sysfs_template_clear_store(struct device *dev,
  699. struct device_attribute *attr, const char *buf, size_t count)
  700. {
  701. int retval;
  702. unsigned int input;
  703. const char cmd[] = {'0', 0};
  704. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  705. if (kstrtouint(buf, 10, &input) != 1)
  706. return -EINVAL;
  707. if (input != 1)
  708. return -EINVAL;
  709. memset(udg->template_data_buf, 0x00, udg->template_data_size);
  710. retval = synaptics_rmi4_reg_write(rmi4_data,
  711. udg->addr.template_data,
  712. udg->template_data_buf,
  713. udg->template_data_size);
  714. if (retval < 0) {
  715. dev_err(rmi4_data->pdev->dev.parent,
  716. "%s: Failed to clear template data\n",
  717. __func__);
  718. return retval;
  719. }
  720. retval = udg_sysfs_template_valid_store(dev, attr, cmd, 1);
  721. if (retval < 0) {
  722. dev_err(rmi4_data->pdev->dev.parent,
  723. "%s: Failed to clear valid bit\n",
  724. __func__);
  725. return retval;
  726. }
  727. #ifdef STORE_GESTURES
  728. udg_read_template_data(udg->template_index);
  729. udg_read_valid_data();
  730. #endif
  731. return count;
  732. }
  733. static ssize_t udg_sysfs_trace_size_show(struct device *dev,
  734. struct device_attribute *attr, char *buf)
  735. {
  736. return snprintf(buf, PAGE_SIZE, "%u\n", udg->trace_size);
  737. }
  738. static ssize_t udg_sysfs_trace_data_show(struct file *data_file,
  739. struct kobject *kobj, struct bin_attribute *attributes,
  740. char *buf, loff_t pos, size_t count)
  741. {
  742. int retval;
  743. unsigned short index = 0;
  744. unsigned short trace_data_size;
  745. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  746. trace_data_size = udg->trace_size * 5;
  747. if (trace_data_size == 0)
  748. return -EINVAL;
  749. if (count < trace_data_size) {
  750. dev_err(rmi4_data->pdev->dev.parent,
  751. "%s: Not enough space (%d bytes) in buffer\n",
  752. __func__, (unsigned int)count);
  753. return -EINVAL;
  754. }
  755. if (udg->trace_data_buf_size < trace_data_size) {
  756. if (udg->trace_data_buf_size)
  757. kfree(udg->trace_data_buf);
  758. udg->trace_data_buf = kzalloc(trace_data_size, GFP_KERNEL);
  759. if (!udg->trace_data_buf) {
  760. dev_err(rmi4_data->pdev->dev.parent,
  761. "%s: Failed to alloc mem for trace data buffer\n",
  762. __func__);
  763. udg->trace_data_buf_size = 0;
  764. return -ENOMEM;
  765. }
  766. udg->trace_data_buf_size = trace_data_size;
  767. }
  768. retval = synaptics_rmi4_reg_read(rmi4_data,
  769. udg->addr.trace_x,
  770. &udg->trace_data_buf[index],
  771. udg->trace_size * 2);
  772. if (retval < 0) {
  773. dev_err(rmi4_data->pdev->dev.parent,
  774. "%s: Failed to read trace X data\n",
  775. __func__);
  776. return retval;
  777. }
  778. index += udg->trace_size * 2;
  779. retval = synaptics_rmi4_reg_read(rmi4_data,
  780. udg->addr.trace_y,
  781. &udg->trace_data_buf[index],
  782. udg->trace_size * 2);
  783. if (retval < 0) {
  784. dev_err(rmi4_data->pdev->dev.parent,
  785. "%s: Failed to read trace Y data\n",
  786. __func__);
  787. return retval;
  788. }
  789. index += udg->trace_size * 2;
  790. retval = synaptics_rmi4_reg_read(rmi4_data,
  791. udg->addr.trace_segment,
  792. &udg->trace_data_buf[index],
  793. udg->trace_size);
  794. if (retval < 0) {
  795. dev_err(rmi4_data->pdev->dev.parent,
  796. "%s: Failed to read trace segment data\n",
  797. __func__);
  798. return retval;
  799. }
  800. retval = secure_memcpy(buf, count, udg->trace_data_buf,
  801. udg->trace_data_buf_size, trace_data_size);
  802. if (retval < 0) {
  803. dev_err(rmi4_data->pdev->dev.parent,
  804. "%s: Failed to copy trace data\n",
  805. __func__);
  806. return retval;
  807. }
  808. return trace_data_size;
  809. }
  810. static ssize_t udg_sysfs_template_data_show(struct file *data_file,
  811. struct kobject *kobj, struct bin_attribute *attributes,
  812. char *buf, loff_t pos, size_t count)
  813. {
  814. int retval;
  815. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  816. if (count < udg->template_data_size) {
  817. dev_err(rmi4_data->pdev->dev.parent,
  818. "%s: Not enough space (%d bytes) in buffer\n",
  819. __func__, (unsigned int)count);
  820. return -EINVAL;
  821. }
  822. retval = synaptics_rmi4_reg_read(rmi4_data,
  823. udg->addr.template_data,
  824. udg->template_data_buf,
  825. udg->template_data_size);
  826. if (retval < 0) {
  827. dev_err(rmi4_data->pdev->dev.parent,
  828. "%s: Failed to read template data\n",
  829. __func__);
  830. return retval;
  831. }
  832. retval = secure_memcpy(buf, count, udg->template_data_buf,
  833. udg->template_data_size, udg->template_data_size);
  834. if (retval < 0) {
  835. dev_err(rmi4_data->pdev->dev.parent,
  836. "%s: Failed to copy template data\n",
  837. __func__);
  838. return retval;
  839. }
  840. #ifdef STORE_GESTURES
  841. udg_read_template_data(udg->template_index);
  842. udg_read_valid_data();
  843. #endif
  844. return udg->template_data_size;
  845. }
  846. static ssize_t udg_sysfs_template_data_store(struct file *data_file,
  847. struct kobject *kobj, struct bin_attribute *attributes,
  848. char *buf, loff_t pos, size_t count)
  849. {
  850. int retval;
  851. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  852. retval = secure_memcpy(udg->template_data_buf, udg->template_data_size,
  853. buf, count, count);
  854. if (retval < 0) {
  855. dev_err(rmi4_data->pdev->dev.parent,
  856. "%s: Failed to copy template data\n",
  857. __func__);
  858. return retval;
  859. }
  860. retval = synaptics_rmi4_reg_write(rmi4_data,
  861. udg->addr.template_data,
  862. udg->template_data_buf,
  863. count);
  864. if (retval < 0) {
  865. dev_err(rmi4_data->pdev->dev.parent,
  866. "%s: Failed to write template data\n",
  867. __func__);
  868. return retval;
  869. }
  870. #ifdef STORE_GESTURES
  871. udg_read_template_data(udg->template_index);
  872. udg_read_valid_data();
  873. #endif
  874. return count;
  875. }
  876. static ssize_t udg_sysfs_template_displacement_show(struct device *dev,
  877. struct device_attribute *attr, char *buf)
  878. {
  879. int retval;
  880. unsigned short template_displacement;
  881. retval = udg_read_tuning_params();
  882. if (retval < 0)
  883. return retval;
  884. template_displacement =
  885. ((unsigned short)udg->tuning.template_disp_lsb << 0) |
  886. ((unsigned short)udg->tuning.template_disp_msb << 8);
  887. return snprintf(buf, PAGE_SIZE, "%u\n", template_displacement);
  888. }
  889. static ssize_t udg_sysfs_template_displacement_store(struct device *dev,
  890. struct device_attribute *attr, const char *buf, size_t count)
  891. {
  892. int retval;
  893. unsigned long input;
  894. retval = sstrtoul(buf, 10, &input);
  895. if (retval)
  896. return retval;
  897. retval = udg_read_tuning_params();
  898. if (retval < 0)
  899. return retval;
  900. udg->tuning.template_disp_lsb = (unsigned char)(input >> 0);
  901. udg->tuning.template_disp_msb = (unsigned char)(input >> 8);
  902. retval = udg_write_tuning_params();
  903. if (retval < 0)
  904. return retval;
  905. return count;
  906. }
  907. static ssize_t udg_sysfs_rotation_invariance_show(struct device *dev,
  908. struct device_attribute *attr, char *buf)
  909. {
  910. int retval;
  911. unsigned short rotation_invariance;
  912. retval = udg_read_tuning_params();
  913. if (retval < 0)
  914. return retval;
  915. rotation_invariance =
  916. ((unsigned short)udg->tuning.rotation_inv_lsb << 0) |
  917. ((unsigned short)udg->tuning.rotation_inv_msb << 8);
  918. return snprintf(buf, PAGE_SIZE, "%u\n", rotation_invariance);
  919. }
  920. static ssize_t udg_sysfs_rotation_invariance_store(struct device *dev,
  921. struct device_attribute *attr, const char *buf, size_t count)
  922. {
  923. int retval;
  924. unsigned long input;
  925. retval = sstrtoul(buf, 10, &input);
  926. if (retval)
  927. return retval;
  928. retval = udg_read_tuning_params();
  929. if (retval < 0)
  930. return retval;
  931. udg->tuning.rotation_inv_lsb = (unsigned char)(input >> 0);
  932. udg->tuning.rotation_inv_msb = (unsigned char)(input >> 8);
  933. retval = udg_write_tuning_params();
  934. if (retval < 0)
  935. return retval;
  936. return count;
  937. }
  938. static ssize_t udg_sysfs_scale_invariance_show(struct device *dev,
  939. struct device_attribute *attr, char *buf)
  940. {
  941. int retval;
  942. unsigned short scale_invariance;
  943. retval = udg_read_tuning_params();
  944. if (retval < 0)
  945. return retval;
  946. scale_invariance =
  947. ((unsigned short)udg->tuning.scale_inv_lsb << 0) |
  948. ((unsigned short)udg->tuning.scale_inv_msb << 8);
  949. return snprintf(buf, PAGE_SIZE, "%u\n", scale_invariance);
  950. }
  951. static ssize_t udg_sysfs_scale_invariance_store(struct device *dev,
  952. struct device_attribute *attr, const char *buf, size_t count)
  953. {
  954. int retval;
  955. unsigned long input;
  956. retval = sstrtoul(buf, 10, &input);
  957. if (retval)
  958. return retval;
  959. retval = udg_read_tuning_params();
  960. if (retval < 0)
  961. return retval;
  962. udg->tuning.scale_inv_lsb = (unsigned char)(input >> 0);
  963. udg->tuning.scale_inv_msb = (unsigned char)(input >> 8);
  964. retval = udg_write_tuning_params();
  965. if (retval < 0)
  966. return retval;
  967. return count;
  968. }
  969. static ssize_t udg_sysfs_threshold_factor_show(struct device *dev,
  970. struct device_attribute *attr, char *buf)
  971. {
  972. int retval;
  973. unsigned short threshold_factor;
  974. retval = udg_read_tuning_params();
  975. if (retval < 0)
  976. return retval;
  977. threshold_factor =
  978. ((unsigned short)udg->tuning.thres_factor_lsb << 0) |
  979. ((unsigned short)udg->tuning.thres_factor_msb << 8);
  980. return snprintf(buf, PAGE_SIZE, "%u\n", threshold_factor);
  981. }
  982. static ssize_t udg_sysfs_threshold_factor_store(struct device *dev,
  983. struct device_attribute *attr, const char *buf, size_t count)
  984. {
  985. int retval;
  986. unsigned long input;
  987. retval = sstrtoul(buf, 10, &input);
  988. if (retval)
  989. return retval;
  990. retval = udg_read_tuning_params();
  991. if (retval < 0)
  992. return retval;
  993. udg->tuning.thres_factor_lsb = (unsigned char)(input >> 0);
  994. udg->tuning.thres_factor_msb = (unsigned char)(input >> 8);
  995. retval = udg_write_tuning_params();
  996. if (retval < 0)
  997. return retval;
  998. return count;
  999. }
  1000. static ssize_t udg_sysfs_match_metric_threshold_show(struct device *dev,
  1001. struct device_attribute *attr, char *buf)
  1002. {
  1003. int retval;
  1004. unsigned short match_metric_threshold;
  1005. retval = udg_read_tuning_params();
  1006. if (retval < 0)
  1007. return retval;
  1008. match_metric_threshold =
  1009. ((unsigned short)udg->tuning.metric_thres_lsb << 0) |
  1010. ((unsigned short)udg->tuning.metric_thres_msb << 8);
  1011. return snprintf(buf, PAGE_SIZE, "%u\n", match_metric_threshold);
  1012. }
  1013. static ssize_t udg_sysfs_match_metric_threshold_store(struct device *dev,
  1014. struct device_attribute *attr, const char *buf, size_t count)
  1015. {
  1016. int retval;
  1017. unsigned long input;
  1018. retval = sstrtoul(buf, 10, &input);
  1019. if (retval)
  1020. return retval;
  1021. retval = udg_read_tuning_params();
  1022. if (retval < 0)
  1023. return retval;
  1024. udg->tuning.metric_thres_lsb = (unsigned char)(input >> 0);
  1025. udg->tuning.metric_thres_msb = (unsigned char)(input >> 8);
  1026. retval = udg_write_tuning_params();
  1027. if (retval < 0)
  1028. return retval;
  1029. return count;
  1030. }
  1031. static ssize_t udg_sysfs_max_inter_stroke_time_show(struct device *dev,
  1032. struct device_attribute *attr, char *buf)
  1033. {
  1034. int retval;
  1035. unsigned short max_inter_stroke_time;
  1036. retval = udg_read_tuning_params();
  1037. if (retval < 0)
  1038. return retval;
  1039. max_inter_stroke_time =
  1040. ((unsigned short)udg->tuning.inter_stroke_lsb << 0) |
  1041. ((unsigned short)udg->tuning.inter_stroke_msb << 8);
  1042. return snprintf(buf, PAGE_SIZE, "%u\n", max_inter_stroke_time);
  1043. }
  1044. static ssize_t udg_sysfs_max_inter_stroke_time_store(struct device *dev,
  1045. struct device_attribute *attr, const char *buf, size_t count)
  1046. {
  1047. int retval;
  1048. unsigned long input;
  1049. retval = sstrtoul(buf, 10, &input);
  1050. if (retval)
  1051. return retval;
  1052. retval = udg_read_tuning_params();
  1053. if (retval < 0)
  1054. return retval;
  1055. udg->tuning.inter_stroke_lsb = (unsigned char)(input >> 0);
  1056. udg->tuning.inter_stroke_msb = (unsigned char)(input >> 8);
  1057. retval = udg_write_tuning_params();
  1058. if (retval < 0)
  1059. return retval;
  1060. return count;
  1061. }
  1062. static int udg_ctrl_subpacket(unsigned char ctrlreg,
  1063. unsigned char subpacket,
  1064. struct synaptics_rmi4_f12_query_5 *query_5)
  1065. {
  1066. int retval;
  1067. unsigned char cnt;
  1068. unsigned char regnum;
  1069. unsigned char bitnum;
  1070. unsigned char q5_index;
  1071. unsigned char q6_index;
  1072. unsigned char offset;
  1073. unsigned char max_ctrlreg;
  1074. unsigned char *query_6;
  1075. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  1076. max_ctrlreg = (sizeof(query_5->data) - 1) * 8 - 1;
  1077. if (ctrlreg > max_ctrlreg) {
  1078. dev_err(rmi4_data->pdev->dev.parent,
  1079. "%s: Control register number (%d) over limit\n",
  1080. __func__, ctrlreg);
  1081. return -EINVAL;
  1082. }
  1083. q5_index = ctrlreg / 8 + 1;
  1084. bitnum = ctrlreg % 8;
  1085. if ((query_5->data[q5_index] & (1 << bitnum)) == 0x00) {
  1086. dev_err(rmi4_data->pdev->dev.parent,
  1087. "%s: Control %d is not present\n",
  1088. __func__, ctrlreg);
  1089. return -EINVAL;
  1090. }
  1091. query_6 = kmalloc(query_5->size_of_query6, GFP_KERNEL);
  1092. if (!query_6) {
  1093. dev_err(rmi4_data->pdev->dev.parent,
  1094. "%s: Failed to alloc mem for query 6\n",
  1095. __func__);
  1096. return -ENOMEM;
  1097. }
  1098. retval = synaptics_rmi4_reg_read(rmi4_data,
  1099. udg->query_base_addr + 6,
  1100. query_6,
  1101. query_5->size_of_query6);
  1102. if (retval < 0)
  1103. goto exit;
  1104. q6_index = 0;
  1105. for (regnum = 0; regnum < ctrlreg; regnum++) {
  1106. q5_index = regnum / 8 + 1;
  1107. bitnum = regnum % 8;
  1108. if ((query_5->data[q5_index] & (1 << bitnum)) == 0x00)
  1109. continue;
  1110. if (query_6[q6_index] == 0x00)
  1111. q6_index += 3;
  1112. else
  1113. q6_index++;
  1114. while (query_6[q6_index] & ~MASK_7BIT)
  1115. q6_index++;
  1116. q6_index++;
  1117. }
  1118. cnt = 0;
  1119. q6_index++;
  1120. offset = subpacket / 7;
  1121. bitnum = subpacket % 7;
  1122. do {
  1123. if (cnt == offset) {
  1124. if (query_6[q6_index + cnt] & (1 << bitnum))
  1125. retval = 1;
  1126. else
  1127. retval = 0;
  1128. goto exit;
  1129. }
  1130. cnt++;
  1131. } while (query_6[q6_index + cnt - 1] & ~MASK_7BIT);
  1132. retval = 0;
  1133. exit:
  1134. kfree(query_6);
  1135. return retval;
  1136. }
  1137. static int udg_read_tuning_params(void)
  1138. {
  1139. int retval;
  1140. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  1141. retval = synaptics_rmi4_reg_read(rmi4_data,
  1142. udg->addr.ctrl_18,
  1143. udg->ctrl_buf,
  1144. udg->ctrl_18_sub10_off + sizeof(struct udg_tuning));
  1145. if (retval < 0)
  1146. return retval;
  1147. secure_memcpy(udg->tuning.data,
  1148. sizeof(udg->tuning.data),
  1149. (unsigned char *)&udg->ctrl_buf[udg->ctrl_18_sub10_off],
  1150. sizeof(struct udg_tuning),
  1151. sizeof(struct udg_tuning));
  1152. return 0;
  1153. }
  1154. static int udg_write_tuning_params(void)
  1155. {
  1156. int retval;
  1157. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  1158. secure_memcpy((unsigned char *)&udg->ctrl_buf[udg->ctrl_18_sub10_off],
  1159. sizeof(struct udg_tuning),
  1160. udg->tuning.data,
  1161. sizeof(udg->tuning.data),
  1162. sizeof(struct udg_tuning));
  1163. retval = synaptics_rmi4_reg_write(rmi4_data,
  1164. udg->addr.ctrl_18,
  1165. udg->ctrl_buf,
  1166. udg->ctrl_18_sub10_off + sizeof(struct udg_tuning));
  1167. if (retval < 0)
  1168. return retval;
  1169. return 0;
  1170. }
  1171. static int udg_detection_enable(bool enable)
  1172. {
  1173. int retval;
  1174. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  1175. retval = synaptics_rmi4_reg_read(rmi4_data,
  1176. udg->addr.ctrl_20,
  1177. udg->ctrl_buf,
  1178. udg->ctrl_20_sub1_off + 1);
  1179. if (retval < 0)
  1180. return retval;
  1181. if (enable)
  1182. udg->ctrl_buf[udg->ctrl_20_sub1_off] = WAKEUP_GESTURE_MODE;
  1183. else
  1184. udg->ctrl_buf[udg->ctrl_20_sub1_off] = udg->report_flags;
  1185. retval = synaptics_rmi4_reg_write(rmi4_data,
  1186. udg->addr.ctrl_20,
  1187. udg->ctrl_buf,
  1188. udg->ctrl_20_sub1_off + 1);
  1189. if (retval < 0)
  1190. return retval;
  1191. return 0;
  1192. }
  1193. static int udg_engine_enable(bool enable)
  1194. {
  1195. int retval;
  1196. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  1197. if (enable) {
  1198. retval = synaptics_rmi4_reg_read(rmi4_data,
  1199. udg->addr.ctrl_27,
  1200. udg->ctrl_buf,
  1201. udg->ctrl_27_sub5_off + 1);
  1202. if (retval < 0)
  1203. return retval;
  1204. udg->ctrl_buf[udg->ctrl_27_sub5_off] |=
  1205. (1 << CTRL27_UDG_ENABLE_BIT);
  1206. retval = synaptics_rmi4_reg_write(rmi4_data,
  1207. udg->addr.ctrl_27,
  1208. udg->ctrl_buf,
  1209. udg->ctrl_27_sub5_off + 1);
  1210. if (retval < 0)
  1211. return retval;
  1212. } else {
  1213. retval = synaptics_rmi4_reg_read(rmi4_data,
  1214. udg->addr.ctrl_27,
  1215. udg->ctrl_buf,
  1216. udg->ctrl_27_sub5_off + 1);
  1217. if (retval < 0)
  1218. return retval;
  1219. udg->ctrl_buf[udg->ctrl_27_sub5_off] &=
  1220. ~(1 << CTRL27_UDG_ENABLE_BIT);
  1221. retval = synaptics_rmi4_reg_write(rmi4_data,
  1222. udg->addr.ctrl_27,
  1223. udg->ctrl_buf,
  1224. udg->ctrl_27_sub5_off + 1);
  1225. if (retval < 0)
  1226. return retval;
  1227. }
  1228. return 0;
  1229. }
  1230. static void udg_report(void)
  1231. {
  1232. int retval;
  1233. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  1234. atomic_set(&udg->attn_event, 1);
  1235. if (rmi4_data->suspend) {
  1236. if (rmi4_data->gesture_detection[0] == 0) {
  1237. retval = synaptics_rmi4_reg_read(rmi4_data,
  1238. udg->addr.data_4,
  1239. rmi4_data->gesture_detection,
  1240. sizeof(rmi4_data->gesture_detection));
  1241. if (retval < 0) {
  1242. dev_err(rmi4_data->pdev->dev.parent,
  1243. "%s: Failed to read gesture detection\n",
  1244. __func__);
  1245. return;
  1246. }
  1247. }
  1248. udg->detection_status = rmi4_data->gesture_detection[0];
  1249. rmi4_data->gesture_detection[0] = 0;
  1250. if (udg->detection_status == DETECTION) {
  1251. input_report_key(udg->udg_dev, KEY_WAKEUP, 1);
  1252. input_sync(udg->udg_dev);
  1253. input_report_key(udg->udg_dev, KEY_WAKEUP, 0);
  1254. input_sync(udg->udg_dev);
  1255. rmi4_data->suspend = false;
  1256. }
  1257. }
  1258. return;
  1259. }
  1260. static int udg_set_index(unsigned char index)
  1261. {
  1262. int retval;
  1263. struct synaptics_rmi4_f12_control_41 control_41;
  1264. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  1265. if (index >= udg->max_num_templates)
  1266. return -EINVAL;
  1267. udg->template_index = index;
  1268. retval = synaptics_rmi4_reg_read(rmi4_data,
  1269. udg->addr.ctrl_41,
  1270. control_41.data,
  1271. sizeof(control_41.data));
  1272. if (retval < 0)
  1273. return retval;
  1274. control_41.template_index = udg->template_index;
  1275. retval = synaptics_rmi4_reg_write(rmi4_data,
  1276. udg->addr.ctrl_41,
  1277. control_41.data,
  1278. sizeof(control_41.data));
  1279. if (retval < 0)
  1280. return retval;
  1281. return 0;
  1282. }
  1283. #ifdef STORE_GESTURES
  1284. static int udg_read_valid_data(void)
  1285. {
  1286. int retval;
  1287. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  1288. retval = synaptics_rmi4_reg_read(rmi4_data,
  1289. udg->addr.template_flags,
  1290. udg->valid_buf,
  1291. sizeof(udg->valid_buf));
  1292. if (retval < 0)
  1293. return retval;
  1294. return 0;
  1295. }
  1296. static int udg_write_valid_data(void)
  1297. {
  1298. int retval;
  1299. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  1300. retval = synaptics_rmi4_reg_write(rmi4_data,
  1301. udg->addr.template_flags,
  1302. udg->valid_buf,
  1303. sizeof(udg->valid_buf));
  1304. if (retval < 0)
  1305. return retval;
  1306. return 0;
  1307. }
  1308. static int udg_read_template_data(unsigned char index)
  1309. {
  1310. int retval;
  1311. unsigned char *storage;
  1312. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  1313. udg_set_index(index);
  1314. storage = &(udg->storage_buf[index * udg->template_data_size]);
  1315. retval = synaptics_rmi4_reg_read(rmi4_data,
  1316. udg->addr.template_data,
  1317. storage,
  1318. udg->template_data_size);
  1319. if (retval < 0) {
  1320. dev_err(rmi4_data->pdev->dev.parent,
  1321. "%s: Failed to read template data\n",
  1322. __func__);
  1323. return retval;
  1324. }
  1325. return 0;
  1326. }
  1327. static int udg_write_template_data(void)
  1328. {
  1329. int retval;
  1330. unsigned char ii;
  1331. unsigned char *storage;
  1332. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  1333. for (ii = 0; ii < udg->gestures_to_store; ii++) {
  1334. udg_set_index(ii);
  1335. storage = &(udg->storage_buf[ii * udg->template_data_size]);
  1336. retval = synaptics_rmi4_reg_write(rmi4_data,
  1337. udg->addr.template_data,
  1338. storage,
  1339. udg->template_data_size);
  1340. if (retval < 0) {
  1341. dev_err(rmi4_data->pdev->dev.parent,
  1342. "%s: Failed to write template data\n",
  1343. __func__);
  1344. return retval;
  1345. }
  1346. }
  1347. return 0;
  1348. }
  1349. #endif
  1350. static int udg_reg_init(void)
  1351. {
  1352. int retval;
  1353. unsigned char ii;
  1354. unsigned char data_offset;
  1355. unsigned char size_of_query;
  1356. unsigned char ctrl_18_offset;
  1357. unsigned char ctrl_20_offset;
  1358. unsigned char ctrl_23_offset;
  1359. unsigned char ctrl_27_offset;
  1360. unsigned char ctrl_41_offset;
  1361. struct synaptics_rmi4_f12_query_0 query_0;
  1362. struct synaptics_rmi4_f12_query_5 query_5;
  1363. struct synaptics_rmi4_f12_query_8 query_8;
  1364. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  1365. retval = synaptics_rmi4_reg_read(rmi4_data,
  1366. udg->query_base_addr + 7,
  1367. &size_of_query,
  1368. sizeof(size_of_query));
  1369. if (retval < 0)
  1370. return retval;
  1371. if (size_of_query < 4) {
  1372. dev_err(rmi4_data->pdev->dev.parent,
  1373. "%s: User defined gesture support unavailable (missing data registers)\n",
  1374. __func__);
  1375. retval = -ENODEV;
  1376. return retval;
  1377. }
  1378. retval = synaptics_rmi4_reg_read(rmi4_data,
  1379. udg->query_base_addr + 8,
  1380. query_8.data,
  1381. sizeof(query_8.data));
  1382. if (retval < 0)
  1383. return retval;
  1384. if ((query_8.data16_is_present) &&
  1385. (query_8.data17_is_present) &&
  1386. (query_8.data18_is_present) &&
  1387. (query_8.data19_is_present) &&
  1388. (query_8.data20_is_present) &&
  1389. (query_8.data21_is_present)) {
  1390. data_offset = query_8.data0_is_present +
  1391. query_8.data1_is_present +
  1392. query_8.data2_is_present +
  1393. query_8.data3_is_present;
  1394. udg->addr.data_4 = udg->data_base_addr + data_offset;
  1395. data_offset = data_offset +
  1396. query_8.data4_is_present +
  1397. query_8.data5_is_present +
  1398. query_8.data6_is_present +
  1399. query_8.data7_is_present +
  1400. query_8.data8_is_present +
  1401. query_8.data9_is_present +
  1402. query_8.data10_is_present +
  1403. query_8.data11_is_present +
  1404. query_8.data12_is_present +
  1405. query_8.data13_is_present +
  1406. query_8.data14_is_present +
  1407. query_8.data15_is_present;
  1408. udg->addr.trace_x = udg->data_base_addr + data_offset;
  1409. udg->addr.trace_y = udg->addr.trace_x + 1;
  1410. udg->addr.trace_segment = udg->addr.trace_y + 1;
  1411. udg->addr.template_helper = udg->addr.trace_segment + 1;
  1412. udg->addr.template_data = udg->addr.template_helper + 1;
  1413. udg->addr.template_flags = udg->addr.template_data + 1;
  1414. } else {
  1415. dev_err(rmi4_data->pdev->dev.parent,
  1416. "%s: User defined gesture support unavailable (missing data registers)\n",
  1417. __func__);
  1418. retval = -ENODEV;
  1419. return retval;
  1420. }
  1421. retval = synaptics_rmi4_reg_read(rmi4_data,
  1422. udg->query_base_addr + 4,
  1423. &size_of_query,
  1424. sizeof(size_of_query));
  1425. if (retval < 0)
  1426. return retval;
  1427. if (size_of_query < 7) {
  1428. dev_err(rmi4_data->pdev->dev.parent,
  1429. "%s: User defined gesture support unavailable (missing control registers)\n",
  1430. __func__);
  1431. retval = -ENODEV;
  1432. return retval;
  1433. }
  1434. retval = synaptics_rmi4_reg_read(rmi4_data,
  1435. udg->query_base_addr + 5,
  1436. query_5.data,
  1437. sizeof(query_5.data));
  1438. if (retval < 0)
  1439. return retval;
  1440. ctrl_18_offset = query_5.ctrl0_is_present +
  1441. query_5.ctrl1_is_present +
  1442. query_5.ctrl2_is_present +
  1443. query_5.ctrl3_is_present +
  1444. query_5.ctrl4_is_present +
  1445. query_5.ctrl5_is_present +
  1446. query_5.ctrl6_is_present +
  1447. query_5.ctrl7_is_present +
  1448. query_5.ctrl8_is_present +
  1449. query_5.ctrl9_is_present +
  1450. query_5.ctrl10_is_present +
  1451. query_5.ctrl11_is_present +
  1452. query_5.ctrl12_is_present +
  1453. query_5.ctrl13_is_present +
  1454. query_5.ctrl14_is_present +
  1455. query_5.ctrl15_is_present +
  1456. query_5.ctrl16_is_present +
  1457. query_5.ctrl17_is_present;
  1458. ctrl_20_offset = ctrl_18_offset +
  1459. query_5.ctrl18_is_present +
  1460. query_5.ctrl19_is_present;
  1461. ctrl_23_offset = ctrl_20_offset +
  1462. query_5.ctrl20_is_present +
  1463. query_5.ctrl21_is_present +
  1464. query_5.ctrl22_is_present;
  1465. ctrl_27_offset = ctrl_23_offset+
  1466. query_5.ctrl23_is_present +
  1467. query_5.ctrl24_is_present +
  1468. query_5.ctrl25_is_present +
  1469. query_5.ctrl26_is_present;
  1470. ctrl_41_offset = ctrl_27_offset+
  1471. query_5.ctrl27_is_present +
  1472. query_5.ctrl28_is_present +
  1473. query_5.ctrl29_is_present +
  1474. query_5.ctrl30_is_present +
  1475. query_5.ctrl31_is_present +
  1476. query_5.ctrl32_is_present +
  1477. query_5.ctrl33_is_present +
  1478. query_5.ctrl34_is_present +
  1479. query_5.ctrl35_is_present +
  1480. query_5.ctrl36_is_present +
  1481. query_5.ctrl37_is_present +
  1482. query_5.ctrl38_is_present +
  1483. query_5.ctrl39_is_present +
  1484. query_5.ctrl40_is_present;
  1485. udg->addr.ctrl_18 = udg->control_base_addr + ctrl_18_offset;
  1486. udg->addr.ctrl_20 = udg->control_base_addr + ctrl_20_offset;
  1487. udg->addr.ctrl_23 = udg->control_base_addr + ctrl_23_offset;
  1488. udg->addr.ctrl_27 = udg->control_base_addr + ctrl_27_offset;
  1489. udg->addr.ctrl_41 = udg->control_base_addr + ctrl_41_offset;
  1490. udg->ctrl_18_sub10_off = 0;
  1491. for (ii = 0; ii < 10; ii++) {
  1492. retval = udg_ctrl_subpacket(18, ii, &query_5);
  1493. if (retval == 1)
  1494. udg->ctrl_18_sub10_off += ctrl_18_sub_size[ii];
  1495. else if (retval < 0)
  1496. return retval;
  1497. }
  1498. udg->ctrl_20_sub1_off = 0;
  1499. for (ii = 0; ii < 1; ii++) {
  1500. retval = udg_ctrl_subpacket(20, ii, &query_5);
  1501. if (retval == 1)
  1502. udg->ctrl_20_sub1_off += ctrl_20_sub_size[ii];
  1503. else if (retval < 0)
  1504. return retval;
  1505. }
  1506. udg->ctrl_23_sub3_off = 0;
  1507. for (ii = 0; ii < 3; ii++) {
  1508. retval = udg_ctrl_subpacket(23, ii, &query_5);
  1509. if (retval == 1)
  1510. udg->ctrl_23_sub3_off += ctrl_23_sub_size[ii];
  1511. else if (retval < 0)
  1512. return retval;
  1513. }
  1514. retval = udg_ctrl_subpacket(23, 3, &query_5);
  1515. if (retval == 0)
  1516. udg->ctrl_23_sub3_off = 0;
  1517. else if (retval < 0)
  1518. return retval;
  1519. udg->ctrl_27_sub5_off = 0;
  1520. for (ii = 0; ii < 5; ii++) {
  1521. retval = udg_ctrl_subpacket(27, ii, &query_5);
  1522. if (retval == 1)
  1523. udg->ctrl_27_sub5_off += ctrl_27_sub_size[ii];
  1524. else if (retval < 0)
  1525. return retval;
  1526. }
  1527. retval = synaptics_rmi4_reg_read(rmi4_data,
  1528. udg->query_base_addr + 0,
  1529. query_0.data,
  1530. sizeof(query_0.data));
  1531. if (retval < 0)
  1532. return retval;
  1533. udg->max_num_templates = query_0.max_num_templates;
  1534. udg->template_size =
  1535. ((unsigned short)query_0.template_size_lsb << 0) |
  1536. ((unsigned short)query_0.template_size_msb << 8);
  1537. udg->template_data_size = udg->template_size * 4 * 2 + 4 + 1;
  1538. #ifdef STORE_GESTURES
  1539. udg->gestures_to_store = udg->max_num_templates;
  1540. if (GESTURES_TO_STORE < udg->gestures_to_store)
  1541. udg->gestures_to_store = GESTURES_TO_STORE;
  1542. #endif
  1543. retval = synaptics_rmi4_reg_read(rmi4_data,
  1544. udg->addr.ctrl_20,
  1545. udg->ctrl_buf,
  1546. udg->ctrl_20_sub1_off + 1);
  1547. if (retval < 0)
  1548. return retval;
  1549. udg->report_flags = udg->ctrl_buf[udg->ctrl_20_sub1_off];
  1550. retval = synaptics_rmi4_reg_read(rmi4_data,
  1551. udg->addr.ctrl_23,
  1552. udg->ctrl_buf,
  1553. udg->ctrl_23_sub3_off + 1);
  1554. if (retval < 0)
  1555. return retval;
  1556. udg->object_type_enable1 = udg->ctrl_buf[0];
  1557. if (udg->ctrl_23_sub3_off)
  1558. udg->object_type_enable2 = udg->ctrl_buf[udg->ctrl_23_sub3_off];
  1559. return retval;
  1560. }
  1561. static int udg_scan_pdt(void)
  1562. {
  1563. int retval;
  1564. unsigned char ii;
  1565. unsigned char page;
  1566. unsigned char intr_count = 0;
  1567. unsigned char intr_off;
  1568. unsigned char intr_src;
  1569. unsigned short addr;
  1570. struct synaptics_rmi4_fn_desc fd;
  1571. struct synaptics_rmi4_data *rmi4_data = udg->rmi4_data;
  1572. for (page = 0; page < PAGES_TO_SERVICE; page++) {
  1573. for (addr = PDT_START; addr > PDT_END; addr -= PDT_ENTRY_SIZE) {
  1574. addr |= (page << 8);
  1575. retval = synaptics_rmi4_reg_read(rmi4_data,
  1576. addr,
  1577. (unsigned char *)&fd,
  1578. sizeof(fd));
  1579. if (retval < 0)
  1580. return retval;
  1581. addr &= ~(MASK_8BIT << 8);
  1582. if (fd.fn_number) {
  1583. dev_dbg(rmi4_data->pdev->dev.parent,
  1584. "%s: Found F%02x\n",
  1585. __func__, fd.fn_number);
  1586. switch (fd.fn_number) {
  1587. case SYNAPTICS_RMI4_F12:
  1588. goto f12_found;
  1589. break;
  1590. }
  1591. } else {
  1592. break;
  1593. }
  1594. intr_count += fd.intr_src_count;
  1595. }
  1596. }
  1597. dev_err(rmi4_data->pdev->dev.parent,
  1598. "%s: Failed to find F12\n",
  1599. __func__);
  1600. return -EINVAL;
  1601. f12_found:
  1602. udg->query_base_addr = fd.query_base_addr | (page << 8);
  1603. udg->control_base_addr = fd.ctrl_base_addr | (page << 8);
  1604. udg->data_base_addr = fd.data_base_addr | (page << 8);
  1605. udg->command_base_addr = fd.cmd_base_addr | (page << 8);
  1606. retval = udg_reg_init();
  1607. if (retval < 0) {
  1608. dev_err(rmi4_data->pdev->dev.parent,
  1609. "%s: Failed to initialize user defined gesture registers\n",
  1610. __func__);
  1611. return retval;
  1612. }
  1613. udg->intr_mask = 0;
  1614. intr_src = fd.intr_src_count;
  1615. intr_off = intr_count % 8;
  1616. for (ii = intr_off;
  1617. ii < (intr_src + intr_off);
  1618. ii++) {
  1619. udg->intr_mask |= 1 << ii;
  1620. }
  1621. rmi4_data->intr_mask[0] |= udg->intr_mask;
  1622. addr = rmi4_data->f01_ctrl_base_addr + 1;
  1623. retval = synaptics_rmi4_reg_write(rmi4_data,
  1624. addr,
  1625. &rmi4_data->intr_mask[0],
  1626. sizeof(rmi4_data->intr_mask[0]));
  1627. if (retval < 0) {
  1628. dev_err(rmi4_data->pdev->dev.parent,
  1629. "%s: Failed to set interrupt enable bit\n",
  1630. __func__);
  1631. return retval;
  1632. }
  1633. return 0;
  1634. }
  1635. static void synaptics_rmi4_udg_attn(struct synaptics_rmi4_data *rmi4_data,
  1636. unsigned char intr_mask)
  1637. {
  1638. if (!udg)
  1639. return;
  1640. if (udg->intr_mask & intr_mask)
  1641. udg_report();
  1642. return;
  1643. }
  1644. static int synaptics_rmi4_udg_init(struct synaptics_rmi4_data *rmi4_data)
  1645. {
  1646. int retval;
  1647. unsigned char ii;
  1648. unsigned char size;
  1649. unsigned char attr_count;
  1650. unsigned char param_count;
  1651. if (udg) {
  1652. dev_dbg(rmi4_data->pdev->dev.parent,
  1653. "%s: Handle already exists\n",
  1654. __func__);
  1655. return 0;
  1656. }
  1657. udg = kzalloc(sizeof(*udg), GFP_KERNEL);
  1658. if (!udg) {
  1659. dev_err(rmi4_data->pdev->dev.parent,
  1660. "%s: Failed to alloc mem for udg\n",
  1661. __func__);
  1662. retval = -ENOMEM;
  1663. goto exit;
  1664. }
  1665. size = 0;
  1666. for (ii = 0; ii < sizeof(ctrl_18_sub_size); ii++)
  1667. size += ctrl_18_sub_size[ii];
  1668. size += sizeof(struct udg_tuning);
  1669. udg->ctrl_buf = kzalloc(size, GFP_KERNEL);
  1670. if (!udg->ctrl_buf) {
  1671. dev_err(rmi4_data->pdev->dev.parent,
  1672. "%s: Failed to alloc mem for ctrl_buf\n",
  1673. __func__);
  1674. retval = -ENOMEM;
  1675. goto exit_free_udg;
  1676. }
  1677. udg->rmi4_data = rmi4_data;
  1678. retval = udg_scan_pdt();
  1679. if (retval < 0)
  1680. goto exit_free_ctrl_buf;
  1681. udg->template_data_buf = kzalloc(udg->template_data_size, GFP_KERNEL);
  1682. if (!udg->template_data_buf) {
  1683. dev_err(rmi4_data->pdev->dev.parent,
  1684. "%s: Failed to alloc mem for template_data_buf\n",
  1685. __func__);
  1686. retval = -ENOMEM;
  1687. goto exit_free_ctrl_buf;
  1688. }
  1689. #ifdef STORE_GESTURES
  1690. udg->storage_buf = kzalloc(
  1691. udg->template_data_size * udg->gestures_to_store,
  1692. GFP_KERNEL);
  1693. if (!udg->storage_buf) {
  1694. dev_err(rmi4_data->pdev->dev.parent,
  1695. "%s: Failed to alloc mem for storage_buf\n",
  1696. __func__);
  1697. kfree(udg->template_data_buf);
  1698. retval = -ENOMEM;
  1699. goto exit_free_ctrl_buf;
  1700. }
  1701. #endif
  1702. udg->udg_dev = input_allocate_device();
  1703. if (udg->udg_dev == NULL) {
  1704. dev_err(rmi4_data->pdev->dev.parent,
  1705. "%s: Failed to allocate gesture device\n",
  1706. __func__);
  1707. retval = -ENOMEM;
  1708. goto exit_free_template_data_buf;
  1709. }
  1710. udg->udg_dev->name = GESTURE_DRIVER_NAME;
  1711. udg->udg_dev->phys = GESTURE_PHYS_NAME;
  1712. udg->udg_dev->id.product = SYNAPTICS_DSX_DRIVER_PRODUCT;
  1713. udg->udg_dev->id.version = SYNAPTICS_DSX_DRIVER_VERSION;
  1714. udg->udg_dev->dev.parent = rmi4_data->pdev->dev.parent;
  1715. input_set_drvdata(udg->udg_dev, rmi4_data);
  1716. set_bit(EV_KEY, udg->udg_dev->evbit);
  1717. set_bit(KEY_WAKEUP, udg->udg_dev->keybit);
  1718. input_set_capability(udg->udg_dev, EV_KEY, KEY_WAKEUP);
  1719. retval = input_register_device(udg->udg_dev);
  1720. if (retval) {
  1721. dev_err(rmi4_data->pdev->dev.parent,
  1722. "%s: Failed to register gesture device\n",
  1723. __func__);
  1724. input_free_device(udg->udg_dev);
  1725. goto exit_free_template_data_buf;
  1726. }
  1727. udg->tuning_dir = kobject_create_and_add(TUNING_SYSFS_DIR_NAME,
  1728. &udg->udg_dev->dev.kobj);
  1729. if (!udg->tuning_dir) {
  1730. dev_err(rmi4_data->pdev->dev.parent,
  1731. "%s: Failed to create tuning sysfs directory\n",
  1732. __func__);
  1733. goto exit_unregister_input_device;
  1734. }
  1735. retval = sysfs_create_bin_file(&udg->udg_dev->dev.kobj, &template_data);
  1736. if (retval < 0) {
  1737. dev_err(rmi4_data->pdev->dev.parent,
  1738. "%s: Failed to create template data bin file\n",
  1739. __func__);
  1740. goto exit_remove_sysfs_directory;
  1741. }
  1742. retval = sysfs_create_bin_file(&udg->udg_dev->dev.kobj, &trace_data);
  1743. if (retval < 0) {
  1744. dev_err(rmi4_data->pdev->dev.parent,
  1745. "%s: Failed to create trace data bin file\n",
  1746. __func__);
  1747. goto exit_remove_bin_file;
  1748. }
  1749. for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
  1750. retval = sysfs_create_file(&udg->udg_dev->dev.kobj,
  1751. &attrs[attr_count].attr);
  1752. if (retval < 0) {
  1753. dev_err(rmi4_data->pdev->dev.parent,
  1754. "%s: Failed to create sysfs attributes\n",
  1755. __func__);
  1756. retval = -ENODEV;
  1757. goto exit_remove_attrs;
  1758. }
  1759. }
  1760. for (param_count = 0; param_count < ARRAY_SIZE(params); param_count++) {
  1761. retval = sysfs_create_file(udg->tuning_dir,
  1762. &params[param_count].attr);
  1763. if (retval < 0) {
  1764. dev_err(rmi4_data->pdev->dev.parent,
  1765. "%s: Failed to create tuning parameters\n",
  1766. __func__);
  1767. retval = -ENODEV;
  1768. goto exit_remove_params;
  1769. }
  1770. }
  1771. retval = udg_engine_enable(true);
  1772. if (retval < 0) {
  1773. dev_err(rmi4_data->pdev->dev.parent,
  1774. "%s: Failed to enable gesture engine\n",
  1775. __func__);
  1776. goto exit_remove_params;
  1777. }
  1778. return 0;
  1779. exit_remove_params:
  1780. for (param_count--; param_count >= 0; param_count--) {
  1781. sysfs_remove_file(udg->tuning_dir,
  1782. &params[param_count].attr);
  1783. }
  1784. exit_remove_attrs:
  1785. for (attr_count--; attr_count >= 0; attr_count--) {
  1786. sysfs_remove_file(&udg->udg_dev->dev.kobj,
  1787. &attrs[attr_count].attr);
  1788. }
  1789. sysfs_remove_bin_file(&udg->udg_dev->dev.kobj, &trace_data);
  1790. exit_remove_bin_file:
  1791. sysfs_remove_bin_file(&udg->udg_dev->dev.kobj, &template_data);
  1792. exit_remove_sysfs_directory:
  1793. kobject_put(udg->tuning_dir);
  1794. exit_unregister_input_device:
  1795. input_unregister_device(udg->udg_dev);
  1796. exit_free_template_data_buf:
  1797. #ifdef STORE_GESTURES
  1798. kfree(udg->storage_buf);
  1799. #endif
  1800. kfree(udg->template_data_buf);
  1801. exit_free_ctrl_buf:
  1802. kfree(udg->ctrl_buf);
  1803. exit_free_udg:
  1804. kfree(udg);
  1805. udg = NULL;
  1806. exit:
  1807. return retval;
  1808. }
  1809. static void synaptics_rmi4_udg_remove(struct synaptics_rmi4_data *rmi4_data)
  1810. {
  1811. unsigned char count;
  1812. if (!udg)
  1813. goto exit;
  1814. for (count = 0; count < ARRAY_SIZE(params); count++) {
  1815. sysfs_remove_file(udg->tuning_dir,
  1816. &params[count].attr);
  1817. }
  1818. for (count = 0; count < ARRAY_SIZE(attrs); count++) {
  1819. sysfs_remove_file(&udg->udg_dev->dev.kobj,
  1820. &attrs[count].attr);
  1821. }
  1822. sysfs_remove_bin_file(&udg->udg_dev->dev.kobj, &trace_data);
  1823. sysfs_remove_bin_file(&udg->udg_dev->dev.kobj, &template_data);
  1824. kobject_put(udg->tuning_dir);
  1825. input_unregister_device(udg->udg_dev);
  1826. #ifdef STORE_GESTURES
  1827. kfree(udg->storage_buf);
  1828. #endif
  1829. kfree(udg->template_data_buf);
  1830. kfree(udg->trace_data_buf);
  1831. kfree(udg->ctrl_buf);
  1832. kfree(udg);
  1833. udg = NULL;
  1834. exit:
  1835. complete(&udg_remove_complete);
  1836. }
  1837. static void synaptics_rmi4_udg_reset(struct synaptics_rmi4_data *rmi4_data)
  1838. {
  1839. if (!udg) {
  1840. synaptics_rmi4_udg_init(rmi4_data);
  1841. return;
  1842. }
  1843. udg_scan_pdt();
  1844. udg_engine_enable(true);
  1845. #ifdef STORE_GESTURES
  1846. udg_write_template_data();
  1847. udg_write_valid_data();
  1848. #endif
  1849. }
  1850. static void synaptics_rmi4_udg_reinit(struct synaptics_rmi4_data *rmi4_data)
  1851. {
  1852. if (!udg)
  1853. return;
  1854. udg_engine_enable(true);
  1855. #ifdef STORE_GESTURES
  1856. udg_write_template_data();
  1857. udg_write_valid_data();
  1858. #endif
  1859. }
  1860. static void synaptics_rmi4_udg_e_suspend(struct synaptics_rmi4_data *rmi4_data)
  1861. {
  1862. if (!udg)
  1863. return;
  1864. rmi4_data->sleep_enable(rmi4_data, false);
  1865. rmi4_data->irq_enable(rmi4_data, true, false);
  1866. enable_irq_wake(rmi4_data->irq);
  1867. udg_engine_enable(true);
  1868. udg_detection_enable(true);
  1869. }
  1870. static void synaptics_rmi4_udg_suspend(struct synaptics_rmi4_data *rmi4_data)
  1871. {
  1872. if (!udg)
  1873. return;
  1874. rmi4_data->sleep_enable(rmi4_data, false);
  1875. rmi4_data->irq_enable(rmi4_data, true, false);
  1876. enable_irq_wake(rmi4_data->irq);
  1877. udg_engine_enable(true);
  1878. udg_detection_enable(true);
  1879. }
  1880. static void synaptics_rmi4_udg_resume(struct synaptics_rmi4_data *rmi4_data)
  1881. {
  1882. if (!udg)
  1883. return;
  1884. disable_irq_wake(rmi4_data->irq);
  1885. udg_detection_enable(false);
  1886. }
  1887. static void synaptics_rmi4_udg_l_resume(struct synaptics_rmi4_data *rmi4_data)
  1888. {
  1889. if (!udg)
  1890. return;
  1891. disable_irq_wake(rmi4_data->irq);
  1892. udg_detection_enable(false);
  1893. }
  1894. static struct synaptics_rmi4_exp_fn gesture_module = {
  1895. .fn_type = RMI_GESTURE,
  1896. .init = synaptics_rmi4_udg_init,
  1897. .remove = synaptics_rmi4_udg_remove,
  1898. .reset = synaptics_rmi4_udg_reset,
  1899. .reinit = synaptics_rmi4_udg_reinit,
  1900. .early_suspend = synaptics_rmi4_udg_e_suspend,
  1901. .suspend = synaptics_rmi4_udg_suspend,
  1902. .resume = synaptics_rmi4_udg_resume,
  1903. .late_resume = synaptics_rmi4_udg_l_resume,
  1904. .attn = synaptics_rmi4_udg_attn,
  1905. };
  1906. static int __init rmi4_gesture_module_init(void)
  1907. {
  1908. synaptics_rmi4_new_function(&gesture_module, true);
  1909. return 0;
  1910. }
  1911. static void __exit rmi4_gesture_module_exit(void)
  1912. {
  1913. synaptics_rmi4_new_function(&gesture_module, false);
  1914. wait_for_completion(&udg_remove_complete);
  1915. }
  1916. module_init(rmi4_gesture_module_init);
  1917. module_exit(rmi4_gesture_module_exit);
  1918. MODULE_AUTHOR("Synaptics, Inc.");
  1919. MODULE_DESCRIPTION("Synaptics DSX User Defined Gesture Module");
  1920. MODULE_LICENSE("GPL v2");