0004-ipts.patch 86 KB

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