0004-ipts.patch 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180
  1. From a14ce1393617d53e7528910ce6f19ad48aec124f Mon Sep 17 00:00:00 2001
  2. From: Dorian Stoll <dorian.stoll@tmsp.io>
  3. Date: Thu, 30 Jul 2020 13:21:53 +0200
  4. Subject: [PATCH] misc: mei: Add missing IPTS device IDs
  5. Patchset: ipts
  6. ---
  7. drivers/misc/mei/hw-me-regs.h | 1 +
  8. drivers/misc/mei/pci-me.c | 1 +
  9. 2 files changed, 2 insertions(+)
  10. diff --git a/drivers/misc/mei/hw-me-regs.h b/drivers/misc/mei/hw-me-regs.h
  11. index bdc65d50b945..08723c01d727 100644
  12. --- a/drivers/misc/mei/hw-me-regs.h
  13. +++ b/drivers/misc/mei/hw-me-regs.h
  14. @@ -92,6 +92,7 @@
  15. #define MEI_DEV_ID_CDF 0x18D3 /* Cedar Fork */
  16. #define MEI_DEV_ID_ICP_LP 0x34E0 /* Ice Lake Point LP */
  17. +#define MEI_DEV_ID_ICP_LP_3 0x34E4 /* Ice Lake Point LP 3 (iTouch) */
  18. #define MEI_DEV_ID_ICP_N 0x38E0 /* Ice Lake Point N */
  19. #define MEI_DEV_ID_JSP_N 0x4DE0 /* Jasper Lake Point N */
  20. diff --git a/drivers/misc/mei/pci-me.c b/drivers/misc/mei/pci-me.c
  21. index 5bf0d50d55a0..c13864512229 100644
  22. --- a/drivers/misc/mei/pci-me.c
  23. +++ b/drivers/misc/mei/pci-me.c
  24. @@ -97,6 +97,7 @@ static const struct pci_device_id mei_me_pci_tbl[] = {
  25. {MEI_PCI_DEVICE(MEI_DEV_ID_CMP_H_3, MEI_ME_PCH8_ITOUCH_CFG)},
  26. {MEI_PCI_DEVICE(MEI_DEV_ID_ICP_LP, MEI_ME_PCH12_CFG)},
  27. + {MEI_PCI_DEVICE(MEI_DEV_ID_ICP_LP_3, MEI_ME_PCH12_CFG)},
  28. {MEI_PCI_DEVICE(MEI_DEV_ID_ICP_N, MEI_ME_PCH12_CFG)},
  29. {MEI_PCI_DEVICE(MEI_DEV_ID_TGP_LP, MEI_ME_PCH15_CFG)},
  30. --
  31. 2.40.1
  32. From b9c4d883ec315c9653410873df44222756bf3811 Mon Sep 17 00:00:00 2001
  33. From: Liban Hannan <liban.p@gmail.com>
  34. Date: Tue, 12 Apr 2022 23:31:12 +0100
  35. Subject: [PATCH] iommu: ipts: use IOMMU passthrough mode for IPTS
  36. Adds a quirk so that IOMMU uses passthrough mode for the IPTS device.
  37. Otherwise, when IOMMU is enabled, IPTS produces DMAR errors like:
  38. DMAR: [DMA Read NO_PASID] Request device [00:16.4] fault addr
  39. 0x104ea3000 [fault reason 0x06] PTE Read access is not set
  40. This is very similar to the bug described at:
  41. https://bugs.launchpad.net/bugs/1958004
  42. Fixed with the following patch which this patch basically copies:
  43. https://launchpadlibrarian.net/586396847/43255ca.diff
  44. Patchset: ipts
  45. ---
  46. drivers/iommu/intel/iommu.c | 24 ++++++++++++++++++++++++
  47. 1 file changed, 24 insertions(+)
  48. diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
  49. index 7c2f4bd33582..3ebd2260cdab 100644
  50. --- a/drivers/iommu/intel/iommu.c
  51. +++ b/drivers/iommu/intel/iommu.c
  52. @@ -37,6 +37,8 @@
  53. #define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
  54. #define IS_USB_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_SERIAL_USB)
  55. #define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
  56. +#define IS_IPTS(pdev) ((pdev)->vendor == PCI_VENDOR_ID_INTEL && \
  57. + ((pdev)->device == 0x9d3e))
  58. #define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
  59. #define IOAPIC_RANGE_START (0xfee00000)
  60. @@ -287,12 +289,14 @@ int intel_iommu_enabled = 0;
  61. EXPORT_SYMBOL_GPL(intel_iommu_enabled);
  62. static int dmar_map_gfx = 1;
  63. +static int dmar_map_ipts = 1;
  64. static int intel_iommu_superpage = 1;
  65. static int iommu_identity_mapping;
  66. static int iommu_skip_te_disable;
  67. #define IDENTMAP_GFX 2
  68. #define IDENTMAP_AZALIA 4
  69. +#define IDENTMAP_IPTS 16
  70. const struct iommu_ops intel_iommu_ops;
  71. @@ -2588,6 +2592,9 @@ static int device_def_domain_type(struct device *dev)
  72. if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
  73. return IOMMU_DOMAIN_IDENTITY;
  74. +
  75. + if ((iommu_identity_mapping & IDENTMAP_IPTS) && IS_IPTS(pdev))
  76. + return IOMMU_DOMAIN_IDENTITY;
  77. }
  78. return 0;
  79. @@ -2977,6 +2984,9 @@ static int __init init_dmars(void)
  80. if (!dmar_map_gfx)
  81. iommu_identity_mapping |= IDENTMAP_GFX;
  82. + if (!dmar_map_ipts)
  83. + iommu_identity_mapping |= IDENTMAP_IPTS;
  84. +
  85. check_tylersburg_isoch();
  86. ret = si_domain_init(hw_pass_through);
  87. @@ -4819,6 +4829,17 @@ static void quirk_iommu_igfx(struct pci_dev *dev)
  88. dmar_map_gfx = 0;
  89. }
  90. +static void quirk_iommu_ipts(struct pci_dev *dev)
  91. +{
  92. + if (!IS_IPTS(dev))
  93. + return;
  94. +
  95. + if (risky_device(dev))
  96. + return;
  97. +
  98. + pci_info(dev, "Passthrough IOMMU for IPTS\n");
  99. + dmar_map_ipts = 0;
  100. +}
  101. /* G4x/GM45 integrated gfx dmar support is totally busted. */
  102. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_igfx);
  103. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_igfx);
  104. @@ -4854,6 +4875,9 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x1632, quirk_iommu_igfx);
  105. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x163A, quirk_iommu_igfx);
  106. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x163D, quirk_iommu_igfx);
  107. +/* disable IPTS dmar support */
  108. +DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x9D3E, quirk_iommu_ipts);
  109. +
  110. static void quirk_iommu_rwbf(struct pci_dev *dev)
  111. {
  112. if (risky_device(dev))
  113. --
  114. 2.40.1
  115. From 7e7f475cb844ac46bb993d667eb2f6e6b0df6521 Mon Sep 17 00:00:00 2001
  116. From: Dorian Stoll <dorian.stoll@tmsp.io>
  117. Date: Sun, 11 Dec 2022 12:00:59 +0100
  118. Subject: [PATCH] hid: Add support for Intel Precise Touch and Stylus
  119. Based on linux-surface/intel-precise-touch@8abe268
  120. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  121. Patchset: ipts
  122. ---
  123. drivers/hid/Kconfig | 2 +
  124. drivers/hid/Makefile | 2 +
  125. drivers/hid/ipts/Kconfig | 14 +
  126. drivers/hid/ipts/Makefile | 14 +
  127. drivers/hid/ipts/cmd.c | 62 +++++
  128. drivers/hid/ipts/cmd.h | 61 ++++
  129. drivers/hid/ipts/context.h | 51 ++++
  130. drivers/hid/ipts/control.c | 495 +++++++++++++++++++++++++++++++++
  131. drivers/hid/ipts/control.h | 127 +++++++++
  132. drivers/hid/ipts/desc.h | 81 ++++++
  133. drivers/hid/ipts/hid.c | 348 +++++++++++++++++++++++
  134. drivers/hid/ipts/hid.h | 22 ++
  135. drivers/hid/ipts/main.c | 127 +++++++++
  136. drivers/hid/ipts/mei.c | 189 +++++++++++++
  137. drivers/hid/ipts/mei.h | 67 +++++
  138. drivers/hid/ipts/receiver.c | 249 +++++++++++++++++
  139. drivers/hid/ipts/receiver.h | 17 ++
  140. drivers/hid/ipts/resources.c | 108 +++++++
  141. drivers/hid/ipts/resources.h | 39 +++
  142. drivers/hid/ipts/spec-data.h | 100 +++++++
  143. drivers/hid/ipts/spec-device.h | 285 +++++++++++++++++++
  144. drivers/hid/ipts/spec-hid.h | 35 +++
  145. drivers/hid/ipts/thread.c | 85 ++++++
  146. drivers/hid/ipts/thread.h | 60 ++++
  147. 24 files changed, 2640 insertions(+)
  148. create mode 100644 drivers/hid/ipts/Kconfig
  149. create mode 100644 drivers/hid/ipts/Makefile
  150. create mode 100644 drivers/hid/ipts/cmd.c
  151. create mode 100644 drivers/hid/ipts/cmd.h
  152. create mode 100644 drivers/hid/ipts/context.h
  153. create mode 100644 drivers/hid/ipts/control.c
  154. create mode 100644 drivers/hid/ipts/control.h
  155. create mode 100644 drivers/hid/ipts/desc.h
  156. create mode 100644 drivers/hid/ipts/hid.c
  157. create mode 100644 drivers/hid/ipts/hid.h
  158. create mode 100644 drivers/hid/ipts/main.c
  159. create mode 100644 drivers/hid/ipts/mei.c
  160. create mode 100644 drivers/hid/ipts/mei.h
  161. create mode 100644 drivers/hid/ipts/receiver.c
  162. create mode 100644 drivers/hid/ipts/receiver.h
  163. create mode 100644 drivers/hid/ipts/resources.c
  164. create mode 100644 drivers/hid/ipts/resources.h
  165. create mode 100644 drivers/hid/ipts/spec-data.h
  166. create mode 100644 drivers/hid/ipts/spec-device.h
  167. create mode 100644 drivers/hid/ipts/spec-hid.h
  168. create mode 100644 drivers/hid/ipts/thread.c
  169. create mode 100644 drivers/hid/ipts/thread.h
  170. diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
  171. index 4ce012f83253..7945cb57f531 100644
  172. --- a/drivers/hid/Kconfig
  173. +++ b/drivers/hid/Kconfig
  174. @@ -1316,4 +1316,6 @@ source "drivers/hid/amd-sfh-hid/Kconfig"
  175. source "drivers/hid/surface-hid/Kconfig"
  176. +source "drivers/hid/ipts/Kconfig"
  177. +
  178. endif # HID_SUPPORT
  179. diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
  180. index 5d37cacbde33..285e12d95b0e 100644
  181. --- a/drivers/hid/Makefile
  182. +++ b/drivers/hid/Makefile
  183. @@ -167,3 +167,5 @@ obj-$(INTEL_ISH_FIRMWARE_DOWNLOADER) += intel-ish-hid/
  184. obj-$(CONFIG_AMD_SFH_HID) += amd-sfh-hid/
  185. obj-$(CONFIG_SURFACE_HID_CORE) += surface-hid/
  186. +
  187. +obj-$(CONFIG_HID_IPTS) += ipts/
  188. diff --git a/drivers/hid/ipts/Kconfig b/drivers/hid/ipts/Kconfig
  189. new file mode 100644
  190. index 000000000000..297401bd388d
  191. --- /dev/null
  192. +++ b/drivers/hid/ipts/Kconfig
  193. @@ -0,0 +1,14 @@
  194. +# SPDX-License-Identifier: GPL-2.0-or-later
  195. +
  196. +config HID_IPTS
  197. + tristate "Intel Precise Touch & Stylus"
  198. + depends on INTEL_MEI
  199. + depends on HID
  200. + help
  201. + Say Y here if your system has a touchscreen using Intels
  202. + Precise Touch & Stylus (IPTS) technology.
  203. +
  204. + If unsure say N.
  205. +
  206. + To compile this driver as a module, choose M here: the
  207. + module will be called ipts.
  208. diff --git a/drivers/hid/ipts/Makefile b/drivers/hid/ipts/Makefile
  209. new file mode 100644
  210. index 000000000000..0fe655bccdc0
  211. --- /dev/null
  212. +++ b/drivers/hid/ipts/Makefile
  213. @@ -0,0 +1,14 @@
  214. +# SPDX-License-Identifier: GPL-2.0-or-later
  215. +#
  216. +# Makefile for the IPTS touchscreen driver
  217. +#
  218. +
  219. +obj-$(CONFIG_HID_IPTS) += ipts.o
  220. +ipts-objs := cmd.o
  221. +ipts-objs += control.o
  222. +ipts-objs += hid.o
  223. +ipts-objs += main.o
  224. +ipts-objs += mei.o
  225. +ipts-objs += receiver.o
  226. +ipts-objs += resources.o
  227. +ipts-objs += thread.o
  228. diff --git a/drivers/hid/ipts/cmd.c b/drivers/hid/ipts/cmd.c
  229. new file mode 100644
  230. index 000000000000..7fd69271ccd5
  231. --- /dev/null
  232. +++ b/drivers/hid/ipts/cmd.c
  233. @@ -0,0 +1,62 @@
  234. +// SPDX-License-Identifier: GPL-2.0-or-later
  235. +/*
  236. + * Copyright (c) 2016 Intel Corporation
  237. + * Copyright (c) 2020-2023 Dorian Stoll
  238. + *
  239. + * Linux driver for Intel Precise Touch & Stylus
  240. + */
  241. +
  242. +#include <linux/errno.h>
  243. +#include <linux/types.h>
  244. +
  245. +#include "cmd.h"
  246. +#include "context.h"
  247. +#include "mei.h"
  248. +#include "spec-device.h"
  249. +
  250. +int ipts_cmd_recv_timeout(struct ipts_context *ipts, enum ipts_command_code code,
  251. + struct ipts_response *rsp, u64 timeout)
  252. +{
  253. + int ret = 0;
  254. +
  255. + if (!ipts)
  256. + return -EFAULT;
  257. +
  258. + if (!rsp)
  259. + return -EFAULT;
  260. +
  261. + /*
  262. + * In a response, the command code will have the most significant bit flipped to 1.
  263. + * If code is passed to ipts_mei_recv as is, no messages will be received.
  264. + */
  265. + ret = ipts_mei_recv(&ipts->mei, code | IPTS_RSP_BIT, rsp, timeout);
  266. + if (ret < 0)
  267. + return ret;
  268. +
  269. + dev_dbg(ipts->dev, "Received 0x%02X with status 0x%02X\n", code, rsp->status);
  270. +
  271. + /*
  272. + * Some devices will always return this error.
  273. + * It is allowed to ignore it and to try continuing.
  274. + */
  275. + if (rsp->status == IPTS_STATUS_COMPAT_CHECK_FAIL)
  276. + rsp->status = IPTS_STATUS_SUCCESS;
  277. +
  278. + return 0;
  279. +}
  280. +
  281. +int ipts_cmd_send(struct ipts_context *ipts, enum ipts_command_code code, void *data, size_t size)
  282. +{
  283. + struct ipts_command cmd = { 0 };
  284. +
  285. + if (!ipts)
  286. + return -EFAULT;
  287. +
  288. + cmd.cmd = code;
  289. +
  290. + if (data && size > 0)
  291. + memcpy(cmd.payload, data, size);
  292. +
  293. + dev_dbg(ipts->dev, "Sending 0x%02X with %ld bytes payload\n", code, size);
  294. + return ipts_mei_send(&ipts->mei, &cmd, sizeof(cmd.cmd) + size);
  295. +}
  296. diff --git a/drivers/hid/ipts/cmd.h b/drivers/hid/ipts/cmd.h
  297. new file mode 100644
  298. index 000000000000..924758ffee67
  299. --- /dev/null
  300. +++ b/drivers/hid/ipts/cmd.h
  301. @@ -0,0 +1,61 @@
  302. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  303. +/*
  304. + * Copyright (c) 2016 Intel Corporation
  305. + * Copyright (c) 2020-2023 Dorian Stoll
  306. + *
  307. + * Linux driver for Intel Precise Touch & Stylus
  308. + */
  309. +
  310. +#ifndef IPTS_CMD_H
  311. +#define IPTS_CMD_H
  312. +
  313. +#include <linux/types.h>
  314. +
  315. +#include "context.h"
  316. +#include "spec-device.h"
  317. +
  318. +/*
  319. + * The default timeout for receiving responses
  320. + */
  321. +#define IPTS_CMD_DEFAULT_TIMEOUT 1000
  322. +
  323. +/*
  324. + * ipts_cmd_recv_timeout() - Receives a response to a command.
  325. + * @ipts: The IPTS driver context.
  326. + * @code: The type of the command / response.
  327. + * @rsp: The address that the received response will be copied to.
  328. + * @timeout: How many milliseconds the function will wait at most.
  329. + *
  330. + * A negative timeout means to wait forever.
  331. + *
  332. + * Returns: 0 on success, <0 on error, -EAGAIN if no response has been received.
  333. + */
  334. +int ipts_cmd_recv_timeout(struct ipts_context *ipts, enum ipts_command_code code,
  335. + struct ipts_response *rsp, u64 timeout);
  336. +
  337. +/*
  338. + * ipts_cmd_recv() - Receives a response to a command.
  339. + * @ipts: The IPTS driver context.
  340. + * @code: The type of the command / response.
  341. + * @rsp: The address that the received response will be copied to.
  342. + *
  343. + * Returns: 0 on success, <0 on error, -EAGAIN if no response has been received.
  344. + */
  345. +static inline int ipts_cmd_recv(struct ipts_context *ipts, enum ipts_command_code code,
  346. + struct ipts_response *rsp)
  347. +{
  348. + return ipts_cmd_recv_timeout(ipts, code, rsp, IPTS_CMD_DEFAULT_TIMEOUT);
  349. +}
  350. +
  351. +/*
  352. + * ipts_cmd_send() - Executes a command on the device.
  353. + * @ipts: The IPTS driver context.
  354. + * @code: The type of the command to execute.
  355. + * @data: The payload containing parameters for the command.
  356. + * @size: The size of the payload.
  357. + *
  358. + * Returns: 0 on success, <0 on error.
  359. + */
  360. +int ipts_cmd_send(struct ipts_context *ipts, enum ipts_command_code code, void *data, size_t size);
  361. +
  362. +#endif /* IPTS_CMD_H */
  363. diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h
  364. new file mode 100644
  365. index 000000000000..3450a95e66ee
  366. --- /dev/null
  367. +++ b/drivers/hid/ipts/context.h
  368. @@ -0,0 +1,51 @@
  369. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  370. +/*
  371. + * Copyright (c) 2016 Intel Corporation
  372. + * Copyright (c) 2020-2023 Dorian Stoll
  373. + *
  374. + * Linux driver for Intel Precise Touch & Stylus
  375. + */
  376. +
  377. +#ifndef IPTS_CONTEXT_H
  378. +#define IPTS_CONTEXT_H
  379. +
  380. +#include <linux/completion.h>
  381. +#include <linux/device.h>
  382. +#include <linux/hid.h>
  383. +#include <linux/mei_cl_bus.h>
  384. +#include <linux/mutex.h>
  385. +#include <linux/sched.h>
  386. +#include <linux/types.h>
  387. +
  388. +#include "mei.h"
  389. +#include "resources.h"
  390. +#include "spec-device.h"
  391. +#include "thread.h"
  392. +
  393. +struct ipts_context {
  394. + struct device *dev;
  395. + struct ipts_mei mei;
  396. +
  397. + enum ipts_mode mode;
  398. +
  399. + /*
  400. + * Prevents concurrent GET_FEATURE reports.
  401. + */
  402. + struct mutex feature_lock;
  403. + struct completion feature_event;
  404. +
  405. + /*
  406. + * These are not inside of struct ipts_resources
  407. + * because they don't own the memory they point to.
  408. + */
  409. + struct ipts_buffer feature_report;
  410. + struct ipts_buffer descriptor;
  411. +
  412. + struct hid_device *hid;
  413. + struct ipts_device_info info;
  414. + struct ipts_resources resources;
  415. +
  416. + struct ipts_thread receiver_loop;
  417. +};
  418. +
  419. +#endif /* IPTS_CONTEXT_H */
  420. diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c
  421. new file mode 100644
  422. index 000000000000..2f61500b5119
  423. --- /dev/null
  424. +++ b/drivers/hid/ipts/control.c
  425. @@ -0,0 +1,495 @@
  426. +// SPDX-License-Identifier: GPL-2.0-or-later
  427. +/*
  428. + * Copyright (c) 2016 Intel Corporation
  429. + * Copyright (c) 2020-2023 Dorian Stoll
  430. + *
  431. + * Linux driver for Intel Precise Touch & Stylus
  432. + */
  433. +
  434. +#include <linux/delay.h>
  435. +#include <linux/dev_printk.h>
  436. +#include <linux/errno.h>
  437. +#include <linux/kernel.h>
  438. +#include <linux/kthread.h>
  439. +#include <linux/types.h>
  440. +
  441. +#include "cmd.h"
  442. +#include "context.h"
  443. +#include "control.h"
  444. +#include "desc.h"
  445. +#include "hid.h"
  446. +#include "receiver.h"
  447. +#include "resources.h"
  448. +#include "spec-data.h"
  449. +#include "spec-device.h"
  450. +
  451. +static int ipts_control_get_device_info(struct ipts_context *ipts, struct ipts_device_info *info)
  452. +{
  453. + int ret = 0;
  454. + struct ipts_response rsp = { 0 };
  455. +
  456. + if (!ipts)
  457. + return -EFAULT;
  458. +
  459. + if (!info)
  460. + return -EFAULT;
  461. +
  462. + ret = ipts_cmd_send(ipts, IPTS_CMD_GET_DEVICE_INFO, NULL, 0);
  463. + if (ret) {
  464. + dev_err(ipts->dev, "GET_DEVICE_INFO: send failed: %d\n", ret);
  465. + return ret;
  466. + }
  467. +
  468. + ret = ipts_cmd_recv(ipts, IPTS_CMD_GET_DEVICE_INFO, &rsp);
  469. + if (ret) {
  470. + dev_err(ipts->dev, "GET_DEVICE_INFO: recv failed: %d\n", ret);
  471. + return ret;
  472. + }
  473. +
  474. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  475. + dev_err(ipts->dev, "GET_DEVICE_INFO: cmd failed: %d\n", rsp.status);
  476. + return -EBADR;
  477. + }
  478. +
  479. + memcpy(info, rsp.payload, sizeof(*info));
  480. + return 0;
  481. +}
  482. +
  483. +static int ipts_control_set_mode(struct ipts_context *ipts, enum ipts_mode mode)
  484. +{
  485. + int ret = 0;
  486. + struct ipts_set_mode cmd = { 0 };
  487. + struct ipts_response rsp = { 0 };
  488. +
  489. + if (!ipts)
  490. + return -EFAULT;
  491. +
  492. + cmd.mode = mode;
  493. +
  494. + ret = ipts_cmd_send(ipts, IPTS_CMD_SET_MODE, &cmd, sizeof(cmd));
  495. + if (ret) {
  496. + dev_err(ipts->dev, "SET_MODE: send failed: %d\n", ret);
  497. + return ret;
  498. + }
  499. +
  500. + ret = ipts_cmd_recv(ipts, IPTS_CMD_SET_MODE, &rsp);
  501. + if (ret) {
  502. + dev_err(ipts->dev, "SET_MODE: recv failed: %d\n", ret);
  503. + return ret;
  504. + }
  505. +
  506. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  507. + dev_err(ipts->dev, "SET_MODE: cmd failed: %d\n", rsp.status);
  508. + return -EBADR;
  509. + }
  510. +
  511. + return 0;
  512. +}
  513. +
  514. +static int ipts_control_set_mem_window(struct ipts_context *ipts, struct ipts_resources *res)
  515. +{
  516. + int ret = 0;
  517. + struct ipts_mem_window cmd = { 0 };
  518. + struct ipts_response rsp = { 0 };
  519. +
  520. + if (!ipts)
  521. + return -EFAULT;
  522. +
  523. + if (!res)
  524. + return -EFAULT;
  525. +
  526. + for (int i = 0; i < IPTS_BUFFERS; i++) {
  527. + cmd.data_addr_lower[i] = lower_32_bits(res->data[i].dma_address);
  528. + cmd.data_addr_upper[i] = upper_32_bits(res->data[i].dma_address);
  529. + cmd.feedback_addr_lower[i] = lower_32_bits(res->feedback[i].dma_address);
  530. + cmd.feedback_addr_upper[i] = upper_32_bits(res->feedback[i].dma_address);
  531. + }
  532. +
  533. + cmd.workqueue_addr_lower = lower_32_bits(res->workqueue.dma_address);
  534. + cmd.workqueue_addr_upper = upper_32_bits(res->workqueue.dma_address);
  535. +
  536. + cmd.doorbell_addr_lower = lower_32_bits(res->doorbell.dma_address);
  537. + cmd.doorbell_addr_upper = upper_32_bits(res->doorbell.dma_address);
  538. +
  539. + cmd.hid2me_addr_lower = lower_32_bits(res->hid2me.dma_address);
  540. + cmd.hid2me_addr_upper = upper_32_bits(res->hid2me.dma_address);
  541. +
  542. + cmd.workqueue_size = IPTS_WORKQUEUE_SIZE;
  543. + cmd.workqueue_item_size = IPTS_WORKQUEUE_ITEM_SIZE;
  544. +
  545. + ret = ipts_cmd_send(ipts, IPTS_CMD_SET_MEM_WINDOW, &cmd, sizeof(cmd));
  546. + if (ret) {
  547. + dev_err(ipts->dev, "SET_MEM_WINDOW: send failed: %d\n", ret);
  548. + return ret;
  549. + }
  550. +
  551. + ret = ipts_cmd_recv(ipts, IPTS_CMD_SET_MEM_WINDOW, &rsp);
  552. + if (ret) {
  553. + dev_err(ipts->dev, "SET_MEM_WINDOW: recv failed: %d\n", ret);
  554. + return ret;
  555. + }
  556. +
  557. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  558. + dev_err(ipts->dev, "SET_MEM_WINDOW: cmd failed: %d\n", rsp.status);
  559. + return -EBADR;
  560. + }
  561. +
  562. + return 0;
  563. +}
  564. +
  565. +static int ipts_control_get_descriptor(struct ipts_context *ipts)
  566. +{
  567. + int ret = 0;
  568. + struct ipts_data_header *header = NULL;
  569. + struct ipts_get_descriptor cmd = { 0 };
  570. + struct ipts_response rsp = { 0 };
  571. +
  572. + if (!ipts)
  573. + return -EFAULT;
  574. +
  575. + if (!ipts->resources.descriptor.address)
  576. + return -EFAULT;
  577. +
  578. + memset(ipts->resources.descriptor.address, 0, ipts->resources.descriptor.size);
  579. +
  580. + cmd.addr_lower = lower_32_bits(ipts->resources.descriptor.dma_address);
  581. + cmd.addr_upper = upper_32_bits(ipts->resources.descriptor.dma_address);
  582. + cmd.magic = 8;
  583. +
  584. + ret = ipts_cmd_send(ipts, IPTS_CMD_GET_DESCRIPTOR, &cmd, sizeof(cmd));
  585. + if (ret) {
  586. + dev_err(ipts->dev, "GET_DESCRIPTOR: send failed: %d\n", ret);
  587. + return ret;
  588. + }
  589. +
  590. + ret = ipts_cmd_recv(ipts, IPTS_CMD_GET_DESCRIPTOR, &rsp);
  591. + if (ret) {
  592. + dev_err(ipts->dev, "GET_DESCRIPTOR: recv failed: %d\n", ret);
  593. + return ret;
  594. + }
  595. +
  596. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  597. + dev_err(ipts->dev, "GET_DESCRIPTOR: cmd failed: %d\n", rsp.status);
  598. + return -EBADR;
  599. + }
  600. +
  601. + header = (struct ipts_data_header *)ipts->resources.descriptor.address;
  602. +
  603. + if (header->type == IPTS_DATA_TYPE_DESCRIPTOR) {
  604. + ipts->descriptor.address = &header->data[8];
  605. + ipts->descriptor.size = header->size - 8;
  606. +
  607. + return 0;
  608. + }
  609. +
  610. + return -ENODATA;
  611. +}
  612. +
  613. +int ipts_control_request_flush(struct ipts_context *ipts)
  614. +{
  615. + int ret = 0;
  616. + struct ipts_quiesce_io cmd = { 0 };
  617. +
  618. + if (!ipts)
  619. + return -EFAULT;
  620. +
  621. + ret = ipts_cmd_send(ipts, IPTS_CMD_QUIESCE_IO, &cmd, sizeof(cmd));
  622. + if (ret)
  623. + dev_err(ipts->dev, "QUIESCE_IO: send failed: %d\n", ret);
  624. +
  625. + return ret;
  626. +}
  627. +
  628. +int ipts_control_wait_flush(struct ipts_context *ipts)
  629. +{
  630. + int ret = 0;
  631. + struct ipts_response rsp = { 0 };
  632. +
  633. + if (!ipts)
  634. + return -EFAULT;
  635. +
  636. + ret = ipts_cmd_recv(ipts, IPTS_CMD_QUIESCE_IO, &rsp);
  637. + if (ret) {
  638. + dev_err(ipts->dev, "QUIESCE_IO: recv failed: %d\n", ret);
  639. + return ret;
  640. + }
  641. +
  642. + if (rsp.status == IPTS_STATUS_TIMEOUT)
  643. + return -EAGAIN;
  644. +
  645. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  646. + dev_err(ipts->dev, "QUIESCE_IO: cmd failed: %d\n", rsp.status);
  647. + return -EBADR;
  648. + }
  649. +
  650. + return 0;
  651. +}
  652. +
  653. +int ipts_control_request_data(struct ipts_context *ipts)
  654. +{
  655. + int ret = 0;
  656. +
  657. + if (!ipts)
  658. + return -EFAULT;
  659. +
  660. + ret = ipts_cmd_send(ipts, IPTS_CMD_READY_FOR_DATA, NULL, 0);
  661. + if (ret)
  662. + dev_err(ipts->dev, "READY_FOR_DATA: send failed: %d\n", ret);
  663. +
  664. + return ret;
  665. +}
  666. +
  667. +int ipts_control_wait_data(struct ipts_context *ipts, bool shutdown)
  668. +{
  669. + int ret = 0;
  670. + struct ipts_response rsp = { 0 };
  671. +
  672. + if (!ipts)
  673. + return -EFAULT;
  674. +
  675. + if (!shutdown)
  676. + ret = ipts_cmd_recv_timeout(ipts, IPTS_CMD_READY_FOR_DATA, &rsp, 0);
  677. + else
  678. + ret = ipts_cmd_recv(ipts, IPTS_CMD_READY_FOR_DATA, &rsp);
  679. +
  680. + if (ret) {
  681. + if (ret != -EAGAIN)
  682. + dev_err(ipts->dev, "READY_FOR_DATA: recv failed: %d\n", ret);
  683. +
  684. + return ret;
  685. + }
  686. +
  687. + /*
  688. + * During shutdown, it is possible that the sensor has already been disabled.
  689. + */
  690. + if (rsp.status == IPTS_STATUS_SENSOR_DISABLED)
  691. + return 0;
  692. +
  693. + if (rsp.status == IPTS_STATUS_TIMEOUT)
  694. + return -EAGAIN;
  695. +
  696. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  697. + dev_err(ipts->dev, "READY_FOR_DATA: cmd failed: %d\n", rsp.status);
  698. + return -EBADR;
  699. + }
  700. +
  701. + return 0;
  702. +}
  703. +
  704. +int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer)
  705. +{
  706. + int ret = 0;
  707. + struct ipts_feedback cmd = { 0 };
  708. + struct ipts_response rsp = { 0 };
  709. +
  710. + if (!ipts)
  711. + return -EFAULT;
  712. +
  713. + cmd.buffer = buffer;
  714. +
  715. + ret = ipts_cmd_send(ipts, IPTS_CMD_FEEDBACK, &cmd, sizeof(cmd));
  716. + if (ret) {
  717. + dev_err(ipts->dev, "FEEDBACK: send failed: %d\n", ret);
  718. + return ret;
  719. + }
  720. +
  721. + ret = ipts_cmd_recv(ipts, IPTS_CMD_FEEDBACK, &rsp);
  722. + if (ret) {
  723. + dev_err(ipts->dev, "FEEDBACK: recv failed: %d\n", ret);
  724. + return ret;
  725. + }
  726. +
  727. + /*
  728. + * We don't know what feedback data looks like so we are sending zeros.
  729. + * See also ipts_control_refill_buffer.
  730. + */
  731. + if (rsp.status == IPTS_STATUS_INVALID_PARAMS)
  732. + return 0;
  733. +
  734. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  735. + dev_err(ipts->dev, "FEEDBACK: cmd failed: %d\n", rsp.status);
  736. + return -EBADR;
  737. + }
  738. +
  739. + return 0;
  740. +}
  741. +
  742. +int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_cmd_type cmd,
  743. + enum ipts_feedback_data_type type, void *data, size_t size)
  744. +{
  745. + struct ipts_feedback_header *header = NULL;
  746. +
  747. + if (!ipts)
  748. + return -EFAULT;
  749. +
  750. + if (!ipts->resources.hid2me.address)
  751. + return -EFAULT;
  752. +
  753. + memset(ipts->resources.hid2me.address, 0, ipts->resources.hid2me.size);
  754. + header = (struct ipts_feedback_header *)ipts->resources.hid2me.address;
  755. +
  756. + header->cmd_type = cmd;
  757. + header->data_type = type;
  758. + header->size = size;
  759. + header->buffer = IPTS_HID2ME_BUFFER;
  760. +
  761. + if (size + sizeof(*header) > ipts->resources.hid2me.size)
  762. + return -EINVAL;
  763. +
  764. + if (data && size > 0)
  765. + memcpy(header->payload, data, size);
  766. +
  767. + return ipts_control_send_feedback(ipts, IPTS_HID2ME_BUFFER);
  768. +}
  769. +
  770. +static inline int ipts_control_reset_sensor(struct ipts_context *ipts)
  771. +{
  772. + return ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_SOFT_RESET,
  773. + IPTS_FEEDBACK_DATA_TYPE_VENDOR, NULL, 0);
  774. +}
  775. +
  776. +int ipts_control_start(struct ipts_context *ipts)
  777. +{
  778. + int ret = 0;
  779. + struct ipts_device_info info = { 0 };
  780. +
  781. + if (!ipts)
  782. + return -EFAULT;
  783. +
  784. + dev_info(ipts->dev, "Starting IPTS\n");
  785. +
  786. + ret = ipts_control_get_device_info(ipts, &info);
  787. + if (ret) {
  788. + dev_err(ipts->dev, "Failed to get device info: %d\n", ret);
  789. + return ret;
  790. + }
  791. +
  792. + ipts->info = info;
  793. +
  794. + ret = ipts_resources_init(&ipts->resources, ipts->dev, info.data_size, info.feedback_size);
  795. + if (ret) {
  796. + dev_err(ipts->dev, "Failed to allocate buffers: %d", ret);
  797. + return ret;
  798. + }
  799. +
  800. + dev_info(ipts->dev, "IPTS EDS Version: %d\n", info.intf_eds);
  801. +
  802. + /*
  803. + * Handle newer devices
  804. + */
  805. + if (info.intf_eds > 1) {
  806. + /*
  807. + * Fetching the descriptor will only work on newer devices.
  808. + * For older devices, a fallback descriptor will be used.
  809. + */
  810. + ret = ipts_control_get_descriptor(ipts);
  811. + if (ret) {
  812. + dev_err(ipts->dev, "Failed to fetch HID descriptor: %d\n", ret);
  813. + return ret;
  814. + }
  815. +
  816. + /*
  817. + * Newer devices can be directly initialized in doorbell mode.
  818. + */
  819. + ipts->mode = IPTS_MODE_DOORBELL;
  820. + }
  821. +
  822. + ret = ipts_control_set_mode(ipts, ipts->mode);
  823. + if (ret) {
  824. + dev_err(ipts->dev, "Failed to set mode: %d\n", ret);
  825. + return ret;
  826. + }
  827. +
  828. + ret = ipts_control_set_mem_window(ipts, &ipts->resources);
  829. + if (ret) {
  830. + dev_err(ipts->dev, "Failed to set memory window: %d\n", ret);
  831. + return ret;
  832. + }
  833. +
  834. + ret = ipts_receiver_start(ipts);
  835. + if (ret) {
  836. + dev_err(ipts->dev, "Failed to start receiver: %d\n", ret);
  837. + return ret;
  838. + }
  839. +
  840. + ret = ipts_control_request_data(ipts);
  841. + if (ret) {
  842. + dev_err(ipts->dev, "Failed to request data: %d\n", ret);
  843. + return ret;
  844. + }
  845. +
  846. + ret = ipts_hid_init(ipts, info);
  847. + if (ret) {
  848. + dev_err(ipts->dev, "Failed to initialize HID device: %d\n", ret);
  849. + return ret;
  850. + }
  851. +
  852. + return 0;
  853. +}
  854. +
  855. +static int _ipts_control_stop(struct ipts_context *ipts)
  856. +{
  857. + int ret = 0;
  858. +
  859. + if (!ipts)
  860. + return -EFAULT;
  861. +
  862. + dev_info(ipts->dev, "Stopping IPTS\n");
  863. +
  864. + ret = ipts_receiver_stop(ipts);
  865. + if (ret) {
  866. + dev_err(ipts->dev, "Failed to stop receiver: %d\n", ret);
  867. + return ret;
  868. + }
  869. +
  870. + ret = ipts_control_reset_sensor(ipts);
  871. + if (ret) {
  872. + dev_err(ipts->dev, "Failed to reset sensor: %d\n", ret);
  873. + return ret;
  874. + }
  875. +
  876. + ret = ipts_resources_free(&ipts->resources);
  877. + if (ret) {
  878. + dev_err(ipts->dev, "Failed to free resources: %d\n", ret);
  879. + return ret;
  880. + }
  881. +
  882. + return 0;
  883. +}
  884. +
  885. +int ipts_control_stop(struct ipts_context *ipts)
  886. +{
  887. + int ret = 0;
  888. +
  889. + ret = _ipts_control_stop(ipts);
  890. + if (ret)
  891. + return ret;
  892. +
  893. + ret = ipts_hid_free(ipts);
  894. + if (ret) {
  895. + dev_err(ipts->dev, "Failed to free HID device: %d\n", ret);
  896. + return ret;
  897. + }
  898. +
  899. + return 0;
  900. +}
  901. +
  902. +int ipts_control_restart(struct ipts_context *ipts)
  903. +{
  904. + int ret = 0;
  905. +
  906. + ret = _ipts_control_stop(ipts);
  907. + if (ret)
  908. + return ret;
  909. +
  910. + /*
  911. + * Give the sensor some time to come back from resetting
  912. + */
  913. + msleep(1000);
  914. +
  915. + ret = ipts_control_start(ipts);
  916. + if (ret)
  917. + return ret;
  918. +
  919. + return 0;
  920. +}
  921. diff --git a/drivers/hid/ipts/control.h b/drivers/hid/ipts/control.h
  922. new file mode 100644
  923. index 000000000000..744bb92d682a
  924. --- /dev/null
  925. +++ b/drivers/hid/ipts/control.h
  926. @@ -0,0 +1,127 @@
  927. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  928. +/*
  929. + * Copyright (c) 2016 Intel Corporation
  930. + * Copyright (c) 2020-2023 Dorian Stoll
  931. + *
  932. + * Linux driver for Intel Precise Touch & Stylus
  933. + */
  934. +
  935. +#ifndef IPTS_CONTROL_H
  936. +#define IPTS_CONTROL_H
  937. +
  938. +#include <linux/types.h>
  939. +
  940. +#include "context.h"
  941. +#include "spec-data.h"
  942. +#include "spec-device.h"
  943. +
  944. +/*
  945. + * ipts_control_request_flush() - Stop the data flow.
  946. + * @ipts: The IPTS driver context.
  947. + *
  948. + * Runs the command to stop the data flow on the device.
  949. + * All outstanding data needs to be acknowledged using feedback before the command will return.
  950. + *
  951. + * Returns: 0 on success, <0 on error.
  952. + */
  953. +int ipts_control_request_flush(struct ipts_context *ipts);
  954. +
  955. +/*
  956. + * ipts_control_wait_flush() - Wait until data flow has been stopped.
  957. + * @ipts: The IPTS driver context.
  958. + *
  959. + * Returns: 0 on success, <0 on error.
  960. + */
  961. +int ipts_control_wait_flush(struct ipts_context *ipts);
  962. +
  963. +/*
  964. + * ipts_control_wait_flush() - Notify the device that the driver can receive new data.
  965. + * @ipts: The IPTS driver context.
  966. + *
  967. + * Returns: 0 on success, <0 on error.
  968. + */
  969. +int ipts_control_request_data(struct ipts_context *ipts);
  970. +
  971. +/*
  972. + * ipts_control_wait_data() - Wait until new data is available.
  973. + * @ipts: The IPTS driver context.
  974. + * @block: Whether to block execution until data is available.
  975. + *
  976. + * In doorbell mode, this function will never return while the data flow is active. Instead,
  977. + * the doorbell will be incremented when new data is available.
  978. + *
  979. + * Returns: 0 on success, <0 on error, -EAGAIN if no data is available.
  980. + */
  981. +int ipts_control_wait_data(struct ipts_context *ipts, bool block);
  982. +
  983. +/*
  984. + * ipts_control_send_feedback() - Submits a feedback buffer to the device.
  985. + * @ipts: The IPTS driver context.
  986. + * @buffer: The ID of the buffer containing feedback data.
  987. + *
  988. + * Returns: 0 on success, <0 on error.
  989. + */
  990. +int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer);
  991. +
  992. +/*
  993. + * ipts_control_hid2me_feedback() - Sends HID2ME feedback, a special type of feedback.
  994. + * @ipts: The IPTS driver context.
  995. + * @cmd: The command that will be run on the device.
  996. + * @type: The type of the payload that is sent to the device.
  997. + * @data: The payload of the feedback command.
  998. + * @size: The size of the payload.
  999. + *
  1000. + * HID2ME feedback is a special type of feedback, because it allows interfacing with
  1001. + * the HID API of the device at any moment, without requiring a buffer that has to
  1002. + * be acknowledged.
  1003. + *
  1004. + * Returns: 0 on success, <0 on error.
  1005. + */
  1006. +int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_cmd_type cmd,
  1007. + enum ipts_feedback_data_type type, void *data, size_t size);
  1008. +
  1009. +/*
  1010. + * ipts_control_refill_buffer() - Acknowledges that data in a buffer has been processed.
  1011. + * @ipts: The IPTS driver context.
  1012. + * @buffer: The buffer that has been processed and can be refilled.
  1013. + *
  1014. + * Returns: 0 on success, <0 on error.
  1015. + */
  1016. +static inline int ipts_control_refill_buffer(struct ipts_context *ipts, u32 buffer)
  1017. +{
  1018. + /*
  1019. + * IPTS expects structured data in the feedback buffer matching the buffer that will be
  1020. + * refilled. We don't know what that data looks like, so we just keep the buffer empty.
  1021. + * This results in an INVALID_PARAMS error, but the buffer gets refilled without an issue.
  1022. + * Sending a minimal structure with the buffer ID fixes the error, but breaks refilling
  1023. + * the buffers on some devices.
  1024. + */
  1025. +
  1026. + return ipts_control_send_feedback(ipts, buffer);
  1027. +}
  1028. +
  1029. +/*
  1030. + * ipts_control_start() - Initialized the device and starts the data flow.
  1031. + * @ipts: The IPTS driver context.
  1032. + *
  1033. + * Returns: 0 on success, <0 on error.
  1034. + */
  1035. +int ipts_control_start(struct ipts_context *ipts);
  1036. +
  1037. +/*
  1038. + * ipts_control_stop() - Stops the data flow and resets the device.
  1039. + * @ipts: The IPTS driver context.
  1040. + *
  1041. + * Returns: 0 on success, <0 on error.
  1042. + */
  1043. +int ipts_control_stop(struct ipts_context *ipts);
  1044. +
  1045. +/*
  1046. + * ipts_control_restart() - Stops the device and starts it again.
  1047. + * @ipts: The IPTS driver context.
  1048. + *
  1049. + * Returns: 0 on success, <0 on error.
  1050. + */
  1051. +int ipts_control_restart(struct ipts_context *ipts);
  1052. +
  1053. +#endif /* IPTS_CONTROL_H */
  1054. diff --git a/drivers/hid/ipts/desc.h b/drivers/hid/ipts/desc.h
  1055. new file mode 100644
  1056. index 000000000000..c058974a03a1
  1057. --- /dev/null
  1058. +++ b/drivers/hid/ipts/desc.h
  1059. @@ -0,0 +1,81 @@
  1060. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  1061. +/*
  1062. + * Copyright (c) 2016 Intel Corporation
  1063. + * Copyright (c) 2022-2023 Dorian Stoll
  1064. + *
  1065. + * Linux driver for Intel Precise Touch & Stylus
  1066. + */
  1067. +
  1068. +#ifndef IPTS_DESC_H
  1069. +#define IPTS_DESC_H
  1070. +
  1071. +#include <linux/types.h>
  1072. +
  1073. +#define IPTS_HID_REPORT_SINGLETOUCH 64
  1074. +#define IPTS_HID_REPORT_DATA 65
  1075. +#define IPTS_HID_REPORT_SET_MODE 66
  1076. +
  1077. +#define IPTS_HID_REPORT_DATA_SIZE 7485
  1078. +
  1079. +/*
  1080. + * HID descriptor for singletouch data.
  1081. + * This descriptor should be present on all IPTS devices.
  1082. + */
  1083. +static const u8 ipts_singletouch_descriptor[] = {
  1084. + 0x05, 0x0D, /* Usage Page (Digitizer), */
  1085. + 0x09, 0x04, /* Usage (Touchscreen), */
  1086. + 0xA1, 0x01, /* Collection (Application), */
  1087. + 0x85, 0x40, /* Report ID (64), */
  1088. + 0x09, 0x42, /* Usage (Tip Switch), */
  1089. + 0x15, 0x00, /* Logical Minimum (0), */
  1090. + 0x25, 0x01, /* Logical Maximum (1), */
  1091. + 0x75, 0x01, /* Report Size (1), */
  1092. + 0x95, 0x01, /* Report Count (1), */
  1093. + 0x81, 0x02, /* Input (Variable), */
  1094. + 0x95, 0x07, /* Report Count (7), */
  1095. + 0x81, 0x03, /* Input (Constant, Variable), */
  1096. + 0x05, 0x01, /* Usage Page (Desktop), */
  1097. + 0x09, 0x30, /* Usage (X), */
  1098. + 0x75, 0x10, /* Report Size (16), */
  1099. + 0x95, 0x01, /* Report Count (1), */
  1100. + 0xA4, /* Push, */
  1101. + 0x55, 0x0E, /* Unit Exponent (14), */
  1102. + 0x65, 0x11, /* Unit (Centimeter), */
  1103. + 0x46, 0x76, 0x0B, /* Physical Maximum (2934), */
  1104. + 0x26, 0xFF, 0x7F, /* Logical Maximum (32767), */
  1105. + 0x81, 0x02, /* Input (Variable), */
  1106. + 0x09, 0x31, /* Usage (Y), */
  1107. + 0x46, 0x74, 0x06, /* Physical Maximum (1652), */
  1108. + 0x26, 0xFF, 0x7F, /* Logical Maximum (32767), */
  1109. + 0x81, 0x02, /* Input (Variable), */
  1110. + 0xB4, /* Pop, */
  1111. + 0xC0, /* End Collection */
  1112. +};
  1113. +
  1114. +/*
  1115. + * Fallback HID descriptor for older devices that do not have
  1116. + * the ability to query their HID descriptor.
  1117. + */
  1118. +static const u8 ipts_fallback_descriptor[] = {
  1119. + 0x05, 0x0D, /* Usage Page (Digitizer), */
  1120. + 0x09, 0x0F, /* Usage (Capacitive Hm Digitizer), */
  1121. + 0xA1, 0x01, /* Collection (Application), */
  1122. + 0x85, 0x41, /* Report ID (65), */
  1123. + 0x09, 0x56, /* Usage (Scan Time), */
  1124. + 0x95, 0x01, /* Report Count (1), */
  1125. + 0x75, 0x10, /* Report Size (16), */
  1126. + 0x81, 0x02, /* Input (Variable), */
  1127. + 0x09, 0x61, /* Usage (Gesture Char Quality), */
  1128. + 0x75, 0x08, /* Report Size (8), */
  1129. + 0x96, 0x3D, 0x1D, /* Report Count (7485), */
  1130. + 0x81, 0x03, /* Input (Constant, Variable), */
  1131. + 0x85, 0x42, /* Report ID (66), */
  1132. + 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  1133. + 0x09, 0xC8, /* Usage (C8h), */
  1134. + 0x75, 0x08, /* Report Size (8), */
  1135. + 0x95, 0x01, /* Report Count (1), */
  1136. + 0xB1, 0x02, /* Feature (Variable), */
  1137. + 0xC0, /* End Collection, */
  1138. +};
  1139. +
  1140. +#endif /* IPTS_DESC_H */
  1141. diff --git a/drivers/hid/ipts/hid.c b/drivers/hid/ipts/hid.c
  1142. new file mode 100644
  1143. index 000000000000..6782394e8dde
  1144. --- /dev/null
  1145. +++ b/drivers/hid/ipts/hid.c
  1146. @@ -0,0 +1,348 @@
  1147. +// SPDX-License-Identifier: GPL-2.0-or-later
  1148. +/*
  1149. + * Copyright (c) 2016 Intel Corporation
  1150. + * Copyright (c) 2022-2023 Dorian Stoll
  1151. + *
  1152. + * Linux driver for Intel Precise Touch & Stylus
  1153. + */
  1154. +
  1155. +#include <linux/completion.h>
  1156. +#include <linux/gfp.h>
  1157. +#include <linux/hid.h>
  1158. +#include <linux/mutex.h>
  1159. +#include <linux/slab.h>
  1160. +#include <linux/types.h>
  1161. +
  1162. +#include "context.h"
  1163. +#include "control.h"
  1164. +#include "desc.h"
  1165. +#include "hid.h"
  1166. +#include "spec-data.h"
  1167. +#include "spec-device.h"
  1168. +#include "spec-hid.h"
  1169. +
  1170. +static int ipts_hid_start(struct hid_device *hid)
  1171. +{
  1172. + return 0;
  1173. +}
  1174. +
  1175. +static void ipts_hid_stop(struct hid_device *hid)
  1176. +{
  1177. +}
  1178. +
  1179. +static int ipts_hid_switch_mode(struct ipts_context *ipts, enum ipts_mode mode)
  1180. +{
  1181. + if (!ipts)
  1182. + return -EFAULT;
  1183. +
  1184. + if (ipts->mode == mode)
  1185. + return 0;
  1186. +
  1187. + /*
  1188. + * This is only allowed on older devices.
  1189. + */
  1190. + if (ipts->info.intf_eds > 1)
  1191. + return 0;
  1192. +
  1193. + ipts->mode = mode;
  1194. + return ipts_control_restart(ipts);
  1195. +}
  1196. +
  1197. +static int ipts_hid_parse(struct hid_device *hid)
  1198. +{
  1199. + int ret = 0;
  1200. + struct ipts_context *ipts = NULL;
  1201. +
  1202. + bool has_native_descriptor = false;
  1203. +
  1204. + u8 *buffer = NULL;
  1205. + size_t size = 0;
  1206. +
  1207. + if (!hid)
  1208. + return -ENODEV;
  1209. +
  1210. + ipts = hid->driver_data;
  1211. +
  1212. + if (!ipts)
  1213. + return -EFAULT;
  1214. +
  1215. + size = sizeof(ipts_singletouch_descriptor);
  1216. + has_native_descriptor = ipts->descriptor.address && ipts->descriptor.size > 0;
  1217. +
  1218. + if (has_native_descriptor)
  1219. + size += ipts->descriptor.size;
  1220. + else
  1221. + size += sizeof(ipts_fallback_descriptor);
  1222. +
  1223. + buffer = kzalloc(size, GFP_KERNEL);
  1224. + if (!buffer)
  1225. + return -ENOMEM;
  1226. +
  1227. + memcpy(buffer, ipts_singletouch_descriptor, sizeof(ipts_singletouch_descriptor));
  1228. +
  1229. + if (has_native_descriptor) {
  1230. + memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts->descriptor.address,
  1231. + ipts->descriptor.size);
  1232. + } else {
  1233. + memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts_fallback_descriptor,
  1234. + sizeof(ipts_fallback_descriptor));
  1235. + }
  1236. +
  1237. + ret = hid_parse_report(hid, buffer, size);
  1238. + kfree(buffer);
  1239. +
  1240. + if (ret) {
  1241. + dev_err(ipts->dev, "Failed to parse HID descriptor: %d\n", ret);
  1242. + return ret;
  1243. + }
  1244. +
  1245. + return 0;
  1246. +}
  1247. +
  1248. +static int ipts_hid_get_feature(struct ipts_context *ipts, unsigned char reportnum, __u8 *buf,
  1249. + size_t size, enum ipts_feedback_data_type type)
  1250. +{
  1251. + int ret = 0;
  1252. +
  1253. + if (!ipts)
  1254. + return -EFAULT;
  1255. +
  1256. + if (!buf)
  1257. + return -EFAULT;
  1258. +
  1259. + mutex_lock(&ipts->feature_lock);
  1260. +
  1261. + memset(buf, 0, size);
  1262. + buf[0] = reportnum;
  1263. +
  1264. + memset(&ipts->feature_report, 0, sizeof(ipts->feature_report));
  1265. + reinit_completion(&ipts->feature_event);
  1266. +
  1267. + ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buf, size);
  1268. + if (ret) {
  1269. + dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  1270. + goto out;
  1271. + }
  1272. +
  1273. + ret = wait_for_completion_timeout(&ipts->feature_event, msecs_to_jiffies(5000));
  1274. + if (ret == 0) {
  1275. + dev_warn(ipts->dev, "GET_FEATURES timed out!\n");
  1276. + ret = -EIO;
  1277. + goto out;
  1278. + }
  1279. +
  1280. + if (!ipts->feature_report.address) {
  1281. + ret = -EFAULT;
  1282. + goto out;
  1283. + }
  1284. +
  1285. + if (ipts->feature_report.size > size) {
  1286. + ret = -ETOOSMALL;
  1287. + goto out;
  1288. + }
  1289. +
  1290. + ret = ipts->feature_report.size;
  1291. + memcpy(buf, ipts->feature_report.address, ipts->feature_report.size);
  1292. +
  1293. +out:
  1294. + mutex_unlock(&ipts->feature_lock);
  1295. + return ret;
  1296. +}
  1297. +
  1298. +static int ipts_hid_set_feature(struct ipts_context *ipts, unsigned char reportnum, __u8 *buf,
  1299. + size_t size, enum ipts_feedback_data_type type)
  1300. +{
  1301. + int ret = 0;
  1302. +
  1303. + if (!ipts)
  1304. + return -EFAULT;
  1305. +
  1306. + if (!buf)
  1307. + return -EFAULT;
  1308. +
  1309. + buf[0] = reportnum;
  1310. +
  1311. + ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buf, size);
  1312. + if (ret)
  1313. + dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  1314. +
  1315. + return ret;
  1316. +}
  1317. +
  1318. +static int ipts_hid_raw_request(struct hid_device *hid, unsigned char reportnum, __u8 *buf,
  1319. + size_t size, unsigned char rtype, int reqtype)
  1320. +{
  1321. + int ret = 0;
  1322. + struct ipts_context *ipts = NULL;
  1323. +
  1324. + enum ipts_feedback_data_type type = IPTS_FEEDBACK_DATA_TYPE_VENDOR;
  1325. +
  1326. + if (!hid)
  1327. + return -ENODEV;
  1328. +
  1329. + ipts = hid->driver_data;
  1330. +
  1331. + if (!ipts)
  1332. + return -EFAULT;
  1333. +
  1334. + if (!buf)
  1335. + return -EFAULT;
  1336. +
  1337. + if (rtype == HID_OUTPUT_REPORT && reqtype == HID_REQ_SET_REPORT)
  1338. + type = IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT;
  1339. + else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_GET_REPORT)
  1340. + type = IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES;
  1341. + else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_SET_REPORT)
  1342. + type = IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES;
  1343. + else
  1344. + return -EIO;
  1345. +
  1346. + // Implemente mode switching report for older devices without native HID support
  1347. + if (type == IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES && reportnum == IPTS_HID_REPORT_SET_MODE) {
  1348. + ret = ipts_hid_switch_mode(ipts, buf[1]);
  1349. + if (ret) {
  1350. + dev_err(ipts->dev, "Failed to switch modes: %d\n", ret);
  1351. + return ret;
  1352. + }
  1353. + }
  1354. +
  1355. + if (reqtype == HID_REQ_GET_REPORT)
  1356. + return ipts_hid_get_feature(ipts, reportnum, buf, size, type);
  1357. + else
  1358. + return ipts_hid_set_feature(ipts, reportnum, buf, size, type);
  1359. +}
  1360. +
  1361. +static int ipts_hid_output_report(struct hid_device *hid, __u8 *data, size_t size)
  1362. +{
  1363. + struct ipts_context *ipts = NULL;
  1364. +
  1365. + if (!hid)
  1366. + return -ENODEV;
  1367. +
  1368. + ipts = hid->driver_data;
  1369. +
  1370. + return ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE,
  1371. + IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT, data, size);
  1372. +}
  1373. +
  1374. +static struct hid_ll_driver ipts_hid_driver = {
  1375. + .start = ipts_hid_start,
  1376. + .stop = ipts_hid_stop,
  1377. + .open = ipts_hid_start,
  1378. + .close = ipts_hid_stop,
  1379. + .parse = ipts_hid_parse,
  1380. + .raw_request = ipts_hid_raw_request,
  1381. + .output_report = ipts_hid_output_report,
  1382. +};
  1383. +
  1384. +int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  1385. +{
  1386. + int ret = 0;
  1387. + u8 *temp = NULL;
  1388. + struct ipts_hid_header *frame = NULL;
  1389. + struct ipts_data_header *header = NULL;
  1390. +
  1391. + if (!ipts)
  1392. + return -EFAULT;
  1393. +
  1394. + if (!ipts->hid)
  1395. + return -ENODEV;
  1396. +
  1397. + header = (struct ipts_data_header *)ipts->resources.data[buffer].address;
  1398. +
  1399. + if (!header)
  1400. + return -EFAULT;
  1401. +
  1402. + if (header->size == 0)
  1403. + return 0;
  1404. +
  1405. + if (header->type == IPTS_DATA_TYPE_HID)
  1406. + return hid_input_report(ipts->hid, HID_INPUT_REPORT, header->data, header->size, 1);
  1407. +
  1408. + if (header->type == IPTS_DATA_TYPE_GET_FEATURES) {
  1409. + ipts->feature_report.address = header->data;
  1410. + ipts->feature_report.size = header->size;
  1411. +
  1412. + complete_all(&ipts->feature_event);
  1413. + return 0;
  1414. + }
  1415. +
  1416. + if (header->type != IPTS_DATA_TYPE_FRAME)
  1417. + return 0;
  1418. +
  1419. + if (header->size + 3 + sizeof(struct ipts_hid_header) > IPTS_HID_REPORT_DATA_SIZE)
  1420. + return -ERANGE;
  1421. +
  1422. + temp = kzalloc(IPTS_HID_REPORT_DATA_SIZE, GFP_KERNEL);
  1423. + if (!temp)
  1424. + return -ENOMEM;
  1425. +
  1426. + /*
  1427. + * Synthesize a HID report matching the devices that natively send HID reports
  1428. + */
  1429. + temp[0] = IPTS_HID_REPORT_DATA;
  1430. +
  1431. + frame = (struct ipts_hid_header *)&temp[3];
  1432. + frame->type = IPTS_HID_FRAME_TYPE_RAW;
  1433. + frame->size = header->size + sizeof(*frame);
  1434. +
  1435. + memcpy(frame->data, header->data, header->size);
  1436. +
  1437. + ret = hid_input_report(ipts->hid, HID_INPUT_REPORT, temp, IPTS_HID_REPORT_DATA_SIZE, 1);
  1438. + kfree(temp);
  1439. +
  1440. + return ret;
  1441. +}
  1442. +
  1443. +int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info)
  1444. +{
  1445. + int ret = 0;
  1446. +
  1447. + if (!ipts)
  1448. + return -EFAULT;
  1449. +
  1450. + if (ipts->hid)
  1451. + return 0;
  1452. +
  1453. + ipts->hid = hid_allocate_device();
  1454. + if (IS_ERR(ipts->hid)) {
  1455. + int err = PTR_ERR(ipts->hid);
  1456. +
  1457. + dev_err(ipts->dev, "Failed to allocate HID device: %d\n", err);
  1458. + return err;
  1459. + }
  1460. +
  1461. + ipts->hid->driver_data = ipts;
  1462. + ipts->hid->dev.parent = ipts->dev;
  1463. + ipts->hid->ll_driver = &ipts_hid_driver;
  1464. +
  1465. + ipts->hid->vendor = info.vendor;
  1466. + ipts->hid->product = info.product;
  1467. + ipts->hid->group = HID_GROUP_MULTITOUCH;
  1468. +
  1469. + snprintf(ipts->hid->name, sizeof(ipts->hid->name), "IPTS %04X:%04X", info.vendor,
  1470. + info.product);
  1471. +
  1472. + ret = hid_add_device(ipts->hid);
  1473. + if (ret) {
  1474. + dev_err(ipts->dev, "Failed to add HID device: %d\n", ret);
  1475. + ipts_hid_free(ipts);
  1476. + return ret;
  1477. + }
  1478. +
  1479. + return 0;
  1480. +}
  1481. +
  1482. +int ipts_hid_free(struct ipts_context *ipts)
  1483. +{
  1484. + if (!ipts)
  1485. + return -EFAULT;
  1486. +
  1487. + if (!ipts->hid)
  1488. + return 0;
  1489. +
  1490. + hid_destroy_device(ipts->hid);
  1491. + ipts->hid = NULL;
  1492. +
  1493. + return 0;
  1494. +}
  1495. diff --git a/drivers/hid/ipts/hid.h b/drivers/hid/ipts/hid.h
  1496. new file mode 100644
  1497. index 000000000000..62bf3cd48608
  1498. --- /dev/null
  1499. +++ b/drivers/hid/ipts/hid.h
  1500. @@ -0,0 +1,22 @@
  1501. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  1502. +/*
  1503. + * Copyright (c) 2016 Intel Corporation
  1504. + * Copyright (c) 2022-2023 Dorian Stoll
  1505. + *
  1506. + * Linux driver for Intel Precise Touch & Stylus
  1507. + */
  1508. +
  1509. +#ifndef IPTS_HID_H
  1510. +#define IPTS_HID_H
  1511. +
  1512. +#include <linux/types.h>
  1513. +
  1514. +#include "context.h"
  1515. +#include "spec-device.h"
  1516. +
  1517. +int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer);
  1518. +
  1519. +int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info);
  1520. +int ipts_hid_free(struct ipts_context *ipts);
  1521. +
  1522. +#endif /* IPTS_HID_H */
  1523. diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c
  1524. new file mode 100644
  1525. index 000000000000..0f20c6c08c38
  1526. --- /dev/null
  1527. +++ b/drivers/hid/ipts/main.c
  1528. @@ -0,0 +1,127 @@
  1529. +// SPDX-License-Identifier: GPL-2.0-or-later
  1530. +/*
  1531. + * Copyright (c) 2016 Intel Corporation
  1532. + * Copyright (c) 2020-2023 Dorian Stoll
  1533. + *
  1534. + * Linux driver for Intel Precise Touch & Stylus
  1535. + */
  1536. +
  1537. +#include <linux/completion.h>
  1538. +#include <linux/delay.h>
  1539. +#include <linux/device.h>
  1540. +#include <linux/dma-mapping.h>
  1541. +#include <linux/mei_cl_bus.h>
  1542. +#include <linux/mod_devicetable.h>
  1543. +#include <linux/module.h>
  1544. +#include <linux/mutex.h>
  1545. +#include <linux/slab.h>
  1546. +#include <linux/stddef.h>
  1547. +#include <linux/types.h>
  1548. +
  1549. +#include "context.h"
  1550. +#include "control.h"
  1551. +#include "mei.h"
  1552. +#include "receiver.h"
  1553. +#include "spec-device.h"
  1554. +
  1555. +/*
  1556. + * The MEI client ID for IPTS functionality.
  1557. + */
  1558. +#define IPTS_ID UUID_LE(0x3e8d0870, 0x271a, 0x4208, 0x8e, 0xb5, 0x9a, 0xcb, 0x94, 0x02, 0xae, 0x04)
  1559. +
  1560. +static int ipts_set_dma_mask(struct mei_cl_device *cldev)
  1561. +{
  1562. + if (!cldev)
  1563. + return -EFAULT;
  1564. +
  1565. + if (!dma_coerce_mask_and_coherent(&cldev->dev, DMA_BIT_MASK(64)))
  1566. + return 0;
  1567. +
  1568. + return dma_coerce_mask_and_coherent(&cldev->dev, DMA_BIT_MASK(32));
  1569. +}
  1570. +
  1571. +static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id *id)
  1572. +{
  1573. + int ret = 0;
  1574. + struct ipts_context *ipts = NULL;
  1575. +
  1576. + if (!cldev)
  1577. + return -EFAULT;
  1578. +
  1579. + ret = ipts_set_dma_mask(cldev);
  1580. + if (ret) {
  1581. + dev_err(&cldev->dev, "Failed to set DMA mask for IPTS: %d\n", ret);
  1582. + return ret;
  1583. + }
  1584. +
  1585. + ret = mei_cldev_enable(cldev);
  1586. + if (ret) {
  1587. + dev_err(&cldev->dev, "Failed to enable MEI device: %d\n", ret);
  1588. + return ret;
  1589. + }
  1590. +
  1591. + ipts = devm_kzalloc(&cldev->dev, sizeof(*ipts), GFP_KERNEL);
  1592. + if (!ipts) {
  1593. + mei_cldev_disable(cldev);
  1594. + return -ENOMEM;
  1595. + }
  1596. +
  1597. + ret = ipts_mei_init(&ipts->mei, cldev);
  1598. + if (ret) {
  1599. + dev_err(&cldev->dev, "Failed to init MEI bus logic: %d\n", ret);
  1600. + return ret;
  1601. + }
  1602. +
  1603. + ipts->dev = &cldev->dev;
  1604. + ipts->mode = IPTS_MODE_EVENT;
  1605. +
  1606. + mutex_init(&ipts->feature_lock);
  1607. + init_completion(&ipts->feature_event);
  1608. +
  1609. + mei_cldev_set_drvdata(cldev, ipts);
  1610. +
  1611. + ret = ipts_control_start(ipts);
  1612. + if (ret) {
  1613. + dev_err(&cldev->dev, "Failed to start IPTS: %d\n", ret);
  1614. + return ret;
  1615. + }
  1616. +
  1617. + return 0;
  1618. +}
  1619. +
  1620. +static void ipts_remove(struct mei_cl_device *cldev)
  1621. +{
  1622. + int ret = 0;
  1623. + struct ipts_context *ipts = NULL;
  1624. +
  1625. + if (!cldev) {
  1626. + pr_err("MEI device is NULL!");
  1627. + return;
  1628. + }
  1629. +
  1630. + ipts = mei_cldev_get_drvdata(cldev);
  1631. +
  1632. + ret = ipts_control_stop(ipts);
  1633. + if (ret)
  1634. + dev_err(&cldev->dev, "Failed to stop IPTS: %d\n", ret);
  1635. +
  1636. + mei_cldev_disable(cldev);
  1637. +}
  1638. +
  1639. +static struct mei_cl_device_id ipts_device_id_table[] = {
  1640. + { .uuid = IPTS_ID, .version = MEI_CL_VERSION_ANY },
  1641. + {},
  1642. +};
  1643. +MODULE_DEVICE_TABLE(mei, ipts_device_id_table);
  1644. +
  1645. +static struct mei_cl_driver ipts_driver = {
  1646. + .id_table = ipts_device_id_table,
  1647. + .name = "ipts",
  1648. + .probe = ipts_probe,
  1649. + .remove = ipts_remove,
  1650. +};
  1651. +module_mei_cl_driver(ipts_driver);
  1652. +
  1653. +MODULE_DESCRIPTION("IPTS touchscreen driver");
  1654. +MODULE_AUTHOR("Dorian Stoll <dorian.stoll@tmsp.io>");
  1655. +MODULE_LICENSE("GPL");
  1656. diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c
  1657. new file mode 100644
  1658. index 000000000000..26666fd99b0c
  1659. --- /dev/null
  1660. +++ b/drivers/hid/ipts/mei.c
  1661. @@ -0,0 +1,189 @@
  1662. +// SPDX-License-Identifier: GPL-2.0-or-later
  1663. +/*
  1664. + * Copyright (c) 2016 Intel Corporation
  1665. + * Copyright (c) 2023 Dorian Stoll
  1666. + *
  1667. + * Linux driver for Intel Precise Touch & Stylus
  1668. + */
  1669. +
  1670. +#include <linux/device.h>
  1671. +#include <linux/errno.h>
  1672. +#include <linux/jiffies.h>
  1673. +#include <linux/list.h>
  1674. +#include <linux/mei_cl_bus.h>
  1675. +#include <linux/printk.h>
  1676. +#include <linux/rwsem.h>
  1677. +#include <linux/types.h>
  1678. +#include <linux/wait.h>
  1679. +
  1680. +#include "context.h"
  1681. +#include "mei.h"
  1682. +
  1683. +static void locked_list_add(struct list_head *new, struct list_head *head,
  1684. + struct rw_semaphore *lock)
  1685. +{
  1686. + down_write(lock);
  1687. + list_add(new, head);
  1688. + up_write(lock);
  1689. +}
  1690. +
  1691. +static void locked_list_del(struct list_head *entry, struct rw_semaphore *lock)
  1692. +{
  1693. + down_write(lock);
  1694. + list_del(entry);
  1695. + up_write(lock);
  1696. +}
  1697. +
  1698. +static void ipts_mei_incoming(struct mei_cl_device *cldev)
  1699. +{
  1700. + ssize_t ret = 0;
  1701. + struct ipts_mei_message *entry = NULL;
  1702. + struct ipts_context *ipts = NULL;
  1703. +
  1704. + if (!cldev) {
  1705. + pr_err("MEI device is NULL!");
  1706. + return;
  1707. + }
  1708. +
  1709. + ipts = mei_cldev_get_drvdata(cldev);
  1710. + if (!ipts) {
  1711. + pr_err("IPTS driver context is NULL!");
  1712. + return;
  1713. + }
  1714. +
  1715. + entry = devm_kzalloc(ipts->dev, sizeof(*entry), GFP_KERNEL);
  1716. + if (!entry)
  1717. + return;
  1718. +
  1719. + INIT_LIST_HEAD(&entry->list);
  1720. +
  1721. + do {
  1722. + ret = mei_cldev_recv(cldev, (u8 *)&entry->rsp, sizeof(entry->rsp));
  1723. + } while (ret == -EINTR);
  1724. +
  1725. + if (ret < 0) {
  1726. + dev_err(ipts->dev, "Error while reading response: %ld\n", ret);
  1727. + return;
  1728. + }
  1729. +
  1730. + if (ret == 0) {
  1731. + dev_err(ipts->dev, "Received empty response\n");
  1732. + return;
  1733. + }
  1734. +
  1735. + locked_list_add(&entry->list, &ipts->mei.messages, &ipts->mei.message_lock);
  1736. + wake_up_all(&ipts->mei.message_queue);
  1737. +}
  1738. +
  1739. +static int ipts_mei_search(struct ipts_mei *mei, enum ipts_command_code code,
  1740. + struct ipts_response *rsp)
  1741. +{
  1742. + struct ipts_mei_message *entry = NULL;
  1743. +
  1744. + if (!mei)
  1745. + return -EFAULT;
  1746. +
  1747. + if (!rsp)
  1748. + return -EFAULT;
  1749. +
  1750. + down_read(&mei->message_lock);
  1751. +
  1752. + /*
  1753. + * Iterate over the list of received messages, and check if there is one
  1754. + * matching the requested command code.
  1755. + */
  1756. + list_for_each_entry(entry, &mei->messages, list) {
  1757. + if (entry->rsp.cmd == code)
  1758. + break;
  1759. + }
  1760. +
  1761. + up_read(&mei->message_lock);
  1762. +
  1763. + /*
  1764. + * If entry is not the list head, this means that the loop above has been stopped early,
  1765. + * and that we found a matching element. We drop the message from the list and return it.
  1766. + */
  1767. + if (!list_entry_is_head(entry, &mei->messages, list)) {
  1768. + locked_list_del(&entry->list, &mei->message_lock);
  1769. +
  1770. + *rsp = entry->rsp;
  1771. + devm_kfree(&mei->cldev->dev, entry);
  1772. +
  1773. + return 0;
  1774. + }
  1775. +
  1776. + return -EAGAIN;
  1777. +}
  1778. +
  1779. +int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts_response *rsp,
  1780. + u64 timeout)
  1781. +{
  1782. + int ret = 0;
  1783. +
  1784. + if (!mei)
  1785. + return -EFAULT;
  1786. +
  1787. + /*
  1788. + * A timeout of 0 means check and return immideately.
  1789. + */
  1790. + if (timeout == 0)
  1791. + return ipts_mei_search(mei, code, rsp);
  1792. +
  1793. + /*
  1794. + * A timeout of less than 0 means to wait forever.
  1795. + */
  1796. + if (timeout < 0) {
  1797. + wait_event(mei->message_queue, ipts_mei_search(mei, code, rsp) == 0);
  1798. + return 0;
  1799. + }
  1800. +
  1801. + ret = wait_event_timeout(mei->message_queue, ipts_mei_search(mei, code, rsp) == 0,
  1802. + msecs_to_jiffies(timeout));
  1803. +
  1804. + if (ret > 0)
  1805. + return 0;
  1806. +
  1807. + return -EAGAIN;
  1808. +}
  1809. +
  1810. +int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length)
  1811. +{
  1812. + int ret = 0;
  1813. +
  1814. + if (!mei)
  1815. + return -EFAULT;
  1816. +
  1817. + if (!mei->cldev)
  1818. + return -EFAULT;
  1819. +
  1820. + if (!data)
  1821. + return -EFAULT;
  1822. +
  1823. + do {
  1824. + ret = mei_cldev_send(mei->cldev, (u8 *)data, length);
  1825. + } while (ret == -EINTR);
  1826. +
  1827. + if (ret < 0)
  1828. + return ret;
  1829. +
  1830. + return 0;
  1831. +}
  1832. +
  1833. +int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev)
  1834. +{
  1835. + if (!mei)
  1836. + return -EFAULT;
  1837. +
  1838. + if (!cldev)
  1839. + return -EFAULT;
  1840. +
  1841. + mei->cldev = cldev;
  1842. +
  1843. + INIT_LIST_HEAD(&mei->messages);
  1844. + init_waitqueue_head(&mei->message_queue);
  1845. + init_rwsem(&mei->message_lock);
  1846. +
  1847. + mei_cldev_register_rx_cb(cldev, ipts_mei_incoming);
  1848. +
  1849. + return 0;
  1850. +}
  1851. diff --git a/drivers/hid/ipts/mei.h b/drivers/hid/ipts/mei.h
  1852. new file mode 100644
  1853. index 000000000000..eadacae54c40
  1854. --- /dev/null
  1855. +++ b/drivers/hid/ipts/mei.h
  1856. @@ -0,0 +1,67 @@
  1857. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  1858. +/*
  1859. + * Copyright (c) 2016 Intel Corporation
  1860. + * Copyright (c) 2023 Dorian Stoll
  1861. + *
  1862. + * Linux driver for Intel Precise Touch & Stylus
  1863. + */
  1864. +
  1865. +#ifndef IPTS_MEI_H
  1866. +#define IPTS_MEI_H
  1867. +
  1868. +#include <linux/list.h>
  1869. +#include <linux/mei_cl_bus.h>
  1870. +#include <linux/rwsem.h>
  1871. +#include <linux/types.h>
  1872. +#include <linux/wait.h>
  1873. +
  1874. +#include "spec-device.h"
  1875. +
  1876. +struct ipts_mei_message {
  1877. + struct list_head list;
  1878. + struct ipts_response rsp;
  1879. +};
  1880. +
  1881. +struct ipts_mei {
  1882. + struct mei_cl_device *cldev;
  1883. +
  1884. + struct list_head messages;
  1885. +
  1886. + wait_queue_head_t message_queue;
  1887. + struct rw_semaphore message_lock;
  1888. +};
  1889. +
  1890. +/*
  1891. + * ipts_mei_recv() - Receive data from a MEI device.
  1892. + * @mei: The IPTS MEI device context.
  1893. + * @code: The IPTS command code to look for.
  1894. + * @rsp: The address that the received data will be copied to.
  1895. + * @timeout: How many milliseconds the function will wait at most.
  1896. + *
  1897. + * A negative timeout means to wait forever.
  1898. + *
  1899. + * Returns: 0 on success, <0 on error, -EAGAIN if no response has been received.
  1900. + */
  1901. +int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts_response *rsp,
  1902. + u64 timeout);
  1903. +
  1904. +/*
  1905. + * ipts_mei_send() - Send data to a MEI device.
  1906. + * @ipts: The IPTS MEI device context.
  1907. + * @data: The data to send.
  1908. + * @size: The size of the data.
  1909. + *
  1910. + * Returns: 0 on success, <0 on error.
  1911. + */
  1912. +int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length);
  1913. +
  1914. +/*
  1915. + * ipts_mei_init() - Initialize the MEI device context.
  1916. + * @mei: The MEI device context to initialize.
  1917. + * @cldev: The MEI device the context will be bound to.
  1918. + *
  1919. + * Returns: 0 on success, <0 on error.
  1920. + */
  1921. +int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev);
  1922. +
  1923. +#endif /* IPTS_MEI_H */
  1924. diff --git a/drivers/hid/ipts/receiver.c b/drivers/hid/ipts/receiver.c
  1925. new file mode 100644
  1926. index 000000000000..77234f9e0e17
  1927. --- /dev/null
  1928. +++ b/drivers/hid/ipts/receiver.c
  1929. @@ -0,0 +1,249 @@
  1930. +// SPDX-License-Identifier: GPL-2.0-or-later
  1931. +/*
  1932. + * Copyright (c) 2016 Intel Corporation
  1933. + * Copyright (c) 2020-2023 Dorian Stoll
  1934. + *
  1935. + * Linux driver for Intel Precise Touch & Stylus
  1936. + */
  1937. +
  1938. +#include <linux/delay.h>
  1939. +#include <linux/err.h>
  1940. +#include <linux/kthread.h>
  1941. +#include <linux/time64.h>
  1942. +#include <linux/timekeeping.h>
  1943. +#include <linux/types.h>
  1944. +
  1945. +#include "cmd.h"
  1946. +#include "context.h"
  1947. +#include "control.h"
  1948. +#include "hid.h"
  1949. +#include "resources.h"
  1950. +#include "spec-device.h"
  1951. +#include "thread.h"
  1952. +
  1953. +static void ipts_receiver_next_doorbell(struct ipts_context *ipts)
  1954. +{
  1955. + u32 *doorbell = (u32 *)ipts->resources.doorbell.address;
  1956. + *doorbell = *doorbell + 1;
  1957. +}
  1958. +
  1959. +static u32 ipts_receiver_current_doorbell(struct ipts_context *ipts)
  1960. +{
  1961. + u32 *doorbell = (u32 *)ipts->resources.doorbell.address;
  1962. + return *doorbell;
  1963. +}
  1964. +
  1965. +static void ipts_receiver_backoff(time64_t last, u32 n)
  1966. +{
  1967. + /*
  1968. + * If the last change was less than n seconds ago,
  1969. + * sleep for a shorter period so that new data can be
  1970. + * processed quickly. If there was no change for more than
  1971. + * n seconds, sleep longer to avoid wasting CPU cycles.
  1972. + */
  1973. + if (last + n > ktime_get_seconds())
  1974. + msleep(20);
  1975. + else
  1976. + msleep(200);
  1977. +}
  1978. +
  1979. +static int ipts_receiver_event_loop(struct ipts_thread *thread)
  1980. +{
  1981. + int ret = 0;
  1982. + u32 buffer = 0;
  1983. +
  1984. + struct ipts_context *ipts = NULL;
  1985. + time64_t last = ktime_get_seconds();
  1986. +
  1987. + if (!thread)
  1988. + return -EFAULT;
  1989. +
  1990. + ipts = thread->data;
  1991. +
  1992. + if (!ipts)
  1993. + return -EFAULT;
  1994. +
  1995. + dev_info(ipts->dev, "IPTS running in event mode\n");
  1996. +
  1997. + while (!ipts_thread_should_stop(thread)) {
  1998. + for (int i = 0; i < IPTS_BUFFERS; i++) {
  1999. + ret = ipts_control_wait_data(ipts, false);
  2000. + if (ret == -EAGAIN)
  2001. + break;
  2002. +
  2003. + if (ret) {
  2004. + dev_err(ipts->dev, "Failed to wait for data: %d\n", ret);
  2005. + continue;
  2006. + }
  2007. +
  2008. + buffer = ipts_receiver_current_doorbell(ipts) % IPTS_BUFFERS;
  2009. + ipts_receiver_next_doorbell(ipts);
  2010. +
  2011. + ret = ipts_hid_input_data(ipts, buffer);
  2012. + if (ret)
  2013. + dev_err(ipts->dev, "Failed to process buffer: %d\n", ret);
  2014. +
  2015. + ret = ipts_control_refill_buffer(ipts, buffer);
  2016. + if (ret)
  2017. + dev_err(ipts->dev, "Failed to send feedback: %d\n", ret);
  2018. +
  2019. + ret = ipts_control_request_data(ipts);
  2020. + if (ret)
  2021. + dev_err(ipts->dev, "Failed to request data: %d\n", ret);
  2022. +
  2023. + last = ktime_get_seconds();
  2024. + }
  2025. +
  2026. + ipts_receiver_backoff(last, 5);
  2027. + }
  2028. +
  2029. + ret = ipts_control_request_flush(ipts);
  2030. + if (ret) {
  2031. + dev_err(ipts->dev, "Failed to request flush: %d\n", ret);
  2032. + return ret;
  2033. + }
  2034. +
  2035. + ret = ipts_control_wait_data(ipts, true);
  2036. + if (ret) {
  2037. + dev_err(ipts->dev, "Failed to wait for data: %d\n", ret);
  2038. +
  2039. + if (ret != -EAGAIN)
  2040. + return ret;
  2041. + else
  2042. + return 0;
  2043. + }
  2044. +
  2045. + ret = ipts_control_wait_flush(ipts);
  2046. + if (ret) {
  2047. + dev_err(ipts->dev, "Failed to wait for flush: %d\n", ret);
  2048. +
  2049. + if (ret != -EAGAIN)
  2050. + return ret;
  2051. + else
  2052. + return 0;
  2053. + }
  2054. +
  2055. + return 0;
  2056. +}
  2057. +
  2058. +static int ipts_receiver_doorbell_loop(struct ipts_thread *thread)
  2059. +{
  2060. + int ret = 0;
  2061. + u32 buffer = 0;
  2062. +
  2063. + u32 doorbell = 0;
  2064. + u32 lastdb = 0;
  2065. +
  2066. + struct ipts_context *ipts = NULL;
  2067. + time64_t last = ktime_get_seconds();
  2068. +
  2069. + if (!thread)
  2070. + return -EFAULT;
  2071. +
  2072. + ipts = thread->data;
  2073. +
  2074. + if (!ipts)
  2075. + return -EFAULT;
  2076. +
  2077. + dev_info(ipts->dev, "IPTS running in doorbell mode\n");
  2078. +
  2079. + while (true) {
  2080. + if (ipts_thread_should_stop(thread)) {
  2081. + ret = ipts_control_request_flush(ipts);
  2082. + if (ret) {
  2083. + dev_err(ipts->dev, "Failed to request flush: %d\n", ret);
  2084. + return ret;
  2085. + }
  2086. + }
  2087. +
  2088. + doorbell = ipts_receiver_current_doorbell(ipts);
  2089. +
  2090. + /*
  2091. + * After filling up one of the data buffers, IPTS will increment
  2092. + * the doorbell. The value of the doorbell stands for the *next*
  2093. + * buffer that IPTS is going to fill.
  2094. + */
  2095. + while (lastdb != doorbell) {
  2096. + buffer = lastdb % IPTS_BUFFERS;
  2097. +
  2098. + ret = ipts_hid_input_data(ipts, buffer);
  2099. + if (ret)
  2100. + dev_err(ipts->dev, "Failed to process buffer: %d\n", ret);
  2101. +
  2102. + ret = ipts_control_refill_buffer(ipts, buffer);
  2103. + if (ret)
  2104. + dev_err(ipts->dev, "Failed to send feedback: %d\n", ret);
  2105. +
  2106. + last = ktime_get_seconds();
  2107. + lastdb++;
  2108. + }
  2109. +
  2110. + if (ipts_thread_should_stop(thread))
  2111. + break;
  2112. +
  2113. + ipts_receiver_backoff(last, 5);
  2114. + }
  2115. +
  2116. + ret = ipts_control_wait_data(ipts, true);
  2117. + if (ret) {
  2118. + dev_err(ipts->dev, "Failed to wait for data: %d\n", ret);
  2119. +
  2120. + if (ret != -EAGAIN)
  2121. + return ret;
  2122. + else
  2123. + return 0;
  2124. + }
  2125. +
  2126. + ret = ipts_control_wait_flush(ipts);
  2127. + if (ret) {
  2128. + dev_err(ipts->dev, "Failed to wait for flush: %d\n", ret);
  2129. +
  2130. + if (ret != -EAGAIN)
  2131. + return ret;
  2132. + else
  2133. + return 0;
  2134. + }
  2135. +
  2136. + return 0;
  2137. +}
  2138. +
  2139. +int ipts_receiver_start(struct ipts_context *ipts)
  2140. +{
  2141. + int ret = 0;
  2142. +
  2143. + if (!ipts)
  2144. + return -EFAULT;
  2145. +
  2146. + if (ipts->mode == IPTS_MODE_EVENT) {
  2147. + ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_event_loop, ipts,
  2148. + "ipts_event");
  2149. + } else if (ipts->mode == IPTS_MODE_DOORBELL) {
  2150. + ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_doorbell_loop, ipts,
  2151. + "ipts_doorbell");
  2152. + } else {
  2153. + ret = -EINVAL;
  2154. + }
  2155. +
  2156. + if (ret) {
  2157. + dev_err(ipts->dev, "Failed to start receiver loop: %d\n", ret);
  2158. + return ret;
  2159. + }
  2160. +
  2161. + return 0;
  2162. +}
  2163. +
  2164. +int ipts_receiver_stop(struct ipts_context *ipts)
  2165. +{
  2166. + int ret = 0;
  2167. +
  2168. + if (!ipts)
  2169. + return -EFAULT;
  2170. +
  2171. + ret = ipts_thread_stop(&ipts->receiver_loop);
  2172. + if (ret) {
  2173. + dev_err(ipts->dev, "Failed to stop receiver loop: %d\n", ret);
  2174. + return ret;
  2175. + }
  2176. +
  2177. + return 0;
  2178. +}
  2179. diff --git a/drivers/hid/ipts/receiver.h b/drivers/hid/ipts/receiver.h
  2180. new file mode 100644
  2181. index 000000000000..96070f34fbca
  2182. --- /dev/null
  2183. +++ b/drivers/hid/ipts/receiver.h
  2184. @@ -0,0 +1,17 @@
  2185. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2186. +/*
  2187. + * Copyright (c) 2016 Intel Corporation
  2188. + * Copyright (c) 2020-2023 Dorian Stoll
  2189. + *
  2190. + * Linux driver for Intel Precise Touch & Stylus
  2191. + */
  2192. +
  2193. +#ifndef IPTS_RECEIVER_H
  2194. +#define IPTS_RECEIVER_H
  2195. +
  2196. +#include "context.h"
  2197. +
  2198. +int ipts_receiver_start(struct ipts_context *ipts);
  2199. +int ipts_receiver_stop(struct ipts_context *ipts);
  2200. +
  2201. +#endif /* IPTS_RECEIVER_H */
  2202. diff --git a/drivers/hid/ipts/resources.c b/drivers/hid/ipts/resources.c
  2203. new file mode 100644
  2204. index 000000000000..80ba5885bb55
  2205. --- /dev/null
  2206. +++ b/drivers/hid/ipts/resources.c
  2207. @@ -0,0 +1,108 @@
  2208. +// SPDX-License-Identifier: GPL-2.0-or-later
  2209. +/*
  2210. + * Copyright (c) 2016 Intel Corporation
  2211. + * Copyright (c) 2020-2023 Dorian Stoll
  2212. + *
  2213. + * Linux driver for Intel Precise Touch & Stylus
  2214. + */
  2215. +
  2216. +#include <linux/dma-mapping.h>
  2217. +#include <linux/types.h>
  2218. +
  2219. +#include "resources.h"
  2220. +#include "spec-device.h"
  2221. +
  2222. +static int ipts_resources_alloc_buffer(struct ipts_buffer *buffer, struct device *dev, size_t size)
  2223. +{
  2224. + if (!buffer)
  2225. + return -EFAULT;
  2226. +
  2227. + if (buffer->address)
  2228. + return 0;
  2229. +
  2230. + buffer->address = dma_alloc_coherent(dev, size, &buffer->dma_address, GFP_KERNEL);
  2231. +
  2232. + if (!buffer->address)
  2233. + return -ENOMEM;
  2234. +
  2235. + buffer->size = size;
  2236. + buffer->device = dev;
  2237. +
  2238. + return 0;
  2239. +}
  2240. +
  2241. +static void ipts_resources_free_buffer(struct ipts_buffer *buffer)
  2242. +{
  2243. + if (!buffer->address)
  2244. + return;
  2245. +
  2246. + dma_free_coherent(buffer->device, buffer->size, buffer->address, buffer->dma_address);
  2247. +
  2248. + buffer->address = NULL;
  2249. + buffer->size = 0;
  2250. +
  2251. + buffer->dma_address = 0;
  2252. + buffer->device = NULL;
  2253. +}
  2254. +
  2255. +int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t ds, size_t fs)
  2256. +{
  2257. + int ret = 0;
  2258. +
  2259. + if (!res)
  2260. + return -EFAULT;
  2261. +
  2262. + for (int i = 0; i < IPTS_BUFFERS; i++) {
  2263. + ret = ipts_resources_alloc_buffer(&res->data[i], dev, ds);
  2264. + if (ret)
  2265. + goto err;
  2266. + }
  2267. +
  2268. + for (int i = 0; i < IPTS_BUFFERS; i++) {
  2269. + ret = ipts_resources_alloc_buffer(&res->feedback[i], dev, fs);
  2270. + if (ret)
  2271. + goto err;
  2272. + }
  2273. +
  2274. + ret = ipts_resources_alloc_buffer(&res->doorbell, dev, sizeof(u32));
  2275. + if (ret)
  2276. + goto err;
  2277. +
  2278. + ret = ipts_resources_alloc_buffer(&res->workqueue, dev, sizeof(u32));
  2279. + if (ret)
  2280. + goto err;
  2281. +
  2282. + ret = ipts_resources_alloc_buffer(&res->hid2me, dev, fs);
  2283. + if (ret)
  2284. + goto err;
  2285. +
  2286. + ret = ipts_resources_alloc_buffer(&res->descriptor, dev, ds + 8);
  2287. + if (ret)
  2288. + goto err;
  2289. +
  2290. + return 0;
  2291. +
  2292. +err:
  2293. +
  2294. + ipts_resources_free(res);
  2295. + return ret;
  2296. +}
  2297. +
  2298. +int ipts_resources_free(struct ipts_resources *res)
  2299. +{
  2300. + if (!res)
  2301. + return -EFAULT;
  2302. +
  2303. + for (int i = 0; i < IPTS_BUFFERS; i++)
  2304. + ipts_resources_free_buffer(&res->data[i]);
  2305. +
  2306. + for (int i = 0; i < IPTS_BUFFERS; i++)
  2307. + ipts_resources_free_buffer(&res->feedback[i]);
  2308. +
  2309. + ipts_resources_free_buffer(&res->doorbell);
  2310. + ipts_resources_free_buffer(&res->workqueue);
  2311. + ipts_resources_free_buffer(&res->hid2me);
  2312. + ipts_resources_free_buffer(&res->descriptor);
  2313. +
  2314. + return 0;
  2315. +}
  2316. diff --git a/drivers/hid/ipts/resources.h b/drivers/hid/ipts/resources.h
  2317. new file mode 100644
  2318. index 000000000000..6cbb24a8a054
  2319. --- /dev/null
  2320. +++ b/drivers/hid/ipts/resources.h
  2321. @@ -0,0 +1,39 @@
  2322. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2323. +/*
  2324. + * Copyright (c) 2016 Intel Corporation
  2325. + * Copyright (c) 2020-2023 Dorian Stoll
  2326. + *
  2327. + * Linux driver for Intel Precise Touch & Stylus
  2328. + */
  2329. +
  2330. +#ifndef IPTS_RESOURCES_H
  2331. +#define IPTS_RESOURCES_H
  2332. +
  2333. +#include <linux/device.h>
  2334. +#include <linux/types.h>
  2335. +
  2336. +#include "spec-device.h"
  2337. +
  2338. +struct ipts_buffer {
  2339. + u8 *address;
  2340. + size_t size;
  2341. +
  2342. + dma_addr_t dma_address;
  2343. + struct device *device;
  2344. +};
  2345. +
  2346. +struct ipts_resources {
  2347. + struct ipts_buffer data[IPTS_BUFFERS];
  2348. + struct ipts_buffer feedback[IPTS_BUFFERS];
  2349. +
  2350. + struct ipts_buffer doorbell;
  2351. + struct ipts_buffer workqueue;
  2352. + struct ipts_buffer hid2me;
  2353. +
  2354. + struct ipts_buffer descriptor;
  2355. +};
  2356. +
  2357. +int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t ds, size_t fs);
  2358. +int ipts_resources_free(struct ipts_resources *res);
  2359. +
  2360. +#endif /* IPTS_RESOURCES_H */
  2361. diff --git a/drivers/hid/ipts/spec-data.h b/drivers/hid/ipts/spec-data.h
  2362. new file mode 100644
  2363. index 000000000000..e8dd98895a7e
  2364. --- /dev/null
  2365. +++ b/drivers/hid/ipts/spec-data.h
  2366. @@ -0,0 +1,100 @@
  2367. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2368. +/*
  2369. + * Copyright (c) 2016 Intel Corporation
  2370. + * Copyright (c) 2020-2023 Dorian Stoll
  2371. + *
  2372. + * Linux driver for Intel Precise Touch & Stylus
  2373. + */
  2374. +
  2375. +#ifndef IPTS_SPEC_DATA_H
  2376. +#define IPTS_SPEC_DATA_H
  2377. +
  2378. +#include <linux/build_bug.h>
  2379. +#include <linux/types.h>
  2380. +
  2381. +/**
  2382. + * enum ipts_feedback_cmd_type - Commands that can be executed on the sensor through feedback.
  2383. + */
  2384. +enum ipts_feedback_cmd_type {
  2385. + IPTS_FEEDBACK_CMD_TYPE_NONE = 0,
  2386. + IPTS_FEEDBACK_CMD_TYPE_SOFT_RESET = 1,
  2387. + IPTS_FEEDBACK_CMD_TYPE_GOTO_ARMED = 2,
  2388. + IPTS_FEEDBACK_CMD_TYPE_GOTO_SENSING = 3,
  2389. + IPTS_FEEDBACK_CMD_TYPE_GOTO_SLEEP = 4,
  2390. + IPTS_FEEDBACK_CMD_TYPE_GOTO_DOZE = 5,
  2391. + IPTS_FEEDBACK_CMD_TYPE_HARD_RESET = 6,
  2392. +};
  2393. +
  2394. +/**
  2395. + * enum ipts_feedback_data_type - Defines what data a feedback buffer contains.
  2396. + * @IPTS_FEEDBACK_DATA_TYPE_VENDOR: The buffer contains vendor specific feedback.
  2397. + * @IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES: The buffer contains a HID set features report.
  2398. + * @IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES: The buffer contains a HID get features report.
  2399. + * @IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT: The buffer contains a HID output report.
  2400. + * @IPTS_FEEDBACK_DATA_TYPE_STORE_DATA: The buffer contains calibration data for the sensor.
  2401. + */
  2402. +enum ipts_feedback_data_type {
  2403. + IPTS_FEEDBACK_DATA_TYPE_VENDOR = 0,
  2404. + IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES = 1,
  2405. + IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES = 2,
  2406. + IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT = 3,
  2407. + IPTS_FEEDBACK_DATA_TYPE_STORE_DATA = 4,
  2408. +};
  2409. +
  2410. +/**
  2411. + * struct ipts_feedback_header - Header that is prefixed to the data in a feedback buffer.
  2412. + * @cmd_type: A command that should be executed on the sensor.
  2413. + * @size: The size of the payload to be written.
  2414. + * @buffer: The ID of the buffer that contains this feedback data.
  2415. + * @protocol: The protocol version of the EDS.
  2416. + * @data_type: The type of data that the buffer contains.
  2417. + * @spi_offset: The offset at which to write the payload data to the sensor.
  2418. + * @payload: Payload for the feedback command, or 0 if no payload is sent.
  2419. + */
  2420. +struct ipts_feedback_header {
  2421. + enum ipts_feedback_cmd_type cmd_type;
  2422. + u32 size;
  2423. + u32 buffer;
  2424. + u32 protocol;
  2425. + enum ipts_feedback_data_type data_type;
  2426. + u32 spi_offset;
  2427. + u8 reserved[40];
  2428. + u8 payload[];
  2429. +} __packed;
  2430. +
  2431. +static_assert(sizeof(struct ipts_feedback_header) == 64);
  2432. +
  2433. +/**
  2434. + * enum ipts_data_type - Defines what type of data a buffer contains.
  2435. + * @IPTS_DATA_TYPE_FRAME: Raw data frame.
  2436. + * @IPTS_DATA_TYPE_ERROR: Error data.
  2437. + * @IPTS_DATA_TYPE_VENDOR: Vendor specific data.
  2438. + * @IPTS_DATA_TYPE_HID: A HID report.
  2439. + * @IPTS_DATA_TYPE_GET_FEATURES: The response to a GET_FEATURES HID2ME command.
  2440. + */
  2441. +enum ipts_data_type {
  2442. + IPTS_DATA_TYPE_FRAME = 0x00,
  2443. + IPTS_DATA_TYPE_ERROR = 0x01,
  2444. + IPTS_DATA_TYPE_VENDOR = 0x02,
  2445. + IPTS_DATA_TYPE_HID = 0x03,
  2446. + IPTS_DATA_TYPE_GET_FEATURES = 0x04,
  2447. + IPTS_DATA_TYPE_DESCRIPTOR = 0x05,
  2448. +};
  2449. +
  2450. +/**
  2451. + * struct ipts_data_header - Header that is prefixed to the data in a data buffer.
  2452. + * @type: What data the buffer contains.
  2453. + * @size: How much data the buffer contains.
  2454. + * @buffer: Which buffer the data is in.
  2455. + */
  2456. +struct ipts_data_header {
  2457. + enum ipts_data_type type;
  2458. + u32 size;
  2459. + u32 buffer;
  2460. + u8 reserved[52];
  2461. + u8 data[];
  2462. +} __packed;
  2463. +
  2464. +static_assert(sizeof(struct ipts_data_header) == 64);
  2465. +
  2466. +#endif /* IPTS_SPEC_DATA_H */
  2467. diff --git a/drivers/hid/ipts/spec-device.h b/drivers/hid/ipts/spec-device.h
  2468. new file mode 100644
  2469. index 000000000000..93f673d981f7
  2470. --- /dev/null
  2471. +++ b/drivers/hid/ipts/spec-device.h
  2472. @@ -0,0 +1,285 @@
  2473. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2474. +/*
  2475. + * Copyright (c) 2016 Intel Corporation
  2476. + * Copyright (c) 2020-2023 Dorian Stoll
  2477. + *
  2478. + * Linux driver for Intel Precise Touch & Stylus
  2479. + */
  2480. +
  2481. +#ifndef IPTS_SPEC_DEVICE_H
  2482. +#define IPTS_SPEC_DEVICE_H
  2483. +
  2484. +#include <linux/build_bug.h>
  2485. +#include <linux/types.h>
  2486. +
  2487. +/*
  2488. + * The amount of buffers that IPTS can use for data transfer.
  2489. + */
  2490. +#define IPTS_BUFFERS 16
  2491. +
  2492. +/*
  2493. + * The buffer ID that is used for HID2ME feedback
  2494. + */
  2495. +#define IPTS_HID2ME_BUFFER IPTS_BUFFERS
  2496. +
  2497. +/**
  2498. + * enum ipts_command - Commands that can be sent to the IPTS hardware.
  2499. + * @IPTS_CMD_GET_DEVICE_INFO: Retrieves vendor information from the device.
  2500. + * @IPTS_CMD_SET_MODE: Changes the mode that the device will operate in.
  2501. + * @IPTS_CMD_SET_MEM_WINDOW: Configures memory buffers for passing data between device and driver.
  2502. + * @IPTS_CMD_QUIESCE_IO: Stops the data flow from the device to the driver.
  2503. + * @IPTS_CMD_READY_FOR_DATA: Informs the device that the driver is ready to receive data.
  2504. + * @IPTS_CMD_FEEDBACK: Informs the device that a buffer was processed and can be refilled.
  2505. + * @IPTS_CMD_CLEAR_MEM_WINDOW: Stops the data flow and clears the buffer addresses on the device.
  2506. + * @IPTS_CMD_RESET_SENSOR: Resets the sensor to its default state.
  2507. + * @IPTS_CMD_GET_DESCRIPTOR: Retrieves the HID descriptor of the device.
  2508. + */
  2509. +enum ipts_command_code {
  2510. + IPTS_CMD_GET_DEVICE_INFO = 0x01,
  2511. + IPTS_CMD_SET_MODE = 0x02,
  2512. + IPTS_CMD_SET_MEM_WINDOW = 0x03,
  2513. + IPTS_CMD_QUIESCE_IO = 0x04,
  2514. + IPTS_CMD_READY_FOR_DATA = 0x05,
  2515. + IPTS_CMD_FEEDBACK = 0x06,
  2516. + IPTS_CMD_CLEAR_MEM_WINDOW = 0x07,
  2517. + IPTS_CMD_RESET_SENSOR = 0x0B,
  2518. + IPTS_CMD_GET_DESCRIPTOR = 0x0F,
  2519. +};
  2520. +
  2521. +/**
  2522. + * enum ipts_status - Possible status codes returned by the IPTS device.
  2523. + * @IPTS_STATUS_SUCCESS: Operation completed successfully.
  2524. + * @IPTS_STATUS_INVALID_PARAMS: Command contained an invalid payload.
  2525. + * @IPTS_STATUS_ACCESS_DENIED: ME could not validate a buffer address.
  2526. + * @IPTS_STATUS_CMD_SIZE_ERROR: Command contains an invalid payload.
  2527. + * @IPTS_STATUS_NOT_READY: Buffer addresses have not been set.
  2528. + * @IPTS_STATUS_REQUEST_OUTSTANDING: There is an outstanding command of the same type.
  2529. + * @IPTS_STATUS_NO_SENSOR_FOUND: No sensor could be found.
  2530. + * @IPTS_STATUS_OUT_OF_MEMORY: Not enough free memory for requested operation.
  2531. + * @IPTS_STATUS_INTERNAL_ERROR: An unexpected error occurred.
  2532. + * @IPTS_STATUS_SENSOR_DISABLED: The sensor has been disabled and must be reinitialized.
  2533. + * @IPTS_STATUS_COMPAT_CHECK_FAIL: Compatibility revision check between sensor and ME failed.
  2534. + * The host can ignore this error and attempt to continue.
  2535. + * @IPTS_STATUS_SENSOR_EXPECTED_RESET: The sensor went through a reset initiated by the driver.
  2536. + * @IPTS_STATUS_SENSOR_UNEXPECTED_RESET: The sensor went through an unexpected reset.
  2537. + * @IPTS_STATUS_RESET_FAILED: Requested sensor reset failed to complete.
  2538. + * @IPTS_STATUS_TIMEOUT: The operation timed out.
  2539. + * @IPTS_STATUS_TEST_MODE_FAIL: Test mode pattern did not match expected values.
  2540. + * @IPTS_STATUS_SENSOR_FAIL_FATAL: The sensor reported an error during reset sequence.
  2541. + * Further progress is not possible.
  2542. + * @IPTS_STATUS_SENSOR_FAIL_NONFATAL: The sensor reported an error during reset sequence.
  2543. + * The driver can attempt to continue.
  2544. + * @IPTS_STATUS_INVALID_DEVICE_CAPS: The device reported invalid capabilities.
  2545. + * @IPTS_STATUS_QUIESCE_IO_IN_PROGRESS: Command cannot be completed until Quiesce IO is done.
  2546. + */
  2547. +enum ipts_status {
  2548. + IPTS_STATUS_SUCCESS = 0x00,
  2549. + IPTS_STATUS_INVALID_PARAMS = 0x01,
  2550. + IPTS_STATUS_ACCESS_DENIED = 0x02,
  2551. + IPTS_STATUS_CMD_SIZE_ERROR = 0x03,
  2552. + IPTS_STATUS_NOT_READY = 0x04,
  2553. + IPTS_STATUS_REQUEST_OUTSTANDING = 0x05,
  2554. + IPTS_STATUS_NO_SENSOR_FOUND = 0x06,
  2555. + IPTS_STATUS_OUT_OF_MEMORY = 0x07,
  2556. + IPTS_STATUS_INTERNAL_ERROR = 0x08,
  2557. + IPTS_STATUS_SENSOR_DISABLED = 0x09,
  2558. + IPTS_STATUS_COMPAT_CHECK_FAIL = 0x0A,
  2559. + IPTS_STATUS_SENSOR_EXPECTED_RESET = 0x0B,
  2560. + IPTS_STATUS_SENSOR_UNEXPECTED_RESET = 0x0C,
  2561. + IPTS_STATUS_RESET_FAILED = 0x0D,
  2562. + IPTS_STATUS_TIMEOUT = 0x0E,
  2563. + IPTS_STATUS_TEST_MODE_FAIL = 0x0F,
  2564. + IPTS_STATUS_SENSOR_FAIL_FATAL = 0x10,
  2565. + IPTS_STATUS_SENSOR_FAIL_NONFATAL = 0x11,
  2566. + IPTS_STATUS_INVALID_DEVICE_CAPS = 0x12,
  2567. + IPTS_STATUS_QUIESCE_IO_IN_PROGRESS = 0x13,
  2568. +};
  2569. +
  2570. +/**
  2571. + * struct ipts_command - Message that is sent to the device for calling a command.
  2572. + * @cmd: The command that will be called.
  2573. + * @payload: Payload containing parameters for the called command.
  2574. + */
  2575. +struct ipts_command {
  2576. + enum ipts_command_code cmd;
  2577. + u8 payload[320];
  2578. +} __packed;
  2579. +
  2580. +static_assert(sizeof(struct ipts_command) == 324);
  2581. +
  2582. +/**
  2583. + * enum ipts_mode - Configures what data the device produces and how its sent.
  2584. + * @IPTS_MODE_EVENT: The device will send an event once a buffer was filled.
  2585. + * Older devices will return singletouch data in this mode.
  2586. + * @IPTS_MODE_DOORBELL: The device will notify the driver by incrementing the doorbell value.
  2587. + * Older devices will return multitouch data in this mode.
  2588. + */
  2589. +enum ipts_mode {
  2590. + IPTS_MODE_EVENT = 0x00,
  2591. + IPTS_MODE_DOORBELL = 0x01,
  2592. +};
  2593. +
  2594. +/**
  2595. + * struct ipts_set_mode - Payload for the SET_MODE command.
  2596. + * @mode: Changes the mode that IPTS will operate in.
  2597. + */
  2598. +struct ipts_set_mode {
  2599. + enum ipts_mode mode;
  2600. + u8 reserved[12];
  2601. +} __packed;
  2602. +
  2603. +static_assert(sizeof(struct ipts_set_mode) == 16);
  2604. +
  2605. +#define IPTS_WORKQUEUE_SIZE 8192
  2606. +#define IPTS_WORKQUEUE_ITEM_SIZE 16
  2607. +
  2608. +/**
  2609. + * struct ipts_mem_window - Payload for the SET_MEM_WINDOW command.
  2610. + * @data_addr_lower: Lower 32 bits of the data buffer addresses.
  2611. + * @data_addr_upper: Upper 32 bits of the data buffer addresses.
  2612. + * @workqueue_addr_lower: Lower 32 bits of the workqueue buffer address.
  2613. + * @workqueue_addr_upper: Upper 32 bits of the workqueue buffer address.
  2614. + * @doorbell_addr_lower: Lower 32 bits of the doorbell buffer address.
  2615. + * @doorbell_addr_upper: Upper 32 bits of the doorbell buffer address.
  2616. + * @feedbackaddr_lower: Lower 32 bits of the feedback buffer addresses.
  2617. + * @feedbackaddr_upper: Upper 32 bits of the feedback buffer addresses.
  2618. + * @hid2me_addr_lower: Lower 32 bits of the hid2me buffer address.
  2619. + * @hid2me_addr_upper: Upper 32 bits of the hid2me buffer address.
  2620. + * @hid2me_size: Size of the hid2me feedback buffer.
  2621. + * @workqueue_item_size: Magic value. Must be 16.
  2622. + * @workqueue_size: Magic value. Must be 8192.
  2623. + *
  2624. + * The workqueue related items in this struct are required for using
  2625. + * GuC submission with binary processing firmware. Since this driver does
  2626. + * not use GuC submission and instead exports raw data to userspace, these
  2627. + * items are not actually used, but they need to be allocated and passed
  2628. + * to the device, otherwise initialization will fail.
  2629. + */
  2630. +struct ipts_mem_window {
  2631. + u32 data_addr_lower[IPTS_BUFFERS];
  2632. + u32 data_addr_upper[IPTS_BUFFERS];
  2633. + u32 workqueue_addr_lower;
  2634. + u32 workqueue_addr_upper;
  2635. + u32 doorbell_addr_lower;
  2636. + u32 doorbell_addr_upper;
  2637. + u32 feedback_addr_lower[IPTS_BUFFERS];
  2638. + u32 feedback_addr_upper[IPTS_BUFFERS];
  2639. + u32 hid2me_addr_lower;
  2640. + u32 hid2me_addr_upper;
  2641. + u32 hid2me_size;
  2642. + u8 reserved1;
  2643. + u8 workqueue_item_size;
  2644. + u16 workqueue_size;
  2645. + u8 reserved[32];
  2646. +} __packed;
  2647. +
  2648. +static_assert(sizeof(struct ipts_mem_window) == 320);
  2649. +
  2650. +/**
  2651. + * struct ipts_quiesce_io - Payload for the QUIESCE_IO command.
  2652. + */
  2653. +struct ipts_quiesce_io {
  2654. + u8 reserved[12];
  2655. +} __packed;
  2656. +
  2657. +static_assert(sizeof(struct ipts_quiesce_io) == 12);
  2658. +
  2659. +/**
  2660. + * struct ipts_feedback - Payload for the FEEDBACK command.
  2661. + * @buffer: The buffer that the device should refill.
  2662. + */
  2663. +struct ipts_feedback {
  2664. + u32 buffer;
  2665. + u8 reserved[12];
  2666. +} __packed;
  2667. +
  2668. +static_assert(sizeof(struct ipts_feedback) == 16);
  2669. +
  2670. +/**
  2671. + * enum ipts_reset_type - Possible ways of resetting the device.
  2672. + * @IPTS_RESET_TYPE_HARD: Perform hardware reset using GPIO pin.
  2673. + * @IPTS_RESET_TYPE_SOFT: Perform software reset using SPI command.
  2674. + */
  2675. +enum ipts_reset_type {
  2676. + IPTS_RESET_TYPE_HARD = 0x00,
  2677. + IPTS_RESET_TYPE_SOFT = 0x01,
  2678. +};
  2679. +
  2680. +/**
  2681. + * struct ipts_reset - Payload for the RESET_SENSOR command.
  2682. + * @type: How the device should get reset.
  2683. + */
  2684. +struct ipts_reset_sensor {
  2685. + enum ipts_reset_type type;
  2686. + u8 reserved[4];
  2687. +} __packed;
  2688. +
  2689. +static_assert(sizeof(struct ipts_reset_sensor) == 8);
  2690. +
  2691. +/**
  2692. + * struct ipts_get_descriptor - Payload for the GET_DESCRIPTOR command.
  2693. + * @addr_lower: The lower 32 bits of the descriptor buffer address.
  2694. + * @addr_upper: The upper 32 bits of the descriptor buffer address.
  2695. + * @magic: A magic value. Must be 8.
  2696. + */
  2697. +struct ipts_get_descriptor {
  2698. + u32 addr_lower;
  2699. + u32 addr_upper;
  2700. + u32 magic;
  2701. + u8 reserved[12];
  2702. +} __packed;
  2703. +
  2704. +static_assert(sizeof(struct ipts_get_descriptor) == 24);
  2705. +
  2706. +/*
  2707. + * The type of a response is indicated by a
  2708. + * command code, with the most significant bit flipped to 1.
  2709. + */
  2710. +#define IPTS_RSP_BIT BIT(31)
  2711. +
  2712. +/**
  2713. + * struct ipts_response - Data returned from the device in response to a command.
  2714. + * @cmd: The command that this response answers (IPTS_RSP_BIT will be 1).
  2715. + * @status: The return code of the command.
  2716. + * @payload: The data that was produced by the command.
  2717. + */
  2718. +struct ipts_response {
  2719. + enum ipts_command_code cmd;
  2720. + enum ipts_status status;
  2721. + u8 payload[80];
  2722. +} __packed;
  2723. +
  2724. +static_assert(sizeof(struct ipts_response) == 88);
  2725. +
  2726. +/**
  2727. + * struct ipts_device_info - Vendor information of the IPTS device.
  2728. + * @vendor: Vendor ID of this device.
  2729. + * @product: Product ID of this device.
  2730. + * @hw_version: Hardware revision of this device.
  2731. + * @fw_version: Firmware revision of this device.
  2732. + * @data_size: Requested size for a data buffer.
  2733. + * @feedback_size: Requested size for a feedback buffer.
  2734. + * @mode: Mode that the device currently operates in.
  2735. + * @max_contacts: Maximum amount of concurrent touches the sensor can process.
  2736. + */
  2737. +struct ipts_device_info {
  2738. + u16 vendor;
  2739. + u16 product;
  2740. + u32 hw_version;
  2741. + u32 fw_version;
  2742. + u32 data_size;
  2743. + u32 feedback_size;
  2744. + enum ipts_mode mode;
  2745. + u8 max_contacts;
  2746. + u8 reserved1[3];
  2747. + u8 sensor_min_eds;
  2748. + u8 sensor_maj_eds;
  2749. + u8 me_min_eds;
  2750. + u8 me_maj_eds;
  2751. + u8 intf_eds;
  2752. + u8 reserved2[11];
  2753. +} __packed;
  2754. +
  2755. +static_assert(sizeof(struct ipts_device_info) == 44);
  2756. +
  2757. +#endif /* IPTS_SPEC_DEVICE_H */
  2758. diff --git a/drivers/hid/ipts/spec-hid.h b/drivers/hid/ipts/spec-hid.h
  2759. new file mode 100644
  2760. index 000000000000..ea70f29ff00c
  2761. --- /dev/null
  2762. +++ b/drivers/hid/ipts/spec-hid.h
  2763. @@ -0,0 +1,35 @@
  2764. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2765. +/*
  2766. + * Copyright (c) 2016 Intel Corporation
  2767. + * Copyright (c) 2020-2023 Dorian Stoll
  2768. + *
  2769. + * Linux driver for Intel Precise Touch & Stylus
  2770. + */
  2771. +
  2772. +#ifndef IPTS_SPEC_HID_H
  2773. +#define IPTS_SPEC_HID_H
  2774. +
  2775. +#include <linux/build_bug.h>
  2776. +#include <linux/types.h>
  2777. +
  2778. +/*
  2779. + * Made-up type for passing raw IPTS data in a HID report.
  2780. + */
  2781. +#define IPTS_HID_FRAME_TYPE_RAW 0xEE
  2782. +
  2783. +/**
  2784. + * struct ipts_hid_frame - Header that is prefixed to raw IPTS data wrapped in a HID report.
  2785. + * @size: Size of the data inside the report, including this header.
  2786. + * @type: What type of data does this report contain.
  2787. + */
  2788. +struct ipts_hid_header {
  2789. + u32 size;
  2790. + u8 reserved1;
  2791. + u8 type;
  2792. + u8 reserved2;
  2793. + u8 data[];
  2794. +} __packed;
  2795. +
  2796. +static_assert(sizeof(struct ipts_hid_header) == 7);
  2797. +
  2798. +#endif /* IPTS_SPEC_HID_H */
  2799. diff --git a/drivers/hid/ipts/thread.c b/drivers/hid/ipts/thread.c
  2800. new file mode 100644
  2801. index 000000000000..8b46f775c107
  2802. --- /dev/null
  2803. +++ b/drivers/hid/ipts/thread.c
  2804. @@ -0,0 +1,85 @@
  2805. +// SPDX-License-Identifier: GPL-2.0-or-later
  2806. +/*
  2807. + * Copyright (c) 2016 Intel Corporation
  2808. + * Copyright (c) 2023 Dorian Stoll
  2809. + *
  2810. + * Linux driver for Intel Precise Touch & Stylus
  2811. + */
  2812. +
  2813. +#include <linux/completion.h>
  2814. +#include <linux/err.h>
  2815. +#include <linux/kthread.h>
  2816. +#include <linux/mutex.h>
  2817. +
  2818. +#include "thread.h"
  2819. +
  2820. +bool ipts_thread_should_stop(struct ipts_thread *thread)
  2821. +{
  2822. + if (!thread)
  2823. + return false;
  2824. +
  2825. + return READ_ONCE(thread->should_stop);
  2826. +}
  2827. +
  2828. +static int ipts_thread_runner(void *data)
  2829. +{
  2830. + int ret = 0;
  2831. + struct ipts_thread *thread = data;
  2832. +
  2833. + if (!thread)
  2834. + return -EFAULT;
  2835. +
  2836. + if (!thread->threadfn)
  2837. + return -EFAULT;
  2838. +
  2839. + ret = thread->threadfn(thread);
  2840. + complete_all(&thread->done);
  2841. +
  2842. + return ret;
  2843. +}
  2844. +
  2845. +int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_thread *thread),
  2846. + void *data, const char *name)
  2847. +{
  2848. + if (!thread)
  2849. + return -EFAULT;
  2850. +
  2851. + if (!threadfn)
  2852. + return -EFAULT;
  2853. +
  2854. + init_completion(&thread->done);
  2855. +
  2856. + thread->data = data;
  2857. + thread->should_stop = false;
  2858. + thread->threadfn = threadfn;
  2859. +
  2860. + thread->thread = kthread_run(ipts_thread_runner, thread, name);
  2861. + return PTR_ERR_OR_ZERO(thread->thread);
  2862. +}
  2863. +
  2864. +int ipts_thread_stop(struct ipts_thread *thread)
  2865. +{
  2866. + int ret = 0;
  2867. +
  2868. + if (!thread)
  2869. + return -EFAULT;
  2870. +
  2871. + if (!thread->thread)
  2872. + return 0;
  2873. +
  2874. + WRITE_ONCE(thread->should_stop, true);
  2875. +
  2876. + /*
  2877. + * Make sure that the write has gone through before waiting.
  2878. + */
  2879. + wmb();
  2880. +
  2881. + wait_for_completion(&thread->done);
  2882. + ret = kthread_stop(thread->thread);
  2883. +
  2884. + thread->thread = NULL;
  2885. + thread->data = NULL;
  2886. + thread->threadfn = NULL;
  2887. +
  2888. + return ret;
  2889. +}
  2890. diff --git a/drivers/hid/ipts/thread.h b/drivers/hid/ipts/thread.h
  2891. new file mode 100644
  2892. index 000000000000..a314843599fc
  2893. --- /dev/null
  2894. +++ b/drivers/hid/ipts/thread.h
  2895. @@ -0,0 +1,60 @@
  2896. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2897. +/*
  2898. + * Copyright (c) 2016 Intel Corporation
  2899. + * Copyright (c) 2023 Dorian Stoll
  2900. + *
  2901. + * Linux driver for Intel Precise Touch & Stylus
  2902. + */
  2903. +
  2904. +#ifndef IPTS_THREAD_H
  2905. +#define IPTS_THREAD_H
  2906. +
  2907. +#include <linux/completion.h>
  2908. +#include <linux/mutex.h>
  2909. +#include <linux/sched.h>
  2910. +
  2911. +/*
  2912. + * This wrapper over kthread is necessary, because calling kthread_stop makes it impossible
  2913. + * to issue MEI commands from that thread while it shuts itself down. By using a custom
  2914. + * boolean variable and a completion object, we can call kthread_stop only when the thread
  2915. + * already finished all of its work and has returned.
  2916. + */
  2917. +struct ipts_thread {
  2918. + struct task_struct *thread;
  2919. +
  2920. + bool should_stop;
  2921. + struct completion done;
  2922. +
  2923. + void *data;
  2924. + int (*threadfn)(struct ipts_thread *thread);
  2925. +};
  2926. +
  2927. +/*
  2928. + * ipts_thread_should_stop() - Returns true if the thread is asked to terminate.
  2929. + * @thread: The current thread.
  2930. + *
  2931. + * Returns: true if the thread should stop, false if not.
  2932. + */
  2933. +bool ipts_thread_should_stop(struct ipts_thread *thread);
  2934. +
  2935. +/*
  2936. + * ipts_thread_start() - Starts an IPTS thread.
  2937. + * @thread: The thread to initialize and start.
  2938. + * @threadfn: The function to execute.
  2939. + * @data: An argument that will be passed to threadfn.
  2940. + * @name: The name of the new thread.
  2941. + *
  2942. + * Returns: 0 on success, <0 on error.
  2943. + */
  2944. +int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_thread *thread),
  2945. + void *data, const char name[]);
  2946. +
  2947. +/*
  2948. + * ipts_thread_stop() - Asks the thread to terminate and waits until it has finished.
  2949. + * @thread: The thread that should stop.
  2950. + *
  2951. + * Returns: The return value of the thread function.
  2952. + */
  2953. +int ipts_thread_stop(struct ipts_thread *thread);
  2954. +
  2955. +#endif /* IPTS_THREAD_H */
  2956. --
  2957. 2.40.1
  2958. From 6e4d69adf4555cb69048791f924940847f9729ad Mon Sep 17 00:00:00 2001
  2959. From: Dorian Stoll <dorian.stoll@tmsp.io>
  2960. Date: Fri, 28 Apr 2023 15:41:12 +0200
  2961. Subject: [PATCH] Update IPTS from module repo
  2962. Changes:
  2963. * Fix redefinition error on AOSP clang
  2964. * Increase the polling frequency to reduce latency
  2965. * Don't allocate a new buffer for every HID report
  2966. * Always use the generic HID driver instead of forcing hid-multitouch
  2967. Based on https://github.com/linux-surface/intel-precise-touch/commit/a2b675d72dbde80ebe36a5b6ceaebd596c030314
  2968. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  2969. Patchset: ipts
  2970. ---
  2971. drivers/hid/ipts/hid.c | 15 +++++----------
  2972. drivers/hid/ipts/receiver.c | 2 +-
  2973. drivers/hid/ipts/resources.c | 35 +++++++++++++++++++++++++++++++----
  2974. drivers/hid/ipts/resources.h | 3 +++
  2975. 4 files changed, 40 insertions(+), 15 deletions(-)
  2976. diff --git a/drivers/hid/ipts/hid.c b/drivers/hid/ipts/hid.c
  2977. index 6782394e8dde..a2471219615b 100644
  2978. --- a/drivers/hid/ipts/hid.c
  2979. +++ b/drivers/hid/ipts/hid.c
  2980. @@ -237,7 +237,6 @@ static struct hid_ll_driver ipts_hid_driver = {
  2981. int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  2982. {
  2983. - int ret = 0;
  2984. u8 *temp = NULL;
  2985. struct ipts_hid_header *frame = NULL;
  2986. struct ipts_data_header *header = NULL;
  2987. @@ -250,6 +249,9 @@ int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  2988. header = (struct ipts_data_header *)ipts->resources.data[buffer].address;
  2989. + temp = ipts->resources.report.address;
  2990. + memset(temp, 0, ipts->resources.report.size);
  2991. +
  2992. if (!header)
  2993. return -EFAULT;
  2994. @@ -273,10 +275,6 @@ int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  2995. if (header->size + 3 + sizeof(struct ipts_hid_header) > IPTS_HID_REPORT_DATA_SIZE)
  2996. return -ERANGE;
  2997. - temp = kzalloc(IPTS_HID_REPORT_DATA_SIZE, GFP_KERNEL);
  2998. - if (!temp)
  2999. - return -ENOMEM;
  3000. -
  3001. /*
  3002. * Synthesize a HID report matching the devices that natively send HID reports
  3003. */
  3004. @@ -288,10 +286,7 @@ int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  3005. memcpy(frame->data, header->data, header->size);
  3006. - ret = hid_input_report(ipts->hid, HID_INPUT_REPORT, temp, IPTS_HID_REPORT_DATA_SIZE, 1);
  3007. - kfree(temp);
  3008. -
  3009. - return ret;
  3010. + return hid_input_report(ipts->hid, HID_INPUT_REPORT, temp, IPTS_HID_REPORT_DATA_SIZE, 1);
  3011. }
  3012. int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info)
  3013. @@ -318,7 +313,7 @@ int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info)
  3014. ipts->hid->vendor = info.vendor;
  3015. ipts->hid->product = info.product;
  3016. - ipts->hid->group = HID_GROUP_MULTITOUCH;
  3017. + ipts->hid->group = HID_GROUP_GENERIC;
  3018. snprintf(ipts->hid->name, sizeof(ipts->hid->name), "IPTS %04X:%04X", info.vendor,
  3019. info.product);
  3020. diff --git a/drivers/hid/ipts/receiver.c b/drivers/hid/ipts/receiver.c
  3021. index 77234f9e0e17..f56e9ed32d57 100644
  3022. --- a/drivers/hid/ipts/receiver.c
  3023. +++ b/drivers/hid/ipts/receiver.c
  3024. @@ -42,7 +42,7 @@ static void ipts_receiver_backoff(time64_t last, u32 n)
  3025. * n seconds, sleep longer to avoid wasting CPU cycles.
  3026. */
  3027. if (last + n > ktime_get_seconds())
  3028. - msleep(20);
  3029. + usleep_range(1 * USEC_PER_MSEC, 5 * USEC_PER_MSEC);
  3030. else
  3031. msleep(200);
  3032. }
  3033. diff --git a/drivers/hid/ipts/resources.c b/drivers/hid/ipts/resources.c
  3034. index 80ba5885bb55..5e924d58c488 100644
  3035. --- a/drivers/hid/ipts/resources.c
  3036. +++ b/drivers/hid/ipts/resources.c
  3037. @@ -9,6 +9,7 @@
  3038. #include <linux/dma-mapping.h>
  3039. #include <linux/types.h>
  3040. +#include "desc.h"
  3041. #include "resources.h"
  3042. #include "spec-device.h"
  3043. @@ -49,16 +50,22 @@ int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t d
  3044. {
  3045. int ret = 0;
  3046. + /*
  3047. + * Some compilers (AOSP clang) complain about a redefined
  3048. + * variable when this is declared inside of the for loop.
  3049. + */
  3050. + int i = 0;
  3051. +
  3052. if (!res)
  3053. return -EFAULT;
  3054. - for (int i = 0; i < IPTS_BUFFERS; i++) {
  3055. + for (i = 0; i < IPTS_BUFFERS; i++) {
  3056. ret = ipts_resources_alloc_buffer(&res->data[i], dev, ds);
  3057. if (ret)
  3058. goto err;
  3059. }
  3060. - for (int i = 0; i < IPTS_BUFFERS; i++) {
  3061. + for (i = 0; i < IPTS_BUFFERS; i++) {
  3062. ret = ipts_resources_alloc_buffer(&res->feedback[i], dev, fs);
  3063. if (ret)
  3064. goto err;
  3065. @@ -80,6 +87,16 @@ int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t d
  3066. if (ret)
  3067. goto err;
  3068. + if (!res->report.address) {
  3069. + res->report.size = IPTS_HID_REPORT_DATA_SIZE;
  3070. + res->report.address = kzalloc(res->report.size, GFP_KERNEL);
  3071. +
  3072. + if (!res->report.address) {
  3073. + ret = -ENOMEM;
  3074. + goto err;
  3075. + }
  3076. + }
  3077. +
  3078. return 0;
  3079. err:
  3080. @@ -90,13 +107,19 @@ int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t d
  3081. int ipts_resources_free(struct ipts_resources *res)
  3082. {
  3083. + /*
  3084. + * Some compilers (AOSP clang) complain about a redefined
  3085. + * variable when this is declared inside of the for loop.
  3086. + */
  3087. + int i = 0;
  3088. +
  3089. if (!res)
  3090. return -EFAULT;
  3091. - for (int i = 0; i < IPTS_BUFFERS; i++)
  3092. + for (i = 0; i < IPTS_BUFFERS; i++)
  3093. ipts_resources_free_buffer(&res->data[i]);
  3094. - for (int i = 0; i < IPTS_BUFFERS; i++)
  3095. + for (i = 0; i < IPTS_BUFFERS; i++)
  3096. ipts_resources_free_buffer(&res->feedback[i]);
  3097. ipts_resources_free_buffer(&res->doorbell);
  3098. @@ -104,5 +127,9 @@ int ipts_resources_free(struct ipts_resources *res)
  3099. ipts_resources_free_buffer(&res->hid2me);
  3100. ipts_resources_free_buffer(&res->descriptor);
  3101. + kfree(res->report.address);
  3102. + res->report.address = NULL;
  3103. + res->report.size = 0;
  3104. +
  3105. return 0;
  3106. }
  3107. diff --git a/drivers/hid/ipts/resources.h b/drivers/hid/ipts/resources.h
  3108. index 6cbb24a8a054..e0c400f420b9 100644
  3109. --- a/drivers/hid/ipts/resources.h
  3110. +++ b/drivers/hid/ipts/resources.h
  3111. @@ -31,6 +31,9 @@ struct ipts_resources {
  3112. struct ipts_buffer hid2me;
  3113. struct ipts_buffer descriptor;
  3114. +
  3115. + // Buffer for synthesizing HID reports
  3116. + struct ipts_buffer report;
  3117. };
  3118. int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t ds, size_t fs);
  3119. --
  3120. 2.40.1