0005-ipts.patch 86 KB

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