0005-ipts.patch 87 KB

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