0005-ipts.patch 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241
  1. From 9afe48759efd50b5a28f2e01118464a72f1cfb70 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 a5f88ec97df7..e379dc2373b5 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 d6ff9d82ae94..a1b714505f43 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.49.0
  33. From 0e2a5e55b467f538bdf2ec6c91c4b7aa36fc083f 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 9ab5371c3538..1839a831a89f 100644
  52. --- a/drivers/iommu/intel/iommu.c
  53. +++ b/drivers/iommu/intel/iommu.c
  54. @@ -40,6 +40,11 @@
  55. #define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
  56. #define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
  57. +#define IS_IPTS(pdev) ( \
  58. + ((pdev)->vendor == PCI_VENDOR_ID_INTEL && (pdev)->device == 0x9D3E) || \
  59. + ((pdev)->vendor == PCI_VENDOR_ID_INTEL && (pdev)->device == 0x34E4) \
  60. + )
  61. +
  62. #define IOAPIC_RANGE_START (0xfee00000)
  63. #define IOAPIC_RANGE_END (0xfeefffff)
  64. #define IOVA_START_ADDR (0x1000)
  65. @@ -208,12 +213,14 @@ int intel_iommu_sm = IS_ENABLED(CONFIG_INTEL_IOMMU_SCALABLE_MODE_DEFAULT_ON);
  66. int intel_iommu_enabled = 0;
  67. EXPORT_SYMBOL_GPL(intel_iommu_enabled);
  68. +static int dmar_map_ipts = 1;
  69. static int intel_iommu_superpage = 1;
  70. static int iommu_identity_mapping;
  71. static int iommu_skip_te_disable;
  72. static int disable_igfx_iommu;
  73. #define IDENTMAP_AZALIA 4
  74. +#define IDENTMAP_IPTS 16
  75. const struct iommu_ops intel_iommu_ops;
  76. static const struct iommu_dirty_ops intel_dirty_ops;
  77. @@ -1903,6 +1910,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. @@ -2201,6 +2211,9 @@ static int __init init_dmars(void)
  86. iommu_set_root_entry(iommu);
  87. }
  88. + if (!dmar_map_ipts)
  89. + iommu_identity_mapping |= IDENTMAP_IPTS;
  90. +
  91. check_tylersburg_isoch();
  92. /*
  93. @@ -4509,6 +4522,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. @@ -4544,6 +4569,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.49.0
  123. From bed448bd281fe2969f17049a5a2192d1e5374618 Mon Sep 17 00:00:00 2001
  124. From: Dorian Stoll <dorian.stoll@tmsp.io>
  125. Date: Sun, 11 Dec 2022 12:00:59 +0100
  126. Subject: [PATCH] hid: Add support for Intel Precise Touch and Stylus
  127. Based on linux-surface/intel-precise-touch@8abe268
  128. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  129. Patchset: ipts
  130. ---
  131. drivers/hid/Kconfig | 2 +
  132. drivers/hid/Makefile | 2 +
  133. drivers/hid/ipts/Kconfig | 14 +
  134. drivers/hid/ipts/Makefile | 16 ++
  135. drivers/hid/ipts/cmd.c | 61 +++++
  136. drivers/hid/ipts/cmd.h | 60 ++++
  137. drivers/hid/ipts/context.h | 52 ++++
  138. drivers/hid/ipts/control.c | 486 +++++++++++++++++++++++++++++++++
  139. drivers/hid/ipts/control.h | 126 +++++++++
  140. drivers/hid/ipts/desc.h | 80 ++++++
  141. drivers/hid/ipts/eds1.c | 104 +++++++
  142. drivers/hid/ipts/eds1.h | 35 +++
  143. drivers/hid/ipts/eds2.c | 145 ++++++++++
  144. drivers/hid/ipts/eds2.h | 35 +++
  145. drivers/hid/ipts/hid.c | 225 +++++++++++++++
  146. drivers/hid/ipts/hid.h | 24 ++
  147. drivers/hid/ipts/main.c | 126 +++++++++
  148. drivers/hid/ipts/mei.c | 188 +++++++++++++
  149. drivers/hid/ipts/mei.h | 66 +++++
  150. drivers/hid/ipts/receiver.c | 251 +++++++++++++++++
  151. drivers/hid/ipts/receiver.h | 16 ++
  152. drivers/hid/ipts/resources.c | 131 +++++++++
  153. drivers/hid/ipts/resources.h | 41 +++
  154. drivers/hid/ipts/spec-data.h | 100 +++++++
  155. drivers/hid/ipts/spec-device.h | 290 ++++++++++++++++++++
  156. drivers/hid/ipts/spec-hid.h | 34 +++
  157. drivers/hid/ipts/thread.c | 84 ++++++
  158. drivers/hid/ipts/thread.h | 59 ++++
  159. 28 files changed, 2853 insertions(+)
  160. create mode 100644 drivers/hid/ipts/Kconfig
  161. create mode 100644 drivers/hid/ipts/Makefile
  162. create mode 100644 drivers/hid/ipts/cmd.c
  163. create mode 100644 drivers/hid/ipts/cmd.h
  164. create mode 100644 drivers/hid/ipts/context.h
  165. create mode 100644 drivers/hid/ipts/control.c
  166. create mode 100644 drivers/hid/ipts/control.h
  167. create mode 100644 drivers/hid/ipts/desc.h
  168. create mode 100644 drivers/hid/ipts/eds1.c
  169. create mode 100644 drivers/hid/ipts/eds1.h
  170. create mode 100644 drivers/hid/ipts/eds2.c
  171. create mode 100644 drivers/hid/ipts/eds2.h
  172. create mode 100644 drivers/hid/ipts/hid.c
  173. create mode 100644 drivers/hid/ipts/hid.h
  174. create mode 100644 drivers/hid/ipts/main.c
  175. create mode 100644 drivers/hid/ipts/mei.c
  176. create mode 100644 drivers/hid/ipts/mei.h
  177. create mode 100644 drivers/hid/ipts/receiver.c
  178. create mode 100644 drivers/hid/ipts/receiver.h
  179. create mode 100644 drivers/hid/ipts/resources.c
  180. create mode 100644 drivers/hid/ipts/resources.h
  181. create mode 100644 drivers/hid/ipts/spec-data.h
  182. create mode 100644 drivers/hid/ipts/spec-device.h
  183. create mode 100644 drivers/hid/ipts/spec-hid.h
  184. create mode 100644 drivers/hid/ipts/thread.c
  185. create mode 100644 drivers/hid/ipts/thread.h
  186. diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
  187. index 363c860835d3..799aceeeea89 100644
  188. --- a/drivers/hid/Kconfig
  189. +++ b/drivers/hid/Kconfig
  190. @@ -1387,4 +1387,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 1f50a6ecadbb..b82016429b01 100644
  197. --- a/drivers/hid/Makefile
  198. +++ b/drivers/hid/Makefile
  199. @@ -170,3 +170,5 @@ obj-$(CONFIG_INTEL_ISH_HID) += 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..7b9f54388a9f
  1150. --- /dev/null
  1151. +++ b/drivers/hid/ipts/eds1.c
  1152. @@ -0,0 +1,104 @@
  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 "eds1.h"
  1170. +#include "spec-device.h"
  1171. +
  1172. +int ipts_eds1_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t *desc_size)
  1173. +{
  1174. + size_t size = 0;
  1175. + u8 *buffer = NULL;
  1176. +
  1177. + if (!ipts)
  1178. + return -EFAULT;
  1179. +
  1180. + if (!desc_buffer)
  1181. + return -EFAULT;
  1182. +
  1183. + if (!desc_size)
  1184. + return -EFAULT;
  1185. +
  1186. + size = sizeof(ipts_singletouch_descriptor) + sizeof(ipts_fallback_descriptor);
  1187. +
  1188. + buffer = kzalloc(size, GFP_KERNEL);
  1189. + if (!buffer)
  1190. + return -ENOMEM;
  1191. +
  1192. + memcpy(buffer, ipts_singletouch_descriptor, sizeof(ipts_singletouch_descriptor));
  1193. + memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts_fallback_descriptor,
  1194. + sizeof(ipts_fallback_descriptor));
  1195. +
  1196. + *desc_size = size;
  1197. + *desc_buffer = buffer;
  1198. +
  1199. + return 0;
  1200. +}
  1201. +
  1202. +static int ipts_eds1_switch_mode(struct ipts_context *ipts, enum ipts_mode mode)
  1203. +{
  1204. + int ret = 0;
  1205. +
  1206. + if (!ipts)
  1207. + return -EFAULT;
  1208. +
  1209. + if (ipts->mode == mode)
  1210. + return 0;
  1211. +
  1212. + ipts->mode = mode;
  1213. +
  1214. + ret = ipts_control_restart(ipts);
  1215. + if (ret)
  1216. + dev_err(ipts->dev, "Failed to switch modes: %d\n", ret);
  1217. +
  1218. + return ret;
  1219. +}
  1220. +
  1221. +int ipts_eds1_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  1222. + enum hid_report_type report_type, enum hid_class_request request_type)
  1223. +{
  1224. + int ret = 0;
  1225. +
  1226. + if (!ipts)
  1227. + return -EFAULT;
  1228. +
  1229. + if (!buffer)
  1230. + return -EFAULT;
  1231. +
  1232. + if (report_id != IPTS_HID_REPORT_SET_MODE)
  1233. + return -EIO;
  1234. +
  1235. + if (report_type != HID_FEATURE_REPORT)
  1236. + return -EIO;
  1237. +
  1238. + if (size != 2)
  1239. + return -EINVAL;
  1240. +
  1241. + /*
  1242. + * Implement mode switching report for older devices without native HID support.
  1243. + */
  1244. +
  1245. + if (request_type == HID_REQ_GET_REPORT) {
  1246. + memset(buffer, 0, size);
  1247. + buffer[0] = report_id;
  1248. + buffer[1] = ipts->mode;
  1249. + } else if (request_type == HID_REQ_SET_REPORT) {
  1250. + return ipts_eds1_switch_mode(ipts, buffer[1]);
  1251. + } else {
  1252. + return -EIO;
  1253. + }
  1254. +
  1255. + return ret;
  1256. +}
  1257. diff --git a/drivers/hid/ipts/eds1.h b/drivers/hid/ipts/eds1.h
  1258. new file mode 100644
  1259. index 000000000000..eeeb6575e3e8
  1260. --- /dev/null
  1261. +++ b/drivers/hid/ipts/eds1.h
  1262. @@ -0,0 +1,35 @@
  1263. +// SPDX-License-Identifier: GPL-2.0-or-later
  1264. +/*
  1265. + * Copyright (c) 2023 Dorian Stoll
  1266. + *
  1267. + * Linux driver for Intel Precise Touch & Stylus
  1268. + */
  1269. +
  1270. +#include <linux/hid.h>
  1271. +#include <linux/types.h>
  1272. +
  1273. +#include "context.h"
  1274. +
  1275. +/**
  1276. + * ipts_eds1_get_descriptor() - Assembles the HID descriptor of the device.
  1277. + * @ipts: The IPTS driver context.
  1278. + * @desc_buffer: A pointer to the location where the address of the allocated buffer is stored.
  1279. + * @desc_size: A pointer to the location where the size of the allocated buffer is stored.
  1280. + *
  1281. + * Returns: 0 on success, <0 on error.
  1282. + */
  1283. +int ipts_eds1_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t *desc_size);
  1284. +
  1285. +/**
  1286. + * ipts_eds1_raw_request() - Executes an output or feature report on the device.
  1287. + * @ipts: The IPTS driver context.
  1288. + * @buffer: The buffer containing the report.
  1289. + * @size: The size of the buffer.
  1290. + * @report_id: The HID report ID.
  1291. + * @report_type: Whether this report is an output or a feature report.
  1292. + * @request_type: Whether this report requests or sends data.
  1293. + *
  1294. + * Returns: 0 on success, <0 on error.
  1295. + */
  1296. +int ipts_eds1_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  1297. + enum hid_report_type report_type, enum hid_class_request request_type);
  1298. diff --git a/drivers/hid/ipts/eds2.c b/drivers/hid/ipts/eds2.c
  1299. new file mode 100644
  1300. index 000000000000..639940794615
  1301. --- /dev/null
  1302. +++ b/drivers/hid/ipts/eds2.c
  1303. @@ -0,0 +1,145 @@
  1304. +// SPDX-License-Identifier: GPL-2.0-or-later
  1305. +/*
  1306. + * Copyright (c) 2023 Dorian Stoll
  1307. + *
  1308. + * Linux driver for Intel Precise Touch & Stylus
  1309. + */
  1310. +
  1311. +#include <linux/completion.h>
  1312. +#include <linux/err.h>
  1313. +#include <linux/gfp.h>
  1314. +#include <linux/mutex.h>
  1315. +#include <linux/slab.h>
  1316. +#include <linux/types.h>
  1317. +
  1318. +#include "context.h"
  1319. +#include "control.h"
  1320. +#include "desc.h"
  1321. +#include "eds2.h"
  1322. +#include "spec-data.h"
  1323. +
  1324. +int ipts_eds2_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t *desc_size)
  1325. +{
  1326. + size_t size = 0;
  1327. + u8 *buffer = NULL;
  1328. +
  1329. + if (!ipts)
  1330. + return -EFAULT;
  1331. +
  1332. + if (!desc_buffer)
  1333. + return -EFAULT;
  1334. +
  1335. + if (!desc_size)
  1336. + return -EFAULT;
  1337. +
  1338. + size = sizeof(ipts_singletouch_descriptor) + ipts->descriptor.size;
  1339. +
  1340. + buffer = kzalloc(size, GFP_KERNEL);
  1341. + if (!buffer)
  1342. + return -ENOMEM;
  1343. +
  1344. + memcpy(buffer, ipts_singletouch_descriptor, sizeof(ipts_singletouch_descriptor));
  1345. + memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts->descriptor.address,
  1346. + ipts->descriptor.size);
  1347. +
  1348. + *desc_size = size;
  1349. + *desc_buffer = buffer;
  1350. +
  1351. + return 0;
  1352. +}
  1353. +
  1354. +static int ipts_eds2_get_feature(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  1355. + enum ipts_feedback_data_type type)
  1356. +{
  1357. + int ret = 0;
  1358. +
  1359. + if (!ipts)
  1360. + return -EFAULT;
  1361. +
  1362. + if (!buffer)
  1363. + return -EFAULT;
  1364. +
  1365. + mutex_lock(&ipts->feature_lock);
  1366. +
  1367. + memset(buffer, 0, size);
  1368. + buffer[0] = report_id;
  1369. +
  1370. + memset(&ipts->feature_report, 0, sizeof(ipts->feature_report));
  1371. + reinit_completion(&ipts->feature_event);
  1372. +
  1373. + ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buffer, size);
  1374. + if (ret) {
  1375. + dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  1376. + goto out;
  1377. + }
  1378. +
  1379. + ret = wait_for_completion_timeout(&ipts->feature_event, msecs_to_jiffies(5000));
  1380. + if (ret == 0) {
  1381. + dev_warn(ipts->dev, "GET_FEATURES timed out!\n");
  1382. + ret = -EIO;
  1383. + goto out;
  1384. + }
  1385. +
  1386. + if (!ipts->feature_report.address) {
  1387. + ret = -EFAULT;
  1388. + goto out;
  1389. + }
  1390. +
  1391. + if (ipts->feature_report.size > size) {
  1392. + ret = -ETOOSMALL;
  1393. + goto out;
  1394. + }
  1395. +
  1396. + ret = ipts->feature_report.size;
  1397. + memcpy(buffer, ipts->feature_report.address, ipts->feature_report.size);
  1398. +
  1399. +out:
  1400. + mutex_unlock(&ipts->feature_lock);
  1401. + return ret;
  1402. +}
  1403. +
  1404. +static int ipts_eds2_set_feature(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  1405. + enum ipts_feedback_data_type type)
  1406. +{
  1407. + int ret = 0;
  1408. +
  1409. + if (!ipts)
  1410. + return -EFAULT;
  1411. +
  1412. + if (!buffer)
  1413. + return -EFAULT;
  1414. +
  1415. + buffer[0] = report_id;
  1416. +
  1417. + ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buffer, size);
  1418. + if (ret)
  1419. + dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  1420. +
  1421. + return ret;
  1422. +}
  1423. +
  1424. +int ipts_eds2_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  1425. + enum hid_report_type report_type, enum hid_class_request request_type)
  1426. +{
  1427. + enum ipts_feedback_data_type feedback_type = IPTS_FEEDBACK_DATA_TYPE_VENDOR;
  1428. +
  1429. + if (!ipts)
  1430. + return -EFAULT;
  1431. +
  1432. + if (!buffer)
  1433. + return -EFAULT;
  1434. +
  1435. + if (report_type == HID_OUTPUT_REPORT && request_type == HID_REQ_SET_REPORT)
  1436. + feedback_type = IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT;
  1437. + else if (report_type == HID_FEATURE_REPORT && request_type == HID_REQ_GET_REPORT)
  1438. + feedback_type = IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES;
  1439. + else if (report_type == HID_FEATURE_REPORT && request_type == HID_REQ_SET_REPORT)
  1440. + feedback_type = IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES;
  1441. + else
  1442. + return -EIO;
  1443. +
  1444. + if (request_type == HID_REQ_GET_REPORT)
  1445. + return ipts_eds2_get_feature(ipts, buffer, size, report_id, feedback_type);
  1446. + else
  1447. + return ipts_eds2_set_feature(ipts, buffer, size, report_id, feedback_type);
  1448. +}
  1449. diff --git a/drivers/hid/ipts/eds2.h b/drivers/hid/ipts/eds2.h
  1450. new file mode 100644
  1451. index 000000000000..064e3716907a
  1452. --- /dev/null
  1453. +++ b/drivers/hid/ipts/eds2.h
  1454. @@ -0,0 +1,35 @@
  1455. +// SPDX-License-Identifier: GPL-2.0-or-later
  1456. +/*
  1457. + * Copyright (c) 2023 Dorian Stoll
  1458. + *
  1459. + * Linux driver for Intel Precise Touch & Stylus
  1460. + */
  1461. +
  1462. +#include <linux/hid.h>
  1463. +#include <linux/types.h>
  1464. +
  1465. +#include "context.h"
  1466. +
  1467. +/**
  1468. + * ipts_eds2_get_descriptor() - Assembles the HID descriptor of the device.
  1469. + * @ipts: The IPTS driver context.
  1470. + * @desc_buffer: A pointer to the location where the address of the allocated buffer is stored.
  1471. + * @desc_size: A pointer to the location where the size of the allocated buffer is stored.
  1472. + *
  1473. + * Returns: 0 on success, <0 on error.
  1474. + */
  1475. +int ipts_eds2_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t *desc_size);
  1476. +
  1477. +/**
  1478. + * ipts_eds2_raw_request() - Executes an output or feature report on the device.
  1479. + * @ipts: The IPTS driver context.
  1480. + * @buffer: The buffer containing the report.
  1481. + * @size: The size of the buffer.
  1482. + * @report_id: The HID report ID.
  1483. + * @report_type: Whether this report is an output or a feature report.
  1484. + * @request_type: Whether this report requests or sends data.
  1485. + *
  1486. + * Returns: 0 on success, <0 on error.
  1487. + */
  1488. +int ipts_eds2_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  1489. + enum hid_report_type report_type, enum hid_class_request request_type);
  1490. diff --git a/drivers/hid/ipts/hid.c b/drivers/hid/ipts/hid.c
  1491. new file mode 100644
  1492. index 000000000000..e34a1a4f9fa7
  1493. --- /dev/null
  1494. +++ b/drivers/hid/ipts/hid.c
  1495. @@ -0,0 +1,225 @@
  1496. +// SPDX-License-Identifier: GPL-2.0-or-later
  1497. +/*
  1498. + * Copyright (c) 2022-2023 Dorian Stoll
  1499. + *
  1500. + * Linux driver for Intel Precise Touch & Stylus
  1501. + */
  1502. +
  1503. +#include <linux/completion.h>
  1504. +#include <linux/err.h>
  1505. +#include <linux/gfp.h>
  1506. +#include <linux/hid.h>
  1507. +#include <linux/mutex.h>
  1508. +#include <linux/slab.h>
  1509. +#include <linux/types.h>
  1510. +
  1511. +#include "context.h"
  1512. +#include "desc.h"
  1513. +#include "eds1.h"
  1514. +#include "eds2.h"
  1515. +#include "hid.h"
  1516. +#include "spec-data.h"
  1517. +#include "spec-hid.h"
  1518. +
  1519. +void ipts_hid_enable(struct ipts_context *ipts)
  1520. +{
  1521. + WRITE_ONCE(ipts->hid_active, true);
  1522. +}
  1523. +
  1524. +void ipts_hid_disable(struct ipts_context *ipts)
  1525. +{
  1526. + WRITE_ONCE(ipts->hid_active, false);
  1527. +}
  1528. +
  1529. +static int ipts_hid_start(struct hid_device *hid)
  1530. +{
  1531. + return 0;
  1532. +}
  1533. +
  1534. +static void ipts_hid_stop(struct hid_device *hid)
  1535. +{
  1536. +}
  1537. +
  1538. +static int ipts_hid_parse(struct hid_device *hid)
  1539. +{
  1540. + int ret = 0;
  1541. + struct ipts_context *ipts = NULL;
  1542. +
  1543. + u8 *buffer = NULL;
  1544. + size_t size = 0;
  1545. +
  1546. + if (!hid)
  1547. + return -ENODEV;
  1548. +
  1549. + ipts = hid->driver_data;
  1550. +
  1551. + if (!ipts)
  1552. + return -EFAULT;
  1553. +
  1554. + if (!READ_ONCE(ipts->hid_active))
  1555. + return -ENODEV;
  1556. +
  1557. + if (ipts->info.intf_eds == 1)
  1558. + ret = ipts_eds1_get_descriptor(ipts, &buffer, &size);
  1559. + else
  1560. + ret = ipts_eds2_get_descriptor(ipts, &buffer, &size);
  1561. +
  1562. + if (ret) {
  1563. + dev_err(ipts->dev, "Failed to allocate HID descriptor: %d\n", ret);
  1564. + return ret;
  1565. + }
  1566. +
  1567. + ret = hid_parse_report(hid, buffer, size);
  1568. + kfree(buffer);
  1569. +
  1570. + if (ret) {
  1571. + dev_err(ipts->dev, "Failed to parse HID descriptor: %d\n", ret);
  1572. + return ret;
  1573. + }
  1574. +
  1575. + return 0;
  1576. +}
  1577. +
  1578. +static int ipts_hid_raw_request(struct hid_device *hid, unsigned char report_id, __u8 *buffer,
  1579. + size_t size, unsigned char report_type, int request_type)
  1580. +{
  1581. + struct ipts_context *ipts = NULL;
  1582. +
  1583. + if (!hid)
  1584. + return -ENODEV;
  1585. +
  1586. + ipts = hid->driver_data;
  1587. +
  1588. + if (!ipts)
  1589. + return -EFAULT;
  1590. +
  1591. + if (!READ_ONCE(ipts->hid_active))
  1592. + return -ENODEV;
  1593. +
  1594. + if (ipts->info.intf_eds == 1) {
  1595. + return ipts_eds1_raw_request(ipts, buffer, size, report_id, report_type,
  1596. + request_type);
  1597. + } else {
  1598. + return ipts_eds2_raw_request(ipts, buffer, size, report_id, report_type,
  1599. + request_type);
  1600. + }
  1601. +}
  1602. +
  1603. +static struct hid_ll_driver ipts_hid_driver = {
  1604. + .start = ipts_hid_start,
  1605. + .stop = ipts_hid_stop,
  1606. + .open = ipts_hid_start,
  1607. + .close = ipts_hid_stop,
  1608. + .parse = ipts_hid_parse,
  1609. + .raw_request = ipts_hid_raw_request,
  1610. +};
  1611. +
  1612. +int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  1613. +{
  1614. + u8 *temp = NULL;
  1615. + struct ipts_hid_header *frame = NULL;
  1616. + struct ipts_data_header *header = NULL;
  1617. +
  1618. + if (!ipts)
  1619. + return -EFAULT;
  1620. +
  1621. + if (!ipts->hid)
  1622. + return -ENODEV;
  1623. +
  1624. + if (!READ_ONCE(ipts->hid_active))
  1625. + return -ENODEV;
  1626. +
  1627. + header = (struct ipts_data_header *)ipts->resources.data[buffer].address;
  1628. +
  1629. + temp = ipts->resources.report.address;
  1630. + memset(temp, 0, ipts->resources.report.size);
  1631. +
  1632. + if (!header)
  1633. + return -EFAULT;
  1634. +
  1635. + if (header->size == 0)
  1636. + return 0;
  1637. +
  1638. + if (header->type == IPTS_DATA_TYPE_HID)
  1639. + return hid_input_report(ipts->hid, HID_INPUT_REPORT, header->data, header->size, 1);
  1640. +
  1641. + if (header->type == IPTS_DATA_TYPE_GET_FEATURES) {
  1642. + ipts->feature_report.address = header->data;
  1643. + ipts->feature_report.size = header->size;
  1644. +
  1645. + complete_all(&ipts->feature_event);
  1646. + return 0;
  1647. + }
  1648. +
  1649. + if (header->type != IPTS_DATA_TYPE_FRAME)
  1650. + return 0;
  1651. +
  1652. + if (header->size + 3 + sizeof(struct ipts_hid_header) > IPTS_HID_REPORT_DATA_SIZE)
  1653. + return -ERANGE;
  1654. +
  1655. + /*
  1656. + * Synthesize a HID report matching the devices that natively send HID reports
  1657. + */
  1658. + temp[0] = IPTS_HID_REPORT_DATA;
  1659. +
  1660. + frame = (struct ipts_hid_header *)&temp[3];
  1661. + frame->type = IPTS_HID_FRAME_TYPE_RAW;
  1662. + frame->size = header->size + sizeof(*frame);
  1663. +
  1664. + memcpy(frame->data, header->data, header->size);
  1665. +
  1666. + return hid_input_report(ipts->hid, HID_INPUT_REPORT, temp, IPTS_HID_REPORT_DATA_SIZE, 1);
  1667. +}
  1668. +
  1669. +int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info)
  1670. +{
  1671. + int ret = 0;
  1672. +
  1673. + if (!ipts)
  1674. + return -EFAULT;
  1675. +
  1676. + if (ipts->hid)
  1677. + return 0;
  1678. +
  1679. + ipts->hid = hid_allocate_device();
  1680. + if (IS_ERR(ipts->hid)) {
  1681. + int err = PTR_ERR(ipts->hid);
  1682. +
  1683. + dev_err(ipts->dev, "Failed to allocate HID device: %d\n", err);
  1684. + return err;
  1685. + }
  1686. +
  1687. + ipts->hid->driver_data = ipts;
  1688. + ipts->hid->dev.parent = ipts->dev;
  1689. + ipts->hid->ll_driver = &ipts_hid_driver;
  1690. +
  1691. + ipts->hid->vendor = info.vendor;
  1692. + ipts->hid->product = info.product;
  1693. + ipts->hid->group = HID_GROUP_GENERIC;
  1694. +
  1695. + snprintf(ipts->hid->name, sizeof(ipts->hid->name), "IPTS %04X:%04X", info.vendor,
  1696. + info.product);
  1697. +
  1698. + ret = hid_add_device(ipts->hid);
  1699. + if (ret) {
  1700. + dev_err(ipts->dev, "Failed to add HID device: %d\n", ret);
  1701. + ipts_hid_free(ipts);
  1702. + return ret;
  1703. + }
  1704. +
  1705. + return 0;
  1706. +}
  1707. +
  1708. +int ipts_hid_free(struct ipts_context *ipts)
  1709. +{
  1710. + if (!ipts)
  1711. + return -EFAULT;
  1712. +
  1713. + if (!ipts->hid)
  1714. + return 0;
  1715. +
  1716. + hid_destroy_device(ipts->hid);
  1717. + ipts->hid = NULL;
  1718. +
  1719. + return 0;
  1720. +}
  1721. diff --git a/drivers/hid/ipts/hid.h b/drivers/hid/ipts/hid.h
  1722. new file mode 100644
  1723. index 000000000000..1ebe77447903
  1724. --- /dev/null
  1725. +++ b/drivers/hid/ipts/hid.h
  1726. @@ -0,0 +1,24 @@
  1727. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  1728. +/*
  1729. + * Copyright (c) 2022-2023 Dorian Stoll
  1730. + *
  1731. + * Linux driver for Intel Precise Touch & Stylus
  1732. + */
  1733. +
  1734. +#ifndef IPTS_HID_H
  1735. +#define IPTS_HID_H
  1736. +
  1737. +#include <linux/types.h>
  1738. +
  1739. +#include "context.h"
  1740. +#include "spec-device.h"
  1741. +
  1742. +void ipts_hid_enable(struct ipts_context *ipts);
  1743. +void ipts_hid_disable(struct ipts_context *ipts);
  1744. +
  1745. +int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer);
  1746. +
  1747. +int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info);
  1748. +int ipts_hid_free(struct ipts_context *ipts);
  1749. +
  1750. +#endif /* IPTS_HID_H */
  1751. diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c
  1752. new file mode 100644
  1753. index 000000000000..fb5b5c13ee3e
  1754. --- /dev/null
  1755. +++ b/drivers/hid/ipts/main.c
  1756. @@ -0,0 +1,126 @@
  1757. +// SPDX-License-Identifier: GPL-2.0-or-later
  1758. +/*
  1759. + * Copyright (c) 2020-2023 Dorian Stoll
  1760. + *
  1761. + * Linux driver for Intel Precise Touch & Stylus
  1762. + */
  1763. +
  1764. +#include <linux/completion.h>
  1765. +#include <linux/delay.h>
  1766. +#include <linux/device.h>
  1767. +#include <linux/dma-mapping.h>
  1768. +#include <linux/mei_cl_bus.h>
  1769. +#include <linux/mod_devicetable.h>
  1770. +#include <linux/module.h>
  1771. +#include <linux/mutex.h>
  1772. +#include <linux/slab.h>
  1773. +#include <linux/stddef.h>
  1774. +#include <linux/types.h>
  1775. +
  1776. +#include "context.h"
  1777. +#include "control.h"
  1778. +#include "mei.h"
  1779. +#include "receiver.h"
  1780. +#include "spec-device.h"
  1781. +
  1782. +/*
  1783. + * The MEI client ID for IPTS functionality.
  1784. + */
  1785. +#define IPTS_ID UUID_LE(0x3e8d0870, 0x271a, 0x4208, 0x8e, 0xb5, 0x9a, 0xcb, 0x94, 0x02, 0xae, 0x04)
  1786. +
  1787. +static int ipts_set_dma_mask(struct mei_cl_device *cldev)
  1788. +{
  1789. + if (!cldev)
  1790. + return -EFAULT;
  1791. +
  1792. + if (!dma_coerce_mask_and_coherent(&cldev->dev, DMA_BIT_MASK(64)))
  1793. + return 0;
  1794. +
  1795. + return dma_coerce_mask_and_coherent(&cldev->dev, DMA_BIT_MASK(32));
  1796. +}
  1797. +
  1798. +static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id *id)
  1799. +{
  1800. + int ret = 0;
  1801. + struct ipts_context *ipts = NULL;
  1802. +
  1803. + if (!cldev)
  1804. + return -EFAULT;
  1805. +
  1806. + ret = ipts_set_dma_mask(cldev);
  1807. + if (ret) {
  1808. + dev_err(&cldev->dev, "Failed to set DMA mask for IPTS: %d\n", ret);
  1809. + return ret;
  1810. + }
  1811. +
  1812. + ret = mei_cldev_enable(cldev);
  1813. + if (ret) {
  1814. + dev_err(&cldev->dev, "Failed to enable MEI device: %d\n", ret);
  1815. + return ret;
  1816. + }
  1817. +
  1818. + ipts = devm_kzalloc(&cldev->dev, sizeof(*ipts), GFP_KERNEL);
  1819. + if (!ipts) {
  1820. + mei_cldev_disable(cldev);
  1821. + return -ENOMEM;
  1822. + }
  1823. +
  1824. + ret = ipts_mei_init(&ipts->mei, cldev);
  1825. + if (ret) {
  1826. + dev_err(&cldev->dev, "Failed to init MEI bus logic: %d\n", ret);
  1827. + return ret;
  1828. + }
  1829. +
  1830. + ipts->dev = &cldev->dev;
  1831. + ipts->mode = IPTS_MODE_EVENT;
  1832. +
  1833. + mutex_init(&ipts->feature_lock);
  1834. + init_completion(&ipts->feature_event);
  1835. +
  1836. + mei_cldev_set_drvdata(cldev, ipts);
  1837. +
  1838. + ret = ipts_control_start(ipts);
  1839. + if (ret) {
  1840. + dev_err(&cldev->dev, "Failed to start IPTS: %d\n", ret);
  1841. + return ret;
  1842. + }
  1843. +
  1844. + return 0;
  1845. +}
  1846. +
  1847. +static void ipts_remove(struct mei_cl_device *cldev)
  1848. +{
  1849. + int ret = 0;
  1850. + struct ipts_context *ipts = NULL;
  1851. +
  1852. + if (!cldev) {
  1853. + pr_err("MEI device is NULL!");
  1854. + return;
  1855. + }
  1856. +
  1857. + ipts = mei_cldev_get_drvdata(cldev);
  1858. +
  1859. + ret = ipts_control_stop(ipts);
  1860. + if (ret)
  1861. + dev_err(&cldev->dev, "Failed to stop IPTS: %d\n", ret);
  1862. +
  1863. + mei_cldev_disable(cldev);
  1864. +}
  1865. +
  1866. +static struct mei_cl_device_id ipts_device_id_table[] = {
  1867. + { .uuid = IPTS_ID, .version = MEI_CL_VERSION_ANY },
  1868. + {},
  1869. +};
  1870. +MODULE_DEVICE_TABLE(mei, ipts_device_id_table);
  1871. +
  1872. +static struct mei_cl_driver ipts_driver = {
  1873. + .id_table = ipts_device_id_table,
  1874. + .name = "ipts",
  1875. + .probe = ipts_probe,
  1876. + .remove = ipts_remove,
  1877. +};
  1878. +module_mei_cl_driver(ipts_driver);
  1879. +
  1880. +MODULE_DESCRIPTION("IPTS touchscreen driver");
  1881. +MODULE_AUTHOR("Dorian Stoll <dorian.stoll@tmsp.io>");
  1882. +MODULE_LICENSE("GPL");
  1883. diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c
  1884. new file mode 100644
  1885. index 000000000000..1e0395ceae4a
  1886. --- /dev/null
  1887. +++ b/drivers/hid/ipts/mei.c
  1888. @@ -0,0 +1,188 @@
  1889. +// SPDX-License-Identifier: GPL-2.0-or-later
  1890. +/*
  1891. + * Copyright (c) 2023 Dorian Stoll
  1892. + *
  1893. + * Linux driver for Intel Precise Touch & Stylus
  1894. + */
  1895. +
  1896. +#include <linux/device.h>
  1897. +#include <linux/errno.h>
  1898. +#include <linux/jiffies.h>
  1899. +#include <linux/list.h>
  1900. +#include <linux/mei_cl_bus.h>
  1901. +#include <linux/printk.h>
  1902. +#include <linux/rwsem.h>
  1903. +#include <linux/types.h>
  1904. +#include <linux/wait.h>
  1905. +
  1906. +#include "context.h"
  1907. +#include "mei.h"
  1908. +
  1909. +static void locked_list_add(struct list_head *new, struct list_head *head,
  1910. + struct rw_semaphore *lock)
  1911. +{
  1912. + down_write(lock);
  1913. + list_add(new, head);
  1914. + up_write(lock);
  1915. +}
  1916. +
  1917. +static void locked_list_del(struct list_head *entry, struct rw_semaphore *lock)
  1918. +{
  1919. + down_write(lock);
  1920. + list_del(entry);
  1921. + up_write(lock);
  1922. +}
  1923. +
  1924. +static void ipts_mei_incoming(struct mei_cl_device *cldev)
  1925. +{
  1926. + ssize_t ret = 0;
  1927. + struct ipts_mei_message *entry = NULL;
  1928. + struct ipts_context *ipts = NULL;
  1929. +
  1930. + if (!cldev) {
  1931. + pr_err("MEI device is NULL!");
  1932. + return;
  1933. + }
  1934. +
  1935. + ipts = mei_cldev_get_drvdata(cldev);
  1936. + if (!ipts) {
  1937. + pr_err("IPTS driver context is NULL!");
  1938. + return;
  1939. + }
  1940. +
  1941. + entry = devm_kzalloc(ipts->dev, sizeof(*entry), GFP_KERNEL);
  1942. + if (!entry)
  1943. + return;
  1944. +
  1945. + INIT_LIST_HEAD(&entry->list);
  1946. +
  1947. + do {
  1948. + ret = mei_cldev_recv(cldev, (u8 *)&entry->rsp, sizeof(entry->rsp));
  1949. + } while (ret == -EINTR);
  1950. +
  1951. + if (ret < 0) {
  1952. + dev_err(ipts->dev, "Error while reading response: %ld\n", ret);
  1953. + return;
  1954. + }
  1955. +
  1956. + if (ret == 0) {
  1957. + dev_err(ipts->dev, "Received empty response\n");
  1958. + return;
  1959. + }
  1960. +
  1961. + locked_list_add(&entry->list, &ipts->mei.messages, &ipts->mei.message_lock);
  1962. + wake_up_all(&ipts->mei.message_queue);
  1963. +}
  1964. +
  1965. +static int ipts_mei_search(struct ipts_mei *mei, enum ipts_command_code code,
  1966. + struct ipts_response *rsp)
  1967. +{
  1968. + struct ipts_mei_message *entry = NULL;
  1969. +
  1970. + if (!mei)
  1971. + return -EFAULT;
  1972. +
  1973. + if (!rsp)
  1974. + return -EFAULT;
  1975. +
  1976. + down_read(&mei->message_lock);
  1977. +
  1978. + /*
  1979. + * Iterate over the list of received messages, and check if there is one
  1980. + * matching the requested command code.
  1981. + */
  1982. + list_for_each_entry(entry, &mei->messages, list) {
  1983. + if (entry->rsp.cmd == code)
  1984. + break;
  1985. + }
  1986. +
  1987. + up_read(&mei->message_lock);
  1988. +
  1989. + /*
  1990. + * If entry is not the list head, this means that the loop above has been stopped early,
  1991. + * and that we found a matching element. We drop the message from the list and return it.
  1992. + */
  1993. + if (!list_entry_is_head(entry, &mei->messages, list)) {
  1994. + locked_list_del(&entry->list, &mei->message_lock);
  1995. +
  1996. + *rsp = entry->rsp;
  1997. + devm_kfree(&mei->cldev->dev, entry);
  1998. +
  1999. + return 0;
  2000. + }
  2001. +
  2002. + return -EAGAIN;
  2003. +}
  2004. +
  2005. +int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts_response *rsp,
  2006. + u64 timeout)
  2007. +{
  2008. + int ret = 0;
  2009. +
  2010. + if (!mei)
  2011. + return -EFAULT;
  2012. +
  2013. + /*
  2014. + * A timeout of 0 means check and return immideately.
  2015. + */
  2016. + if (timeout == 0)
  2017. + return ipts_mei_search(mei, code, rsp);
  2018. +
  2019. + /*
  2020. + * A timeout of less than 0 means to wait forever.
  2021. + */
  2022. + if (timeout < 0) {
  2023. + wait_event(mei->message_queue, ipts_mei_search(mei, code, rsp) == 0);
  2024. + return 0;
  2025. + }
  2026. +
  2027. + ret = wait_event_timeout(mei->message_queue, ipts_mei_search(mei, code, rsp) == 0,
  2028. + msecs_to_jiffies(timeout));
  2029. +
  2030. + if (ret > 0)
  2031. + return 0;
  2032. +
  2033. + return -EAGAIN;
  2034. +}
  2035. +
  2036. +int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length)
  2037. +{
  2038. + int ret = 0;
  2039. +
  2040. + if (!mei)
  2041. + return -EFAULT;
  2042. +
  2043. + if (!mei->cldev)
  2044. + return -EFAULT;
  2045. +
  2046. + if (!data)
  2047. + return -EFAULT;
  2048. +
  2049. + do {
  2050. + ret = mei_cldev_send(mei->cldev, (u8 *)data, length);
  2051. + } while (ret == -EINTR);
  2052. +
  2053. + if (ret < 0)
  2054. + return ret;
  2055. +
  2056. + return 0;
  2057. +}
  2058. +
  2059. +int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev)
  2060. +{
  2061. + if (!mei)
  2062. + return -EFAULT;
  2063. +
  2064. + if (!cldev)
  2065. + return -EFAULT;
  2066. +
  2067. + mei->cldev = cldev;
  2068. +
  2069. + INIT_LIST_HEAD(&mei->messages);
  2070. + init_waitqueue_head(&mei->message_queue);
  2071. + init_rwsem(&mei->message_lock);
  2072. +
  2073. + mei_cldev_register_rx_cb(cldev, ipts_mei_incoming);
  2074. +
  2075. + return 0;
  2076. +}
  2077. diff --git a/drivers/hid/ipts/mei.h b/drivers/hid/ipts/mei.h
  2078. new file mode 100644
  2079. index 000000000000..973bade6b0fd
  2080. --- /dev/null
  2081. +++ b/drivers/hid/ipts/mei.h
  2082. @@ -0,0 +1,66 @@
  2083. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2084. +/*
  2085. + * Copyright (c) 2023 Dorian Stoll
  2086. + *
  2087. + * Linux driver for Intel Precise Touch & Stylus
  2088. + */
  2089. +
  2090. +#ifndef IPTS_MEI_H
  2091. +#define IPTS_MEI_H
  2092. +
  2093. +#include <linux/list.h>
  2094. +#include <linux/mei_cl_bus.h>
  2095. +#include <linux/rwsem.h>
  2096. +#include <linux/types.h>
  2097. +#include <linux/wait.h>
  2098. +
  2099. +#include "spec-device.h"
  2100. +
  2101. +struct ipts_mei_message {
  2102. + struct list_head list;
  2103. + struct ipts_response rsp;
  2104. +};
  2105. +
  2106. +struct ipts_mei {
  2107. + struct mei_cl_device *cldev;
  2108. +
  2109. + struct list_head messages;
  2110. +
  2111. + wait_queue_head_t message_queue;
  2112. + struct rw_semaphore message_lock;
  2113. +};
  2114. +
  2115. +/**
  2116. + * ipts_mei_recv() - Receive data from a MEI device.
  2117. + * @mei: The IPTS MEI device context.
  2118. + * @code: The IPTS command code to look for.
  2119. + * @rsp: The address that the received data will be copied to.
  2120. + * @timeout: How many milliseconds the function will wait at most.
  2121. + *
  2122. + * A negative timeout means to wait forever.
  2123. + *
  2124. + * Returns: 0 on success, <0 on error, -EAGAIN if no response has been received.
  2125. + */
  2126. +int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts_response *rsp,
  2127. + u64 timeout);
  2128. +
  2129. +/**
  2130. + * ipts_mei_send() - Send data to a MEI device.
  2131. + * @ipts: The IPTS MEI device context.
  2132. + * @data: The data to send.
  2133. + * @size: The size of the data.
  2134. + *
  2135. + * Returns: 0 on success, <0 on error.
  2136. + */
  2137. +int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length);
  2138. +
  2139. +/**
  2140. + * ipts_mei_init() - Initialize the MEI device context.
  2141. + * @mei: The MEI device context to initialize.
  2142. + * @cldev: The MEI device the context will be bound to.
  2143. + *
  2144. + * Returns: 0 on success, <0 on error.
  2145. + */
  2146. +int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev);
  2147. +
  2148. +#endif /* IPTS_MEI_H */
  2149. diff --git a/drivers/hid/ipts/receiver.c b/drivers/hid/ipts/receiver.c
  2150. new file mode 100644
  2151. index 000000000000..977724c728c3
  2152. --- /dev/null
  2153. +++ b/drivers/hid/ipts/receiver.c
  2154. @@ -0,0 +1,251 @@
  2155. +// SPDX-License-Identifier: GPL-2.0-or-later
  2156. +/*
  2157. + * Copyright (c) 2020-2023 Dorian Stoll
  2158. + *
  2159. + * Linux driver for Intel Precise Touch & Stylus
  2160. + */
  2161. +
  2162. +#include <linux/delay.h>
  2163. +#include <linux/err.h>
  2164. +#include <linux/kthread.h>
  2165. +#include <linux/time64.h>
  2166. +#include <linux/timekeeping.h>
  2167. +#include <linux/types.h>
  2168. +
  2169. +#include "cmd.h"
  2170. +#include "context.h"
  2171. +#include "control.h"
  2172. +#include "hid.h"
  2173. +#include "receiver.h"
  2174. +#include "resources.h"
  2175. +#include "spec-device.h"
  2176. +#include "thread.h"
  2177. +
  2178. +static void ipts_receiver_next_doorbell(struct ipts_context *ipts)
  2179. +{
  2180. + u32 *doorbell = (u32 *)ipts->resources.doorbell.address;
  2181. + *doorbell = *doorbell + 1;
  2182. +}
  2183. +
  2184. +static u32 ipts_receiver_current_doorbell(struct ipts_context *ipts)
  2185. +{
  2186. + u32 *doorbell = (u32 *)ipts->resources.doorbell.address;
  2187. + return *doorbell;
  2188. +}
  2189. +
  2190. +static void ipts_receiver_backoff(time64_t last, u32 n)
  2191. +{
  2192. + /*
  2193. + * If the last change was less than n seconds ago,
  2194. + * sleep for a shorter period so that new data can be
  2195. + * processed quickly. If there was no change for more than
  2196. + * n seconds, sleep longer to avoid wasting CPU cycles.
  2197. + */
  2198. + if (last + n > ktime_get_seconds())
  2199. + usleep_range(1 * USEC_PER_MSEC, 5 * USEC_PER_MSEC);
  2200. + else
  2201. + msleep(200);
  2202. +}
  2203. +
  2204. +static int ipts_receiver_event_loop(struct ipts_thread *thread)
  2205. +{
  2206. + int ret = 0;
  2207. + u32 buffer = 0;
  2208. +
  2209. + struct ipts_context *ipts = NULL;
  2210. + time64_t last = ktime_get_seconds();
  2211. +
  2212. + if (!thread)
  2213. + return -EFAULT;
  2214. +
  2215. + ipts = thread->data;
  2216. +
  2217. + if (!ipts)
  2218. + return -EFAULT;
  2219. +
  2220. + dev_info(ipts->dev, "IPTS running in event mode\n");
  2221. +
  2222. + while (!ipts_thread_should_stop(thread)) {
  2223. + int i = 0;
  2224. +
  2225. + for (i = 0; i < IPTS_BUFFERS; i++) {
  2226. + ret = ipts_control_wait_data(ipts, false);
  2227. + if (ret == -EAGAIN)
  2228. + break;
  2229. +
  2230. + if (ret) {
  2231. + dev_err(ipts->dev, "Failed to wait for data: %d\n", ret);
  2232. + continue;
  2233. + }
  2234. +
  2235. + buffer = ipts_receiver_current_doorbell(ipts) % IPTS_BUFFERS;
  2236. + ipts_receiver_next_doorbell(ipts);
  2237. +
  2238. + ret = ipts_hid_input_data(ipts, buffer);
  2239. + if (ret)
  2240. + dev_err(ipts->dev, "Failed to process buffer: %d\n", ret);
  2241. +
  2242. + ret = ipts_control_refill_buffer(ipts, buffer);
  2243. + if (ret)
  2244. + dev_err(ipts->dev, "Failed to send feedback: %d\n", ret);
  2245. +
  2246. + ret = ipts_control_request_data(ipts);
  2247. + if (ret)
  2248. + dev_err(ipts->dev, "Failed to request data: %d\n", ret);
  2249. +
  2250. + last = ktime_get_seconds();
  2251. + }
  2252. +
  2253. + ipts_receiver_backoff(last, 5);
  2254. + }
  2255. +
  2256. + ret = ipts_control_request_flush(ipts);
  2257. + if (ret) {
  2258. + dev_err(ipts->dev, "Failed to request flush: %d\n", ret);
  2259. + return ret;
  2260. + }
  2261. +
  2262. + ret = ipts_control_wait_data(ipts, true);
  2263. + if (ret) {
  2264. + dev_err(ipts->dev, "Failed to wait for data: %d\n", ret);
  2265. +
  2266. + if (ret != -EAGAIN)
  2267. + return ret;
  2268. + else
  2269. + return 0;
  2270. + }
  2271. +
  2272. + ret = ipts_control_wait_flush(ipts);
  2273. + if (ret) {
  2274. + dev_err(ipts->dev, "Failed to wait for flush: %d\n", ret);
  2275. +
  2276. + if (ret != -EAGAIN)
  2277. + return ret;
  2278. + else
  2279. + return 0;
  2280. + }
  2281. +
  2282. + return 0;
  2283. +}
  2284. +
  2285. +static int ipts_receiver_poll_loop(struct ipts_thread *thread)
  2286. +{
  2287. + int ret = 0;
  2288. + u32 buffer = 0;
  2289. +
  2290. + u32 doorbell = 0;
  2291. + u32 lastdb = 0;
  2292. +
  2293. + struct ipts_context *ipts = NULL;
  2294. + time64_t last = ktime_get_seconds();
  2295. +
  2296. + if (!thread)
  2297. + return -EFAULT;
  2298. +
  2299. + ipts = thread->data;
  2300. +
  2301. + if (!ipts)
  2302. + return -EFAULT;
  2303. +
  2304. + dev_info(ipts->dev, "IPTS running in poll mode\n");
  2305. +
  2306. + while (true) {
  2307. + if (ipts_thread_should_stop(thread)) {
  2308. + ret = ipts_control_request_flush(ipts);
  2309. + if (ret) {
  2310. + dev_err(ipts->dev, "Failed to request flush: %d\n", ret);
  2311. + return ret;
  2312. + }
  2313. + }
  2314. +
  2315. + doorbell = ipts_receiver_current_doorbell(ipts);
  2316. +
  2317. + /*
  2318. + * After filling up one of the data buffers, IPTS will increment
  2319. + * the doorbell. The value of the doorbell stands for the *next*
  2320. + * buffer that IPTS is going to fill.
  2321. + */
  2322. + while (lastdb != doorbell) {
  2323. + buffer = lastdb % IPTS_BUFFERS;
  2324. +
  2325. + ret = ipts_hid_input_data(ipts, buffer);
  2326. + if (ret)
  2327. + dev_err(ipts->dev, "Failed to process buffer: %d\n", ret);
  2328. +
  2329. + ret = ipts_control_refill_buffer(ipts, buffer);
  2330. + if (ret)
  2331. + dev_err(ipts->dev, "Failed to send feedback: %d\n", ret);
  2332. +
  2333. + last = ktime_get_seconds();
  2334. + lastdb++;
  2335. + }
  2336. +
  2337. + if (ipts_thread_should_stop(thread))
  2338. + break;
  2339. +
  2340. + ipts_receiver_backoff(last, 5);
  2341. + }
  2342. +
  2343. + ret = ipts_control_wait_data(ipts, true);
  2344. + if (ret) {
  2345. + dev_err(ipts->dev, "Failed to wait for data: %d\n", ret);
  2346. +
  2347. + if (ret != -EAGAIN)
  2348. + return ret;
  2349. + else
  2350. + return 0;
  2351. + }
  2352. +
  2353. + ret = ipts_control_wait_flush(ipts);
  2354. + if (ret) {
  2355. + dev_err(ipts->dev, "Failed to wait for flush: %d\n", ret);
  2356. +
  2357. + if (ret != -EAGAIN)
  2358. + return ret;
  2359. + else
  2360. + return 0;
  2361. + }
  2362. +
  2363. + return 0;
  2364. +}
  2365. +
  2366. +int ipts_receiver_start(struct ipts_context *ipts)
  2367. +{
  2368. + int ret = 0;
  2369. +
  2370. + if (!ipts)
  2371. + return -EFAULT;
  2372. +
  2373. + if (ipts->mode == IPTS_MODE_EVENT) {
  2374. + ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_event_loop, ipts,
  2375. + "ipts_event");
  2376. + } else if (ipts->mode == IPTS_MODE_POLL) {
  2377. + ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_poll_loop, ipts,
  2378. + "ipts_poll");
  2379. + } else {
  2380. + ret = -EINVAL;
  2381. + }
  2382. +
  2383. + if (ret) {
  2384. + dev_err(ipts->dev, "Failed to start receiver loop: %d\n", ret);
  2385. + return ret;
  2386. + }
  2387. +
  2388. + return 0;
  2389. +}
  2390. +
  2391. +int ipts_receiver_stop(struct ipts_context *ipts)
  2392. +{
  2393. + int ret = 0;
  2394. +
  2395. + if (!ipts)
  2396. + return -EFAULT;
  2397. +
  2398. + ret = ipts_thread_stop(&ipts->receiver_loop);
  2399. + if (ret) {
  2400. + dev_err(ipts->dev, "Failed to stop receiver loop: %d\n", ret);
  2401. + return ret;
  2402. + }
  2403. +
  2404. + return 0;
  2405. +}
  2406. diff --git a/drivers/hid/ipts/receiver.h b/drivers/hid/ipts/receiver.h
  2407. new file mode 100644
  2408. index 000000000000..3de7da62d40c
  2409. --- /dev/null
  2410. +++ b/drivers/hid/ipts/receiver.h
  2411. @@ -0,0 +1,16 @@
  2412. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2413. +/*
  2414. + * Copyright (c) 2020-2023 Dorian Stoll
  2415. + *
  2416. + * Linux driver for Intel Precise Touch & Stylus
  2417. + */
  2418. +
  2419. +#ifndef IPTS_RECEIVER_H
  2420. +#define IPTS_RECEIVER_H
  2421. +
  2422. +#include "context.h"
  2423. +
  2424. +int ipts_receiver_start(struct ipts_context *ipts);
  2425. +int ipts_receiver_stop(struct ipts_context *ipts);
  2426. +
  2427. +#endif /* IPTS_RECEIVER_H */
  2428. diff --git a/drivers/hid/ipts/resources.c b/drivers/hid/ipts/resources.c
  2429. new file mode 100644
  2430. index 000000000000..cc14653b2a9f
  2431. --- /dev/null
  2432. +++ b/drivers/hid/ipts/resources.c
  2433. @@ -0,0 +1,131 @@
  2434. +// SPDX-License-Identifier: GPL-2.0-or-later
  2435. +/*
  2436. + * Copyright (c) 2020-2023 Dorian Stoll
  2437. + *
  2438. + * Linux driver for Intel Precise Touch & Stylus
  2439. + */
  2440. +
  2441. +#include <linux/dma-mapping.h>
  2442. +#include <linux/slab.h>
  2443. +#include <linux/types.h>
  2444. +
  2445. +#include "desc.h"
  2446. +#include "resources.h"
  2447. +#include "spec-device.h"
  2448. +
  2449. +static int ipts_resources_alloc_buffer(struct ipts_buffer *buffer, struct device *dev, size_t size)
  2450. +{
  2451. + if (!buffer)
  2452. + return -EFAULT;
  2453. +
  2454. + if (buffer->address)
  2455. + return 0;
  2456. +
  2457. + buffer->address = dma_alloc_coherent(dev, size, &buffer->dma_address, GFP_KERNEL);
  2458. +
  2459. + if (!buffer->address)
  2460. + return -ENOMEM;
  2461. +
  2462. + buffer->size = size;
  2463. + buffer->device = dev;
  2464. +
  2465. + return 0;
  2466. +}
  2467. +
  2468. +static void ipts_resources_free_buffer(struct ipts_buffer *buffer)
  2469. +{
  2470. + if (!buffer->address)
  2471. + return;
  2472. +
  2473. + dma_free_coherent(buffer->device, buffer->size, buffer->address, buffer->dma_address);
  2474. +
  2475. + buffer->address = NULL;
  2476. + buffer->size = 0;
  2477. +
  2478. + buffer->dma_address = 0;
  2479. + buffer->device = NULL;
  2480. +}
  2481. +
  2482. +int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t ds, size_t fs)
  2483. +{
  2484. + int ret = 0;
  2485. +
  2486. + /*
  2487. + * Some compilers (AOSP clang) complain about a redefined
  2488. + * variable when this is declared inside of the for loop.
  2489. + */
  2490. + int i = 0;
  2491. +
  2492. + if (!res)
  2493. + return -EFAULT;
  2494. +
  2495. + for (i = 0; i < IPTS_BUFFERS; i++) {
  2496. + ret = ipts_resources_alloc_buffer(&res->data[i], dev, ds);
  2497. + if (ret)
  2498. + goto err;
  2499. + }
  2500. +
  2501. + for (i = 0; i < IPTS_BUFFERS; i++) {
  2502. + ret = ipts_resources_alloc_buffer(&res->feedback[i], dev, fs);
  2503. + if (ret)
  2504. + goto err;
  2505. + }
  2506. +
  2507. + ret = ipts_resources_alloc_buffer(&res->doorbell, dev, sizeof(u32));
  2508. + if (ret)
  2509. + goto err;
  2510. +
  2511. + ret = ipts_resources_alloc_buffer(&res->workqueue, dev, sizeof(u32));
  2512. + if (ret)
  2513. + goto err;
  2514. +
  2515. + ret = ipts_resources_alloc_buffer(&res->hid2me, dev, fs);
  2516. + if (ret)
  2517. + goto err;
  2518. +
  2519. + ret = ipts_resources_alloc_buffer(&res->descriptor, dev, ds + 8);
  2520. + if (ret)
  2521. + goto err;
  2522. +
  2523. + if (!res->report.address) {
  2524. + res->report.size = IPTS_HID_REPORT_DATA_SIZE;
  2525. + res->report.address = kzalloc(res->report.size, GFP_KERNEL);
  2526. +
  2527. + if (!res->report.address) {
  2528. + ret = -ENOMEM;
  2529. + goto err;
  2530. + }
  2531. + }
  2532. +
  2533. + return 0;
  2534. +
  2535. +err:
  2536. +
  2537. + ipts_resources_free(res);
  2538. + return ret;
  2539. +}
  2540. +
  2541. +int ipts_resources_free(struct ipts_resources *res)
  2542. +{
  2543. + int i = 0;
  2544. +
  2545. + if (!res)
  2546. + return -EFAULT;
  2547. +
  2548. + for (i = 0; i < IPTS_BUFFERS; i++)
  2549. + ipts_resources_free_buffer(&res->data[i]);
  2550. +
  2551. + for (i = 0; i < IPTS_BUFFERS; i++)
  2552. + ipts_resources_free_buffer(&res->feedback[i]);
  2553. +
  2554. + ipts_resources_free_buffer(&res->doorbell);
  2555. + ipts_resources_free_buffer(&res->workqueue);
  2556. + ipts_resources_free_buffer(&res->hid2me);
  2557. + ipts_resources_free_buffer(&res->descriptor);
  2558. +
  2559. + kfree(res->report.address);
  2560. + res->report.address = NULL;
  2561. + res->report.size = 0;
  2562. +
  2563. + return 0;
  2564. +}
  2565. diff --git a/drivers/hid/ipts/resources.h b/drivers/hid/ipts/resources.h
  2566. new file mode 100644
  2567. index 000000000000..2068e13285f0
  2568. --- /dev/null
  2569. +++ b/drivers/hid/ipts/resources.h
  2570. @@ -0,0 +1,41 @@
  2571. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2572. +/*
  2573. + * Copyright (c) 2020-2023 Dorian Stoll
  2574. + *
  2575. + * Linux driver for Intel Precise Touch & Stylus
  2576. + */
  2577. +
  2578. +#ifndef IPTS_RESOURCES_H
  2579. +#define IPTS_RESOURCES_H
  2580. +
  2581. +#include <linux/device.h>
  2582. +#include <linux/types.h>
  2583. +
  2584. +#include "spec-device.h"
  2585. +
  2586. +struct ipts_buffer {
  2587. + u8 *address;
  2588. + size_t size;
  2589. +
  2590. + dma_addr_t dma_address;
  2591. + struct device *device;
  2592. +};
  2593. +
  2594. +struct ipts_resources {
  2595. + struct ipts_buffer data[IPTS_BUFFERS];
  2596. + struct ipts_buffer feedback[IPTS_BUFFERS];
  2597. +
  2598. + struct ipts_buffer doorbell;
  2599. + struct ipts_buffer workqueue;
  2600. + struct ipts_buffer hid2me;
  2601. +
  2602. + struct ipts_buffer descriptor;
  2603. +
  2604. + // Buffer for synthesizing HID reports
  2605. + struct ipts_buffer report;
  2606. +};
  2607. +
  2608. +int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t ds, size_t fs);
  2609. +int ipts_resources_free(struct ipts_resources *res);
  2610. +
  2611. +#endif /* IPTS_RESOURCES_H */
  2612. diff --git a/drivers/hid/ipts/spec-data.h b/drivers/hid/ipts/spec-data.h
  2613. new file mode 100644
  2614. index 000000000000..e8dd98895a7e
  2615. --- /dev/null
  2616. +++ b/drivers/hid/ipts/spec-data.h
  2617. @@ -0,0 +1,100 @@
  2618. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2619. +/*
  2620. + * Copyright (c) 2016 Intel Corporation
  2621. + * Copyright (c) 2020-2023 Dorian Stoll
  2622. + *
  2623. + * Linux driver for Intel Precise Touch & Stylus
  2624. + */
  2625. +
  2626. +#ifndef IPTS_SPEC_DATA_H
  2627. +#define IPTS_SPEC_DATA_H
  2628. +
  2629. +#include <linux/build_bug.h>
  2630. +#include <linux/types.h>
  2631. +
  2632. +/**
  2633. + * enum ipts_feedback_cmd_type - Commands that can be executed on the sensor through feedback.
  2634. + */
  2635. +enum ipts_feedback_cmd_type {
  2636. + IPTS_FEEDBACK_CMD_TYPE_NONE = 0,
  2637. + IPTS_FEEDBACK_CMD_TYPE_SOFT_RESET = 1,
  2638. + IPTS_FEEDBACK_CMD_TYPE_GOTO_ARMED = 2,
  2639. + IPTS_FEEDBACK_CMD_TYPE_GOTO_SENSING = 3,
  2640. + IPTS_FEEDBACK_CMD_TYPE_GOTO_SLEEP = 4,
  2641. + IPTS_FEEDBACK_CMD_TYPE_GOTO_DOZE = 5,
  2642. + IPTS_FEEDBACK_CMD_TYPE_HARD_RESET = 6,
  2643. +};
  2644. +
  2645. +/**
  2646. + * enum ipts_feedback_data_type - Defines what data a feedback buffer contains.
  2647. + * @IPTS_FEEDBACK_DATA_TYPE_VENDOR: The buffer contains vendor specific feedback.
  2648. + * @IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES: The buffer contains a HID set features report.
  2649. + * @IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES: The buffer contains a HID get features report.
  2650. + * @IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT: The buffer contains a HID output report.
  2651. + * @IPTS_FEEDBACK_DATA_TYPE_STORE_DATA: The buffer contains calibration data for the sensor.
  2652. + */
  2653. +enum ipts_feedback_data_type {
  2654. + IPTS_FEEDBACK_DATA_TYPE_VENDOR = 0,
  2655. + IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES = 1,
  2656. + IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES = 2,
  2657. + IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT = 3,
  2658. + IPTS_FEEDBACK_DATA_TYPE_STORE_DATA = 4,
  2659. +};
  2660. +
  2661. +/**
  2662. + * struct ipts_feedback_header - Header that is prefixed to the data in a feedback buffer.
  2663. + * @cmd_type: A command that should be executed on the sensor.
  2664. + * @size: The size of the payload to be written.
  2665. + * @buffer: The ID of the buffer that contains this feedback data.
  2666. + * @protocol: The protocol version of the EDS.
  2667. + * @data_type: The type of data that the buffer contains.
  2668. + * @spi_offset: The offset at which to write the payload data to the sensor.
  2669. + * @payload: Payload for the feedback command, or 0 if no payload is sent.
  2670. + */
  2671. +struct ipts_feedback_header {
  2672. + enum ipts_feedback_cmd_type cmd_type;
  2673. + u32 size;
  2674. + u32 buffer;
  2675. + u32 protocol;
  2676. + enum ipts_feedback_data_type data_type;
  2677. + u32 spi_offset;
  2678. + u8 reserved[40];
  2679. + u8 payload[];
  2680. +} __packed;
  2681. +
  2682. +static_assert(sizeof(struct ipts_feedback_header) == 64);
  2683. +
  2684. +/**
  2685. + * enum ipts_data_type - Defines what type of data a buffer contains.
  2686. + * @IPTS_DATA_TYPE_FRAME: Raw data frame.
  2687. + * @IPTS_DATA_TYPE_ERROR: Error data.
  2688. + * @IPTS_DATA_TYPE_VENDOR: Vendor specific data.
  2689. + * @IPTS_DATA_TYPE_HID: A HID report.
  2690. + * @IPTS_DATA_TYPE_GET_FEATURES: The response to a GET_FEATURES HID2ME command.
  2691. + */
  2692. +enum ipts_data_type {
  2693. + IPTS_DATA_TYPE_FRAME = 0x00,
  2694. + IPTS_DATA_TYPE_ERROR = 0x01,
  2695. + IPTS_DATA_TYPE_VENDOR = 0x02,
  2696. + IPTS_DATA_TYPE_HID = 0x03,
  2697. + IPTS_DATA_TYPE_GET_FEATURES = 0x04,
  2698. + IPTS_DATA_TYPE_DESCRIPTOR = 0x05,
  2699. +};
  2700. +
  2701. +/**
  2702. + * struct ipts_data_header - Header that is prefixed to the data in a data buffer.
  2703. + * @type: What data the buffer contains.
  2704. + * @size: How much data the buffer contains.
  2705. + * @buffer: Which buffer the data is in.
  2706. + */
  2707. +struct ipts_data_header {
  2708. + enum ipts_data_type type;
  2709. + u32 size;
  2710. + u32 buffer;
  2711. + u8 reserved[52];
  2712. + u8 data[];
  2713. +} __packed;
  2714. +
  2715. +static_assert(sizeof(struct ipts_data_header) == 64);
  2716. +
  2717. +#endif /* IPTS_SPEC_DATA_H */
  2718. diff --git a/drivers/hid/ipts/spec-device.h b/drivers/hid/ipts/spec-device.h
  2719. new file mode 100644
  2720. index 000000000000..41845f9d9025
  2721. --- /dev/null
  2722. +++ b/drivers/hid/ipts/spec-device.h
  2723. @@ -0,0 +1,290 @@
  2724. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2725. +/*
  2726. + * Copyright (c) 2016 Intel Corporation
  2727. + * Copyright (c) 2020-2023 Dorian Stoll
  2728. + *
  2729. + * Linux driver for Intel Precise Touch & Stylus
  2730. + */
  2731. +
  2732. +#ifndef IPTS_SPEC_DEVICE_H
  2733. +#define IPTS_SPEC_DEVICE_H
  2734. +
  2735. +#include <linux/build_bug.h>
  2736. +#include <linux/types.h>
  2737. +
  2738. +/*
  2739. + * The amount of buffers that IPTS can use for data transfer.
  2740. + */
  2741. +#define IPTS_BUFFERS 16
  2742. +
  2743. +/*
  2744. + * The buffer ID that is used for HID2ME feedback
  2745. + */
  2746. +#define IPTS_HID2ME_BUFFER IPTS_BUFFERS
  2747. +
  2748. +/**
  2749. + * enum ipts_command - Commands that can be sent to the IPTS hardware.
  2750. + * @IPTS_CMD_GET_DEVICE_INFO: Retrieves vendor information from the device.
  2751. + * @IPTS_CMD_SET_MODE: Changes the mode that the device will operate in.
  2752. + * @IPTS_CMD_SET_MEM_WINDOW: Configures memory buffers for passing data between device and driver.
  2753. + * @IPTS_CMD_QUIESCE_IO: Stops the data flow from the device to the driver.
  2754. + * @IPTS_CMD_READY_FOR_DATA: Informs the device that the driver is ready to receive data.
  2755. + * @IPTS_CMD_FEEDBACK: Informs the device that a buffer was processed and can be refilled.
  2756. + * @IPTS_CMD_CLEAR_MEM_WINDOW: Stops the data flow and clears the buffer addresses on the device.
  2757. + * @IPTS_CMD_RESET_SENSOR: Resets the sensor to its default state.
  2758. + * @IPTS_CMD_GET_DESCRIPTOR: Retrieves the HID descriptor of the device.
  2759. + */
  2760. +enum ipts_command_code {
  2761. + IPTS_CMD_GET_DEVICE_INFO = 0x01,
  2762. + IPTS_CMD_SET_MODE = 0x02,
  2763. + IPTS_CMD_SET_MEM_WINDOW = 0x03,
  2764. + IPTS_CMD_QUIESCE_IO = 0x04,
  2765. + IPTS_CMD_READY_FOR_DATA = 0x05,
  2766. + IPTS_CMD_FEEDBACK = 0x06,
  2767. + IPTS_CMD_CLEAR_MEM_WINDOW = 0x07,
  2768. + IPTS_CMD_RESET_SENSOR = 0x0B,
  2769. + IPTS_CMD_GET_DESCRIPTOR = 0x0F,
  2770. +};
  2771. +
  2772. +/**
  2773. + * enum ipts_status - Possible status codes returned by the IPTS device.
  2774. + * @IPTS_STATUS_SUCCESS: Operation completed successfully.
  2775. + * @IPTS_STATUS_INVALID_PARAMS: Command contained an invalid payload.
  2776. + * @IPTS_STATUS_ACCESS_DENIED: ME could not validate a buffer address.
  2777. + * @IPTS_STATUS_CMD_SIZE_ERROR: Command contains an invalid payload.
  2778. + * @IPTS_STATUS_NOT_READY: Buffer addresses have not been set.
  2779. + * @IPTS_STATUS_REQUEST_OUTSTANDING: There is an outstanding command of the same type.
  2780. + * @IPTS_STATUS_NO_SENSOR_FOUND: No sensor could be found.
  2781. + * @IPTS_STATUS_OUT_OF_MEMORY: Not enough free memory for requested operation.
  2782. + * @IPTS_STATUS_INTERNAL_ERROR: An unexpected error occurred.
  2783. + * @IPTS_STATUS_SENSOR_DISABLED: The sensor has been disabled and must be reinitialized.
  2784. + * @IPTS_STATUS_COMPAT_CHECK_FAIL: Compatibility revision check between sensor and ME failed.
  2785. + * The host can ignore this error and attempt to continue.
  2786. + * @IPTS_STATUS_SENSOR_EXPECTED_RESET: The sensor went through a reset initiated by the driver.
  2787. + * @IPTS_STATUS_SENSOR_UNEXPECTED_RESET: The sensor went through an unexpected reset.
  2788. + * @IPTS_STATUS_RESET_FAILED: Requested sensor reset failed to complete.
  2789. + * @IPTS_STATUS_TIMEOUT: The operation timed out.
  2790. + * @IPTS_STATUS_TEST_MODE_FAIL: Test mode pattern did not match expected values.
  2791. + * @IPTS_STATUS_SENSOR_FAIL_FATAL: The sensor reported an error during reset sequence.
  2792. + * Further progress is not possible.
  2793. + * @IPTS_STATUS_SENSOR_FAIL_NONFATAL: The sensor reported an error during reset sequence.
  2794. + * The driver can attempt to continue.
  2795. + * @IPTS_STATUS_INVALID_DEVICE_CAPS: The device reported invalid capabilities.
  2796. + * @IPTS_STATUS_QUIESCE_IO_IN_PROGRESS: Command cannot be completed until Quiesce IO is done.
  2797. + */
  2798. +enum ipts_status {
  2799. + IPTS_STATUS_SUCCESS = 0x00,
  2800. + IPTS_STATUS_INVALID_PARAMS = 0x01,
  2801. + IPTS_STATUS_ACCESS_DENIED = 0x02,
  2802. + IPTS_STATUS_CMD_SIZE_ERROR = 0x03,
  2803. + IPTS_STATUS_NOT_READY = 0x04,
  2804. + IPTS_STATUS_REQUEST_OUTSTANDING = 0x05,
  2805. + IPTS_STATUS_NO_SENSOR_FOUND = 0x06,
  2806. + IPTS_STATUS_OUT_OF_MEMORY = 0x07,
  2807. + IPTS_STATUS_INTERNAL_ERROR = 0x08,
  2808. + IPTS_STATUS_SENSOR_DISABLED = 0x09,
  2809. + IPTS_STATUS_COMPAT_CHECK_FAIL = 0x0A,
  2810. + IPTS_STATUS_SENSOR_EXPECTED_RESET = 0x0B,
  2811. + IPTS_STATUS_SENSOR_UNEXPECTED_RESET = 0x0C,
  2812. + IPTS_STATUS_RESET_FAILED = 0x0D,
  2813. + IPTS_STATUS_TIMEOUT = 0x0E,
  2814. + IPTS_STATUS_TEST_MODE_FAIL = 0x0F,
  2815. + IPTS_STATUS_SENSOR_FAIL_FATAL = 0x10,
  2816. + IPTS_STATUS_SENSOR_FAIL_NONFATAL = 0x11,
  2817. + IPTS_STATUS_INVALID_DEVICE_CAPS = 0x12,
  2818. + IPTS_STATUS_QUIESCE_IO_IN_PROGRESS = 0x13,
  2819. +};
  2820. +
  2821. +/**
  2822. + * struct ipts_command - Message that is sent to the device for calling a command.
  2823. + * @cmd: The command that will be called.
  2824. + * @payload: Payload containing parameters for the called command.
  2825. + */
  2826. +struct ipts_command {
  2827. + enum ipts_command_code cmd;
  2828. + u8 payload[320];
  2829. +} __packed;
  2830. +
  2831. +static_assert(sizeof(struct ipts_command) == 324);
  2832. +
  2833. +/**
  2834. + * enum ipts_mode - Configures what data the device produces and how its sent.
  2835. + * @IPTS_MODE_EVENT: The device will send an event once a buffer was filled.
  2836. + * Older devices will return singletouch data in this mode.
  2837. + * @IPTS_MODE_POLL: The device will notify the driver by incrementing the doorbell value.
  2838. + * Older devices will return multitouch data in this mode.
  2839. + */
  2840. +enum ipts_mode {
  2841. + IPTS_MODE_EVENT = 0x00,
  2842. + IPTS_MODE_POLL = 0x01,
  2843. +};
  2844. +
  2845. +/**
  2846. + * struct ipts_set_mode - Payload for the SET_MODE command.
  2847. + * @mode: Changes the mode that IPTS will operate in.
  2848. + */
  2849. +struct ipts_set_mode {
  2850. + enum ipts_mode mode;
  2851. + u8 reserved[12];
  2852. +} __packed;
  2853. +
  2854. +static_assert(sizeof(struct ipts_set_mode) == 16);
  2855. +
  2856. +#define IPTS_WORKQUEUE_SIZE 8192
  2857. +#define IPTS_WORKQUEUE_ITEM_SIZE 16
  2858. +
  2859. +/**
  2860. + * struct ipts_mem_window - Payload for the SET_MEM_WINDOW command.
  2861. + * @data_addr_lower: Lower 32 bits of the data buffer addresses.
  2862. + * @data_addr_upper: Upper 32 bits of the data buffer addresses.
  2863. + * @workqueue_addr_lower: Lower 32 bits of the workqueue buffer address.
  2864. + * @workqueue_addr_upper: Upper 32 bits of the workqueue buffer address.
  2865. + * @doorbell_addr_lower: Lower 32 bits of the doorbell buffer address.
  2866. + * @doorbell_addr_upper: Upper 32 bits of the doorbell buffer address.
  2867. + * @feedbackaddr_lower: Lower 32 bits of the feedback buffer addresses.
  2868. + * @feedbackaddr_upper: Upper 32 bits of the feedback buffer addresses.
  2869. + * @hid2me_addr_lower: Lower 32 bits of the hid2me buffer address.
  2870. + * @hid2me_addr_upper: Upper 32 bits of the hid2me buffer address.
  2871. + * @hid2me_size: Size of the hid2me feedback buffer.
  2872. + * @workqueue_item_size: Magic value. Must be 16.
  2873. + * @workqueue_size: Magic value. Must be 8192.
  2874. + *
  2875. + * The workqueue related items in this struct are required for using
  2876. + * GuC submission with binary processing firmware. Since this driver does
  2877. + * not use GuC submission and instead exports raw data to userspace, these
  2878. + * items are not actually used, but they need to be allocated and passed
  2879. + * to the device, otherwise initialization will fail.
  2880. + */
  2881. +struct ipts_mem_window {
  2882. + u32 data_addr_lower[IPTS_BUFFERS];
  2883. + u32 data_addr_upper[IPTS_BUFFERS];
  2884. + u32 workqueue_addr_lower;
  2885. + u32 workqueue_addr_upper;
  2886. + u32 doorbell_addr_lower;
  2887. + u32 doorbell_addr_upper;
  2888. + u32 feedback_addr_lower[IPTS_BUFFERS];
  2889. + u32 feedback_addr_upper[IPTS_BUFFERS];
  2890. + u32 hid2me_addr_lower;
  2891. + u32 hid2me_addr_upper;
  2892. + u32 hid2me_size;
  2893. + u8 reserved1;
  2894. + u8 workqueue_item_size;
  2895. + u16 workqueue_size;
  2896. + u8 reserved[32];
  2897. +} __packed;
  2898. +
  2899. +static_assert(sizeof(struct ipts_mem_window) == 320);
  2900. +
  2901. +/**
  2902. + * struct ipts_quiesce_io - Payload for the QUIESCE_IO command.
  2903. + */
  2904. +struct ipts_quiesce_io {
  2905. + u8 reserved[12];
  2906. +} __packed;
  2907. +
  2908. +static_assert(sizeof(struct ipts_quiesce_io) == 12);
  2909. +
  2910. +/**
  2911. + * struct ipts_feedback - Payload for the FEEDBACK command.
  2912. + * @buffer: The buffer that the device should refill.
  2913. + */
  2914. +struct ipts_feedback {
  2915. + u32 buffer;
  2916. + u8 reserved[12];
  2917. +} __packed;
  2918. +
  2919. +static_assert(sizeof(struct ipts_feedback) == 16);
  2920. +
  2921. +/**
  2922. + * enum ipts_reset_type - Possible ways of resetting the device.
  2923. + * @IPTS_RESET_TYPE_HARD: Perform hardware reset using GPIO pin.
  2924. + * @IPTS_RESET_TYPE_SOFT: Perform software reset using SPI command.
  2925. + */
  2926. +enum ipts_reset_type {
  2927. + IPTS_RESET_TYPE_HARD = 0x00,
  2928. + IPTS_RESET_TYPE_SOFT = 0x01,
  2929. +};
  2930. +
  2931. +/**
  2932. + * struct ipts_reset - Payload for the RESET_SENSOR command.
  2933. + * @type: How the device should get reset.
  2934. + */
  2935. +struct ipts_reset_sensor {
  2936. + enum ipts_reset_type type;
  2937. + u8 reserved[4];
  2938. +} __packed;
  2939. +
  2940. +static_assert(sizeof(struct ipts_reset_sensor) == 8);
  2941. +
  2942. +/**
  2943. + * struct ipts_get_descriptor - Payload for the GET_DESCRIPTOR command.
  2944. + * @addr_lower: The lower 32 bits of the descriptor buffer address.
  2945. + * @addr_upper: The upper 32 bits of the descriptor buffer address.
  2946. + * @magic: A magic value. Must be 8.
  2947. + */
  2948. +struct ipts_get_descriptor {
  2949. + u32 addr_lower;
  2950. + u32 addr_upper;
  2951. + u32 magic;
  2952. + u8 reserved[12];
  2953. +} __packed;
  2954. +
  2955. +static_assert(sizeof(struct ipts_get_descriptor) == 24);
  2956. +
  2957. +/*
  2958. + * The type of a response is indicated by a
  2959. + * command code, with the most significant bit flipped to 1.
  2960. + */
  2961. +#define IPTS_RSP_BIT BIT(31)
  2962. +
  2963. +/**
  2964. + * struct ipts_response - Data returned from the device in response to a command.
  2965. + * @cmd: The command that this response answers (IPTS_RSP_BIT will be 1).
  2966. + * @status: The return code of the command.
  2967. + * @payload: The data that was produced by the command.
  2968. + */
  2969. +struct ipts_response {
  2970. + enum ipts_command_code cmd;
  2971. + enum ipts_status status;
  2972. + u8 payload[80];
  2973. +} __packed;
  2974. +
  2975. +static_assert(sizeof(struct ipts_response) == 88);
  2976. +
  2977. +/**
  2978. + * struct ipts_device_info - Vendor information of the IPTS device.
  2979. + * @vendor: Vendor ID of this device.
  2980. + * @product: Product ID of this device.
  2981. + * @hw_version: Hardware revision of this device.
  2982. + * @fw_version: Firmware revision of this device.
  2983. + * @data_size: Requested size for a data buffer.
  2984. + * @feedback_size: Requested size for a feedback buffer.
  2985. + * @mode: Mode that the device currently operates in.
  2986. + * @max_contacts: Maximum amount of concurrent touches the sensor can process.
  2987. + * @sensor_min_eds: The minimum EDS version supported by the sensor.
  2988. + * @sensor_max_eds: The maximum EDS version supported by the sensor.
  2989. + * @me_min_eds: The minimum EDS version supported by the ME for communicating with the sensor.
  2990. + * @me_max_eds: The maximum EDS version supported by the ME for communicating with the sensor.
  2991. + * @intf_eds: The EDS version implemented by the interface between ME and host.
  2992. + */
  2993. +struct ipts_device_info {
  2994. + u16 vendor;
  2995. + u16 product;
  2996. + u32 hw_version;
  2997. + u32 fw_version;
  2998. + u32 data_size;
  2999. + u32 feedback_size;
  3000. + enum ipts_mode mode;
  3001. + u8 max_contacts;
  3002. + u8 reserved1[3];
  3003. + u8 sensor_min_eds;
  3004. + u8 sensor_maj_eds;
  3005. + u8 me_min_eds;
  3006. + u8 me_maj_eds;
  3007. + u8 intf_eds;
  3008. + u8 reserved2[11];
  3009. +} __packed;
  3010. +
  3011. +static_assert(sizeof(struct ipts_device_info) == 44);
  3012. +
  3013. +#endif /* IPTS_SPEC_DEVICE_H */
  3014. diff --git a/drivers/hid/ipts/spec-hid.h b/drivers/hid/ipts/spec-hid.h
  3015. new file mode 100644
  3016. index 000000000000..5a58d4a0a610
  3017. --- /dev/null
  3018. +++ b/drivers/hid/ipts/spec-hid.h
  3019. @@ -0,0 +1,34 @@
  3020. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  3021. +/*
  3022. + * Copyright (c) 2020-2023 Dorian Stoll
  3023. + *
  3024. + * Linux driver for Intel Precise Touch & Stylus
  3025. + */
  3026. +
  3027. +#ifndef IPTS_SPEC_HID_H
  3028. +#define IPTS_SPEC_HID_H
  3029. +
  3030. +#include <linux/build_bug.h>
  3031. +#include <linux/types.h>
  3032. +
  3033. +/*
  3034. + * Made-up type for passing raw IPTS data in a HID report.
  3035. + */
  3036. +#define IPTS_HID_FRAME_TYPE_RAW 0xEE
  3037. +
  3038. +/**
  3039. + * struct ipts_hid_frame - Header that is prefixed to raw IPTS data wrapped in a HID report.
  3040. + * @size: Size of the data inside the report, including this header.
  3041. + * @type: What type of data does this report contain.
  3042. + */
  3043. +struct ipts_hid_header {
  3044. + u32 size;
  3045. + u8 reserved1;
  3046. + u8 type;
  3047. + u8 reserved2;
  3048. + u8 data[];
  3049. +} __packed;
  3050. +
  3051. +static_assert(sizeof(struct ipts_hid_header) == 7);
  3052. +
  3053. +#endif /* IPTS_SPEC_HID_H */
  3054. diff --git a/drivers/hid/ipts/thread.c b/drivers/hid/ipts/thread.c
  3055. new file mode 100644
  3056. index 000000000000..355e92bea26f
  3057. --- /dev/null
  3058. +++ b/drivers/hid/ipts/thread.c
  3059. @@ -0,0 +1,84 @@
  3060. +// SPDX-License-Identifier: GPL-2.0-or-later
  3061. +/*
  3062. + * Copyright (c) 2023 Dorian Stoll
  3063. + *
  3064. + * Linux driver for Intel Precise Touch & Stylus
  3065. + */
  3066. +
  3067. +#include <linux/completion.h>
  3068. +#include <linux/err.h>
  3069. +#include <linux/kthread.h>
  3070. +#include <linux/mutex.h>
  3071. +
  3072. +#include "thread.h"
  3073. +
  3074. +bool ipts_thread_should_stop(struct ipts_thread *thread)
  3075. +{
  3076. + if (!thread)
  3077. + return false;
  3078. +
  3079. + return READ_ONCE(thread->should_stop);
  3080. +}
  3081. +
  3082. +static int ipts_thread_runner(void *data)
  3083. +{
  3084. + int ret = 0;
  3085. + struct ipts_thread *thread = data;
  3086. +
  3087. + if (!thread)
  3088. + return -EFAULT;
  3089. +
  3090. + if (!thread->threadfn)
  3091. + return -EFAULT;
  3092. +
  3093. + ret = thread->threadfn(thread);
  3094. + complete_all(&thread->done);
  3095. +
  3096. + return ret;
  3097. +}
  3098. +
  3099. +int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_thread *thread),
  3100. + void *data, const char *name)
  3101. +{
  3102. + if (!thread)
  3103. + return -EFAULT;
  3104. +
  3105. + if (!threadfn)
  3106. + return -EFAULT;
  3107. +
  3108. + init_completion(&thread->done);
  3109. +
  3110. + thread->data = data;
  3111. + thread->should_stop = false;
  3112. + thread->threadfn = threadfn;
  3113. +
  3114. + thread->thread = kthread_run(ipts_thread_runner, thread, name);
  3115. + return PTR_ERR_OR_ZERO(thread->thread);
  3116. +}
  3117. +
  3118. +int ipts_thread_stop(struct ipts_thread *thread)
  3119. +{
  3120. + int ret = 0;
  3121. +
  3122. + if (!thread)
  3123. + return -EFAULT;
  3124. +
  3125. + if (!thread->thread)
  3126. + return 0;
  3127. +
  3128. + WRITE_ONCE(thread->should_stop, true);
  3129. +
  3130. + /*
  3131. + * Make sure that the write has gone through before waiting.
  3132. + */
  3133. + wmb();
  3134. +
  3135. + wait_for_completion(&thread->done);
  3136. + ret = kthread_stop(thread->thread);
  3137. +
  3138. + thread->thread = NULL;
  3139. + thread->data = NULL;
  3140. + thread->threadfn = NULL;
  3141. +
  3142. + return ret;
  3143. +}
  3144. diff --git a/drivers/hid/ipts/thread.h b/drivers/hid/ipts/thread.h
  3145. new file mode 100644
  3146. index 000000000000..1f966b8b32c4
  3147. --- /dev/null
  3148. +++ b/drivers/hid/ipts/thread.h
  3149. @@ -0,0 +1,59 @@
  3150. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  3151. +/*
  3152. + * Copyright (c) 2023 Dorian Stoll
  3153. + *
  3154. + * Linux driver for Intel Precise Touch & Stylus
  3155. + */
  3156. +
  3157. +#ifndef IPTS_THREAD_H
  3158. +#define IPTS_THREAD_H
  3159. +
  3160. +#include <linux/completion.h>
  3161. +#include <linux/mutex.h>
  3162. +#include <linux/sched.h>
  3163. +
  3164. +/*
  3165. + * This wrapper over kthread is necessary, because calling kthread_stop makes it impossible
  3166. + * to issue MEI commands from that thread while it shuts itself down. By using a custom
  3167. + * boolean variable and a completion object, we can call kthread_stop only when the thread
  3168. + * already finished all of its work and has returned.
  3169. + */
  3170. +struct ipts_thread {
  3171. + struct task_struct *thread;
  3172. +
  3173. + bool should_stop;
  3174. + struct completion done;
  3175. +
  3176. + void *data;
  3177. + int (*threadfn)(struct ipts_thread *thread);
  3178. +};
  3179. +
  3180. +/**
  3181. + * ipts_thread_should_stop() - Returns true if the thread is asked to terminate.
  3182. + * @thread: The current thread.
  3183. + *
  3184. + * Returns: true if the thread should stop, false if not.
  3185. + */
  3186. +bool ipts_thread_should_stop(struct ipts_thread *thread);
  3187. +
  3188. +/**
  3189. + * ipts_thread_start() - Starts an IPTS thread.
  3190. + * @thread: The thread to initialize and start.
  3191. + * @threadfn: The function to execute.
  3192. + * @data: An argument that will be passed to threadfn.
  3193. + * @name: The name of the new thread.
  3194. + *
  3195. + * Returns: 0 on success, <0 on error.
  3196. + */
  3197. +int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_thread *thread),
  3198. + void *data, const char name[]);
  3199. +
  3200. +/**
  3201. + * ipts_thread_stop() - Asks the thread to terminate and waits until it has finished.
  3202. + * @thread: The thread that should stop.
  3203. + *
  3204. + * Returns: The return value of the thread function.
  3205. + */
  3206. +int ipts_thread_stop(struct ipts_thread *thread);
  3207. +
  3208. +#endif /* IPTS_THREAD_H */
  3209. --
  3210. 2.49.0