0004-ipts.patch 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435
  1. From 3ec62f7bde89e3c84aae8eac58e5e9a173383d35 Mon Sep 17 00:00:00 2001
  2. From: Dorian Stoll <dorian.stoll@tmsp.io>
  3. Date: Thu, 30 Jul 2020 13:21:53 +0200
  4. Subject: [PATCH] misc: mei: Add missing IPTS device IDs
  5. Patchset: ipts
  6. ---
  7. drivers/misc/mei/hw-me-regs.h | 1 +
  8. drivers/misc/mei/pci-me.c | 1 +
  9. 2 files changed, 2 insertions(+)
  10. diff --git a/drivers/misc/mei/hw-me-regs.h b/drivers/misc/mei/hw-me-regs.h
  11. index bdc65d50b945..08723c01d727 100644
  12. --- a/drivers/misc/mei/hw-me-regs.h
  13. +++ b/drivers/misc/mei/hw-me-regs.h
  14. @@ -92,6 +92,7 @@
  15. #define MEI_DEV_ID_CDF 0x18D3 /* Cedar Fork */
  16. #define MEI_DEV_ID_ICP_LP 0x34E0 /* Ice Lake Point LP */
  17. +#define MEI_DEV_ID_ICP_LP_3 0x34E4 /* Ice Lake Point LP 3 (iTouch) */
  18. #define MEI_DEV_ID_ICP_N 0x38E0 /* Ice Lake Point N */
  19. #define MEI_DEV_ID_JSP_N 0x4DE0 /* Jasper Lake Point N */
  20. diff --git a/drivers/misc/mei/pci-me.c b/drivers/misc/mei/pci-me.c
  21. index 676d566f38dd..6b37dd1f8b2a 100644
  22. --- a/drivers/misc/mei/pci-me.c
  23. +++ b/drivers/misc/mei/pci-me.c
  24. @@ -97,6 +97,7 @@ static const struct pci_device_id mei_me_pci_tbl[] = {
  25. {MEI_PCI_DEVICE(MEI_DEV_ID_CMP_H_3, MEI_ME_PCH8_ITOUCH_CFG)},
  26. {MEI_PCI_DEVICE(MEI_DEV_ID_ICP_LP, MEI_ME_PCH12_CFG)},
  27. + {MEI_PCI_DEVICE(MEI_DEV_ID_ICP_LP_3, MEI_ME_PCH12_CFG)},
  28. {MEI_PCI_DEVICE(MEI_DEV_ID_ICP_N, MEI_ME_PCH12_CFG)},
  29. {MEI_PCI_DEVICE(MEI_DEV_ID_TGP_LP, MEI_ME_PCH15_CFG)},
  30. --
  31. 2.41.0
  32. From 5969d31250c82992b3e5982bf73723dc685099e4 Mon Sep 17 00:00:00 2001
  33. From: Liban Hannan <liban.p@gmail.com>
  34. Date: Tue, 12 Apr 2022 23:31:12 +0100
  35. Subject: [PATCH] iommu: ipts: use IOMMU passthrough mode for IPTS
  36. Adds a quirk so that IOMMU uses passthrough mode for the IPTS device.
  37. Otherwise, when IOMMU is enabled, IPTS produces DMAR errors like:
  38. DMAR: [DMA Read NO_PASID] Request device [00:16.4] fault addr
  39. 0x104ea3000 [fault reason 0x06] PTE Read access is not set
  40. This is very similar to the bug described at:
  41. https://bugs.launchpad.net/bugs/1958004
  42. Fixed with the following patch which this patch basically copies:
  43. https://launchpadlibrarian.net/586396847/43255ca.diff
  44. Patchset: ipts
  45. ---
  46. drivers/iommu/intel/iommu.c | 24 ++++++++++++++++++++++++
  47. 1 file changed, 24 insertions(+)
  48. diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c
  49. index b871a6afd803..0d4542b7365d 100644
  50. --- a/drivers/iommu/intel/iommu.c
  51. +++ b/drivers/iommu/intel/iommu.c
  52. @@ -37,6 +37,8 @@
  53. #define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
  54. #define IS_USB_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_SERIAL_USB)
  55. #define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
  56. +#define IS_IPTS(pdev) ((pdev)->vendor == PCI_VENDOR_ID_INTEL && \
  57. + ((pdev)->device == 0x9d3e))
  58. #define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
  59. #define IOAPIC_RANGE_START (0xfee00000)
  60. @@ -287,12 +289,14 @@ int intel_iommu_enabled = 0;
  61. EXPORT_SYMBOL_GPL(intel_iommu_enabled);
  62. static int dmar_map_gfx = 1;
  63. +static int dmar_map_ipts = 1;
  64. static int intel_iommu_superpage = 1;
  65. static int iommu_identity_mapping;
  66. static int iommu_skip_te_disable;
  67. #define IDENTMAP_GFX 2
  68. #define IDENTMAP_AZALIA 4
  69. +#define IDENTMAP_IPTS 16
  70. const struct iommu_ops intel_iommu_ops;
  71. @@ -2560,6 +2564,9 @@ static int device_def_domain_type(struct device *dev)
  72. if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
  73. return IOMMU_DOMAIN_IDENTITY;
  74. +
  75. + if ((iommu_identity_mapping & IDENTMAP_IPTS) && IS_IPTS(pdev))
  76. + return IOMMU_DOMAIN_IDENTITY;
  77. }
  78. return 0;
  79. @@ -2867,6 +2874,9 @@ static int __init init_dmars(void)
  80. if (!dmar_map_gfx)
  81. iommu_identity_mapping |= IDENTMAP_GFX;
  82. + if (!dmar_map_ipts)
  83. + iommu_identity_mapping |= IDENTMAP_IPTS;
  84. +
  85. check_tylersburg_isoch();
  86. ret = si_domain_init(hw_pass_through);
  87. @@ -4778,6 +4788,17 @@ static void quirk_iommu_igfx(struct pci_dev *dev)
  88. dmar_map_gfx = 0;
  89. }
  90. +static void quirk_iommu_ipts(struct pci_dev *dev)
  91. +{
  92. + if (!IS_IPTS(dev))
  93. + return;
  94. +
  95. + if (risky_device(dev))
  96. + return;
  97. +
  98. + pci_info(dev, "Passthrough IOMMU for IPTS\n");
  99. + dmar_map_ipts = 0;
  100. +}
  101. /* G4x/GM45 integrated gfx dmar support is totally busted. */
  102. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_igfx);
  103. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_igfx);
  104. @@ -4813,6 +4834,9 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x1632, quirk_iommu_igfx);
  105. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x163A, quirk_iommu_igfx);
  106. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x163D, quirk_iommu_igfx);
  107. +/* disable IPTS dmar support */
  108. +DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x9D3E, quirk_iommu_ipts);
  109. +
  110. static void quirk_iommu_rwbf(struct pci_dev *dev)
  111. {
  112. if (risky_device(dev))
  113. --
  114. 2.41.0
  115. From 5570234ede6474477c2e8445c063f7cfac08c45a Mon Sep 17 00:00:00 2001
  116. From: Dorian Stoll <dorian.stoll@tmsp.io>
  117. Date: Sun, 11 Dec 2022 12:00:59 +0100
  118. Subject: [PATCH] hid: Add support for Intel Precise Touch and Stylus
  119. Based on linux-surface/intel-precise-touch@8abe268
  120. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  121. Patchset: ipts
  122. ---
  123. drivers/hid/Kconfig | 2 +
  124. drivers/hid/Makefile | 2 +
  125. drivers/hid/ipts/Kconfig | 14 +
  126. drivers/hid/ipts/Makefile | 14 +
  127. drivers/hid/ipts/cmd.c | 62 +++++
  128. drivers/hid/ipts/cmd.h | 61 ++++
  129. drivers/hid/ipts/context.h | 51 ++++
  130. drivers/hid/ipts/control.c | 495 +++++++++++++++++++++++++++++++++
  131. drivers/hid/ipts/control.h | 127 +++++++++
  132. drivers/hid/ipts/desc.h | 81 ++++++
  133. drivers/hid/ipts/hid.c | 348 +++++++++++++++++++++++
  134. drivers/hid/ipts/hid.h | 22 ++
  135. drivers/hid/ipts/main.c | 127 +++++++++
  136. drivers/hid/ipts/mei.c | 189 +++++++++++++
  137. drivers/hid/ipts/mei.h | 67 +++++
  138. drivers/hid/ipts/receiver.c | 249 +++++++++++++++++
  139. drivers/hid/ipts/receiver.h | 17 ++
  140. drivers/hid/ipts/resources.c | 108 +++++++
  141. drivers/hid/ipts/resources.h | 39 +++
  142. drivers/hid/ipts/spec-data.h | 100 +++++++
  143. drivers/hid/ipts/spec-device.h | 285 +++++++++++++++++++
  144. drivers/hid/ipts/spec-hid.h | 35 +++
  145. drivers/hid/ipts/thread.c | 85 ++++++
  146. drivers/hid/ipts/thread.h | 60 ++++
  147. 24 files changed, 2640 insertions(+)
  148. create mode 100644 drivers/hid/ipts/Kconfig
  149. create mode 100644 drivers/hid/ipts/Makefile
  150. create mode 100644 drivers/hid/ipts/cmd.c
  151. create mode 100644 drivers/hid/ipts/cmd.h
  152. create mode 100644 drivers/hid/ipts/context.h
  153. create mode 100644 drivers/hid/ipts/control.c
  154. create mode 100644 drivers/hid/ipts/control.h
  155. create mode 100644 drivers/hid/ipts/desc.h
  156. create mode 100644 drivers/hid/ipts/hid.c
  157. create mode 100644 drivers/hid/ipts/hid.h
  158. create mode 100644 drivers/hid/ipts/main.c
  159. create mode 100644 drivers/hid/ipts/mei.c
  160. create mode 100644 drivers/hid/ipts/mei.h
  161. create mode 100644 drivers/hid/ipts/receiver.c
  162. create mode 100644 drivers/hid/ipts/receiver.h
  163. create mode 100644 drivers/hid/ipts/resources.c
  164. create mode 100644 drivers/hid/ipts/resources.h
  165. create mode 100644 drivers/hid/ipts/spec-data.h
  166. create mode 100644 drivers/hid/ipts/spec-device.h
  167. create mode 100644 drivers/hid/ipts/spec-hid.h
  168. create mode 100644 drivers/hid/ipts/thread.c
  169. create mode 100644 drivers/hid/ipts/thread.h
  170. diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
  171. index b977450cac75..dcedd5b90f35 100644
  172. --- a/drivers/hid/Kconfig
  173. +++ b/drivers/hid/Kconfig
  174. @@ -1316,4 +1316,6 @@ source "drivers/hid/amd-sfh-hid/Kconfig"
  175. source "drivers/hid/surface-hid/Kconfig"
  176. +source "drivers/hid/ipts/Kconfig"
  177. +
  178. endif # HID_SUPPORT
  179. diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
  180. index 5d37cacbde33..285e12d95b0e 100644
  181. --- a/drivers/hid/Makefile
  182. +++ b/drivers/hid/Makefile
  183. @@ -167,3 +167,5 @@ obj-$(INTEL_ISH_FIRMWARE_DOWNLOADER) += intel-ish-hid/
  184. obj-$(CONFIG_AMD_SFH_HID) += amd-sfh-hid/
  185. obj-$(CONFIG_SURFACE_HID_CORE) += surface-hid/
  186. +
  187. +obj-$(CONFIG_HID_IPTS) += ipts/
  188. diff --git a/drivers/hid/ipts/Kconfig b/drivers/hid/ipts/Kconfig
  189. new file mode 100644
  190. index 000000000000..297401bd388d
  191. --- /dev/null
  192. +++ b/drivers/hid/ipts/Kconfig
  193. @@ -0,0 +1,14 @@
  194. +# SPDX-License-Identifier: GPL-2.0-or-later
  195. +
  196. +config HID_IPTS
  197. + tristate "Intel Precise Touch & Stylus"
  198. + depends on INTEL_MEI
  199. + depends on HID
  200. + help
  201. + Say Y here if your system has a touchscreen using Intels
  202. + Precise Touch & Stylus (IPTS) technology.
  203. +
  204. + If unsure say N.
  205. +
  206. + To compile this driver as a module, choose M here: the
  207. + module will be called ipts.
  208. diff --git a/drivers/hid/ipts/Makefile b/drivers/hid/ipts/Makefile
  209. new file mode 100644
  210. index 000000000000..0fe655bccdc0
  211. --- /dev/null
  212. +++ b/drivers/hid/ipts/Makefile
  213. @@ -0,0 +1,14 @@
  214. +# SPDX-License-Identifier: GPL-2.0-or-later
  215. +#
  216. +# Makefile for the IPTS touchscreen driver
  217. +#
  218. +
  219. +obj-$(CONFIG_HID_IPTS) += ipts.o
  220. +ipts-objs := cmd.o
  221. +ipts-objs += control.o
  222. +ipts-objs += hid.o
  223. +ipts-objs += main.o
  224. +ipts-objs += mei.o
  225. +ipts-objs += receiver.o
  226. +ipts-objs += resources.o
  227. +ipts-objs += thread.o
  228. diff --git a/drivers/hid/ipts/cmd.c b/drivers/hid/ipts/cmd.c
  229. new file mode 100644
  230. index 000000000000..7fd69271ccd5
  231. --- /dev/null
  232. +++ b/drivers/hid/ipts/cmd.c
  233. @@ -0,0 +1,62 @@
  234. +// SPDX-License-Identifier: GPL-2.0-or-later
  235. +/*
  236. + * Copyright (c) 2016 Intel Corporation
  237. + * Copyright (c) 2020-2023 Dorian Stoll
  238. + *
  239. + * Linux driver for Intel Precise Touch & Stylus
  240. + */
  241. +
  242. +#include <linux/errno.h>
  243. +#include <linux/types.h>
  244. +
  245. +#include "cmd.h"
  246. +#include "context.h"
  247. +#include "mei.h"
  248. +#include "spec-device.h"
  249. +
  250. +int ipts_cmd_recv_timeout(struct ipts_context *ipts, enum ipts_command_code code,
  251. + struct ipts_response *rsp, u64 timeout)
  252. +{
  253. + int ret = 0;
  254. +
  255. + if (!ipts)
  256. + return -EFAULT;
  257. +
  258. + if (!rsp)
  259. + return -EFAULT;
  260. +
  261. + /*
  262. + * In a response, the command code will have the most significant bit flipped to 1.
  263. + * If code is passed to ipts_mei_recv as is, no messages will be received.
  264. + */
  265. + ret = ipts_mei_recv(&ipts->mei, code | IPTS_RSP_BIT, rsp, timeout);
  266. + if (ret < 0)
  267. + return ret;
  268. +
  269. + dev_dbg(ipts->dev, "Received 0x%02X with status 0x%02X\n", code, rsp->status);
  270. +
  271. + /*
  272. + * Some devices will always return this error.
  273. + * It is allowed to ignore it and to try continuing.
  274. + */
  275. + if (rsp->status == IPTS_STATUS_COMPAT_CHECK_FAIL)
  276. + rsp->status = IPTS_STATUS_SUCCESS;
  277. +
  278. + return 0;
  279. +}
  280. +
  281. +int ipts_cmd_send(struct ipts_context *ipts, enum ipts_command_code code, void *data, size_t size)
  282. +{
  283. + struct ipts_command cmd = { 0 };
  284. +
  285. + if (!ipts)
  286. + return -EFAULT;
  287. +
  288. + cmd.cmd = code;
  289. +
  290. + if (data && size > 0)
  291. + memcpy(cmd.payload, data, size);
  292. +
  293. + dev_dbg(ipts->dev, "Sending 0x%02X with %ld bytes payload\n", code, size);
  294. + return ipts_mei_send(&ipts->mei, &cmd, sizeof(cmd.cmd) + size);
  295. +}
  296. diff --git a/drivers/hid/ipts/cmd.h b/drivers/hid/ipts/cmd.h
  297. new file mode 100644
  298. index 000000000000..924758ffee67
  299. --- /dev/null
  300. +++ b/drivers/hid/ipts/cmd.h
  301. @@ -0,0 +1,61 @@
  302. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  303. +/*
  304. + * Copyright (c) 2016 Intel Corporation
  305. + * Copyright (c) 2020-2023 Dorian Stoll
  306. + *
  307. + * Linux driver for Intel Precise Touch & Stylus
  308. + */
  309. +
  310. +#ifndef IPTS_CMD_H
  311. +#define IPTS_CMD_H
  312. +
  313. +#include <linux/types.h>
  314. +
  315. +#include "context.h"
  316. +#include "spec-device.h"
  317. +
  318. +/*
  319. + * The default timeout for receiving responses
  320. + */
  321. +#define IPTS_CMD_DEFAULT_TIMEOUT 1000
  322. +
  323. +/*
  324. + * ipts_cmd_recv_timeout() - Receives a response to a command.
  325. + * @ipts: The IPTS driver context.
  326. + * @code: The type of the command / response.
  327. + * @rsp: The address that the received response will be copied to.
  328. + * @timeout: How many milliseconds the function will wait at most.
  329. + *
  330. + * A negative timeout means to wait forever.
  331. + *
  332. + * Returns: 0 on success, <0 on error, -EAGAIN if no response has been received.
  333. + */
  334. +int ipts_cmd_recv_timeout(struct ipts_context *ipts, enum ipts_command_code code,
  335. + struct ipts_response *rsp, u64 timeout);
  336. +
  337. +/*
  338. + * ipts_cmd_recv() - Receives a response to a command.
  339. + * @ipts: The IPTS driver context.
  340. + * @code: The type of the command / response.
  341. + * @rsp: The address that the received response will be copied to.
  342. + *
  343. + * Returns: 0 on success, <0 on error, -EAGAIN if no response has been received.
  344. + */
  345. +static inline int ipts_cmd_recv(struct ipts_context *ipts, enum ipts_command_code code,
  346. + struct ipts_response *rsp)
  347. +{
  348. + return ipts_cmd_recv_timeout(ipts, code, rsp, IPTS_CMD_DEFAULT_TIMEOUT);
  349. +}
  350. +
  351. +/*
  352. + * ipts_cmd_send() - Executes a command on the device.
  353. + * @ipts: The IPTS driver context.
  354. + * @code: The type of the command to execute.
  355. + * @data: The payload containing parameters for the command.
  356. + * @size: The size of the payload.
  357. + *
  358. + * Returns: 0 on success, <0 on error.
  359. + */
  360. +int ipts_cmd_send(struct ipts_context *ipts, enum ipts_command_code code, void *data, size_t size);
  361. +
  362. +#endif /* IPTS_CMD_H */
  363. diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h
  364. new file mode 100644
  365. index 000000000000..3450a95e66ee
  366. --- /dev/null
  367. +++ b/drivers/hid/ipts/context.h
  368. @@ -0,0 +1,51 @@
  369. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  370. +/*
  371. + * Copyright (c) 2016 Intel Corporation
  372. + * Copyright (c) 2020-2023 Dorian Stoll
  373. + *
  374. + * Linux driver for Intel Precise Touch & Stylus
  375. + */
  376. +
  377. +#ifndef IPTS_CONTEXT_H
  378. +#define IPTS_CONTEXT_H
  379. +
  380. +#include <linux/completion.h>
  381. +#include <linux/device.h>
  382. +#include <linux/hid.h>
  383. +#include <linux/mei_cl_bus.h>
  384. +#include <linux/mutex.h>
  385. +#include <linux/sched.h>
  386. +#include <linux/types.h>
  387. +
  388. +#include "mei.h"
  389. +#include "resources.h"
  390. +#include "spec-device.h"
  391. +#include "thread.h"
  392. +
  393. +struct ipts_context {
  394. + struct device *dev;
  395. + struct ipts_mei mei;
  396. +
  397. + enum ipts_mode mode;
  398. +
  399. + /*
  400. + * Prevents concurrent GET_FEATURE reports.
  401. + */
  402. + struct mutex feature_lock;
  403. + struct completion feature_event;
  404. +
  405. + /*
  406. + * These are not inside of struct ipts_resources
  407. + * because they don't own the memory they point to.
  408. + */
  409. + struct ipts_buffer feature_report;
  410. + struct ipts_buffer descriptor;
  411. +
  412. + struct hid_device *hid;
  413. + struct ipts_device_info info;
  414. + struct ipts_resources resources;
  415. +
  416. + struct ipts_thread receiver_loop;
  417. +};
  418. +
  419. +#endif /* IPTS_CONTEXT_H */
  420. diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c
  421. new file mode 100644
  422. index 000000000000..2f61500b5119
  423. --- /dev/null
  424. +++ b/drivers/hid/ipts/control.c
  425. @@ -0,0 +1,495 @@
  426. +// SPDX-License-Identifier: GPL-2.0-or-later
  427. +/*
  428. + * Copyright (c) 2016 Intel Corporation
  429. + * Copyright (c) 2020-2023 Dorian Stoll
  430. + *
  431. + * Linux driver for Intel Precise Touch & Stylus
  432. + */
  433. +
  434. +#include <linux/delay.h>
  435. +#include <linux/dev_printk.h>
  436. +#include <linux/errno.h>
  437. +#include <linux/kernel.h>
  438. +#include <linux/kthread.h>
  439. +#include <linux/types.h>
  440. +
  441. +#include "cmd.h"
  442. +#include "context.h"
  443. +#include "control.h"
  444. +#include "desc.h"
  445. +#include "hid.h"
  446. +#include "receiver.h"
  447. +#include "resources.h"
  448. +#include "spec-data.h"
  449. +#include "spec-device.h"
  450. +
  451. +static int ipts_control_get_device_info(struct ipts_context *ipts, struct ipts_device_info *info)
  452. +{
  453. + int ret = 0;
  454. + struct ipts_response rsp = { 0 };
  455. +
  456. + if (!ipts)
  457. + return -EFAULT;
  458. +
  459. + if (!info)
  460. + return -EFAULT;
  461. +
  462. + ret = ipts_cmd_send(ipts, IPTS_CMD_GET_DEVICE_INFO, NULL, 0);
  463. + if (ret) {
  464. + dev_err(ipts->dev, "GET_DEVICE_INFO: send failed: %d\n", ret);
  465. + return ret;
  466. + }
  467. +
  468. + ret = ipts_cmd_recv(ipts, IPTS_CMD_GET_DEVICE_INFO, &rsp);
  469. + if (ret) {
  470. + dev_err(ipts->dev, "GET_DEVICE_INFO: recv failed: %d\n", ret);
  471. + return ret;
  472. + }
  473. +
  474. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  475. + dev_err(ipts->dev, "GET_DEVICE_INFO: cmd failed: %d\n", rsp.status);
  476. + return -EBADR;
  477. + }
  478. +
  479. + memcpy(info, rsp.payload, sizeof(*info));
  480. + return 0;
  481. +}
  482. +
  483. +static int ipts_control_set_mode(struct ipts_context *ipts, enum ipts_mode mode)
  484. +{
  485. + int ret = 0;
  486. + struct ipts_set_mode cmd = { 0 };
  487. + struct ipts_response rsp = { 0 };
  488. +
  489. + if (!ipts)
  490. + return -EFAULT;
  491. +
  492. + cmd.mode = mode;
  493. +
  494. + ret = ipts_cmd_send(ipts, IPTS_CMD_SET_MODE, &cmd, sizeof(cmd));
  495. + if (ret) {
  496. + dev_err(ipts->dev, "SET_MODE: send failed: %d\n", ret);
  497. + return ret;
  498. + }
  499. +
  500. + ret = ipts_cmd_recv(ipts, IPTS_CMD_SET_MODE, &rsp);
  501. + if (ret) {
  502. + dev_err(ipts->dev, "SET_MODE: recv failed: %d\n", ret);
  503. + return ret;
  504. + }
  505. +
  506. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  507. + dev_err(ipts->dev, "SET_MODE: cmd failed: %d\n", rsp.status);
  508. + return -EBADR;
  509. + }
  510. +
  511. + return 0;
  512. +}
  513. +
  514. +static int ipts_control_set_mem_window(struct ipts_context *ipts, struct ipts_resources *res)
  515. +{
  516. + int ret = 0;
  517. + struct ipts_mem_window cmd = { 0 };
  518. + struct ipts_response rsp = { 0 };
  519. +
  520. + if (!ipts)
  521. + return -EFAULT;
  522. +
  523. + if (!res)
  524. + return -EFAULT;
  525. +
  526. + for (int i = 0; i < IPTS_BUFFERS; i++) {
  527. + cmd.data_addr_lower[i] = lower_32_bits(res->data[i].dma_address);
  528. + cmd.data_addr_upper[i] = upper_32_bits(res->data[i].dma_address);
  529. + cmd.feedback_addr_lower[i] = lower_32_bits(res->feedback[i].dma_address);
  530. + cmd.feedback_addr_upper[i] = upper_32_bits(res->feedback[i].dma_address);
  531. + }
  532. +
  533. + cmd.workqueue_addr_lower = lower_32_bits(res->workqueue.dma_address);
  534. + cmd.workqueue_addr_upper = upper_32_bits(res->workqueue.dma_address);
  535. +
  536. + cmd.doorbell_addr_lower = lower_32_bits(res->doorbell.dma_address);
  537. + cmd.doorbell_addr_upper = upper_32_bits(res->doorbell.dma_address);
  538. +
  539. + cmd.hid2me_addr_lower = lower_32_bits(res->hid2me.dma_address);
  540. + cmd.hid2me_addr_upper = upper_32_bits(res->hid2me.dma_address);
  541. +
  542. + cmd.workqueue_size = IPTS_WORKQUEUE_SIZE;
  543. + cmd.workqueue_item_size = IPTS_WORKQUEUE_ITEM_SIZE;
  544. +
  545. + ret = ipts_cmd_send(ipts, IPTS_CMD_SET_MEM_WINDOW, &cmd, sizeof(cmd));
  546. + if (ret) {
  547. + dev_err(ipts->dev, "SET_MEM_WINDOW: send failed: %d\n", ret);
  548. + return ret;
  549. + }
  550. +
  551. + ret = ipts_cmd_recv(ipts, IPTS_CMD_SET_MEM_WINDOW, &rsp);
  552. + if (ret) {
  553. + dev_err(ipts->dev, "SET_MEM_WINDOW: recv failed: %d\n", ret);
  554. + return ret;
  555. + }
  556. +
  557. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  558. + dev_err(ipts->dev, "SET_MEM_WINDOW: cmd failed: %d\n", rsp.status);
  559. + return -EBADR;
  560. + }
  561. +
  562. + return 0;
  563. +}
  564. +
  565. +static int ipts_control_get_descriptor(struct ipts_context *ipts)
  566. +{
  567. + int ret = 0;
  568. + struct ipts_data_header *header = NULL;
  569. + struct ipts_get_descriptor cmd = { 0 };
  570. + struct ipts_response rsp = { 0 };
  571. +
  572. + if (!ipts)
  573. + return -EFAULT;
  574. +
  575. + if (!ipts->resources.descriptor.address)
  576. + return -EFAULT;
  577. +
  578. + memset(ipts->resources.descriptor.address, 0, ipts->resources.descriptor.size);
  579. +
  580. + cmd.addr_lower = lower_32_bits(ipts->resources.descriptor.dma_address);
  581. + cmd.addr_upper = upper_32_bits(ipts->resources.descriptor.dma_address);
  582. + cmd.magic = 8;
  583. +
  584. + ret = ipts_cmd_send(ipts, IPTS_CMD_GET_DESCRIPTOR, &cmd, sizeof(cmd));
  585. + if (ret) {
  586. + dev_err(ipts->dev, "GET_DESCRIPTOR: send failed: %d\n", ret);
  587. + return ret;
  588. + }
  589. +
  590. + ret = ipts_cmd_recv(ipts, IPTS_CMD_GET_DESCRIPTOR, &rsp);
  591. + if (ret) {
  592. + dev_err(ipts->dev, "GET_DESCRIPTOR: recv failed: %d\n", ret);
  593. + return ret;
  594. + }
  595. +
  596. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  597. + dev_err(ipts->dev, "GET_DESCRIPTOR: cmd failed: %d\n", rsp.status);
  598. + return -EBADR;
  599. + }
  600. +
  601. + header = (struct ipts_data_header *)ipts->resources.descriptor.address;
  602. +
  603. + if (header->type == IPTS_DATA_TYPE_DESCRIPTOR) {
  604. + ipts->descriptor.address = &header->data[8];
  605. + ipts->descriptor.size = header->size - 8;
  606. +
  607. + return 0;
  608. + }
  609. +
  610. + return -ENODATA;
  611. +}
  612. +
  613. +int ipts_control_request_flush(struct ipts_context *ipts)
  614. +{
  615. + int ret = 0;
  616. + struct ipts_quiesce_io cmd = { 0 };
  617. +
  618. + if (!ipts)
  619. + return -EFAULT;
  620. +
  621. + ret = ipts_cmd_send(ipts, IPTS_CMD_QUIESCE_IO, &cmd, sizeof(cmd));
  622. + if (ret)
  623. + dev_err(ipts->dev, "QUIESCE_IO: send failed: %d\n", ret);
  624. +
  625. + return ret;
  626. +}
  627. +
  628. +int ipts_control_wait_flush(struct ipts_context *ipts)
  629. +{
  630. + int ret = 0;
  631. + struct ipts_response rsp = { 0 };
  632. +
  633. + if (!ipts)
  634. + return -EFAULT;
  635. +
  636. + ret = ipts_cmd_recv(ipts, IPTS_CMD_QUIESCE_IO, &rsp);
  637. + if (ret) {
  638. + dev_err(ipts->dev, "QUIESCE_IO: recv failed: %d\n", ret);
  639. + return ret;
  640. + }
  641. +
  642. + if (rsp.status == IPTS_STATUS_TIMEOUT)
  643. + return -EAGAIN;
  644. +
  645. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  646. + dev_err(ipts->dev, "QUIESCE_IO: cmd failed: %d\n", rsp.status);
  647. + return -EBADR;
  648. + }
  649. +
  650. + return 0;
  651. +}
  652. +
  653. +int ipts_control_request_data(struct ipts_context *ipts)
  654. +{
  655. + int ret = 0;
  656. +
  657. + if (!ipts)
  658. + return -EFAULT;
  659. +
  660. + ret = ipts_cmd_send(ipts, IPTS_CMD_READY_FOR_DATA, NULL, 0);
  661. + if (ret)
  662. + dev_err(ipts->dev, "READY_FOR_DATA: send failed: %d\n", ret);
  663. +
  664. + return ret;
  665. +}
  666. +
  667. +int ipts_control_wait_data(struct ipts_context *ipts, bool shutdown)
  668. +{
  669. + int ret = 0;
  670. + struct ipts_response rsp = { 0 };
  671. +
  672. + if (!ipts)
  673. + return -EFAULT;
  674. +
  675. + if (!shutdown)
  676. + ret = ipts_cmd_recv_timeout(ipts, IPTS_CMD_READY_FOR_DATA, &rsp, 0);
  677. + else
  678. + ret = ipts_cmd_recv(ipts, IPTS_CMD_READY_FOR_DATA, &rsp);
  679. +
  680. + if (ret) {
  681. + if (ret != -EAGAIN)
  682. + dev_err(ipts->dev, "READY_FOR_DATA: recv failed: %d\n", ret);
  683. +
  684. + return ret;
  685. + }
  686. +
  687. + /*
  688. + * During shutdown, it is possible that the sensor has already been disabled.
  689. + */
  690. + if (rsp.status == IPTS_STATUS_SENSOR_DISABLED)
  691. + return 0;
  692. +
  693. + if (rsp.status == IPTS_STATUS_TIMEOUT)
  694. + return -EAGAIN;
  695. +
  696. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  697. + dev_err(ipts->dev, "READY_FOR_DATA: cmd failed: %d\n", rsp.status);
  698. + return -EBADR;
  699. + }
  700. +
  701. + return 0;
  702. +}
  703. +
  704. +int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer)
  705. +{
  706. + int ret = 0;
  707. + struct ipts_feedback cmd = { 0 };
  708. + struct ipts_response rsp = { 0 };
  709. +
  710. + if (!ipts)
  711. + return -EFAULT;
  712. +
  713. + cmd.buffer = buffer;
  714. +
  715. + ret = ipts_cmd_send(ipts, IPTS_CMD_FEEDBACK, &cmd, sizeof(cmd));
  716. + if (ret) {
  717. + dev_err(ipts->dev, "FEEDBACK: send failed: %d\n", ret);
  718. + return ret;
  719. + }
  720. +
  721. + ret = ipts_cmd_recv(ipts, IPTS_CMD_FEEDBACK, &rsp);
  722. + if (ret) {
  723. + dev_err(ipts->dev, "FEEDBACK: recv failed: %d\n", ret);
  724. + return ret;
  725. + }
  726. +
  727. + /*
  728. + * We don't know what feedback data looks like so we are sending zeros.
  729. + * See also ipts_control_refill_buffer.
  730. + */
  731. + if (rsp.status == IPTS_STATUS_INVALID_PARAMS)
  732. + return 0;
  733. +
  734. + if (rsp.status != IPTS_STATUS_SUCCESS) {
  735. + dev_err(ipts->dev, "FEEDBACK: cmd failed: %d\n", rsp.status);
  736. + return -EBADR;
  737. + }
  738. +
  739. + return 0;
  740. +}
  741. +
  742. +int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_cmd_type cmd,
  743. + enum ipts_feedback_data_type type, void *data, size_t size)
  744. +{
  745. + struct ipts_feedback_header *header = NULL;
  746. +
  747. + if (!ipts)
  748. + return -EFAULT;
  749. +
  750. + if (!ipts->resources.hid2me.address)
  751. + return -EFAULT;
  752. +
  753. + memset(ipts->resources.hid2me.address, 0, ipts->resources.hid2me.size);
  754. + header = (struct ipts_feedback_header *)ipts->resources.hid2me.address;
  755. +
  756. + header->cmd_type = cmd;
  757. + header->data_type = type;
  758. + header->size = size;
  759. + header->buffer = IPTS_HID2ME_BUFFER;
  760. +
  761. + if (size + sizeof(*header) > ipts->resources.hid2me.size)
  762. + return -EINVAL;
  763. +
  764. + if (data && size > 0)
  765. + memcpy(header->payload, data, size);
  766. +
  767. + return ipts_control_send_feedback(ipts, IPTS_HID2ME_BUFFER);
  768. +}
  769. +
  770. +static inline int ipts_control_reset_sensor(struct ipts_context *ipts)
  771. +{
  772. + return ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_SOFT_RESET,
  773. + IPTS_FEEDBACK_DATA_TYPE_VENDOR, NULL, 0);
  774. +}
  775. +
  776. +int ipts_control_start(struct ipts_context *ipts)
  777. +{
  778. + int ret = 0;
  779. + struct ipts_device_info info = { 0 };
  780. +
  781. + if (!ipts)
  782. + return -EFAULT;
  783. +
  784. + dev_info(ipts->dev, "Starting IPTS\n");
  785. +
  786. + ret = ipts_control_get_device_info(ipts, &info);
  787. + if (ret) {
  788. + dev_err(ipts->dev, "Failed to get device info: %d\n", ret);
  789. + return ret;
  790. + }
  791. +
  792. + ipts->info = info;
  793. +
  794. + ret = ipts_resources_init(&ipts->resources, ipts->dev, info.data_size, info.feedback_size);
  795. + if (ret) {
  796. + dev_err(ipts->dev, "Failed to allocate buffers: %d", ret);
  797. + return ret;
  798. + }
  799. +
  800. + dev_info(ipts->dev, "IPTS EDS Version: %d\n", info.intf_eds);
  801. +
  802. + /*
  803. + * Handle newer devices
  804. + */
  805. + if (info.intf_eds > 1) {
  806. + /*
  807. + * Fetching the descriptor will only work on newer devices.
  808. + * For older devices, a fallback descriptor will be used.
  809. + */
  810. + ret = ipts_control_get_descriptor(ipts);
  811. + if (ret) {
  812. + dev_err(ipts->dev, "Failed to fetch HID descriptor: %d\n", ret);
  813. + return ret;
  814. + }
  815. +
  816. + /*
  817. + * Newer devices can be directly initialized in doorbell mode.
  818. + */
  819. + ipts->mode = IPTS_MODE_DOORBELL;
  820. + }
  821. +
  822. + ret = ipts_control_set_mode(ipts, ipts->mode);
  823. + if (ret) {
  824. + dev_err(ipts->dev, "Failed to set mode: %d\n", ret);
  825. + return ret;
  826. + }
  827. +
  828. + ret = ipts_control_set_mem_window(ipts, &ipts->resources);
  829. + if (ret) {
  830. + dev_err(ipts->dev, "Failed to set memory window: %d\n", ret);
  831. + return ret;
  832. + }
  833. +
  834. + ret = ipts_receiver_start(ipts);
  835. + if (ret) {
  836. + dev_err(ipts->dev, "Failed to start receiver: %d\n", ret);
  837. + return ret;
  838. + }
  839. +
  840. + ret = ipts_control_request_data(ipts);
  841. + if (ret) {
  842. + dev_err(ipts->dev, "Failed to request data: %d\n", ret);
  843. + return ret;
  844. + }
  845. +
  846. + ret = ipts_hid_init(ipts, info);
  847. + if (ret) {
  848. + dev_err(ipts->dev, "Failed to initialize HID device: %d\n", ret);
  849. + return ret;
  850. + }
  851. +
  852. + return 0;
  853. +}
  854. +
  855. +static int _ipts_control_stop(struct ipts_context *ipts)
  856. +{
  857. + int ret = 0;
  858. +
  859. + if (!ipts)
  860. + return -EFAULT;
  861. +
  862. + dev_info(ipts->dev, "Stopping IPTS\n");
  863. +
  864. + ret = ipts_receiver_stop(ipts);
  865. + if (ret) {
  866. + dev_err(ipts->dev, "Failed to stop receiver: %d\n", ret);
  867. + return ret;
  868. + }
  869. +
  870. + ret = ipts_control_reset_sensor(ipts);
  871. + if (ret) {
  872. + dev_err(ipts->dev, "Failed to reset sensor: %d\n", ret);
  873. + return ret;
  874. + }
  875. +
  876. + ret = ipts_resources_free(&ipts->resources);
  877. + if (ret) {
  878. + dev_err(ipts->dev, "Failed to free resources: %d\n", ret);
  879. + return ret;
  880. + }
  881. +
  882. + return 0;
  883. +}
  884. +
  885. +int ipts_control_stop(struct ipts_context *ipts)
  886. +{
  887. + int ret = 0;
  888. +
  889. + ret = _ipts_control_stop(ipts);
  890. + if (ret)
  891. + return ret;
  892. +
  893. + ret = ipts_hid_free(ipts);
  894. + if (ret) {
  895. + dev_err(ipts->dev, "Failed to free HID device: %d\n", ret);
  896. + return ret;
  897. + }
  898. +
  899. + return 0;
  900. +}
  901. +
  902. +int ipts_control_restart(struct ipts_context *ipts)
  903. +{
  904. + int ret = 0;
  905. +
  906. + ret = _ipts_control_stop(ipts);
  907. + if (ret)
  908. + return ret;
  909. +
  910. + /*
  911. + * Give the sensor some time to come back from resetting
  912. + */
  913. + msleep(1000);
  914. +
  915. + ret = ipts_control_start(ipts);
  916. + if (ret)
  917. + return ret;
  918. +
  919. + return 0;
  920. +}
  921. diff --git a/drivers/hid/ipts/control.h b/drivers/hid/ipts/control.h
  922. new file mode 100644
  923. index 000000000000..744bb92d682a
  924. --- /dev/null
  925. +++ b/drivers/hid/ipts/control.h
  926. @@ -0,0 +1,127 @@
  927. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  928. +/*
  929. + * Copyright (c) 2016 Intel Corporation
  930. + * Copyright (c) 2020-2023 Dorian Stoll
  931. + *
  932. + * Linux driver for Intel Precise Touch & Stylus
  933. + */
  934. +
  935. +#ifndef IPTS_CONTROL_H
  936. +#define IPTS_CONTROL_H
  937. +
  938. +#include <linux/types.h>
  939. +
  940. +#include "context.h"
  941. +#include "spec-data.h"
  942. +#include "spec-device.h"
  943. +
  944. +/*
  945. + * ipts_control_request_flush() - Stop the data flow.
  946. + * @ipts: The IPTS driver context.
  947. + *
  948. + * Runs the command to stop the data flow on the device.
  949. + * All outstanding data needs to be acknowledged using feedback before the command will return.
  950. + *
  951. + * Returns: 0 on success, <0 on error.
  952. + */
  953. +int ipts_control_request_flush(struct ipts_context *ipts);
  954. +
  955. +/*
  956. + * ipts_control_wait_flush() - Wait until data flow has been stopped.
  957. + * @ipts: The IPTS driver context.
  958. + *
  959. + * Returns: 0 on success, <0 on error.
  960. + */
  961. +int ipts_control_wait_flush(struct ipts_context *ipts);
  962. +
  963. +/*
  964. + * ipts_control_wait_flush() - Notify the device that the driver can receive new data.
  965. + * @ipts: The IPTS driver context.
  966. + *
  967. + * Returns: 0 on success, <0 on error.
  968. + */
  969. +int ipts_control_request_data(struct ipts_context *ipts);
  970. +
  971. +/*
  972. + * ipts_control_wait_data() - Wait until new data is available.
  973. + * @ipts: The IPTS driver context.
  974. + * @block: Whether to block execution until data is available.
  975. + *
  976. + * In doorbell mode, this function will never return while the data flow is active. Instead,
  977. + * the doorbell will be incremented when new data is available.
  978. + *
  979. + * Returns: 0 on success, <0 on error, -EAGAIN if no data is available.
  980. + */
  981. +int ipts_control_wait_data(struct ipts_context *ipts, bool block);
  982. +
  983. +/*
  984. + * ipts_control_send_feedback() - Submits a feedback buffer to the device.
  985. + * @ipts: The IPTS driver context.
  986. + * @buffer: The ID of the buffer containing feedback data.
  987. + *
  988. + * Returns: 0 on success, <0 on error.
  989. + */
  990. +int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer);
  991. +
  992. +/*
  993. + * ipts_control_hid2me_feedback() - Sends HID2ME feedback, a special type of feedback.
  994. + * @ipts: The IPTS driver context.
  995. + * @cmd: The command that will be run on the device.
  996. + * @type: The type of the payload that is sent to the device.
  997. + * @data: The payload of the feedback command.
  998. + * @size: The size of the payload.
  999. + *
  1000. + * HID2ME feedback is a special type of feedback, because it allows interfacing with
  1001. + * the HID API of the device at any moment, without requiring a buffer that has to
  1002. + * be acknowledged.
  1003. + *
  1004. + * Returns: 0 on success, <0 on error.
  1005. + */
  1006. +int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_cmd_type cmd,
  1007. + enum ipts_feedback_data_type type, void *data, size_t size);
  1008. +
  1009. +/*
  1010. + * ipts_control_refill_buffer() - Acknowledges that data in a buffer has been processed.
  1011. + * @ipts: The IPTS driver context.
  1012. + * @buffer: The buffer that has been processed and can be refilled.
  1013. + *
  1014. + * Returns: 0 on success, <0 on error.
  1015. + */
  1016. +static inline int ipts_control_refill_buffer(struct ipts_context *ipts, u32 buffer)
  1017. +{
  1018. + /*
  1019. + * IPTS expects structured data in the feedback buffer matching the buffer that will be
  1020. + * refilled. We don't know what that data looks like, so we just keep the buffer empty.
  1021. + * This results in an INVALID_PARAMS error, but the buffer gets refilled without an issue.
  1022. + * Sending a minimal structure with the buffer ID fixes the error, but breaks refilling
  1023. + * the buffers on some devices.
  1024. + */
  1025. +
  1026. + return ipts_control_send_feedback(ipts, buffer);
  1027. +}
  1028. +
  1029. +/*
  1030. + * ipts_control_start() - Initialized the device and starts the data flow.
  1031. + * @ipts: The IPTS driver context.
  1032. + *
  1033. + * Returns: 0 on success, <0 on error.
  1034. + */
  1035. +int ipts_control_start(struct ipts_context *ipts);
  1036. +
  1037. +/*
  1038. + * ipts_control_stop() - Stops the data flow and resets the device.
  1039. + * @ipts: The IPTS driver context.
  1040. + *
  1041. + * Returns: 0 on success, <0 on error.
  1042. + */
  1043. +int ipts_control_stop(struct ipts_context *ipts);
  1044. +
  1045. +/*
  1046. + * ipts_control_restart() - Stops the device and starts it again.
  1047. + * @ipts: The IPTS driver context.
  1048. + *
  1049. + * Returns: 0 on success, <0 on error.
  1050. + */
  1051. +int ipts_control_restart(struct ipts_context *ipts);
  1052. +
  1053. +#endif /* IPTS_CONTROL_H */
  1054. diff --git a/drivers/hid/ipts/desc.h b/drivers/hid/ipts/desc.h
  1055. new file mode 100644
  1056. index 000000000000..c058974a03a1
  1057. --- /dev/null
  1058. +++ b/drivers/hid/ipts/desc.h
  1059. @@ -0,0 +1,81 @@
  1060. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  1061. +/*
  1062. + * Copyright (c) 2016 Intel Corporation
  1063. + * Copyright (c) 2022-2023 Dorian Stoll
  1064. + *
  1065. + * Linux driver for Intel Precise Touch & Stylus
  1066. + */
  1067. +
  1068. +#ifndef IPTS_DESC_H
  1069. +#define IPTS_DESC_H
  1070. +
  1071. +#include <linux/types.h>
  1072. +
  1073. +#define IPTS_HID_REPORT_SINGLETOUCH 64
  1074. +#define IPTS_HID_REPORT_DATA 65
  1075. +#define IPTS_HID_REPORT_SET_MODE 66
  1076. +
  1077. +#define IPTS_HID_REPORT_DATA_SIZE 7485
  1078. +
  1079. +/*
  1080. + * HID descriptor for singletouch data.
  1081. + * This descriptor should be present on all IPTS devices.
  1082. + */
  1083. +static const u8 ipts_singletouch_descriptor[] = {
  1084. + 0x05, 0x0D, /* Usage Page (Digitizer), */
  1085. + 0x09, 0x04, /* Usage (Touchscreen), */
  1086. + 0xA1, 0x01, /* Collection (Application), */
  1087. + 0x85, 0x40, /* Report ID (64), */
  1088. + 0x09, 0x42, /* Usage (Tip Switch), */
  1089. + 0x15, 0x00, /* Logical Minimum (0), */
  1090. + 0x25, 0x01, /* Logical Maximum (1), */
  1091. + 0x75, 0x01, /* Report Size (1), */
  1092. + 0x95, 0x01, /* Report Count (1), */
  1093. + 0x81, 0x02, /* Input (Variable), */
  1094. + 0x95, 0x07, /* Report Count (7), */
  1095. + 0x81, 0x03, /* Input (Constant, Variable), */
  1096. + 0x05, 0x01, /* Usage Page (Desktop), */
  1097. + 0x09, 0x30, /* Usage (X), */
  1098. + 0x75, 0x10, /* Report Size (16), */
  1099. + 0x95, 0x01, /* Report Count (1), */
  1100. + 0xA4, /* Push, */
  1101. + 0x55, 0x0E, /* Unit Exponent (14), */
  1102. + 0x65, 0x11, /* Unit (Centimeter), */
  1103. + 0x46, 0x76, 0x0B, /* Physical Maximum (2934), */
  1104. + 0x26, 0xFF, 0x7F, /* Logical Maximum (32767), */
  1105. + 0x81, 0x02, /* Input (Variable), */
  1106. + 0x09, 0x31, /* Usage (Y), */
  1107. + 0x46, 0x74, 0x06, /* Physical Maximum (1652), */
  1108. + 0x26, 0xFF, 0x7F, /* Logical Maximum (32767), */
  1109. + 0x81, 0x02, /* Input (Variable), */
  1110. + 0xB4, /* Pop, */
  1111. + 0xC0, /* End Collection */
  1112. +};
  1113. +
  1114. +/*
  1115. + * Fallback HID descriptor for older devices that do not have
  1116. + * the ability to query their HID descriptor.
  1117. + */
  1118. +static const u8 ipts_fallback_descriptor[] = {
  1119. + 0x05, 0x0D, /* Usage Page (Digitizer), */
  1120. + 0x09, 0x0F, /* Usage (Capacitive Hm Digitizer), */
  1121. + 0xA1, 0x01, /* Collection (Application), */
  1122. + 0x85, 0x41, /* Report ID (65), */
  1123. + 0x09, 0x56, /* Usage (Scan Time), */
  1124. + 0x95, 0x01, /* Report Count (1), */
  1125. + 0x75, 0x10, /* Report Size (16), */
  1126. + 0x81, 0x02, /* Input (Variable), */
  1127. + 0x09, 0x61, /* Usage (Gesture Char Quality), */
  1128. + 0x75, 0x08, /* Report Size (8), */
  1129. + 0x96, 0x3D, 0x1D, /* Report Count (7485), */
  1130. + 0x81, 0x03, /* Input (Constant, Variable), */
  1131. + 0x85, 0x42, /* Report ID (66), */
  1132. + 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
  1133. + 0x09, 0xC8, /* Usage (C8h), */
  1134. + 0x75, 0x08, /* Report Size (8), */
  1135. + 0x95, 0x01, /* Report Count (1), */
  1136. + 0xB1, 0x02, /* Feature (Variable), */
  1137. + 0xC0, /* End Collection, */
  1138. +};
  1139. +
  1140. +#endif /* IPTS_DESC_H */
  1141. diff --git a/drivers/hid/ipts/hid.c b/drivers/hid/ipts/hid.c
  1142. new file mode 100644
  1143. index 000000000000..6782394e8dde
  1144. --- /dev/null
  1145. +++ b/drivers/hid/ipts/hid.c
  1146. @@ -0,0 +1,348 @@
  1147. +// SPDX-License-Identifier: GPL-2.0-or-later
  1148. +/*
  1149. + * Copyright (c) 2016 Intel Corporation
  1150. + * Copyright (c) 2022-2023 Dorian Stoll
  1151. + *
  1152. + * Linux driver for Intel Precise Touch & Stylus
  1153. + */
  1154. +
  1155. +#include <linux/completion.h>
  1156. +#include <linux/gfp.h>
  1157. +#include <linux/hid.h>
  1158. +#include <linux/mutex.h>
  1159. +#include <linux/slab.h>
  1160. +#include <linux/types.h>
  1161. +
  1162. +#include "context.h"
  1163. +#include "control.h"
  1164. +#include "desc.h"
  1165. +#include "hid.h"
  1166. +#include "spec-data.h"
  1167. +#include "spec-device.h"
  1168. +#include "spec-hid.h"
  1169. +
  1170. +static int ipts_hid_start(struct hid_device *hid)
  1171. +{
  1172. + return 0;
  1173. +}
  1174. +
  1175. +static void ipts_hid_stop(struct hid_device *hid)
  1176. +{
  1177. +}
  1178. +
  1179. +static int ipts_hid_switch_mode(struct ipts_context *ipts, enum ipts_mode mode)
  1180. +{
  1181. + if (!ipts)
  1182. + return -EFAULT;
  1183. +
  1184. + if (ipts->mode == mode)
  1185. + return 0;
  1186. +
  1187. + /*
  1188. + * This is only allowed on older devices.
  1189. + */
  1190. + if (ipts->info.intf_eds > 1)
  1191. + return 0;
  1192. +
  1193. + ipts->mode = mode;
  1194. + return ipts_control_restart(ipts);
  1195. +}
  1196. +
  1197. +static int ipts_hid_parse(struct hid_device *hid)
  1198. +{
  1199. + int ret = 0;
  1200. + struct ipts_context *ipts = NULL;
  1201. +
  1202. + bool has_native_descriptor = false;
  1203. +
  1204. + u8 *buffer = NULL;
  1205. + size_t size = 0;
  1206. +
  1207. + if (!hid)
  1208. + return -ENODEV;
  1209. +
  1210. + ipts = hid->driver_data;
  1211. +
  1212. + if (!ipts)
  1213. + return -EFAULT;
  1214. +
  1215. + size = sizeof(ipts_singletouch_descriptor);
  1216. + has_native_descriptor = ipts->descriptor.address && ipts->descriptor.size > 0;
  1217. +
  1218. + if (has_native_descriptor)
  1219. + size += ipts->descriptor.size;
  1220. + else
  1221. + size += sizeof(ipts_fallback_descriptor);
  1222. +
  1223. + buffer = kzalloc(size, GFP_KERNEL);
  1224. + if (!buffer)
  1225. + return -ENOMEM;
  1226. +
  1227. + memcpy(buffer, ipts_singletouch_descriptor, sizeof(ipts_singletouch_descriptor));
  1228. +
  1229. + if (has_native_descriptor) {
  1230. + memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts->descriptor.address,
  1231. + ipts->descriptor.size);
  1232. + } else {
  1233. + memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts_fallback_descriptor,
  1234. + sizeof(ipts_fallback_descriptor));
  1235. + }
  1236. +
  1237. + ret = hid_parse_report(hid, buffer, size);
  1238. + kfree(buffer);
  1239. +
  1240. + if (ret) {
  1241. + dev_err(ipts->dev, "Failed to parse HID descriptor: %d\n", ret);
  1242. + return ret;
  1243. + }
  1244. +
  1245. + return 0;
  1246. +}
  1247. +
  1248. +static int ipts_hid_get_feature(struct ipts_context *ipts, unsigned char reportnum, __u8 *buf,
  1249. + size_t size, enum ipts_feedback_data_type type)
  1250. +{
  1251. + int ret = 0;
  1252. +
  1253. + if (!ipts)
  1254. + return -EFAULT;
  1255. +
  1256. + if (!buf)
  1257. + return -EFAULT;
  1258. +
  1259. + mutex_lock(&ipts->feature_lock);
  1260. +
  1261. + memset(buf, 0, size);
  1262. + buf[0] = reportnum;
  1263. +
  1264. + memset(&ipts->feature_report, 0, sizeof(ipts->feature_report));
  1265. + reinit_completion(&ipts->feature_event);
  1266. +
  1267. + ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buf, size);
  1268. + if (ret) {
  1269. + dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  1270. + goto out;
  1271. + }
  1272. +
  1273. + ret = wait_for_completion_timeout(&ipts->feature_event, msecs_to_jiffies(5000));
  1274. + if (ret == 0) {
  1275. + dev_warn(ipts->dev, "GET_FEATURES timed out!\n");
  1276. + ret = -EIO;
  1277. + goto out;
  1278. + }
  1279. +
  1280. + if (!ipts->feature_report.address) {
  1281. + ret = -EFAULT;
  1282. + goto out;
  1283. + }
  1284. +
  1285. + if (ipts->feature_report.size > size) {
  1286. + ret = -ETOOSMALL;
  1287. + goto out;
  1288. + }
  1289. +
  1290. + ret = ipts->feature_report.size;
  1291. + memcpy(buf, ipts->feature_report.address, ipts->feature_report.size);
  1292. +
  1293. +out:
  1294. + mutex_unlock(&ipts->feature_lock);
  1295. + return ret;
  1296. +}
  1297. +
  1298. +static int ipts_hid_set_feature(struct ipts_context *ipts, unsigned char reportnum, __u8 *buf,
  1299. + size_t size, enum ipts_feedback_data_type type)
  1300. +{
  1301. + int ret = 0;
  1302. +
  1303. + if (!ipts)
  1304. + return -EFAULT;
  1305. +
  1306. + if (!buf)
  1307. + return -EFAULT;
  1308. +
  1309. + buf[0] = reportnum;
  1310. +
  1311. + ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buf, size);
  1312. + if (ret)
  1313. + dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  1314. +
  1315. + return ret;
  1316. +}
  1317. +
  1318. +static int ipts_hid_raw_request(struct hid_device *hid, unsigned char reportnum, __u8 *buf,
  1319. + size_t size, unsigned char rtype, int reqtype)
  1320. +{
  1321. + int ret = 0;
  1322. + struct ipts_context *ipts = NULL;
  1323. +
  1324. + enum ipts_feedback_data_type type = IPTS_FEEDBACK_DATA_TYPE_VENDOR;
  1325. +
  1326. + if (!hid)
  1327. + return -ENODEV;
  1328. +
  1329. + ipts = hid->driver_data;
  1330. +
  1331. + if (!ipts)
  1332. + return -EFAULT;
  1333. +
  1334. + if (!buf)
  1335. + return -EFAULT;
  1336. +
  1337. + if (rtype == HID_OUTPUT_REPORT && reqtype == HID_REQ_SET_REPORT)
  1338. + type = IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT;
  1339. + else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_GET_REPORT)
  1340. + type = IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES;
  1341. + else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_SET_REPORT)
  1342. + type = IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES;
  1343. + else
  1344. + return -EIO;
  1345. +
  1346. + // Implemente mode switching report for older devices without native HID support
  1347. + if (type == IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES && reportnum == IPTS_HID_REPORT_SET_MODE) {
  1348. + ret = ipts_hid_switch_mode(ipts, buf[1]);
  1349. + if (ret) {
  1350. + dev_err(ipts->dev, "Failed to switch modes: %d\n", ret);
  1351. + return ret;
  1352. + }
  1353. + }
  1354. +
  1355. + if (reqtype == HID_REQ_GET_REPORT)
  1356. + return ipts_hid_get_feature(ipts, reportnum, buf, size, type);
  1357. + else
  1358. + return ipts_hid_set_feature(ipts, reportnum, buf, size, type);
  1359. +}
  1360. +
  1361. +static int ipts_hid_output_report(struct hid_device *hid, __u8 *data, size_t size)
  1362. +{
  1363. + struct ipts_context *ipts = NULL;
  1364. +
  1365. + if (!hid)
  1366. + return -ENODEV;
  1367. +
  1368. + ipts = hid->driver_data;
  1369. +
  1370. + return ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE,
  1371. + IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT, data, size);
  1372. +}
  1373. +
  1374. +static struct hid_ll_driver ipts_hid_driver = {
  1375. + .start = ipts_hid_start,
  1376. + .stop = ipts_hid_stop,
  1377. + .open = ipts_hid_start,
  1378. + .close = ipts_hid_stop,
  1379. + .parse = ipts_hid_parse,
  1380. + .raw_request = ipts_hid_raw_request,
  1381. + .output_report = ipts_hid_output_report,
  1382. +};
  1383. +
  1384. +int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  1385. +{
  1386. + int ret = 0;
  1387. + u8 *temp = NULL;
  1388. + struct ipts_hid_header *frame = NULL;
  1389. + struct ipts_data_header *header = NULL;
  1390. +
  1391. + if (!ipts)
  1392. + return -EFAULT;
  1393. +
  1394. + if (!ipts->hid)
  1395. + return -ENODEV;
  1396. +
  1397. + header = (struct ipts_data_header *)ipts->resources.data[buffer].address;
  1398. +
  1399. + if (!header)
  1400. + return -EFAULT;
  1401. +
  1402. + if (header->size == 0)
  1403. + return 0;
  1404. +
  1405. + if (header->type == IPTS_DATA_TYPE_HID)
  1406. + return hid_input_report(ipts->hid, HID_INPUT_REPORT, header->data, header->size, 1);
  1407. +
  1408. + if (header->type == IPTS_DATA_TYPE_GET_FEATURES) {
  1409. + ipts->feature_report.address = header->data;
  1410. + ipts->feature_report.size = header->size;
  1411. +
  1412. + complete_all(&ipts->feature_event);
  1413. + return 0;
  1414. + }
  1415. +
  1416. + if (header->type != IPTS_DATA_TYPE_FRAME)
  1417. + return 0;
  1418. +
  1419. + if (header->size + 3 + sizeof(struct ipts_hid_header) > IPTS_HID_REPORT_DATA_SIZE)
  1420. + return -ERANGE;
  1421. +
  1422. + temp = kzalloc(IPTS_HID_REPORT_DATA_SIZE, GFP_KERNEL);
  1423. + if (!temp)
  1424. + return -ENOMEM;
  1425. +
  1426. + /*
  1427. + * Synthesize a HID report matching the devices that natively send HID reports
  1428. + */
  1429. + temp[0] = IPTS_HID_REPORT_DATA;
  1430. +
  1431. + frame = (struct ipts_hid_header *)&temp[3];
  1432. + frame->type = IPTS_HID_FRAME_TYPE_RAW;
  1433. + frame->size = header->size + sizeof(*frame);
  1434. +
  1435. + memcpy(frame->data, header->data, header->size);
  1436. +
  1437. + ret = hid_input_report(ipts->hid, HID_INPUT_REPORT, temp, IPTS_HID_REPORT_DATA_SIZE, 1);
  1438. + kfree(temp);
  1439. +
  1440. + return ret;
  1441. +}
  1442. +
  1443. +int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info)
  1444. +{
  1445. + int ret = 0;
  1446. +
  1447. + if (!ipts)
  1448. + return -EFAULT;
  1449. +
  1450. + if (ipts->hid)
  1451. + return 0;
  1452. +
  1453. + ipts->hid = hid_allocate_device();
  1454. + if (IS_ERR(ipts->hid)) {
  1455. + int err = PTR_ERR(ipts->hid);
  1456. +
  1457. + dev_err(ipts->dev, "Failed to allocate HID device: %d\n", err);
  1458. + return err;
  1459. + }
  1460. +
  1461. + ipts->hid->driver_data = ipts;
  1462. + ipts->hid->dev.parent = ipts->dev;
  1463. + ipts->hid->ll_driver = &ipts_hid_driver;
  1464. +
  1465. + ipts->hid->vendor = info.vendor;
  1466. + ipts->hid->product = info.product;
  1467. + ipts->hid->group = HID_GROUP_MULTITOUCH;
  1468. +
  1469. + snprintf(ipts->hid->name, sizeof(ipts->hid->name), "IPTS %04X:%04X", info.vendor,
  1470. + info.product);
  1471. +
  1472. + ret = hid_add_device(ipts->hid);
  1473. + if (ret) {
  1474. + dev_err(ipts->dev, "Failed to add HID device: %d\n", ret);
  1475. + ipts_hid_free(ipts);
  1476. + return ret;
  1477. + }
  1478. +
  1479. + return 0;
  1480. +}
  1481. +
  1482. +int ipts_hid_free(struct ipts_context *ipts)
  1483. +{
  1484. + if (!ipts)
  1485. + return -EFAULT;
  1486. +
  1487. + if (!ipts->hid)
  1488. + return 0;
  1489. +
  1490. + hid_destroy_device(ipts->hid);
  1491. + ipts->hid = NULL;
  1492. +
  1493. + return 0;
  1494. +}
  1495. diff --git a/drivers/hid/ipts/hid.h b/drivers/hid/ipts/hid.h
  1496. new file mode 100644
  1497. index 000000000000..62bf3cd48608
  1498. --- /dev/null
  1499. +++ b/drivers/hid/ipts/hid.h
  1500. @@ -0,0 +1,22 @@
  1501. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  1502. +/*
  1503. + * Copyright (c) 2016 Intel Corporation
  1504. + * Copyright (c) 2022-2023 Dorian Stoll
  1505. + *
  1506. + * Linux driver for Intel Precise Touch & Stylus
  1507. + */
  1508. +
  1509. +#ifndef IPTS_HID_H
  1510. +#define IPTS_HID_H
  1511. +
  1512. +#include <linux/types.h>
  1513. +
  1514. +#include "context.h"
  1515. +#include "spec-device.h"
  1516. +
  1517. +int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer);
  1518. +
  1519. +int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info);
  1520. +int ipts_hid_free(struct ipts_context *ipts);
  1521. +
  1522. +#endif /* IPTS_HID_H */
  1523. diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c
  1524. new file mode 100644
  1525. index 000000000000..0f20c6c08c38
  1526. --- /dev/null
  1527. +++ b/drivers/hid/ipts/main.c
  1528. @@ -0,0 +1,127 @@
  1529. +// SPDX-License-Identifier: GPL-2.0-or-later
  1530. +/*
  1531. + * Copyright (c) 2016 Intel Corporation
  1532. + * Copyright (c) 2020-2023 Dorian Stoll
  1533. + *
  1534. + * Linux driver for Intel Precise Touch & Stylus
  1535. + */
  1536. +
  1537. +#include <linux/completion.h>
  1538. +#include <linux/delay.h>
  1539. +#include <linux/device.h>
  1540. +#include <linux/dma-mapping.h>
  1541. +#include <linux/mei_cl_bus.h>
  1542. +#include <linux/mod_devicetable.h>
  1543. +#include <linux/module.h>
  1544. +#include <linux/mutex.h>
  1545. +#include <linux/slab.h>
  1546. +#include <linux/stddef.h>
  1547. +#include <linux/types.h>
  1548. +
  1549. +#include "context.h"
  1550. +#include "control.h"
  1551. +#include "mei.h"
  1552. +#include "receiver.h"
  1553. +#include "spec-device.h"
  1554. +
  1555. +/*
  1556. + * The MEI client ID for IPTS functionality.
  1557. + */
  1558. +#define IPTS_ID UUID_LE(0x3e8d0870, 0x271a, 0x4208, 0x8e, 0xb5, 0x9a, 0xcb, 0x94, 0x02, 0xae, 0x04)
  1559. +
  1560. +static int ipts_set_dma_mask(struct mei_cl_device *cldev)
  1561. +{
  1562. + if (!cldev)
  1563. + return -EFAULT;
  1564. +
  1565. + if (!dma_coerce_mask_and_coherent(&cldev->dev, DMA_BIT_MASK(64)))
  1566. + return 0;
  1567. +
  1568. + return dma_coerce_mask_and_coherent(&cldev->dev, DMA_BIT_MASK(32));
  1569. +}
  1570. +
  1571. +static int ipts_probe(struct mei_cl_device *cldev, const struct mei_cl_device_id *id)
  1572. +{
  1573. + int ret = 0;
  1574. + struct ipts_context *ipts = NULL;
  1575. +
  1576. + if (!cldev)
  1577. + return -EFAULT;
  1578. +
  1579. + ret = ipts_set_dma_mask(cldev);
  1580. + if (ret) {
  1581. + dev_err(&cldev->dev, "Failed to set DMA mask for IPTS: %d\n", ret);
  1582. + return ret;
  1583. + }
  1584. +
  1585. + ret = mei_cldev_enable(cldev);
  1586. + if (ret) {
  1587. + dev_err(&cldev->dev, "Failed to enable MEI device: %d\n", ret);
  1588. + return ret;
  1589. + }
  1590. +
  1591. + ipts = devm_kzalloc(&cldev->dev, sizeof(*ipts), GFP_KERNEL);
  1592. + if (!ipts) {
  1593. + mei_cldev_disable(cldev);
  1594. + return -ENOMEM;
  1595. + }
  1596. +
  1597. + ret = ipts_mei_init(&ipts->mei, cldev);
  1598. + if (ret) {
  1599. + dev_err(&cldev->dev, "Failed to init MEI bus logic: %d\n", ret);
  1600. + return ret;
  1601. + }
  1602. +
  1603. + ipts->dev = &cldev->dev;
  1604. + ipts->mode = IPTS_MODE_EVENT;
  1605. +
  1606. + mutex_init(&ipts->feature_lock);
  1607. + init_completion(&ipts->feature_event);
  1608. +
  1609. + mei_cldev_set_drvdata(cldev, ipts);
  1610. +
  1611. + ret = ipts_control_start(ipts);
  1612. + if (ret) {
  1613. + dev_err(&cldev->dev, "Failed to start IPTS: %d\n", ret);
  1614. + return ret;
  1615. + }
  1616. +
  1617. + return 0;
  1618. +}
  1619. +
  1620. +static void ipts_remove(struct mei_cl_device *cldev)
  1621. +{
  1622. + int ret = 0;
  1623. + struct ipts_context *ipts = NULL;
  1624. +
  1625. + if (!cldev) {
  1626. + pr_err("MEI device is NULL!");
  1627. + return;
  1628. + }
  1629. +
  1630. + ipts = mei_cldev_get_drvdata(cldev);
  1631. +
  1632. + ret = ipts_control_stop(ipts);
  1633. + if (ret)
  1634. + dev_err(&cldev->dev, "Failed to stop IPTS: %d\n", ret);
  1635. +
  1636. + mei_cldev_disable(cldev);
  1637. +}
  1638. +
  1639. +static struct mei_cl_device_id ipts_device_id_table[] = {
  1640. + { .uuid = IPTS_ID, .version = MEI_CL_VERSION_ANY },
  1641. + {},
  1642. +};
  1643. +MODULE_DEVICE_TABLE(mei, ipts_device_id_table);
  1644. +
  1645. +static struct mei_cl_driver ipts_driver = {
  1646. + .id_table = ipts_device_id_table,
  1647. + .name = "ipts",
  1648. + .probe = ipts_probe,
  1649. + .remove = ipts_remove,
  1650. +};
  1651. +module_mei_cl_driver(ipts_driver);
  1652. +
  1653. +MODULE_DESCRIPTION("IPTS touchscreen driver");
  1654. +MODULE_AUTHOR("Dorian Stoll <dorian.stoll@tmsp.io>");
  1655. +MODULE_LICENSE("GPL");
  1656. diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c
  1657. new file mode 100644
  1658. index 000000000000..26666fd99b0c
  1659. --- /dev/null
  1660. +++ b/drivers/hid/ipts/mei.c
  1661. @@ -0,0 +1,189 @@
  1662. +// SPDX-License-Identifier: GPL-2.0-or-later
  1663. +/*
  1664. + * Copyright (c) 2016 Intel Corporation
  1665. + * Copyright (c) 2023 Dorian Stoll
  1666. + *
  1667. + * Linux driver for Intel Precise Touch & Stylus
  1668. + */
  1669. +
  1670. +#include <linux/device.h>
  1671. +#include <linux/errno.h>
  1672. +#include <linux/jiffies.h>
  1673. +#include <linux/list.h>
  1674. +#include <linux/mei_cl_bus.h>
  1675. +#include <linux/printk.h>
  1676. +#include <linux/rwsem.h>
  1677. +#include <linux/types.h>
  1678. +#include <linux/wait.h>
  1679. +
  1680. +#include "context.h"
  1681. +#include "mei.h"
  1682. +
  1683. +static void locked_list_add(struct list_head *new, struct list_head *head,
  1684. + struct rw_semaphore *lock)
  1685. +{
  1686. + down_write(lock);
  1687. + list_add(new, head);
  1688. + up_write(lock);
  1689. +}
  1690. +
  1691. +static void locked_list_del(struct list_head *entry, struct rw_semaphore *lock)
  1692. +{
  1693. + down_write(lock);
  1694. + list_del(entry);
  1695. + up_write(lock);
  1696. +}
  1697. +
  1698. +static void ipts_mei_incoming(struct mei_cl_device *cldev)
  1699. +{
  1700. + ssize_t ret = 0;
  1701. + struct ipts_mei_message *entry = NULL;
  1702. + struct ipts_context *ipts = NULL;
  1703. +
  1704. + if (!cldev) {
  1705. + pr_err("MEI device is NULL!");
  1706. + return;
  1707. + }
  1708. +
  1709. + ipts = mei_cldev_get_drvdata(cldev);
  1710. + if (!ipts) {
  1711. + pr_err("IPTS driver context is NULL!");
  1712. + return;
  1713. + }
  1714. +
  1715. + entry = devm_kzalloc(ipts->dev, sizeof(*entry), GFP_KERNEL);
  1716. + if (!entry)
  1717. + return;
  1718. +
  1719. + INIT_LIST_HEAD(&entry->list);
  1720. +
  1721. + do {
  1722. + ret = mei_cldev_recv(cldev, (u8 *)&entry->rsp, sizeof(entry->rsp));
  1723. + } while (ret == -EINTR);
  1724. +
  1725. + if (ret < 0) {
  1726. + dev_err(ipts->dev, "Error while reading response: %ld\n", ret);
  1727. + return;
  1728. + }
  1729. +
  1730. + if (ret == 0) {
  1731. + dev_err(ipts->dev, "Received empty response\n");
  1732. + return;
  1733. + }
  1734. +
  1735. + locked_list_add(&entry->list, &ipts->mei.messages, &ipts->mei.message_lock);
  1736. + wake_up_all(&ipts->mei.message_queue);
  1737. +}
  1738. +
  1739. +static int ipts_mei_search(struct ipts_mei *mei, enum ipts_command_code code,
  1740. + struct ipts_response *rsp)
  1741. +{
  1742. + struct ipts_mei_message *entry = NULL;
  1743. +
  1744. + if (!mei)
  1745. + return -EFAULT;
  1746. +
  1747. + if (!rsp)
  1748. + return -EFAULT;
  1749. +
  1750. + down_read(&mei->message_lock);
  1751. +
  1752. + /*
  1753. + * Iterate over the list of received messages, and check if there is one
  1754. + * matching the requested command code.
  1755. + */
  1756. + list_for_each_entry(entry, &mei->messages, list) {
  1757. + if (entry->rsp.cmd == code)
  1758. + break;
  1759. + }
  1760. +
  1761. + up_read(&mei->message_lock);
  1762. +
  1763. + /*
  1764. + * If entry is not the list head, this means that the loop above has been stopped early,
  1765. + * and that we found a matching element. We drop the message from the list and return it.
  1766. + */
  1767. + if (!list_entry_is_head(entry, &mei->messages, list)) {
  1768. + locked_list_del(&entry->list, &mei->message_lock);
  1769. +
  1770. + *rsp = entry->rsp;
  1771. + devm_kfree(&mei->cldev->dev, entry);
  1772. +
  1773. + return 0;
  1774. + }
  1775. +
  1776. + return -EAGAIN;
  1777. +}
  1778. +
  1779. +int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts_response *rsp,
  1780. + u64 timeout)
  1781. +{
  1782. + int ret = 0;
  1783. +
  1784. + if (!mei)
  1785. + return -EFAULT;
  1786. +
  1787. + /*
  1788. + * A timeout of 0 means check and return immideately.
  1789. + */
  1790. + if (timeout == 0)
  1791. + return ipts_mei_search(mei, code, rsp);
  1792. +
  1793. + /*
  1794. + * A timeout of less than 0 means to wait forever.
  1795. + */
  1796. + if (timeout < 0) {
  1797. + wait_event(mei->message_queue, ipts_mei_search(mei, code, rsp) == 0);
  1798. + return 0;
  1799. + }
  1800. +
  1801. + ret = wait_event_timeout(mei->message_queue, ipts_mei_search(mei, code, rsp) == 0,
  1802. + msecs_to_jiffies(timeout));
  1803. +
  1804. + if (ret > 0)
  1805. + return 0;
  1806. +
  1807. + return -EAGAIN;
  1808. +}
  1809. +
  1810. +int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length)
  1811. +{
  1812. + int ret = 0;
  1813. +
  1814. + if (!mei)
  1815. + return -EFAULT;
  1816. +
  1817. + if (!mei->cldev)
  1818. + return -EFAULT;
  1819. +
  1820. + if (!data)
  1821. + return -EFAULT;
  1822. +
  1823. + do {
  1824. + ret = mei_cldev_send(mei->cldev, (u8 *)data, length);
  1825. + } while (ret == -EINTR);
  1826. +
  1827. + if (ret < 0)
  1828. + return ret;
  1829. +
  1830. + return 0;
  1831. +}
  1832. +
  1833. +int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev)
  1834. +{
  1835. + if (!mei)
  1836. + return -EFAULT;
  1837. +
  1838. + if (!cldev)
  1839. + return -EFAULT;
  1840. +
  1841. + mei->cldev = cldev;
  1842. +
  1843. + INIT_LIST_HEAD(&mei->messages);
  1844. + init_waitqueue_head(&mei->message_queue);
  1845. + init_rwsem(&mei->message_lock);
  1846. +
  1847. + mei_cldev_register_rx_cb(cldev, ipts_mei_incoming);
  1848. +
  1849. + return 0;
  1850. +}
  1851. diff --git a/drivers/hid/ipts/mei.h b/drivers/hid/ipts/mei.h
  1852. new file mode 100644
  1853. index 000000000000..eadacae54c40
  1854. --- /dev/null
  1855. +++ b/drivers/hid/ipts/mei.h
  1856. @@ -0,0 +1,67 @@
  1857. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  1858. +/*
  1859. + * Copyright (c) 2016 Intel Corporation
  1860. + * Copyright (c) 2023 Dorian Stoll
  1861. + *
  1862. + * Linux driver for Intel Precise Touch & Stylus
  1863. + */
  1864. +
  1865. +#ifndef IPTS_MEI_H
  1866. +#define IPTS_MEI_H
  1867. +
  1868. +#include <linux/list.h>
  1869. +#include <linux/mei_cl_bus.h>
  1870. +#include <linux/rwsem.h>
  1871. +#include <linux/types.h>
  1872. +#include <linux/wait.h>
  1873. +
  1874. +#include "spec-device.h"
  1875. +
  1876. +struct ipts_mei_message {
  1877. + struct list_head list;
  1878. + struct ipts_response rsp;
  1879. +};
  1880. +
  1881. +struct ipts_mei {
  1882. + struct mei_cl_device *cldev;
  1883. +
  1884. + struct list_head messages;
  1885. +
  1886. + wait_queue_head_t message_queue;
  1887. + struct rw_semaphore message_lock;
  1888. +};
  1889. +
  1890. +/*
  1891. + * ipts_mei_recv() - Receive data from a MEI device.
  1892. + * @mei: The IPTS MEI device context.
  1893. + * @code: The IPTS command code to look for.
  1894. + * @rsp: The address that the received data will be copied to.
  1895. + * @timeout: How many milliseconds the function will wait at most.
  1896. + *
  1897. + * A negative timeout means to wait forever.
  1898. + *
  1899. + * Returns: 0 on success, <0 on error, -EAGAIN if no response has been received.
  1900. + */
  1901. +int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts_response *rsp,
  1902. + u64 timeout);
  1903. +
  1904. +/*
  1905. + * ipts_mei_send() - Send data to a MEI device.
  1906. + * @ipts: The IPTS MEI device context.
  1907. + * @data: The data to send.
  1908. + * @size: The size of the data.
  1909. + *
  1910. + * Returns: 0 on success, <0 on error.
  1911. + */
  1912. +int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length);
  1913. +
  1914. +/*
  1915. + * ipts_mei_init() - Initialize the MEI device context.
  1916. + * @mei: The MEI device context to initialize.
  1917. + * @cldev: The MEI device the context will be bound to.
  1918. + *
  1919. + * Returns: 0 on success, <0 on error.
  1920. + */
  1921. +int ipts_mei_init(struct ipts_mei *mei, struct mei_cl_device *cldev);
  1922. +
  1923. +#endif /* IPTS_MEI_H */
  1924. diff --git a/drivers/hid/ipts/receiver.c b/drivers/hid/ipts/receiver.c
  1925. new file mode 100644
  1926. index 000000000000..77234f9e0e17
  1927. --- /dev/null
  1928. +++ b/drivers/hid/ipts/receiver.c
  1929. @@ -0,0 +1,249 @@
  1930. +// SPDX-License-Identifier: GPL-2.0-or-later
  1931. +/*
  1932. + * Copyright (c) 2016 Intel Corporation
  1933. + * Copyright (c) 2020-2023 Dorian Stoll
  1934. + *
  1935. + * Linux driver for Intel Precise Touch & Stylus
  1936. + */
  1937. +
  1938. +#include <linux/delay.h>
  1939. +#include <linux/err.h>
  1940. +#include <linux/kthread.h>
  1941. +#include <linux/time64.h>
  1942. +#include <linux/timekeeping.h>
  1943. +#include <linux/types.h>
  1944. +
  1945. +#include "cmd.h"
  1946. +#include "context.h"
  1947. +#include "control.h"
  1948. +#include "hid.h"
  1949. +#include "resources.h"
  1950. +#include "spec-device.h"
  1951. +#include "thread.h"
  1952. +
  1953. +static void ipts_receiver_next_doorbell(struct ipts_context *ipts)
  1954. +{
  1955. + u32 *doorbell = (u32 *)ipts->resources.doorbell.address;
  1956. + *doorbell = *doorbell + 1;
  1957. +}
  1958. +
  1959. +static u32 ipts_receiver_current_doorbell(struct ipts_context *ipts)
  1960. +{
  1961. + u32 *doorbell = (u32 *)ipts->resources.doorbell.address;
  1962. + return *doorbell;
  1963. +}
  1964. +
  1965. +static void ipts_receiver_backoff(time64_t last, u32 n)
  1966. +{
  1967. + /*
  1968. + * If the last change was less than n seconds ago,
  1969. + * sleep for a shorter period so that new data can be
  1970. + * processed quickly. If there was no change for more than
  1971. + * n seconds, sleep longer to avoid wasting CPU cycles.
  1972. + */
  1973. + if (last + n > ktime_get_seconds())
  1974. + msleep(20);
  1975. + else
  1976. + msleep(200);
  1977. +}
  1978. +
  1979. +static int ipts_receiver_event_loop(struct ipts_thread *thread)
  1980. +{
  1981. + int ret = 0;
  1982. + u32 buffer = 0;
  1983. +
  1984. + struct ipts_context *ipts = NULL;
  1985. + time64_t last = ktime_get_seconds();
  1986. +
  1987. + if (!thread)
  1988. + return -EFAULT;
  1989. +
  1990. + ipts = thread->data;
  1991. +
  1992. + if (!ipts)
  1993. + return -EFAULT;
  1994. +
  1995. + dev_info(ipts->dev, "IPTS running in event mode\n");
  1996. +
  1997. + while (!ipts_thread_should_stop(thread)) {
  1998. + for (int i = 0; i < IPTS_BUFFERS; i++) {
  1999. + ret = ipts_control_wait_data(ipts, false);
  2000. + if (ret == -EAGAIN)
  2001. + break;
  2002. +
  2003. + if (ret) {
  2004. + dev_err(ipts->dev, "Failed to wait for data: %d\n", ret);
  2005. + continue;
  2006. + }
  2007. +
  2008. + buffer = ipts_receiver_current_doorbell(ipts) % IPTS_BUFFERS;
  2009. + ipts_receiver_next_doorbell(ipts);
  2010. +
  2011. + ret = ipts_hid_input_data(ipts, buffer);
  2012. + if (ret)
  2013. + dev_err(ipts->dev, "Failed to process buffer: %d\n", ret);
  2014. +
  2015. + ret = ipts_control_refill_buffer(ipts, buffer);
  2016. + if (ret)
  2017. + dev_err(ipts->dev, "Failed to send feedback: %d\n", ret);
  2018. +
  2019. + ret = ipts_control_request_data(ipts);
  2020. + if (ret)
  2021. + dev_err(ipts->dev, "Failed to request data: %d\n", ret);
  2022. +
  2023. + last = ktime_get_seconds();
  2024. + }
  2025. +
  2026. + ipts_receiver_backoff(last, 5);
  2027. + }
  2028. +
  2029. + ret = ipts_control_request_flush(ipts);
  2030. + if (ret) {
  2031. + dev_err(ipts->dev, "Failed to request flush: %d\n", ret);
  2032. + return ret;
  2033. + }
  2034. +
  2035. + ret = ipts_control_wait_data(ipts, true);
  2036. + if (ret) {
  2037. + dev_err(ipts->dev, "Failed to wait for data: %d\n", ret);
  2038. +
  2039. + if (ret != -EAGAIN)
  2040. + return ret;
  2041. + else
  2042. + return 0;
  2043. + }
  2044. +
  2045. + ret = ipts_control_wait_flush(ipts);
  2046. + if (ret) {
  2047. + dev_err(ipts->dev, "Failed to wait for flush: %d\n", ret);
  2048. +
  2049. + if (ret != -EAGAIN)
  2050. + return ret;
  2051. + else
  2052. + return 0;
  2053. + }
  2054. +
  2055. + return 0;
  2056. +}
  2057. +
  2058. +static int ipts_receiver_doorbell_loop(struct ipts_thread *thread)
  2059. +{
  2060. + int ret = 0;
  2061. + u32 buffer = 0;
  2062. +
  2063. + u32 doorbell = 0;
  2064. + u32 lastdb = 0;
  2065. +
  2066. + struct ipts_context *ipts = NULL;
  2067. + time64_t last = ktime_get_seconds();
  2068. +
  2069. + if (!thread)
  2070. + return -EFAULT;
  2071. +
  2072. + ipts = thread->data;
  2073. +
  2074. + if (!ipts)
  2075. + return -EFAULT;
  2076. +
  2077. + dev_info(ipts->dev, "IPTS running in doorbell mode\n");
  2078. +
  2079. + while (true) {
  2080. + if (ipts_thread_should_stop(thread)) {
  2081. + ret = ipts_control_request_flush(ipts);
  2082. + if (ret) {
  2083. + dev_err(ipts->dev, "Failed to request flush: %d\n", ret);
  2084. + return ret;
  2085. + }
  2086. + }
  2087. +
  2088. + doorbell = ipts_receiver_current_doorbell(ipts);
  2089. +
  2090. + /*
  2091. + * After filling up one of the data buffers, IPTS will increment
  2092. + * the doorbell. The value of the doorbell stands for the *next*
  2093. + * buffer that IPTS is going to fill.
  2094. + */
  2095. + while (lastdb != doorbell) {
  2096. + buffer = lastdb % IPTS_BUFFERS;
  2097. +
  2098. + ret = ipts_hid_input_data(ipts, buffer);
  2099. + if (ret)
  2100. + dev_err(ipts->dev, "Failed to process buffer: %d\n", ret);
  2101. +
  2102. + ret = ipts_control_refill_buffer(ipts, buffer);
  2103. + if (ret)
  2104. + dev_err(ipts->dev, "Failed to send feedback: %d\n", ret);
  2105. +
  2106. + last = ktime_get_seconds();
  2107. + lastdb++;
  2108. + }
  2109. +
  2110. + if (ipts_thread_should_stop(thread))
  2111. + break;
  2112. +
  2113. + ipts_receiver_backoff(last, 5);
  2114. + }
  2115. +
  2116. + ret = ipts_control_wait_data(ipts, true);
  2117. + if (ret) {
  2118. + dev_err(ipts->dev, "Failed to wait for data: %d\n", ret);
  2119. +
  2120. + if (ret != -EAGAIN)
  2121. + return ret;
  2122. + else
  2123. + return 0;
  2124. + }
  2125. +
  2126. + ret = ipts_control_wait_flush(ipts);
  2127. + if (ret) {
  2128. + dev_err(ipts->dev, "Failed to wait for flush: %d\n", ret);
  2129. +
  2130. + if (ret != -EAGAIN)
  2131. + return ret;
  2132. + else
  2133. + return 0;
  2134. + }
  2135. +
  2136. + return 0;
  2137. +}
  2138. +
  2139. +int ipts_receiver_start(struct ipts_context *ipts)
  2140. +{
  2141. + int ret = 0;
  2142. +
  2143. + if (!ipts)
  2144. + return -EFAULT;
  2145. +
  2146. + if (ipts->mode == IPTS_MODE_EVENT) {
  2147. + ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_event_loop, ipts,
  2148. + "ipts_event");
  2149. + } else if (ipts->mode == IPTS_MODE_DOORBELL) {
  2150. + ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_doorbell_loop, ipts,
  2151. + "ipts_doorbell");
  2152. + } else {
  2153. + ret = -EINVAL;
  2154. + }
  2155. +
  2156. + if (ret) {
  2157. + dev_err(ipts->dev, "Failed to start receiver loop: %d\n", ret);
  2158. + return ret;
  2159. + }
  2160. +
  2161. + return 0;
  2162. +}
  2163. +
  2164. +int ipts_receiver_stop(struct ipts_context *ipts)
  2165. +{
  2166. + int ret = 0;
  2167. +
  2168. + if (!ipts)
  2169. + return -EFAULT;
  2170. +
  2171. + ret = ipts_thread_stop(&ipts->receiver_loop);
  2172. + if (ret) {
  2173. + dev_err(ipts->dev, "Failed to stop receiver loop: %d\n", ret);
  2174. + return ret;
  2175. + }
  2176. +
  2177. + return 0;
  2178. +}
  2179. diff --git a/drivers/hid/ipts/receiver.h b/drivers/hid/ipts/receiver.h
  2180. new file mode 100644
  2181. index 000000000000..96070f34fbca
  2182. --- /dev/null
  2183. +++ b/drivers/hid/ipts/receiver.h
  2184. @@ -0,0 +1,17 @@
  2185. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2186. +/*
  2187. + * Copyright (c) 2016 Intel Corporation
  2188. + * Copyright (c) 2020-2023 Dorian Stoll
  2189. + *
  2190. + * Linux driver for Intel Precise Touch & Stylus
  2191. + */
  2192. +
  2193. +#ifndef IPTS_RECEIVER_H
  2194. +#define IPTS_RECEIVER_H
  2195. +
  2196. +#include "context.h"
  2197. +
  2198. +int ipts_receiver_start(struct ipts_context *ipts);
  2199. +int ipts_receiver_stop(struct ipts_context *ipts);
  2200. +
  2201. +#endif /* IPTS_RECEIVER_H */
  2202. diff --git a/drivers/hid/ipts/resources.c b/drivers/hid/ipts/resources.c
  2203. new file mode 100644
  2204. index 000000000000..80ba5885bb55
  2205. --- /dev/null
  2206. +++ b/drivers/hid/ipts/resources.c
  2207. @@ -0,0 +1,108 @@
  2208. +// SPDX-License-Identifier: GPL-2.0-or-later
  2209. +/*
  2210. + * Copyright (c) 2016 Intel Corporation
  2211. + * Copyright (c) 2020-2023 Dorian Stoll
  2212. + *
  2213. + * Linux driver for Intel Precise Touch & Stylus
  2214. + */
  2215. +
  2216. +#include <linux/dma-mapping.h>
  2217. +#include <linux/types.h>
  2218. +
  2219. +#include "resources.h"
  2220. +#include "spec-device.h"
  2221. +
  2222. +static int ipts_resources_alloc_buffer(struct ipts_buffer *buffer, struct device *dev, size_t size)
  2223. +{
  2224. + if (!buffer)
  2225. + return -EFAULT;
  2226. +
  2227. + if (buffer->address)
  2228. + return 0;
  2229. +
  2230. + buffer->address = dma_alloc_coherent(dev, size, &buffer->dma_address, GFP_KERNEL);
  2231. +
  2232. + if (!buffer->address)
  2233. + return -ENOMEM;
  2234. +
  2235. + buffer->size = size;
  2236. + buffer->device = dev;
  2237. +
  2238. + return 0;
  2239. +}
  2240. +
  2241. +static void ipts_resources_free_buffer(struct ipts_buffer *buffer)
  2242. +{
  2243. + if (!buffer->address)
  2244. + return;
  2245. +
  2246. + dma_free_coherent(buffer->device, buffer->size, buffer->address, buffer->dma_address);
  2247. +
  2248. + buffer->address = NULL;
  2249. + buffer->size = 0;
  2250. +
  2251. + buffer->dma_address = 0;
  2252. + buffer->device = NULL;
  2253. +}
  2254. +
  2255. +int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t ds, size_t fs)
  2256. +{
  2257. + int ret = 0;
  2258. +
  2259. + if (!res)
  2260. + return -EFAULT;
  2261. +
  2262. + for (int i = 0; i < IPTS_BUFFERS; i++) {
  2263. + ret = ipts_resources_alloc_buffer(&res->data[i], dev, ds);
  2264. + if (ret)
  2265. + goto err;
  2266. + }
  2267. +
  2268. + for (int i = 0; i < IPTS_BUFFERS; i++) {
  2269. + ret = ipts_resources_alloc_buffer(&res->feedback[i], dev, fs);
  2270. + if (ret)
  2271. + goto err;
  2272. + }
  2273. +
  2274. + ret = ipts_resources_alloc_buffer(&res->doorbell, dev, sizeof(u32));
  2275. + if (ret)
  2276. + goto err;
  2277. +
  2278. + ret = ipts_resources_alloc_buffer(&res->workqueue, dev, sizeof(u32));
  2279. + if (ret)
  2280. + goto err;
  2281. +
  2282. + ret = ipts_resources_alloc_buffer(&res->hid2me, dev, fs);
  2283. + if (ret)
  2284. + goto err;
  2285. +
  2286. + ret = ipts_resources_alloc_buffer(&res->descriptor, dev, ds + 8);
  2287. + if (ret)
  2288. + goto err;
  2289. +
  2290. + return 0;
  2291. +
  2292. +err:
  2293. +
  2294. + ipts_resources_free(res);
  2295. + return ret;
  2296. +}
  2297. +
  2298. +int ipts_resources_free(struct ipts_resources *res)
  2299. +{
  2300. + if (!res)
  2301. + return -EFAULT;
  2302. +
  2303. + for (int i = 0; i < IPTS_BUFFERS; i++)
  2304. + ipts_resources_free_buffer(&res->data[i]);
  2305. +
  2306. + for (int i = 0; i < IPTS_BUFFERS; i++)
  2307. + ipts_resources_free_buffer(&res->feedback[i]);
  2308. +
  2309. + ipts_resources_free_buffer(&res->doorbell);
  2310. + ipts_resources_free_buffer(&res->workqueue);
  2311. + ipts_resources_free_buffer(&res->hid2me);
  2312. + ipts_resources_free_buffer(&res->descriptor);
  2313. +
  2314. + return 0;
  2315. +}
  2316. diff --git a/drivers/hid/ipts/resources.h b/drivers/hid/ipts/resources.h
  2317. new file mode 100644
  2318. index 000000000000..6cbb24a8a054
  2319. --- /dev/null
  2320. +++ b/drivers/hid/ipts/resources.h
  2321. @@ -0,0 +1,39 @@
  2322. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2323. +/*
  2324. + * Copyright (c) 2016 Intel Corporation
  2325. + * Copyright (c) 2020-2023 Dorian Stoll
  2326. + *
  2327. + * Linux driver for Intel Precise Touch & Stylus
  2328. + */
  2329. +
  2330. +#ifndef IPTS_RESOURCES_H
  2331. +#define IPTS_RESOURCES_H
  2332. +
  2333. +#include <linux/device.h>
  2334. +#include <linux/types.h>
  2335. +
  2336. +#include "spec-device.h"
  2337. +
  2338. +struct ipts_buffer {
  2339. + u8 *address;
  2340. + size_t size;
  2341. +
  2342. + dma_addr_t dma_address;
  2343. + struct device *device;
  2344. +};
  2345. +
  2346. +struct ipts_resources {
  2347. + struct ipts_buffer data[IPTS_BUFFERS];
  2348. + struct ipts_buffer feedback[IPTS_BUFFERS];
  2349. +
  2350. + struct ipts_buffer doorbell;
  2351. + struct ipts_buffer workqueue;
  2352. + struct ipts_buffer hid2me;
  2353. +
  2354. + struct ipts_buffer descriptor;
  2355. +};
  2356. +
  2357. +int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t ds, size_t fs);
  2358. +int ipts_resources_free(struct ipts_resources *res);
  2359. +
  2360. +#endif /* IPTS_RESOURCES_H */
  2361. diff --git a/drivers/hid/ipts/spec-data.h b/drivers/hid/ipts/spec-data.h
  2362. new file mode 100644
  2363. index 000000000000..e8dd98895a7e
  2364. --- /dev/null
  2365. +++ b/drivers/hid/ipts/spec-data.h
  2366. @@ -0,0 +1,100 @@
  2367. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2368. +/*
  2369. + * Copyright (c) 2016 Intel Corporation
  2370. + * Copyright (c) 2020-2023 Dorian Stoll
  2371. + *
  2372. + * Linux driver for Intel Precise Touch & Stylus
  2373. + */
  2374. +
  2375. +#ifndef IPTS_SPEC_DATA_H
  2376. +#define IPTS_SPEC_DATA_H
  2377. +
  2378. +#include <linux/build_bug.h>
  2379. +#include <linux/types.h>
  2380. +
  2381. +/**
  2382. + * enum ipts_feedback_cmd_type - Commands that can be executed on the sensor through feedback.
  2383. + */
  2384. +enum ipts_feedback_cmd_type {
  2385. + IPTS_FEEDBACK_CMD_TYPE_NONE = 0,
  2386. + IPTS_FEEDBACK_CMD_TYPE_SOFT_RESET = 1,
  2387. + IPTS_FEEDBACK_CMD_TYPE_GOTO_ARMED = 2,
  2388. + IPTS_FEEDBACK_CMD_TYPE_GOTO_SENSING = 3,
  2389. + IPTS_FEEDBACK_CMD_TYPE_GOTO_SLEEP = 4,
  2390. + IPTS_FEEDBACK_CMD_TYPE_GOTO_DOZE = 5,
  2391. + IPTS_FEEDBACK_CMD_TYPE_HARD_RESET = 6,
  2392. +};
  2393. +
  2394. +/**
  2395. + * enum ipts_feedback_data_type - Defines what data a feedback buffer contains.
  2396. + * @IPTS_FEEDBACK_DATA_TYPE_VENDOR: The buffer contains vendor specific feedback.
  2397. + * @IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES: The buffer contains a HID set features report.
  2398. + * @IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES: The buffer contains a HID get features report.
  2399. + * @IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT: The buffer contains a HID output report.
  2400. + * @IPTS_FEEDBACK_DATA_TYPE_STORE_DATA: The buffer contains calibration data for the sensor.
  2401. + */
  2402. +enum ipts_feedback_data_type {
  2403. + IPTS_FEEDBACK_DATA_TYPE_VENDOR = 0,
  2404. + IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES = 1,
  2405. + IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES = 2,
  2406. + IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT = 3,
  2407. + IPTS_FEEDBACK_DATA_TYPE_STORE_DATA = 4,
  2408. +};
  2409. +
  2410. +/**
  2411. + * struct ipts_feedback_header - Header that is prefixed to the data in a feedback buffer.
  2412. + * @cmd_type: A command that should be executed on the sensor.
  2413. + * @size: The size of the payload to be written.
  2414. + * @buffer: The ID of the buffer that contains this feedback data.
  2415. + * @protocol: The protocol version of the EDS.
  2416. + * @data_type: The type of data that the buffer contains.
  2417. + * @spi_offset: The offset at which to write the payload data to the sensor.
  2418. + * @payload: Payload for the feedback command, or 0 if no payload is sent.
  2419. + */
  2420. +struct ipts_feedback_header {
  2421. + enum ipts_feedback_cmd_type cmd_type;
  2422. + u32 size;
  2423. + u32 buffer;
  2424. + u32 protocol;
  2425. + enum ipts_feedback_data_type data_type;
  2426. + u32 spi_offset;
  2427. + u8 reserved[40];
  2428. + u8 payload[];
  2429. +} __packed;
  2430. +
  2431. +static_assert(sizeof(struct ipts_feedback_header) == 64);
  2432. +
  2433. +/**
  2434. + * enum ipts_data_type - Defines what type of data a buffer contains.
  2435. + * @IPTS_DATA_TYPE_FRAME: Raw data frame.
  2436. + * @IPTS_DATA_TYPE_ERROR: Error data.
  2437. + * @IPTS_DATA_TYPE_VENDOR: Vendor specific data.
  2438. + * @IPTS_DATA_TYPE_HID: A HID report.
  2439. + * @IPTS_DATA_TYPE_GET_FEATURES: The response to a GET_FEATURES HID2ME command.
  2440. + */
  2441. +enum ipts_data_type {
  2442. + IPTS_DATA_TYPE_FRAME = 0x00,
  2443. + IPTS_DATA_TYPE_ERROR = 0x01,
  2444. + IPTS_DATA_TYPE_VENDOR = 0x02,
  2445. + IPTS_DATA_TYPE_HID = 0x03,
  2446. + IPTS_DATA_TYPE_GET_FEATURES = 0x04,
  2447. + IPTS_DATA_TYPE_DESCRIPTOR = 0x05,
  2448. +};
  2449. +
  2450. +/**
  2451. + * struct ipts_data_header - Header that is prefixed to the data in a data buffer.
  2452. + * @type: What data the buffer contains.
  2453. + * @size: How much data the buffer contains.
  2454. + * @buffer: Which buffer the data is in.
  2455. + */
  2456. +struct ipts_data_header {
  2457. + enum ipts_data_type type;
  2458. + u32 size;
  2459. + u32 buffer;
  2460. + u8 reserved[52];
  2461. + u8 data[];
  2462. +} __packed;
  2463. +
  2464. +static_assert(sizeof(struct ipts_data_header) == 64);
  2465. +
  2466. +#endif /* IPTS_SPEC_DATA_H */
  2467. diff --git a/drivers/hid/ipts/spec-device.h b/drivers/hid/ipts/spec-device.h
  2468. new file mode 100644
  2469. index 000000000000..93f673d981f7
  2470. --- /dev/null
  2471. +++ b/drivers/hid/ipts/spec-device.h
  2472. @@ -0,0 +1,285 @@
  2473. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2474. +/*
  2475. + * Copyright (c) 2016 Intel Corporation
  2476. + * Copyright (c) 2020-2023 Dorian Stoll
  2477. + *
  2478. + * Linux driver for Intel Precise Touch & Stylus
  2479. + */
  2480. +
  2481. +#ifndef IPTS_SPEC_DEVICE_H
  2482. +#define IPTS_SPEC_DEVICE_H
  2483. +
  2484. +#include <linux/build_bug.h>
  2485. +#include <linux/types.h>
  2486. +
  2487. +/*
  2488. + * The amount of buffers that IPTS can use for data transfer.
  2489. + */
  2490. +#define IPTS_BUFFERS 16
  2491. +
  2492. +/*
  2493. + * The buffer ID that is used for HID2ME feedback
  2494. + */
  2495. +#define IPTS_HID2ME_BUFFER IPTS_BUFFERS
  2496. +
  2497. +/**
  2498. + * enum ipts_command - Commands that can be sent to the IPTS hardware.
  2499. + * @IPTS_CMD_GET_DEVICE_INFO: Retrieves vendor information from the device.
  2500. + * @IPTS_CMD_SET_MODE: Changes the mode that the device will operate in.
  2501. + * @IPTS_CMD_SET_MEM_WINDOW: Configures memory buffers for passing data between device and driver.
  2502. + * @IPTS_CMD_QUIESCE_IO: Stops the data flow from the device to the driver.
  2503. + * @IPTS_CMD_READY_FOR_DATA: Informs the device that the driver is ready to receive data.
  2504. + * @IPTS_CMD_FEEDBACK: Informs the device that a buffer was processed and can be refilled.
  2505. + * @IPTS_CMD_CLEAR_MEM_WINDOW: Stops the data flow and clears the buffer addresses on the device.
  2506. + * @IPTS_CMD_RESET_SENSOR: Resets the sensor to its default state.
  2507. + * @IPTS_CMD_GET_DESCRIPTOR: Retrieves the HID descriptor of the device.
  2508. + */
  2509. +enum ipts_command_code {
  2510. + IPTS_CMD_GET_DEVICE_INFO = 0x01,
  2511. + IPTS_CMD_SET_MODE = 0x02,
  2512. + IPTS_CMD_SET_MEM_WINDOW = 0x03,
  2513. + IPTS_CMD_QUIESCE_IO = 0x04,
  2514. + IPTS_CMD_READY_FOR_DATA = 0x05,
  2515. + IPTS_CMD_FEEDBACK = 0x06,
  2516. + IPTS_CMD_CLEAR_MEM_WINDOW = 0x07,
  2517. + IPTS_CMD_RESET_SENSOR = 0x0B,
  2518. + IPTS_CMD_GET_DESCRIPTOR = 0x0F,
  2519. +};
  2520. +
  2521. +/**
  2522. + * enum ipts_status - Possible status codes returned by the IPTS device.
  2523. + * @IPTS_STATUS_SUCCESS: Operation completed successfully.
  2524. + * @IPTS_STATUS_INVALID_PARAMS: Command contained an invalid payload.
  2525. + * @IPTS_STATUS_ACCESS_DENIED: ME could not validate a buffer address.
  2526. + * @IPTS_STATUS_CMD_SIZE_ERROR: Command contains an invalid payload.
  2527. + * @IPTS_STATUS_NOT_READY: Buffer addresses have not been set.
  2528. + * @IPTS_STATUS_REQUEST_OUTSTANDING: There is an outstanding command of the same type.
  2529. + * @IPTS_STATUS_NO_SENSOR_FOUND: No sensor could be found.
  2530. + * @IPTS_STATUS_OUT_OF_MEMORY: Not enough free memory for requested operation.
  2531. + * @IPTS_STATUS_INTERNAL_ERROR: An unexpected error occurred.
  2532. + * @IPTS_STATUS_SENSOR_DISABLED: The sensor has been disabled and must be reinitialized.
  2533. + * @IPTS_STATUS_COMPAT_CHECK_FAIL: Compatibility revision check between sensor and ME failed.
  2534. + * The host can ignore this error and attempt to continue.
  2535. + * @IPTS_STATUS_SENSOR_EXPECTED_RESET: The sensor went through a reset initiated by the driver.
  2536. + * @IPTS_STATUS_SENSOR_UNEXPECTED_RESET: The sensor went through an unexpected reset.
  2537. + * @IPTS_STATUS_RESET_FAILED: Requested sensor reset failed to complete.
  2538. + * @IPTS_STATUS_TIMEOUT: The operation timed out.
  2539. + * @IPTS_STATUS_TEST_MODE_FAIL: Test mode pattern did not match expected values.
  2540. + * @IPTS_STATUS_SENSOR_FAIL_FATAL: The sensor reported an error during reset sequence.
  2541. + * Further progress is not possible.
  2542. + * @IPTS_STATUS_SENSOR_FAIL_NONFATAL: The sensor reported an error during reset sequence.
  2543. + * The driver can attempt to continue.
  2544. + * @IPTS_STATUS_INVALID_DEVICE_CAPS: The device reported invalid capabilities.
  2545. + * @IPTS_STATUS_QUIESCE_IO_IN_PROGRESS: Command cannot be completed until Quiesce IO is done.
  2546. + */
  2547. +enum ipts_status {
  2548. + IPTS_STATUS_SUCCESS = 0x00,
  2549. + IPTS_STATUS_INVALID_PARAMS = 0x01,
  2550. + IPTS_STATUS_ACCESS_DENIED = 0x02,
  2551. + IPTS_STATUS_CMD_SIZE_ERROR = 0x03,
  2552. + IPTS_STATUS_NOT_READY = 0x04,
  2553. + IPTS_STATUS_REQUEST_OUTSTANDING = 0x05,
  2554. + IPTS_STATUS_NO_SENSOR_FOUND = 0x06,
  2555. + IPTS_STATUS_OUT_OF_MEMORY = 0x07,
  2556. + IPTS_STATUS_INTERNAL_ERROR = 0x08,
  2557. + IPTS_STATUS_SENSOR_DISABLED = 0x09,
  2558. + IPTS_STATUS_COMPAT_CHECK_FAIL = 0x0A,
  2559. + IPTS_STATUS_SENSOR_EXPECTED_RESET = 0x0B,
  2560. + IPTS_STATUS_SENSOR_UNEXPECTED_RESET = 0x0C,
  2561. + IPTS_STATUS_RESET_FAILED = 0x0D,
  2562. + IPTS_STATUS_TIMEOUT = 0x0E,
  2563. + IPTS_STATUS_TEST_MODE_FAIL = 0x0F,
  2564. + IPTS_STATUS_SENSOR_FAIL_FATAL = 0x10,
  2565. + IPTS_STATUS_SENSOR_FAIL_NONFATAL = 0x11,
  2566. + IPTS_STATUS_INVALID_DEVICE_CAPS = 0x12,
  2567. + IPTS_STATUS_QUIESCE_IO_IN_PROGRESS = 0x13,
  2568. +};
  2569. +
  2570. +/**
  2571. + * struct ipts_command - Message that is sent to the device for calling a command.
  2572. + * @cmd: The command that will be called.
  2573. + * @payload: Payload containing parameters for the called command.
  2574. + */
  2575. +struct ipts_command {
  2576. + enum ipts_command_code cmd;
  2577. + u8 payload[320];
  2578. +} __packed;
  2579. +
  2580. +static_assert(sizeof(struct ipts_command) == 324);
  2581. +
  2582. +/**
  2583. + * enum ipts_mode - Configures what data the device produces and how its sent.
  2584. + * @IPTS_MODE_EVENT: The device will send an event once a buffer was filled.
  2585. + * Older devices will return singletouch data in this mode.
  2586. + * @IPTS_MODE_DOORBELL: The device will notify the driver by incrementing the doorbell value.
  2587. + * Older devices will return multitouch data in this mode.
  2588. + */
  2589. +enum ipts_mode {
  2590. + IPTS_MODE_EVENT = 0x00,
  2591. + IPTS_MODE_DOORBELL = 0x01,
  2592. +};
  2593. +
  2594. +/**
  2595. + * struct ipts_set_mode - Payload for the SET_MODE command.
  2596. + * @mode: Changes the mode that IPTS will operate in.
  2597. + */
  2598. +struct ipts_set_mode {
  2599. + enum ipts_mode mode;
  2600. + u8 reserved[12];
  2601. +} __packed;
  2602. +
  2603. +static_assert(sizeof(struct ipts_set_mode) == 16);
  2604. +
  2605. +#define IPTS_WORKQUEUE_SIZE 8192
  2606. +#define IPTS_WORKQUEUE_ITEM_SIZE 16
  2607. +
  2608. +/**
  2609. + * struct ipts_mem_window - Payload for the SET_MEM_WINDOW command.
  2610. + * @data_addr_lower: Lower 32 bits of the data buffer addresses.
  2611. + * @data_addr_upper: Upper 32 bits of the data buffer addresses.
  2612. + * @workqueue_addr_lower: Lower 32 bits of the workqueue buffer address.
  2613. + * @workqueue_addr_upper: Upper 32 bits of the workqueue buffer address.
  2614. + * @doorbell_addr_lower: Lower 32 bits of the doorbell buffer address.
  2615. + * @doorbell_addr_upper: Upper 32 bits of the doorbell buffer address.
  2616. + * @feedbackaddr_lower: Lower 32 bits of the feedback buffer addresses.
  2617. + * @feedbackaddr_upper: Upper 32 bits of the feedback buffer addresses.
  2618. + * @hid2me_addr_lower: Lower 32 bits of the hid2me buffer address.
  2619. + * @hid2me_addr_upper: Upper 32 bits of the hid2me buffer address.
  2620. + * @hid2me_size: Size of the hid2me feedback buffer.
  2621. + * @workqueue_item_size: Magic value. Must be 16.
  2622. + * @workqueue_size: Magic value. Must be 8192.
  2623. + *
  2624. + * The workqueue related items in this struct are required for using
  2625. + * GuC submission with binary processing firmware. Since this driver does
  2626. + * not use GuC submission and instead exports raw data to userspace, these
  2627. + * items are not actually used, but they need to be allocated and passed
  2628. + * to the device, otherwise initialization will fail.
  2629. + */
  2630. +struct ipts_mem_window {
  2631. + u32 data_addr_lower[IPTS_BUFFERS];
  2632. + u32 data_addr_upper[IPTS_BUFFERS];
  2633. + u32 workqueue_addr_lower;
  2634. + u32 workqueue_addr_upper;
  2635. + u32 doorbell_addr_lower;
  2636. + u32 doorbell_addr_upper;
  2637. + u32 feedback_addr_lower[IPTS_BUFFERS];
  2638. + u32 feedback_addr_upper[IPTS_BUFFERS];
  2639. + u32 hid2me_addr_lower;
  2640. + u32 hid2me_addr_upper;
  2641. + u32 hid2me_size;
  2642. + u8 reserved1;
  2643. + u8 workqueue_item_size;
  2644. + u16 workqueue_size;
  2645. + u8 reserved[32];
  2646. +} __packed;
  2647. +
  2648. +static_assert(sizeof(struct ipts_mem_window) == 320);
  2649. +
  2650. +/**
  2651. + * struct ipts_quiesce_io - Payload for the QUIESCE_IO command.
  2652. + */
  2653. +struct ipts_quiesce_io {
  2654. + u8 reserved[12];
  2655. +} __packed;
  2656. +
  2657. +static_assert(sizeof(struct ipts_quiesce_io) == 12);
  2658. +
  2659. +/**
  2660. + * struct ipts_feedback - Payload for the FEEDBACK command.
  2661. + * @buffer: The buffer that the device should refill.
  2662. + */
  2663. +struct ipts_feedback {
  2664. + u32 buffer;
  2665. + u8 reserved[12];
  2666. +} __packed;
  2667. +
  2668. +static_assert(sizeof(struct ipts_feedback) == 16);
  2669. +
  2670. +/**
  2671. + * enum ipts_reset_type - Possible ways of resetting the device.
  2672. + * @IPTS_RESET_TYPE_HARD: Perform hardware reset using GPIO pin.
  2673. + * @IPTS_RESET_TYPE_SOFT: Perform software reset using SPI command.
  2674. + */
  2675. +enum ipts_reset_type {
  2676. + IPTS_RESET_TYPE_HARD = 0x00,
  2677. + IPTS_RESET_TYPE_SOFT = 0x01,
  2678. +};
  2679. +
  2680. +/**
  2681. + * struct ipts_reset - Payload for the RESET_SENSOR command.
  2682. + * @type: How the device should get reset.
  2683. + */
  2684. +struct ipts_reset_sensor {
  2685. + enum ipts_reset_type type;
  2686. + u8 reserved[4];
  2687. +} __packed;
  2688. +
  2689. +static_assert(sizeof(struct ipts_reset_sensor) == 8);
  2690. +
  2691. +/**
  2692. + * struct ipts_get_descriptor - Payload for the GET_DESCRIPTOR command.
  2693. + * @addr_lower: The lower 32 bits of the descriptor buffer address.
  2694. + * @addr_upper: The upper 32 bits of the descriptor buffer address.
  2695. + * @magic: A magic value. Must be 8.
  2696. + */
  2697. +struct ipts_get_descriptor {
  2698. + u32 addr_lower;
  2699. + u32 addr_upper;
  2700. + u32 magic;
  2701. + u8 reserved[12];
  2702. +} __packed;
  2703. +
  2704. +static_assert(sizeof(struct ipts_get_descriptor) == 24);
  2705. +
  2706. +/*
  2707. + * The type of a response is indicated by a
  2708. + * command code, with the most significant bit flipped to 1.
  2709. + */
  2710. +#define IPTS_RSP_BIT BIT(31)
  2711. +
  2712. +/**
  2713. + * struct ipts_response - Data returned from the device in response to a command.
  2714. + * @cmd: The command that this response answers (IPTS_RSP_BIT will be 1).
  2715. + * @status: The return code of the command.
  2716. + * @payload: The data that was produced by the command.
  2717. + */
  2718. +struct ipts_response {
  2719. + enum ipts_command_code cmd;
  2720. + enum ipts_status status;
  2721. + u8 payload[80];
  2722. +} __packed;
  2723. +
  2724. +static_assert(sizeof(struct ipts_response) == 88);
  2725. +
  2726. +/**
  2727. + * struct ipts_device_info - Vendor information of the IPTS device.
  2728. + * @vendor: Vendor ID of this device.
  2729. + * @product: Product ID of this device.
  2730. + * @hw_version: Hardware revision of this device.
  2731. + * @fw_version: Firmware revision of this device.
  2732. + * @data_size: Requested size for a data buffer.
  2733. + * @feedback_size: Requested size for a feedback buffer.
  2734. + * @mode: Mode that the device currently operates in.
  2735. + * @max_contacts: Maximum amount of concurrent touches the sensor can process.
  2736. + */
  2737. +struct ipts_device_info {
  2738. + u16 vendor;
  2739. + u16 product;
  2740. + u32 hw_version;
  2741. + u32 fw_version;
  2742. + u32 data_size;
  2743. + u32 feedback_size;
  2744. + enum ipts_mode mode;
  2745. + u8 max_contacts;
  2746. + u8 reserved1[3];
  2747. + u8 sensor_min_eds;
  2748. + u8 sensor_maj_eds;
  2749. + u8 me_min_eds;
  2750. + u8 me_maj_eds;
  2751. + u8 intf_eds;
  2752. + u8 reserved2[11];
  2753. +} __packed;
  2754. +
  2755. +static_assert(sizeof(struct ipts_device_info) == 44);
  2756. +
  2757. +#endif /* IPTS_SPEC_DEVICE_H */
  2758. diff --git a/drivers/hid/ipts/spec-hid.h b/drivers/hid/ipts/spec-hid.h
  2759. new file mode 100644
  2760. index 000000000000..ea70f29ff00c
  2761. --- /dev/null
  2762. +++ b/drivers/hid/ipts/spec-hid.h
  2763. @@ -0,0 +1,35 @@
  2764. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2765. +/*
  2766. + * Copyright (c) 2016 Intel Corporation
  2767. + * Copyright (c) 2020-2023 Dorian Stoll
  2768. + *
  2769. + * Linux driver for Intel Precise Touch & Stylus
  2770. + */
  2771. +
  2772. +#ifndef IPTS_SPEC_HID_H
  2773. +#define IPTS_SPEC_HID_H
  2774. +
  2775. +#include <linux/build_bug.h>
  2776. +#include <linux/types.h>
  2777. +
  2778. +/*
  2779. + * Made-up type for passing raw IPTS data in a HID report.
  2780. + */
  2781. +#define IPTS_HID_FRAME_TYPE_RAW 0xEE
  2782. +
  2783. +/**
  2784. + * struct ipts_hid_frame - Header that is prefixed to raw IPTS data wrapped in a HID report.
  2785. + * @size: Size of the data inside the report, including this header.
  2786. + * @type: What type of data does this report contain.
  2787. + */
  2788. +struct ipts_hid_header {
  2789. + u32 size;
  2790. + u8 reserved1;
  2791. + u8 type;
  2792. + u8 reserved2;
  2793. + u8 data[];
  2794. +} __packed;
  2795. +
  2796. +static_assert(sizeof(struct ipts_hid_header) == 7);
  2797. +
  2798. +#endif /* IPTS_SPEC_HID_H */
  2799. diff --git a/drivers/hid/ipts/thread.c b/drivers/hid/ipts/thread.c
  2800. new file mode 100644
  2801. index 000000000000..8b46f775c107
  2802. --- /dev/null
  2803. +++ b/drivers/hid/ipts/thread.c
  2804. @@ -0,0 +1,85 @@
  2805. +// SPDX-License-Identifier: GPL-2.0-or-later
  2806. +/*
  2807. + * Copyright (c) 2016 Intel Corporation
  2808. + * Copyright (c) 2023 Dorian Stoll
  2809. + *
  2810. + * Linux driver for Intel Precise Touch & Stylus
  2811. + */
  2812. +
  2813. +#include <linux/completion.h>
  2814. +#include <linux/err.h>
  2815. +#include <linux/kthread.h>
  2816. +#include <linux/mutex.h>
  2817. +
  2818. +#include "thread.h"
  2819. +
  2820. +bool ipts_thread_should_stop(struct ipts_thread *thread)
  2821. +{
  2822. + if (!thread)
  2823. + return false;
  2824. +
  2825. + return READ_ONCE(thread->should_stop);
  2826. +}
  2827. +
  2828. +static int ipts_thread_runner(void *data)
  2829. +{
  2830. + int ret = 0;
  2831. + struct ipts_thread *thread = data;
  2832. +
  2833. + if (!thread)
  2834. + return -EFAULT;
  2835. +
  2836. + if (!thread->threadfn)
  2837. + return -EFAULT;
  2838. +
  2839. + ret = thread->threadfn(thread);
  2840. + complete_all(&thread->done);
  2841. +
  2842. + return ret;
  2843. +}
  2844. +
  2845. +int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_thread *thread),
  2846. + void *data, const char *name)
  2847. +{
  2848. + if (!thread)
  2849. + return -EFAULT;
  2850. +
  2851. + if (!threadfn)
  2852. + return -EFAULT;
  2853. +
  2854. + init_completion(&thread->done);
  2855. +
  2856. + thread->data = data;
  2857. + thread->should_stop = false;
  2858. + thread->threadfn = threadfn;
  2859. +
  2860. + thread->thread = kthread_run(ipts_thread_runner, thread, name);
  2861. + return PTR_ERR_OR_ZERO(thread->thread);
  2862. +}
  2863. +
  2864. +int ipts_thread_stop(struct ipts_thread *thread)
  2865. +{
  2866. + int ret = 0;
  2867. +
  2868. + if (!thread)
  2869. + return -EFAULT;
  2870. +
  2871. + if (!thread->thread)
  2872. + return 0;
  2873. +
  2874. + WRITE_ONCE(thread->should_stop, true);
  2875. +
  2876. + /*
  2877. + * Make sure that the write has gone through before waiting.
  2878. + */
  2879. + wmb();
  2880. +
  2881. + wait_for_completion(&thread->done);
  2882. + ret = kthread_stop(thread->thread);
  2883. +
  2884. + thread->thread = NULL;
  2885. + thread->data = NULL;
  2886. + thread->threadfn = NULL;
  2887. +
  2888. + return ret;
  2889. +}
  2890. diff --git a/drivers/hid/ipts/thread.h b/drivers/hid/ipts/thread.h
  2891. new file mode 100644
  2892. index 000000000000..a314843599fc
  2893. --- /dev/null
  2894. +++ b/drivers/hid/ipts/thread.h
  2895. @@ -0,0 +1,60 @@
  2896. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  2897. +/*
  2898. + * Copyright (c) 2016 Intel Corporation
  2899. + * Copyright (c) 2023 Dorian Stoll
  2900. + *
  2901. + * Linux driver for Intel Precise Touch & Stylus
  2902. + */
  2903. +
  2904. +#ifndef IPTS_THREAD_H
  2905. +#define IPTS_THREAD_H
  2906. +
  2907. +#include <linux/completion.h>
  2908. +#include <linux/mutex.h>
  2909. +#include <linux/sched.h>
  2910. +
  2911. +/*
  2912. + * This wrapper over kthread is necessary, because calling kthread_stop makes it impossible
  2913. + * to issue MEI commands from that thread while it shuts itself down. By using a custom
  2914. + * boolean variable and a completion object, we can call kthread_stop only when the thread
  2915. + * already finished all of its work and has returned.
  2916. + */
  2917. +struct ipts_thread {
  2918. + struct task_struct *thread;
  2919. +
  2920. + bool should_stop;
  2921. + struct completion done;
  2922. +
  2923. + void *data;
  2924. + int (*threadfn)(struct ipts_thread *thread);
  2925. +};
  2926. +
  2927. +/*
  2928. + * ipts_thread_should_stop() - Returns true if the thread is asked to terminate.
  2929. + * @thread: The current thread.
  2930. + *
  2931. + * Returns: true if the thread should stop, false if not.
  2932. + */
  2933. +bool ipts_thread_should_stop(struct ipts_thread *thread);
  2934. +
  2935. +/*
  2936. + * ipts_thread_start() - Starts an IPTS thread.
  2937. + * @thread: The thread to initialize and start.
  2938. + * @threadfn: The function to execute.
  2939. + * @data: An argument that will be passed to threadfn.
  2940. + * @name: The name of the new thread.
  2941. + *
  2942. + * Returns: 0 on success, <0 on error.
  2943. + */
  2944. +int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_thread *thread),
  2945. + void *data, const char name[]);
  2946. +
  2947. +/*
  2948. + * ipts_thread_stop() - Asks the thread to terminate and waits until it has finished.
  2949. + * @thread: The thread that should stop.
  2950. + *
  2951. + * Returns: The return value of the thread function.
  2952. + */
  2953. +int ipts_thread_stop(struct ipts_thread *thread);
  2954. +
  2955. +#endif /* IPTS_THREAD_H */
  2956. --
  2957. 2.41.0
  2958. From e52a30a8f4812f8af1ec6cc5b70e70123be609f1 Mon Sep 17 00:00:00 2001
  2959. From: Dorian Stoll <dorian.stoll@tmsp.io>
  2960. Date: Fri, 28 Apr 2023 15:41:12 +0200
  2961. Subject: [PATCH] Update IPTS from module repo
  2962. Changes:
  2963. * Fix redefinition error on AOSP clang
  2964. * Increase the polling frequency to reduce latency
  2965. * Don't allocate a new buffer for every HID report
  2966. * Always use the generic HID driver instead of forcing hid-multitouch
  2967. Based on https://github.com/linux-surface/intel-precise-touch/commit/a2b675d72dbde80ebe36a5b6ceaebd596c030314
  2968. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  2969. Patchset: ipts
  2970. ---
  2971. drivers/hid/ipts/hid.c | 15 +++++----------
  2972. drivers/hid/ipts/receiver.c | 2 +-
  2973. drivers/hid/ipts/resources.c | 35 +++++++++++++++++++++++++++++++----
  2974. drivers/hid/ipts/resources.h | 3 +++
  2975. 4 files changed, 40 insertions(+), 15 deletions(-)
  2976. diff --git a/drivers/hid/ipts/hid.c b/drivers/hid/ipts/hid.c
  2977. index 6782394e8dde..a2471219615b 100644
  2978. --- a/drivers/hid/ipts/hid.c
  2979. +++ b/drivers/hid/ipts/hid.c
  2980. @@ -237,7 +237,6 @@ static struct hid_ll_driver ipts_hid_driver = {
  2981. int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  2982. {
  2983. - int ret = 0;
  2984. u8 *temp = NULL;
  2985. struct ipts_hid_header *frame = NULL;
  2986. struct ipts_data_header *header = NULL;
  2987. @@ -250,6 +249,9 @@ int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  2988. header = (struct ipts_data_header *)ipts->resources.data[buffer].address;
  2989. + temp = ipts->resources.report.address;
  2990. + memset(temp, 0, ipts->resources.report.size);
  2991. +
  2992. if (!header)
  2993. return -EFAULT;
  2994. @@ -273,10 +275,6 @@ int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  2995. if (header->size + 3 + sizeof(struct ipts_hid_header) > IPTS_HID_REPORT_DATA_SIZE)
  2996. return -ERANGE;
  2997. - temp = kzalloc(IPTS_HID_REPORT_DATA_SIZE, GFP_KERNEL);
  2998. - if (!temp)
  2999. - return -ENOMEM;
  3000. -
  3001. /*
  3002. * Synthesize a HID report matching the devices that natively send HID reports
  3003. */
  3004. @@ -288,10 +286,7 @@ int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  3005. memcpy(frame->data, header->data, header->size);
  3006. - ret = hid_input_report(ipts->hid, HID_INPUT_REPORT, temp, IPTS_HID_REPORT_DATA_SIZE, 1);
  3007. - kfree(temp);
  3008. -
  3009. - return ret;
  3010. + return hid_input_report(ipts->hid, HID_INPUT_REPORT, temp, IPTS_HID_REPORT_DATA_SIZE, 1);
  3011. }
  3012. int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info)
  3013. @@ -318,7 +313,7 @@ int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info)
  3014. ipts->hid->vendor = info.vendor;
  3015. ipts->hid->product = info.product;
  3016. - ipts->hid->group = HID_GROUP_MULTITOUCH;
  3017. + ipts->hid->group = HID_GROUP_GENERIC;
  3018. snprintf(ipts->hid->name, sizeof(ipts->hid->name), "IPTS %04X:%04X", info.vendor,
  3019. info.product);
  3020. diff --git a/drivers/hid/ipts/receiver.c b/drivers/hid/ipts/receiver.c
  3021. index 77234f9e0e17..f56e9ed32d57 100644
  3022. --- a/drivers/hid/ipts/receiver.c
  3023. +++ b/drivers/hid/ipts/receiver.c
  3024. @@ -42,7 +42,7 @@ static void ipts_receiver_backoff(time64_t last, u32 n)
  3025. * n seconds, sleep longer to avoid wasting CPU cycles.
  3026. */
  3027. if (last + n > ktime_get_seconds())
  3028. - msleep(20);
  3029. + usleep_range(1 * USEC_PER_MSEC, 5 * USEC_PER_MSEC);
  3030. else
  3031. msleep(200);
  3032. }
  3033. diff --git a/drivers/hid/ipts/resources.c b/drivers/hid/ipts/resources.c
  3034. index 80ba5885bb55..5e924d58c488 100644
  3035. --- a/drivers/hid/ipts/resources.c
  3036. +++ b/drivers/hid/ipts/resources.c
  3037. @@ -9,6 +9,7 @@
  3038. #include <linux/dma-mapping.h>
  3039. #include <linux/types.h>
  3040. +#include "desc.h"
  3041. #include "resources.h"
  3042. #include "spec-device.h"
  3043. @@ -49,16 +50,22 @@ int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t d
  3044. {
  3045. int ret = 0;
  3046. + /*
  3047. + * Some compilers (AOSP clang) complain about a redefined
  3048. + * variable when this is declared inside of the for loop.
  3049. + */
  3050. + int i = 0;
  3051. +
  3052. if (!res)
  3053. return -EFAULT;
  3054. - for (int i = 0; i < IPTS_BUFFERS; i++) {
  3055. + for (i = 0; i < IPTS_BUFFERS; i++) {
  3056. ret = ipts_resources_alloc_buffer(&res->data[i], dev, ds);
  3057. if (ret)
  3058. goto err;
  3059. }
  3060. - for (int i = 0; i < IPTS_BUFFERS; i++) {
  3061. + for (i = 0; i < IPTS_BUFFERS; i++) {
  3062. ret = ipts_resources_alloc_buffer(&res->feedback[i], dev, fs);
  3063. if (ret)
  3064. goto err;
  3065. @@ -80,6 +87,16 @@ int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t d
  3066. if (ret)
  3067. goto err;
  3068. + if (!res->report.address) {
  3069. + res->report.size = IPTS_HID_REPORT_DATA_SIZE;
  3070. + res->report.address = kzalloc(res->report.size, GFP_KERNEL);
  3071. +
  3072. + if (!res->report.address) {
  3073. + ret = -ENOMEM;
  3074. + goto err;
  3075. + }
  3076. + }
  3077. +
  3078. return 0;
  3079. err:
  3080. @@ -90,13 +107,19 @@ int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t d
  3081. int ipts_resources_free(struct ipts_resources *res)
  3082. {
  3083. + /*
  3084. + * Some compilers (AOSP clang) complain about a redefined
  3085. + * variable when this is declared inside of the for loop.
  3086. + */
  3087. + int i = 0;
  3088. +
  3089. if (!res)
  3090. return -EFAULT;
  3091. - for (int i = 0; i < IPTS_BUFFERS; i++)
  3092. + for (i = 0; i < IPTS_BUFFERS; i++)
  3093. ipts_resources_free_buffer(&res->data[i]);
  3094. - for (int i = 0; i < IPTS_BUFFERS; i++)
  3095. + for (i = 0; i < IPTS_BUFFERS; i++)
  3096. ipts_resources_free_buffer(&res->feedback[i]);
  3097. ipts_resources_free_buffer(&res->doorbell);
  3098. @@ -104,5 +127,9 @@ int ipts_resources_free(struct ipts_resources *res)
  3099. ipts_resources_free_buffer(&res->hid2me);
  3100. ipts_resources_free_buffer(&res->descriptor);
  3101. + kfree(res->report.address);
  3102. + res->report.address = NULL;
  3103. + res->report.size = 0;
  3104. +
  3105. return 0;
  3106. }
  3107. diff --git a/drivers/hid/ipts/resources.h b/drivers/hid/ipts/resources.h
  3108. index 6cbb24a8a054..e0c400f420b9 100644
  3109. --- a/drivers/hid/ipts/resources.h
  3110. +++ b/drivers/hid/ipts/resources.h
  3111. @@ -31,6 +31,9 @@ struct ipts_resources {
  3112. struct ipts_buffer hid2me;
  3113. struct ipts_buffer descriptor;
  3114. +
  3115. + // Buffer for synthesizing HID reports
  3116. + struct ipts_buffer report;
  3117. };
  3118. int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t ds, size_t fs);
  3119. --
  3120. 2.41.0
  3121. From 35874dc856b04e0be4fc6e9a18e978640272a46e Mon Sep 17 00:00:00 2001
  3122. From: Dorian Stoll <dorian.stoll@tmsp.io>
  3123. Date: Mon, 17 Jul 2023 18:10:43 +0200
  3124. Subject: [PATCH] Update IPTS from module repo
  3125. Changes:
  3126. * Remove usages of C11 for compatibility with older (Android) kernels
  3127. * Remove sensor reset during shutdown
  3128. * Fix compiling resources.c on Android-x86 kernel
  3129. * Split out HID behaviour for different EDS versions into different files
  3130. * Fix kernel-doc comments
  3131. * Add missing docs for members of the device info struct
  3132. * Drop Intel copyright
  3133. * Rename doorbell mode to poll mode
  3134. * Disable the HID interface when the hardware is shutting down.
  3135. Based on https://github.com/linux-surface/intel-precise-touch/commit/56765da3f87d87c5e5cf09e928abe0f579525e5e
  3136. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  3137. Patchset: ipts
  3138. ---
  3139. drivers/hid/ipts/Makefile | 2 +
  3140. drivers/hid/ipts/cmd.c | 1 -
  3141. drivers/hid/ipts/cmd.h | 7 +-
  3142. drivers/hid/ipts/context.h | 3 +-
  3143. drivers/hid/ipts/control.c | 25 ++---
  3144. drivers/hid/ipts/control.h | 25 +++--
  3145. drivers/hid/ipts/desc.h | 1 -
  3146. drivers/hid/ipts/eds1.c | 103 ++++++++++++++++++
  3147. drivers/hid/ipts/eds1.h | 35 +++++++
  3148. drivers/hid/ipts/eds2.c | 144 ++++++++++++++++++++++++++
  3149. drivers/hid/ipts/eds2.h | 35 +++++++
  3150. drivers/hid/ipts/hid.c | 184 ++++++---------------------------
  3151. drivers/hid/ipts/hid.h | 4 +-
  3152. drivers/hid/ipts/main.c | 1 -
  3153. drivers/hid/ipts/mei.c | 1 -
  3154. drivers/hid/ipts/mei.h | 7 +-
  3155. drivers/hid/ipts/receiver.c | 15 +--
  3156. drivers/hid/ipts/receiver.h | 1 -
  3157. drivers/hid/ipts/resources.c | 6 +-
  3158. drivers/hid/ipts/resources.h | 1 -
  3159. drivers/hid/ipts/spec-device.h | 31 +++---
  3160. drivers/hid/ipts/spec-hid.h | 1 -
  3161. drivers/hid/ipts/thread.c | 1 -
  3162. drivers/hid/ipts/thread.h | 7 +-
  3163. 24 files changed, 413 insertions(+), 228 deletions(-)
  3164. create mode 100644 drivers/hid/ipts/eds1.c
  3165. create mode 100644 drivers/hid/ipts/eds1.h
  3166. create mode 100644 drivers/hid/ipts/eds2.c
  3167. create mode 100644 drivers/hid/ipts/eds2.h
  3168. diff --git a/drivers/hid/ipts/Makefile b/drivers/hid/ipts/Makefile
  3169. index 0fe655bccdc0..883896f68e6a 100644
  3170. --- a/drivers/hid/ipts/Makefile
  3171. +++ b/drivers/hid/ipts/Makefile
  3172. @@ -6,6 +6,8 @@
  3173. obj-$(CONFIG_HID_IPTS) += ipts.o
  3174. ipts-objs := cmd.o
  3175. ipts-objs += control.o
  3176. +ipts-objs += eds1.o
  3177. +ipts-objs += eds2.o
  3178. ipts-objs += hid.o
  3179. ipts-objs += main.o
  3180. ipts-objs += mei.o
  3181. diff --git a/drivers/hid/ipts/cmd.c b/drivers/hid/ipts/cmd.c
  3182. index 7fd69271ccd5..63a4934bbc5f 100644
  3183. --- a/drivers/hid/ipts/cmd.c
  3184. +++ b/drivers/hid/ipts/cmd.c
  3185. @@ -1,6 +1,5 @@
  3186. // SPDX-License-Identifier: GPL-2.0-or-later
  3187. /*
  3188. - * Copyright (c) 2016 Intel Corporation
  3189. * Copyright (c) 2020-2023 Dorian Stoll
  3190. *
  3191. * Linux driver for Intel Precise Touch & Stylus
  3192. diff --git a/drivers/hid/ipts/cmd.h b/drivers/hid/ipts/cmd.h
  3193. index 924758ffee67..2b4079075b64 100644
  3194. --- a/drivers/hid/ipts/cmd.h
  3195. +++ b/drivers/hid/ipts/cmd.h
  3196. @@ -1,6 +1,5 @@
  3197. /* SPDX-License-Identifier: GPL-2.0-or-later */
  3198. /*
  3199. - * Copyright (c) 2016 Intel Corporation
  3200. * Copyright (c) 2020-2023 Dorian Stoll
  3201. *
  3202. * Linux driver for Intel Precise Touch & Stylus
  3203. @@ -19,7 +18,7 @@
  3204. */
  3205. #define IPTS_CMD_DEFAULT_TIMEOUT 1000
  3206. -/*
  3207. +/**
  3208. * ipts_cmd_recv_timeout() - Receives a response to a command.
  3209. * @ipts: The IPTS driver context.
  3210. * @code: The type of the command / response.
  3211. @@ -33,7 +32,7 @@
  3212. int ipts_cmd_recv_timeout(struct ipts_context *ipts, enum ipts_command_code code,
  3213. struct ipts_response *rsp, u64 timeout);
  3214. -/*
  3215. +/**
  3216. * ipts_cmd_recv() - Receives a response to a command.
  3217. * @ipts: The IPTS driver context.
  3218. * @code: The type of the command / response.
  3219. @@ -47,7 +46,7 @@ static inline int ipts_cmd_recv(struct ipts_context *ipts, enum ipts_command_cod
  3220. return ipts_cmd_recv_timeout(ipts, code, rsp, IPTS_CMD_DEFAULT_TIMEOUT);
  3221. }
  3222. -/*
  3223. +/**
  3224. * ipts_cmd_send() - Executes a command on the device.
  3225. * @ipts: The IPTS driver context.
  3226. * @code: The type of the command to execute.
  3227. diff --git a/drivers/hid/ipts/context.h b/drivers/hid/ipts/context.h
  3228. index 3450a95e66ee..ba33259f1f7c 100644
  3229. --- a/drivers/hid/ipts/context.h
  3230. +++ b/drivers/hid/ipts/context.h
  3231. @@ -1,6 +1,5 @@
  3232. /* SPDX-License-Identifier: GPL-2.0-or-later */
  3233. /*
  3234. - * Copyright (c) 2016 Intel Corporation
  3235. * Copyright (c) 2020-2023 Dorian Stoll
  3236. *
  3237. * Linux driver for Intel Precise Touch & Stylus
  3238. @@ -41,7 +40,9 @@ struct ipts_context {
  3239. struct ipts_buffer feature_report;
  3240. struct ipts_buffer descriptor;
  3241. + bool hid_active;
  3242. struct hid_device *hid;
  3243. +
  3244. struct ipts_device_info info;
  3245. struct ipts_resources resources;
  3246. diff --git a/drivers/hid/ipts/control.c b/drivers/hid/ipts/control.c
  3247. index 2f61500b5119..5360842d260b 100644
  3248. --- a/drivers/hid/ipts/control.c
  3249. +++ b/drivers/hid/ipts/control.c
  3250. @@ -1,6 +1,5 @@
  3251. // SPDX-License-Identifier: GPL-2.0-or-later
  3252. /*
  3253. - * Copyright (c) 2016 Intel Corporation
  3254. * Copyright (c) 2020-2023 Dorian Stoll
  3255. *
  3256. * Linux driver for Intel Precise Touch & Stylus
  3257. @@ -88,6 +87,7 @@ static int ipts_control_set_mode(struct ipts_context *ipts, enum ipts_mode mode)
  3258. static int ipts_control_set_mem_window(struct ipts_context *ipts, struct ipts_resources *res)
  3259. {
  3260. + int i = 0;
  3261. int ret = 0;
  3262. struct ipts_mem_window cmd = { 0 };
  3263. struct ipts_response rsp = { 0 };
  3264. @@ -98,7 +98,7 @@ static int ipts_control_set_mem_window(struct ipts_context *ipts, struct ipts_re
  3265. if (!res)
  3266. return -EFAULT;
  3267. - for (int i = 0; i < IPTS_BUFFERS; i++) {
  3268. + for (i = 0; i < IPTS_BUFFERS; i++) {
  3269. cmd.data_addr_lower[i] = lower_32_bits(res->data[i].dma_address);
  3270. cmd.data_addr_upper[i] = upper_32_bits(res->data[i].dma_address);
  3271. cmd.feedback_addr_lower[i] = lower_32_bits(res->feedback[i].dma_address);
  3272. @@ -342,12 +342,6 @@ int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_c
  3273. return ipts_control_send_feedback(ipts, IPTS_HID2ME_BUFFER);
  3274. }
  3275. -static inline int ipts_control_reset_sensor(struct ipts_context *ipts)
  3276. -{
  3277. - return ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_SOFT_RESET,
  3278. - IPTS_FEEDBACK_DATA_TYPE_VENDOR, NULL, 0);
  3279. -}
  3280. -
  3281. int ipts_control_start(struct ipts_context *ipts)
  3282. {
  3283. int ret = 0;
  3284. @@ -389,9 +383,9 @@ int ipts_control_start(struct ipts_context *ipts)
  3285. }
  3286. /*
  3287. - * Newer devices can be directly initialized in doorbell mode.
  3288. + * Newer devices can be directly initialized in polling mode.
  3289. */
  3290. - ipts->mode = IPTS_MODE_DOORBELL;
  3291. + ipts->mode = IPTS_MODE_POLL;
  3292. }
  3293. ret = ipts_control_set_mode(ipts, ipts->mode);
  3294. @@ -418,6 +412,8 @@ int ipts_control_start(struct ipts_context *ipts)
  3295. return ret;
  3296. }
  3297. + ipts_hid_enable(ipts);
  3298. +
  3299. ret = ipts_hid_init(ipts, info);
  3300. if (ret) {
  3301. dev_err(ipts->dev, "Failed to initialize HID device: %d\n", ret);
  3302. @@ -434,6 +430,7 @@ static int _ipts_control_stop(struct ipts_context *ipts)
  3303. if (!ipts)
  3304. return -EFAULT;
  3305. + ipts_hid_disable(ipts);
  3306. dev_info(ipts->dev, "Stopping IPTS\n");
  3307. ret = ipts_receiver_stop(ipts);
  3308. @@ -442,12 +439,6 @@ static int _ipts_control_stop(struct ipts_context *ipts)
  3309. return ret;
  3310. }
  3311. - ret = ipts_control_reset_sensor(ipts);
  3312. - if (ret) {
  3313. - dev_err(ipts->dev, "Failed to reset sensor: %d\n", ret);
  3314. - return ret;
  3315. - }
  3316. -
  3317. ret = ipts_resources_free(&ipts->resources);
  3318. if (ret) {
  3319. dev_err(ipts->dev, "Failed to free resources: %d\n", ret);
  3320. @@ -483,7 +474,7 @@ int ipts_control_restart(struct ipts_context *ipts)
  3321. return ret;
  3322. /*
  3323. - * Give the sensor some time to come back from resetting
  3324. + * Wait a second to give the sensor time to fully shut down.
  3325. */
  3326. msleep(1000);
  3327. diff --git a/drivers/hid/ipts/control.h b/drivers/hid/ipts/control.h
  3328. index 744bb92d682a..26629c5144ed 100644
  3329. --- a/drivers/hid/ipts/control.h
  3330. +++ b/drivers/hid/ipts/control.h
  3331. @@ -1,6 +1,5 @@
  3332. /* SPDX-License-Identifier: GPL-2.0-or-later */
  3333. /*
  3334. - * Copyright (c) 2016 Intel Corporation
  3335. * Copyright (c) 2020-2023 Dorian Stoll
  3336. *
  3337. * Linux driver for Intel Precise Touch & Stylus
  3338. @@ -15,7 +14,7 @@
  3339. #include "spec-data.h"
  3340. #include "spec-device.h"
  3341. -/*
  3342. +/**
  3343. * ipts_control_request_flush() - Stop the data flow.
  3344. * @ipts: The IPTS driver context.
  3345. *
  3346. @@ -26,7 +25,7 @@
  3347. */
  3348. int ipts_control_request_flush(struct ipts_context *ipts);
  3349. -/*
  3350. +/**
  3351. * ipts_control_wait_flush() - Wait until data flow has been stopped.
  3352. * @ipts: The IPTS driver context.
  3353. *
  3354. @@ -34,7 +33,7 @@ int ipts_control_request_flush(struct ipts_context *ipts);
  3355. */
  3356. int ipts_control_wait_flush(struct ipts_context *ipts);
  3357. -/*
  3358. +/**
  3359. * ipts_control_wait_flush() - Notify the device that the driver can receive new data.
  3360. * @ipts: The IPTS driver context.
  3361. *
  3362. @@ -42,19 +41,19 @@ int ipts_control_wait_flush(struct ipts_context *ipts);
  3363. */
  3364. int ipts_control_request_data(struct ipts_context *ipts);
  3365. -/*
  3366. +/**
  3367. * ipts_control_wait_data() - Wait until new data is available.
  3368. * @ipts: The IPTS driver context.
  3369. * @block: Whether to block execution until data is available.
  3370. *
  3371. - * In doorbell mode, this function will never return while the data flow is active. Instead,
  3372. - * the doorbell will be incremented when new data is available.
  3373. + * In poll mode, this function will never return while the data flow is active. Instead,
  3374. + * the poll will be incremented when new data is available.
  3375. *
  3376. * Returns: 0 on success, <0 on error, -EAGAIN if no data is available.
  3377. */
  3378. int ipts_control_wait_data(struct ipts_context *ipts, bool block);
  3379. -/*
  3380. +/**
  3381. * ipts_control_send_feedback() - Submits a feedback buffer to the device.
  3382. * @ipts: The IPTS driver context.
  3383. * @buffer: The ID of the buffer containing feedback data.
  3384. @@ -63,7 +62,7 @@ int ipts_control_wait_data(struct ipts_context *ipts, bool block);
  3385. */
  3386. int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer);
  3387. -/*
  3388. +/**
  3389. * ipts_control_hid2me_feedback() - Sends HID2ME feedback, a special type of feedback.
  3390. * @ipts: The IPTS driver context.
  3391. * @cmd: The command that will be run on the device.
  3392. @@ -80,7 +79,7 @@ int ipts_control_send_feedback(struct ipts_context *ipts, u32 buffer);
  3393. int ipts_control_hid2me_feedback(struct ipts_context *ipts, enum ipts_feedback_cmd_type cmd,
  3394. enum ipts_feedback_data_type type, void *data, size_t size);
  3395. -/*
  3396. +/**
  3397. * ipts_control_refill_buffer() - Acknowledges that data in a buffer has been processed.
  3398. * @ipts: The IPTS driver context.
  3399. * @buffer: The buffer that has been processed and can be refilled.
  3400. @@ -100,7 +99,7 @@ static inline int ipts_control_refill_buffer(struct ipts_context *ipts, u32 buff
  3401. return ipts_control_send_feedback(ipts, buffer);
  3402. }
  3403. -/*
  3404. +/**
  3405. * ipts_control_start() - Initialized the device and starts the data flow.
  3406. * @ipts: The IPTS driver context.
  3407. *
  3408. @@ -108,7 +107,7 @@ static inline int ipts_control_refill_buffer(struct ipts_context *ipts, u32 buff
  3409. */
  3410. int ipts_control_start(struct ipts_context *ipts);
  3411. -/*
  3412. +/**
  3413. * ipts_control_stop() - Stops the data flow and resets the device.
  3414. * @ipts: The IPTS driver context.
  3415. *
  3416. @@ -116,7 +115,7 @@ int ipts_control_start(struct ipts_context *ipts);
  3417. */
  3418. int ipts_control_stop(struct ipts_context *ipts);
  3419. -/*
  3420. +/**
  3421. * ipts_control_restart() - Stops the device and starts it again.
  3422. * @ipts: The IPTS driver context.
  3423. *
  3424. diff --git a/drivers/hid/ipts/desc.h b/drivers/hid/ipts/desc.h
  3425. index c058974a03a1..307438c7c80c 100644
  3426. --- a/drivers/hid/ipts/desc.h
  3427. +++ b/drivers/hid/ipts/desc.h
  3428. @@ -1,6 +1,5 @@
  3429. /* SPDX-License-Identifier: GPL-2.0-or-later */
  3430. /*
  3431. - * Copyright (c) 2016 Intel Corporation
  3432. * Copyright (c) 2022-2023 Dorian Stoll
  3433. *
  3434. * Linux driver for Intel Precise Touch & Stylus
  3435. diff --git a/drivers/hid/ipts/eds1.c b/drivers/hid/ipts/eds1.c
  3436. new file mode 100644
  3437. index 000000000000..ecbb3a8bdaf6
  3438. --- /dev/null
  3439. +++ b/drivers/hid/ipts/eds1.c
  3440. @@ -0,0 +1,103 @@
  3441. +// SPDX-License-Identifier: GPL-2.0-or-later
  3442. +/*
  3443. + * Copyright (c) 2023 Dorian Stoll
  3444. + *
  3445. + * Linux driver for Intel Precise Touch & Stylus
  3446. + */
  3447. +
  3448. +#include <linux/err.h>
  3449. +#include <linux/gfp.h>
  3450. +#include <linux/hid.h>
  3451. +#include <linux/slab.h>
  3452. +#include <linux/types.h>
  3453. +
  3454. +#include "context.h"
  3455. +#include "control.h"
  3456. +#include "desc.h"
  3457. +#include "spec-device.h"
  3458. +
  3459. +int ipts_eds1_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t *desc_size)
  3460. +{
  3461. + size_t size = 0;
  3462. + u8 *buffer = NULL;
  3463. +
  3464. + if (!ipts)
  3465. + return -EFAULT;
  3466. +
  3467. + if (!desc_buffer)
  3468. + return -EFAULT;
  3469. +
  3470. + if (!desc_size)
  3471. + return -EFAULT;
  3472. +
  3473. + size = sizeof(ipts_singletouch_descriptor) + sizeof(ipts_fallback_descriptor);
  3474. +
  3475. + buffer = kzalloc(size, GFP_KERNEL);
  3476. + if (!buffer)
  3477. + return -ENOMEM;
  3478. +
  3479. + memcpy(buffer, ipts_singletouch_descriptor, sizeof(ipts_singletouch_descriptor));
  3480. + memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts_fallback_descriptor,
  3481. + sizeof(ipts_fallback_descriptor));
  3482. +
  3483. + *desc_size = size;
  3484. + *desc_buffer = buffer;
  3485. +
  3486. + return 0;
  3487. +}
  3488. +
  3489. +static int ipts_eds1_switch_mode(struct ipts_context *ipts, enum ipts_mode mode)
  3490. +{
  3491. + int ret = 0;
  3492. +
  3493. + if (!ipts)
  3494. + return -EFAULT;
  3495. +
  3496. + if (ipts->mode == mode)
  3497. + return 0;
  3498. +
  3499. + ipts->mode = mode;
  3500. +
  3501. + ret = ipts_control_restart(ipts);
  3502. + if (ret)
  3503. + dev_err(ipts->dev, "Failed to switch modes: %d\n", ret);
  3504. +
  3505. + return ret;
  3506. +}
  3507. +
  3508. +int ipts_eds1_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  3509. + enum hid_report_type report_type, enum hid_class_request request_type)
  3510. +{
  3511. + int ret = 0;
  3512. +
  3513. + if (!ipts)
  3514. + return -EFAULT;
  3515. +
  3516. + if (!buffer)
  3517. + return -EFAULT;
  3518. +
  3519. + if (report_id != IPTS_HID_REPORT_SET_MODE)
  3520. + return -EIO;
  3521. +
  3522. + if (report_type != HID_FEATURE_REPORT)
  3523. + return -EIO;
  3524. +
  3525. + if (size != 2)
  3526. + return -EINVAL;
  3527. +
  3528. + /*
  3529. + * Implement mode switching report for older devices without native HID support.
  3530. + */
  3531. +
  3532. + if (request_type == HID_REQ_GET_REPORT) {
  3533. + memset(buffer, 0, size);
  3534. + buffer[0] = report_id;
  3535. + buffer[1] = ipts->mode;
  3536. + } else if (request_type == HID_REQ_SET_REPORT) {
  3537. + return ipts_eds1_switch_mode(ipts, buffer[1]);
  3538. + } else {
  3539. + return -EIO;
  3540. + }
  3541. +
  3542. + return ret;
  3543. +}
  3544. diff --git a/drivers/hid/ipts/eds1.h b/drivers/hid/ipts/eds1.h
  3545. new file mode 100644
  3546. index 000000000000..eeeb6575e3e8
  3547. --- /dev/null
  3548. +++ b/drivers/hid/ipts/eds1.h
  3549. @@ -0,0 +1,35 @@
  3550. +// SPDX-License-Identifier: GPL-2.0-or-later
  3551. +/*
  3552. + * Copyright (c) 2023 Dorian Stoll
  3553. + *
  3554. + * Linux driver for Intel Precise Touch & Stylus
  3555. + */
  3556. +
  3557. +#include <linux/hid.h>
  3558. +#include <linux/types.h>
  3559. +
  3560. +#include "context.h"
  3561. +
  3562. +/**
  3563. + * ipts_eds1_get_descriptor() - Assembles the HID descriptor of the device.
  3564. + * @ipts: The IPTS driver context.
  3565. + * @desc_buffer: A pointer to the location where the address of the allocated buffer is stored.
  3566. + * @desc_size: A pointer to the location where the size of the allocated buffer is stored.
  3567. + *
  3568. + * Returns: 0 on success, <0 on error.
  3569. + */
  3570. +int ipts_eds1_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t *desc_size);
  3571. +
  3572. +/**
  3573. + * ipts_eds1_raw_request() - Executes an output or feature report on the device.
  3574. + * @ipts: The IPTS driver context.
  3575. + * @buffer: The buffer containing the report.
  3576. + * @size: The size of the buffer.
  3577. + * @report_id: The HID report ID.
  3578. + * @report_type: Whether this report is an output or a feature report.
  3579. + * @request_type: Whether this report requests or sends data.
  3580. + *
  3581. + * Returns: 0 on success, <0 on error.
  3582. + */
  3583. +int ipts_eds1_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  3584. + enum hid_report_type report_type, enum hid_class_request request_type);
  3585. diff --git a/drivers/hid/ipts/eds2.c b/drivers/hid/ipts/eds2.c
  3586. new file mode 100644
  3587. index 000000000000..e835b460aa79
  3588. --- /dev/null
  3589. +++ b/drivers/hid/ipts/eds2.c
  3590. @@ -0,0 +1,144 @@
  3591. +// SPDX-License-Identifier: GPL-2.0-or-later
  3592. +/*
  3593. + * Copyright (c) 2023 Dorian Stoll
  3594. + *
  3595. + * Linux driver for Intel Precise Touch & Stylus
  3596. + */
  3597. +
  3598. +#include <linux/completion.h>
  3599. +#include <linux/err.h>
  3600. +#include <linux/gfp.h>
  3601. +#include <linux/mutex.h>
  3602. +#include <linux/slab.h>
  3603. +#include <linux/types.h>
  3604. +
  3605. +#include "context.h"
  3606. +#include "control.h"
  3607. +#include "desc.h"
  3608. +#include "spec-data.h"
  3609. +
  3610. +int ipts_eds2_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t *desc_size)
  3611. +{
  3612. + size_t size = 0;
  3613. + u8 *buffer = NULL;
  3614. +
  3615. + if (!ipts)
  3616. + return -EFAULT;
  3617. +
  3618. + if (!desc_buffer)
  3619. + return -EFAULT;
  3620. +
  3621. + if (!desc_size)
  3622. + return -EFAULT;
  3623. +
  3624. + size = sizeof(ipts_singletouch_descriptor) + ipts->descriptor.size;
  3625. +
  3626. + buffer = kzalloc(size, GFP_KERNEL);
  3627. + if (!*buffer)
  3628. + return -ENOMEM;
  3629. +
  3630. + memcpy(buffer, ipts_singletouch_descriptor, sizeof(ipts_singletouch_descriptor));
  3631. + memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts->descriptor.address,
  3632. + ipts->descriptor.size);
  3633. +
  3634. + *desc_size = size;
  3635. + *desc_buffer = buffer;
  3636. +
  3637. + return 0;
  3638. +}
  3639. +
  3640. +static int ipts_eds2_get_feature(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  3641. + enum ipts_feedback_data_type type)
  3642. +{
  3643. + int ret = 0;
  3644. +
  3645. + if (!ipts)
  3646. + return -EFAULT;
  3647. +
  3648. + if (!buffer)
  3649. + return -EFAULT;
  3650. +
  3651. + mutex_lock(&ipts->feature_lock);
  3652. +
  3653. + memset(buffer, 0, size);
  3654. + buffer[0] = report_id;
  3655. +
  3656. + memset(&ipts->feature_report, 0, sizeof(ipts->feature_report));
  3657. + reinit_completion(&ipts->feature_event);
  3658. +
  3659. + ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buffer, size);
  3660. + if (ret) {
  3661. + dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  3662. + goto out;
  3663. + }
  3664. +
  3665. + ret = wait_for_completion_timeout(&ipts->feature_event, msecs_to_jiffies(5000));
  3666. + if (ret == 0) {
  3667. + dev_warn(ipts->dev, "GET_FEATURES timed out!\n");
  3668. + ret = -EIO;
  3669. + goto out;
  3670. + }
  3671. +
  3672. + if (!ipts->feature_report.address) {
  3673. + ret = -EFAULT;
  3674. + goto out;
  3675. + }
  3676. +
  3677. + if (ipts->feature_report.size > size) {
  3678. + ret = -ETOOSMALL;
  3679. + goto out;
  3680. + }
  3681. +
  3682. + ret = ipts->feature_report.size;
  3683. + memcpy(buffer, ipts->feature_report.address, ipts->feature_report.size);
  3684. +
  3685. +out:
  3686. + mutex_unlock(&ipts->feature_lock);
  3687. + return ret;
  3688. +}
  3689. +
  3690. +static int ipts_eds2_set_feature(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  3691. + enum ipts_feedback_data_type type)
  3692. +{
  3693. + int ret = 0;
  3694. +
  3695. + if (!ipts)
  3696. + return -EFAULT;
  3697. +
  3698. + if (!buffer)
  3699. + return -EFAULT;
  3700. +
  3701. + buffer[0] = report_id;
  3702. +
  3703. + ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buffer, size);
  3704. + if (ret)
  3705. + dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  3706. +
  3707. + return ret;
  3708. +}
  3709. +
  3710. +int ipts_eds2_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  3711. + enum hid_report_type report_type, enum hid_class_request request_type)
  3712. +{
  3713. + enum ipts_feedback_data_type feedback_type = IPTS_FEEDBACK_DATA_TYPE_VENDOR;
  3714. +
  3715. + if (!ipts)
  3716. + return -EFAULT;
  3717. +
  3718. + if (!buffer)
  3719. + return -EFAULT;
  3720. +
  3721. + if (report_type == HID_OUTPUT_REPORT && request_type == HID_REQ_SET_REPORT)
  3722. + feedback_type = IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT;
  3723. + else if (report_type == HID_FEATURE_REPORT && request_type == HID_REQ_GET_REPORT)
  3724. + feedback_type = IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES;
  3725. + else if (report_type == HID_FEATURE_REPORT && request_type == HID_REQ_SET_REPORT)
  3726. + feedback_type = IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES;
  3727. + else
  3728. + return -EIO;
  3729. +
  3730. + if (request_type == HID_REQ_GET_REPORT)
  3731. + return ipts_eds2_get_feature(ipts, buffer, report_id, size, feedback_type);
  3732. + else
  3733. + return ipts_eds2_set_feature(ipts, buffer, report_id, size, feedback_type);
  3734. +}
  3735. diff --git a/drivers/hid/ipts/eds2.h b/drivers/hid/ipts/eds2.h
  3736. new file mode 100644
  3737. index 000000000000..064e3716907a
  3738. --- /dev/null
  3739. +++ b/drivers/hid/ipts/eds2.h
  3740. @@ -0,0 +1,35 @@
  3741. +// SPDX-License-Identifier: GPL-2.0-or-later
  3742. +/*
  3743. + * Copyright (c) 2023 Dorian Stoll
  3744. + *
  3745. + * Linux driver for Intel Precise Touch & Stylus
  3746. + */
  3747. +
  3748. +#include <linux/hid.h>
  3749. +#include <linux/types.h>
  3750. +
  3751. +#include "context.h"
  3752. +
  3753. +/**
  3754. + * ipts_eds2_get_descriptor() - Assembles the HID descriptor of the device.
  3755. + * @ipts: The IPTS driver context.
  3756. + * @desc_buffer: A pointer to the location where the address of the allocated buffer is stored.
  3757. + * @desc_size: A pointer to the location where the size of the allocated buffer is stored.
  3758. + *
  3759. + * Returns: 0 on success, <0 on error.
  3760. + */
  3761. +int ipts_eds2_get_descriptor(struct ipts_context *ipts, u8 **desc_buffer, size_t *desc_size);
  3762. +
  3763. +/**
  3764. + * ipts_eds2_raw_request() - Executes an output or feature report on the device.
  3765. + * @ipts: The IPTS driver context.
  3766. + * @buffer: The buffer containing the report.
  3767. + * @size: The size of the buffer.
  3768. + * @report_id: The HID report ID.
  3769. + * @report_type: Whether this report is an output or a feature report.
  3770. + * @request_type: Whether this report requests or sends data.
  3771. + *
  3772. + * Returns: 0 on success, <0 on error.
  3773. + */
  3774. +int ipts_eds2_raw_request(struct ipts_context *ipts, u8 *buffer, size_t size, u8 report_id,
  3775. + enum hid_report_type report_type, enum hid_class_request request_type);
  3776. diff --git a/drivers/hid/ipts/hid.c b/drivers/hid/ipts/hid.c
  3777. index a2471219615b..e34a1a4f9fa7 100644
  3778. --- a/drivers/hid/ipts/hid.c
  3779. +++ b/drivers/hid/ipts/hid.c
  3780. @@ -1,12 +1,12 @@
  3781. // SPDX-License-Identifier: GPL-2.0-or-later
  3782. /*
  3783. - * Copyright (c) 2016 Intel Corporation
  3784. * Copyright (c) 2022-2023 Dorian Stoll
  3785. *
  3786. * Linux driver for Intel Precise Touch & Stylus
  3787. */
  3788. #include <linux/completion.h>
  3789. +#include <linux/err.h>
  3790. #include <linux/gfp.h>
  3791. #include <linux/hid.h>
  3792. #include <linux/mutex.h>
  3793. @@ -14,38 +14,30 @@
  3794. #include <linux/types.h>
  3795. #include "context.h"
  3796. -#include "control.h"
  3797. #include "desc.h"
  3798. +#include "eds1.h"
  3799. +#include "eds2.h"
  3800. #include "hid.h"
  3801. #include "spec-data.h"
  3802. -#include "spec-device.h"
  3803. #include "spec-hid.h"
  3804. -static int ipts_hid_start(struct hid_device *hid)
  3805. +void ipts_hid_enable(struct ipts_context *ipts)
  3806. {
  3807. - return 0;
  3808. + WRITE_ONCE(ipts->hid_active, true);
  3809. }
  3810. -static void ipts_hid_stop(struct hid_device *hid)
  3811. +void ipts_hid_disable(struct ipts_context *ipts)
  3812. {
  3813. + WRITE_ONCE(ipts->hid_active, false);
  3814. }
  3815. -static int ipts_hid_switch_mode(struct ipts_context *ipts, enum ipts_mode mode)
  3816. +static int ipts_hid_start(struct hid_device *hid)
  3817. {
  3818. - if (!ipts)
  3819. - return -EFAULT;
  3820. -
  3821. - if (ipts->mode == mode)
  3822. - return 0;
  3823. -
  3824. - /*
  3825. - * This is only allowed on older devices.
  3826. - */
  3827. - if (ipts->info.intf_eds > 1)
  3828. - return 0;
  3829. + return 0;
  3830. +}
  3831. - ipts->mode = mode;
  3832. - return ipts_control_restart(ipts);
  3833. +static void ipts_hid_stop(struct hid_device *hid)
  3834. +{
  3835. }
  3836. static int ipts_hid_parse(struct hid_device *hid)
  3837. @@ -53,8 +45,6 @@ static int ipts_hid_parse(struct hid_device *hid)
  3838. int ret = 0;
  3839. struct ipts_context *ipts = NULL;
  3840. - bool has_native_descriptor = false;
  3841. -
  3842. u8 *buffer = NULL;
  3843. size_t size = 0;
  3844. @@ -66,26 +56,17 @@ static int ipts_hid_parse(struct hid_device *hid)
  3845. if (!ipts)
  3846. return -EFAULT;
  3847. - size = sizeof(ipts_singletouch_descriptor);
  3848. - has_native_descriptor = ipts->descriptor.address && ipts->descriptor.size > 0;
  3849. + if (!READ_ONCE(ipts->hid_active))
  3850. + return -ENODEV;
  3851. - if (has_native_descriptor)
  3852. - size += ipts->descriptor.size;
  3853. + if (ipts->info.intf_eds == 1)
  3854. + ret = ipts_eds1_get_descriptor(ipts, &buffer, &size);
  3855. else
  3856. - size += sizeof(ipts_fallback_descriptor);
  3857. -
  3858. - buffer = kzalloc(size, GFP_KERNEL);
  3859. - if (!buffer)
  3860. - return -ENOMEM;
  3861. -
  3862. - memcpy(buffer, ipts_singletouch_descriptor, sizeof(ipts_singletouch_descriptor));
  3863. + ret = ipts_eds2_get_descriptor(ipts, &buffer, &size);
  3864. - if (has_native_descriptor) {
  3865. - memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts->descriptor.address,
  3866. - ipts->descriptor.size);
  3867. - } else {
  3868. - memcpy(&buffer[sizeof(ipts_singletouch_descriptor)], ipts_fallback_descriptor,
  3869. - sizeof(ipts_fallback_descriptor));
  3870. + if (ret) {
  3871. + dev_err(ipts->dev, "Failed to allocate HID descriptor: %d\n", ret);
  3872. + return ret;
  3873. }
  3874. ret = hid_parse_report(hid, buffer, size);
  3875. @@ -99,84 +80,11 @@ static int ipts_hid_parse(struct hid_device *hid)
  3876. return 0;
  3877. }
  3878. -static int ipts_hid_get_feature(struct ipts_context *ipts, unsigned char reportnum, __u8 *buf,
  3879. - size_t size, enum ipts_feedback_data_type type)
  3880. -{
  3881. - int ret = 0;
  3882. -
  3883. - if (!ipts)
  3884. - return -EFAULT;
  3885. -
  3886. - if (!buf)
  3887. - return -EFAULT;
  3888. -
  3889. - mutex_lock(&ipts->feature_lock);
  3890. -
  3891. - memset(buf, 0, size);
  3892. - buf[0] = reportnum;
  3893. -
  3894. - memset(&ipts->feature_report, 0, sizeof(ipts->feature_report));
  3895. - reinit_completion(&ipts->feature_event);
  3896. -
  3897. - ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buf, size);
  3898. - if (ret) {
  3899. - dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  3900. - goto out;
  3901. - }
  3902. -
  3903. - ret = wait_for_completion_timeout(&ipts->feature_event, msecs_to_jiffies(5000));
  3904. - if (ret == 0) {
  3905. - dev_warn(ipts->dev, "GET_FEATURES timed out!\n");
  3906. - ret = -EIO;
  3907. - goto out;
  3908. - }
  3909. -
  3910. - if (!ipts->feature_report.address) {
  3911. - ret = -EFAULT;
  3912. - goto out;
  3913. - }
  3914. -
  3915. - if (ipts->feature_report.size > size) {
  3916. - ret = -ETOOSMALL;
  3917. - goto out;
  3918. - }
  3919. -
  3920. - ret = ipts->feature_report.size;
  3921. - memcpy(buf, ipts->feature_report.address, ipts->feature_report.size);
  3922. -
  3923. -out:
  3924. - mutex_unlock(&ipts->feature_lock);
  3925. - return ret;
  3926. -}
  3927. -
  3928. -static int ipts_hid_set_feature(struct ipts_context *ipts, unsigned char reportnum, __u8 *buf,
  3929. - size_t size, enum ipts_feedback_data_type type)
  3930. -{
  3931. - int ret = 0;
  3932. -
  3933. - if (!ipts)
  3934. - return -EFAULT;
  3935. -
  3936. - if (!buf)
  3937. - return -EFAULT;
  3938. -
  3939. - buf[0] = reportnum;
  3940. -
  3941. - ret = ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE, type, buf, size);
  3942. - if (ret)
  3943. - dev_err(ipts->dev, "Failed to send hid2me feedback: %d\n", ret);
  3944. -
  3945. - return ret;
  3946. -}
  3947. -
  3948. -static int ipts_hid_raw_request(struct hid_device *hid, unsigned char reportnum, __u8 *buf,
  3949. - size_t size, unsigned char rtype, int reqtype)
  3950. +static int ipts_hid_raw_request(struct hid_device *hid, unsigned char report_id, __u8 *buffer,
  3951. + size_t size, unsigned char report_type, int request_type)
  3952. {
  3953. - int ret = 0;
  3954. struct ipts_context *ipts = NULL;
  3955. - enum ipts_feedback_data_type type = IPTS_FEEDBACK_DATA_TYPE_VENDOR;
  3956. -
  3957. if (!hid)
  3958. return -ENODEV;
  3959. @@ -185,44 +93,16 @@ static int ipts_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
  3960. if (!ipts)
  3961. return -EFAULT;
  3962. - if (!buf)
  3963. - return -EFAULT;
  3964. -
  3965. - if (rtype == HID_OUTPUT_REPORT && reqtype == HID_REQ_SET_REPORT)
  3966. - type = IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT;
  3967. - else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_GET_REPORT)
  3968. - type = IPTS_FEEDBACK_DATA_TYPE_GET_FEATURES;
  3969. - else if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_SET_REPORT)
  3970. - type = IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES;
  3971. - else
  3972. - return -EIO;
  3973. -
  3974. - // Implemente mode switching report for older devices without native HID support
  3975. - if (type == IPTS_FEEDBACK_DATA_TYPE_SET_FEATURES && reportnum == IPTS_HID_REPORT_SET_MODE) {
  3976. - ret = ipts_hid_switch_mode(ipts, buf[1]);
  3977. - if (ret) {
  3978. - dev_err(ipts->dev, "Failed to switch modes: %d\n", ret);
  3979. - return ret;
  3980. - }
  3981. - }
  3982. -
  3983. - if (reqtype == HID_REQ_GET_REPORT)
  3984. - return ipts_hid_get_feature(ipts, reportnum, buf, size, type);
  3985. - else
  3986. - return ipts_hid_set_feature(ipts, reportnum, buf, size, type);
  3987. -}
  3988. -
  3989. -static int ipts_hid_output_report(struct hid_device *hid, __u8 *data, size_t size)
  3990. -{
  3991. - struct ipts_context *ipts = NULL;
  3992. -
  3993. - if (!hid)
  3994. + if (!READ_ONCE(ipts->hid_active))
  3995. return -ENODEV;
  3996. - ipts = hid->driver_data;
  3997. -
  3998. - return ipts_control_hid2me_feedback(ipts, IPTS_FEEDBACK_CMD_TYPE_NONE,
  3999. - IPTS_FEEDBACK_DATA_TYPE_OUTPUT_REPORT, data, size);
  4000. + if (ipts->info.intf_eds == 1) {
  4001. + return ipts_eds1_raw_request(ipts, buffer, size, report_id, report_type,
  4002. + request_type);
  4003. + } else {
  4004. + return ipts_eds2_raw_request(ipts, buffer, size, report_id, report_type,
  4005. + request_type);
  4006. + }
  4007. }
  4008. static struct hid_ll_driver ipts_hid_driver = {
  4009. @@ -232,7 +112,6 @@ static struct hid_ll_driver ipts_hid_driver = {
  4010. .close = ipts_hid_stop,
  4011. .parse = ipts_hid_parse,
  4012. .raw_request = ipts_hid_raw_request,
  4013. - .output_report = ipts_hid_output_report,
  4014. };
  4015. int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  4016. @@ -247,6 +126,9 @@ int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer)
  4017. if (!ipts->hid)
  4018. return -ENODEV;
  4019. + if (!READ_ONCE(ipts->hid_active))
  4020. + return -ENODEV;
  4021. +
  4022. header = (struct ipts_data_header *)ipts->resources.data[buffer].address;
  4023. temp = ipts->resources.report.address;
  4024. diff --git a/drivers/hid/ipts/hid.h b/drivers/hid/ipts/hid.h
  4025. index 62bf3cd48608..1ebe77447903 100644
  4026. --- a/drivers/hid/ipts/hid.h
  4027. +++ b/drivers/hid/ipts/hid.h
  4028. @@ -1,6 +1,5 @@
  4029. /* SPDX-License-Identifier: GPL-2.0-or-later */
  4030. /*
  4031. - * Copyright (c) 2016 Intel Corporation
  4032. * Copyright (c) 2022-2023 Dorian Stoll
  4033. *
  4034. * Linux driver for Intel Precise Touch & Stylus
  4035. @@ -14,6 +13,9 @@
  4036. #include "context.h"
  4037. #include "spec-device.h"
  4038. +void ipts_hid_enable(struct ipts_context *ipts);
  4039. +void ipts_hid_disable(struct ipts_context *ipts);
  4040. +
  4041. int ipts_hid_input_data(struct ipts_context *ipts, u32 buffer);
  4042. int ipts_hid_init(struct ipts_context *ipts, struct ipts_device_info info);
  4043. diff --git a/drivers/hid/ipts/main.c b/drivers/hid/ipts/main.c
  4044. index 0f20c6c08c38..fb5b5c13ee3e 100644
  4045. --- a/drivers/hid/ipts/main.c
  4046. +++ b/drivers/hid/ipts/main.c
  4047. @@ -1,6 +1,5 @@
  4048. // SPDX-License-Identifier: GPL-2.0-or-later
  4049. /*
  4050. - * Copyright (c) 2016 Intel Corporation
  4051. * Copyright (c) 2020-2023 Dorian Stoll
  4052. *
  4053. * Linux driver for Intel Precise Touch & Stylus
  4054. diff --git a/drivers/hid/ipts/mei.c b/drivers/hid/ipts/mei.c
  4055. index 26666fd99b0c..1e0395ceae4a 100644
  4056. --- a/drivers/hid/ipts/mei.c
  4057. +++ b/drivers/hid/ipts/mei.c
  4058. @@ -1,6 +1,5 @@
  4059. // SPDX-License-Identifier: GPL-2.0-or-later
  4060. /*
  4061. - * Copyright (c) 2016 Intel Corporation
  4062. * Copyright (c) 2023 Dorian Stoll
  4063. *
  4064. * Linux driver for Intel Precise Touch & Stylus
  4065. diff --git a/drivers/hid/ipts/mei.h b/drivers/hid/ipts/mei.h
  4066. index eadacae54c40..973bade6b0fd 100644
  4067. --- a/drivers/hid/ipts/mei.h
  4068. +++ b/drivers/hid/ipts/mei.h
  4069. @@ -1,6 +1,5 @@
  4070. /* SPDX-License-Identifier: GPL-2.0-or-later */
  4071. /*
  4072. - * Copyright (c) 2016 Intel Corporation
  4073. * Copyright (c) 2023 Dorian Stoll
  4074. *
  4075. * Linux driver for Intel Precise Touch & Stylus
  4076. @@ -31,7 +30,7 @@ struct ipts_mei {
  4077. struct rw_semaphore message_lock;
  4078. };
  4079. -/*
  4080. +/**
  4081. * ipts_mei_recv() - Receive data from a MEI device.
  4082. * @mei: The IPTS MEI device context.
  4083. * @code: The IPTS command code to look for.
  4084. @@ -45,7 +44,7 @@ struct ipts_mei {
  4085. int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts_response *rsp,
  4086. u64 timeout);
  4087. -/*
  4088. +/**
  4089. * ipts_mei_send() - Send data to a MEI device.
  4090. * @ipts: The IPTS MEI device context.
  4091. * @data: The data to send.
  4092. @@ -55,7 +54,7 @@ int ipts_mei_recv(struct ipts_mei *mei, enum ipts_command_code code, struct ipts
  4093. */
  4094. int ipts_mei_send(struct ipts_mei *mei, void *data, size_t length);
  4095. -/*
  4096. +/**
  4097. * ipts_mei_init() - Initialize the MEI device context.
  4098. * @mei: The MEI device context to initialize.
  4099. * @cldev: The MEI device the context will be bound to.
  4100. diff --git a/drivers/hid/ipts/receiver.c b/drivers/hid/ipts/receiver.c
  4101. index f56e9ed32d57..ef66c3c9db80 100644
  4102. --- a/drivers/hid/ipts/receiver.c
  4103. +++ b/drivers/hid/ipts/receiver.c
  4104. @@ -1,6 +1,5 @@
  4105. // SPDX-License-Identifier: GPL-2.0-or-later
  4106. /*
  4107. - * Copyright (c) 2016 Intel Corporation
  4108. * Copyright (c) 2020-2023 Dorian Stoll
  4109. *
  4110. * Linux driver for Intel Precise Touch & Stylus
  4111. @@ -66,7 +65,9 @@ static int ipts_receiver_event_loop(struct ipts_thread *thread)
  4112. dev_info(ipts->dev, "IPTS running in event mode\n");
  4113. while (!ipts_thread_should_stop(thread)) {
  4114. - for (int i = 0; i < IPTS_BUFFERS; i++) {
  4115. + int i = 0;
  4116. +
  4117. + for (i = 0; i < IPTS_BUFFERS; i++) {
  4118. ret = ipts_control_wait_data(ipts, false);
  4119. if (ret == -EAGAIN)
  4120. break;
  4121. @@ -126,7 +127,7 @@ static int ipts_receiver_event_loop(struct ipts_thread *thread)
  4122. return 0;
  4123. }
  4124. -static int ipts_receiver_doorbell_loop(struct ipts_thread *thread)
  4125. +static int ipts_receiver_poll_loop(struct ipts_thread *thread)
  4126. {
  4127. int ret = 0;
  4128. u32 buffer = 0;
  4129. @@ -145,7 +146,7 @@ static int ipts_receiver_doorbell_loop(struct ipts_thread *thread)
  4130. if (!ipts)
  4131. return -EFAULT;
  4132. - dev_info(ipts->dev, "IPTS running in doorbell mode\n");
  4133. + dev_info(ipts->dev, "IPTS running in poll mode\n");
  4134. while (true) {
  4135. if (ipts_thread_should_stop(thread)) {
  4136. @@ -217,9 +218,9 @@ int ipts_receiver_start(struct ipts_context *ipts)
  4137. if (ipts->mode == IPTS_MODE_EVENT) {
  4138. ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_event_loop, ipts,
  4139. "ipts_event");
  4140. - } else if (ipts->mode == IPTS_MODE_DOORBELL) {
  4141. - ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_doorbell_loop, ipts,
  4142. - "ipts_doorbell");
  4143. + } else if (ipts->mode == IPTS_MODE_POLL) {
  4144. + ret = ipts_thread_start(&ipts->receiver_loop, ipts_receiver_poll_loop, ipts,
  4145. + "ipts_poll");
  4146. } else {
  4147. ret = -EINVAL;
  4148. }
  4149. diff --git a/drivers/hid/ipts/receiver.h b/drivers/hid/ipts/receiver.h
  4150. index 96070f34fbca..3de7da62d40c 100644
  4151. --- a/drivers/hid/ipts/receiver.h
  4152. +++ b/drivers/hid/ipts/receiver.h
  4153. @@ -1,6 +1,5 @@
  4154. /* SPDX-License-Identifier: GPL-2.0-or-later */
  4155. /*
  4156. - * Copyright (c) 2016 Intel Corporation
  4157. * Copyright (c) 2020-2023 Dorian Stoll
  4158. *
  4159. * Linux driver for Intel Precise Touch & Stylus
  4160. diff --git a/drivers/hid/ipts/resources.c b/drivers/hid/ipts/resources.c
  4161. index 5e924d58c488..cc14653b2a9f 100644
  4162. --- a/drivers/hid/ipts/resources.c
  4163. +++ b/drivers/hid/ipts/resources.c
  4164. @@ -1,12 +1,12 @@
  4165. // SPDX-License-Identifier: GPL-2.0-or-later
  4166. /*
  4167. - * Copyright (c) 2016 Intel Corporation
  4168. * Copyright (c) 2020-2023 Dorian Stoll
  4169. *
  4170. * Linux driver for Intel Precise Touch & Stylus
  4171. */
  4172. #include <linux/dma-mapping.h>
  4173. +#include <linux/slab.h>
  4174. #include <linux/types.h>
  4175. #include "desc.h"
  4176. @@ -107,10 +107,6 @@ int ipts_resources_init(struct ipts_resources *res, struct device *dev, size_t d
  4177. int ipts_resources_free(struct ipts_resources *res)
  4178. {
  4179. - /*
  4180. - * Some compilers (AOSP clang) complain about a redefined
  4181. - * variable when this is declared inside of the for loop.
  4182. - */
  4183. int i = 0;
  4184. if (!res)
  4185. diff --git a/drivers/hid/ipts/resources.h b/drivers/hid/ipts/resources.h
  4186. index e0c400f420b9..2068e13285f0 100644
  4187. --- a/drivers/hid/ipts/resources.h
  4188. +++ b/drivers/hid/ipts/resources.h
  4189. @@ -1,6 +1,5 @@
  4190. /* SPDX-License-Identifier: GPL-2.0-or-later */
  4191. /*
  4192. - * Copyright (c) 2016 Intel Corporation
  4193. * Copyright (c) 2020-2023 Dorian Stoll
  4194. *
  4195. * Linux driver for Intel Precise Touch & Stylus
  4196. diff --git a/drivers/hid/ipts/spec-device.h b/drivers/hid/ipts/spec-device.h
  4197. index 93f673d981f7..41845f9d9025 100644
  4198. --- a/drivers/hid/ipts/spec-device.h
  4199. +++ b/drivers/hid/ipts/spec-device.h
  4200. @@ -109,14 +109,14 @@ static_assert(sizeof(struct ipts_command) == 324);
  4201. /**
  4202. * enum ipts_mode - Configures what data the device produces and how its sent.
  4203. - * @IPTS_MODE_EVENT: The device will send an event once a buffer was filled.
  4204. - * Older devices will return singletouch data in this mode.
  4205. - * @IPTS_MODE_DOORBELL: The device will notify the driver by incrementing the doorbell value.
  4206. - * Older devices will return multitouch data in this mode.
  4207. + * @IPTS_MODE_EVENT: The device will send an event once a buffer was filled.
  4208. + * Older devices will return singletouch data in this mode.
  4209. + * @IPTS_MODE_POLL: The device will notify the driver by incrementing the doorbell value.
  4210. + * Older devices will return multitouch data in this mode.
  4211. */
  4212. enum ipts_mode {
  4213. IPTS_MODE_EVENT = 0x00,
  4214. - IPTS_MODE_DOORBELL = 0x01,
  4215. + IPTS_MODE_POLL = 0x01,
  4216. };
  4217. /**
  4218. @@ -253,14 +253,19 @@ static_assert(sizeof(struct ipts_response) == 88);
  4219. /**
  4220. * struct ipts_device_info - Vendor information of the IPTS device.
  4221. - * @vendor: Vendor ID of this device.
  4222. - * @product: Product ID of this device.
  4223. - * @hw_version: Hardware revision of this device.
  4224. - * @fw_version: Firmware revision of this device.
  4225. - * @data_size: Requested size for a data buffer.
  4226. - * @feedback_size: Requested size for a feedback buffer.
  4227. - * @mode: Mode that the device currently operates in.
  4228. - * @max_contacts: Maximum amount of concurrent touches the sensor can process.
  4229. + * @vendor: Vendor ID of this device.
  4230. + * @product: Product ID of this device.
  4231. + * @hw_version: Hardware revision of this device.
  4232. + * @fw_version: Firmware revision of this device.
  4233. + * @data_size: Requested size for a data buffer.
  4234. + * @feedback_size: Requested size for a feedback buffer.
  4235. + * @mode: Mode that the device currently operates in.
  4236. + * @max_contacts: Maximum amount of concurrent touches the sensor can process.
  4237. + * @sensor_min_eds: The minimum EDS version supported by the sensor.
  4238. + * @sensor_max_eds: The maximum EDS version supported by the sensor.
  4239. + * @me_min_eds: The minimum EDS version supported by the ME for communicating with the sensor.
  4240. + * @me_max_eds: The maximum EDS version supported by the ME for communicating with the sensor.
  4241. + * @intf_eds: The EDS version implemented by the interface between ME and host.
  4242. */
  4243. struct ipts_device_info {
  4244. u16 vendor;
  4245. diff --git a/drivers/hid/ipts/spec-hid.h b/drivers/hid/ipts/spec-hid.h
  4246. index ea70f29ff00c..5a58d4a0a610 100644
  4247. --- a/drivers/hid/ipts/spec-hid.h
  4248. +++ b/drivers/hid/ipts/spec-hid.h
  4249. @@ -1,6 +1,5 @@
  4250. /* SPDX-License-Identifier: GPL-2.0-or-later */
  4251. /*
  4252. - * Copyright (c) 2016 Intel Corporation
  4253. * Copyright (c) 2020-2023 Dorian Stoll
  4254. *
  4255. * Linux driver for Intel Precise Touch & Stylus
  4256. diff --git a/drivers/hid/ipts/thread.c b/drivers/hid/ipts/thread.c
  4257. index 8b46f775c107..355e92bea26f 100644
  4258. --- a/drivers/hid/ipts/thread.c
  4259. +++ b/drivers/hid/ipts/thread.c
  4260. @@ -1,6 +1,5 @@
  4261. // SPDX-License-Identifier: GPL-2.0-or-later
  4262. /*
  4263. - * Copyright (c) 2016 Intel Corporation
  4264. * Copyright (c) 2023 Dorian Stoll
  4265. *
  4266. * Linux driver for Intel Precise Touch & Stylus
  4267. diff --git a/drivers/hid/ipts/thread.h b/drivers/hid/ipts/thread.h
  4268. index a314843599fc..1f966b8b32c4 100644
  4269. --- a/drivers/hid/ipts/thread.h
  4270. +++ b/drivers/hid/ipts/thread.h
  4271. @@ -1,6 +1,5 @@
  4272. /* SPDX-License-Identifier: GPL-2.0-or-later */
  4273. /*
  4274. - * Copyright (c) 2016 Intel Corporation
  4275. * Copyright (c) 2023 Dorian Stoll
  4276. *
  4277. * Linux driver for Intel Precise Touch & Stylus
  4278. @@ -29,7 +28,7 @@ struct ipts_thread {
  4279. int (*threadfn)(struct ipts_thread *thread);
  4280. };
  4281. -/*
  4282. +/**
  4283. * ipts_thread_should_stop() - Returns true if the thread is asked to terminate.
  4284. * @thread: The current thread.
  4285. *
  4286. @@ -37,7 +36,7 @@ struct ipts_thread {
  4287. */
  4288. bool ipts_thread_should_stop(struct ipts_thread *thread);
  4289. -/*
  4290. +/**
  4291. * ipts_thread_start() - Starts an IPTS thread.
  4292. * @thread: The thread to initialize and start.
  4293. * @threadfn: The function to execute.
  4294. @@ -49,7 +48,7 @@ bool ipts_thread_should_stop(struct ipts_thread *thread);
  4295. int ipts_thread_start(struct ipts_thread *thread, int (*threadfn)(struct ipts_thread *thread),
  4296. void *data, const char name[]);
  4297. -/*
  4298. +/**
  4299. * ipts_thread_stop() - Asks the thread to terminate and waits until it has finished.
  4300. * @thread: The thread that should stop.
  4301. *
  4302. --
  4303. 2.41.0