0005-ipts.patch 86 KB

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