0004-ipts.patch 86 KB

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